Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): userProviderHook使用conditional expression时契约可以被收集 #3839

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ function gatherContractComments({ provider, brickName, contractsEntries }) {
contractsEntries.set(fileName, [...contracts, comment]);
}

function collectExpression(expression, parser, contractsEntries, printWarning) {
const data = [];
if (expression?.type === "Literal") {
data.push(expression.value);
} else if (expression?.type === "ConditionalExpression") {
// someVar.query(a ? "provider-a" : "provider-b") Or useProvider(a ? "provider-a" : "provider-b")
data.push(expression.consequent.value);
data.push(expression.alternate.value);
}

if (data.length) {
data.forEach((item) => {
if (
validSDKProviderName.test(item) ||
validFlowApiProviderName.test(item)
) {
const brickName = Object.keys(parser.state.options.entry)[0];
gatherContractComments({
provider: item,
brickName,
contractsEntries,
});
}
});
return;
}

printWarning &&
console.warn(
"[useProvider] Here it is recommended to use string or conditional expression as the provider name。"
);
}

module.exports = class ScanUseProviderHookContractsPlugin {
constructor(options = {}) {
this.options = options;
Expand All @@ -50,25 +83,12 @@ module.exports = class ScanUseProviderHookContractsPlugin {
callee.name === "useProvider" &&
expression.arguments.length
) {
const firstArg = expression.arguments[0];
if (firstArg?.type === "Literal") {
if (
validSDKProviderName.test(firstArg.value) ||
validFlowApiProviderName.test(firstArg.value)
) {
const brickName = Object.keys(parser.state.options.entry)[0];
gatherContractComments({
provider: firstArg.value,
brickName,
contractsEntries,
});
}
return;
}
printWarning &&
console.warn(
"[useProvider] Here it is recommended to use string as the provider name。"
);
collectExpression(
expression.arguments[0],
parser,
contractsEntries,
printWarning
);
}
});

Expand All @@ -84,25 +104,12 @@ module.exports = class ScanUseProviderHookContractsPlugin {
expression.callee.property.name === "query" &&
expression.arguments.length
) {
const firstArg = expression.arguments[0];
if (firstArg?.type === "Literal") {
if (
validSDKProviderName.test(firstArg.value) ||
validFlowApiProviderName.test(firstArg.value)
) {
const brickName = Object.keys(parser.state.options.entry)[0];
gatherContractComments({
provider: firstArg.value,
brickName,
contractsEntries,
});
}
return;
}
printWarning &&
console.warn(
"[useProvider] Here it is recommended to use string as the provider name。"
);
collectExpression(
expression.arguments[0],
parser,
contractsEntries,
printWarning
);
}
});
});
Expand Down
Loading