From 49e3b1cb83a08cea44d542fb6c9eb57f61e053c0 Mon Sep 17 00:00:00 2001 From: alexchen Date: Tue, 16 Jan 2024 15:11:42 +0800 Subject: [PATCH] =?UTF-8?q?feat():=20userProviderHook=E4=BD=BF=E7=94=A8con?= =?UTF-8?q?ditional=20expression=E6=97=B6=E5=A5=91=E7=BA=A6=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=A2=AB=E6=94=B6=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes CMDB_CONSUME-231 --- .../ScanUseProviderHookContractsPlugin.js | 83 ++++++++++--------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/packages/webpack-config-factory/src/bricks/ScanUseProviderHookContractsPlugin.js b/packages/webpack-config-factory/src/bricks/ScanUseProviderHookContractsPlugin.js index ca8ff99476..9e283f7488 100644 --- a/packages/webpack-config-factory/src/bricks/ScanUseProviderHookContractsPlugin.js +++ b/packages/webpack-config-factory/src/bricks/ScanUseProviderHookContractsPlugin.js @@ -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; @@ -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 + ); } }); @@ -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 + ); } }); });