From 5f1678e5e5adf5193a72a495c8858e3afbe8d50e Mon Sep 17 00:00:00 2001 From: lihong Date: Thu, 13 Jan 2022 15:58:08 +0800 Subject: [PATCH 1/2] lihong67@huawei.com update component declare file. Signed-off-by: lihong Change-Id: I6765bcb1ccd1934d4481ce515453d1bf600079d4 --- BUILD.gn | 10 +++- compiler/build_declarations_file.js | 82 ++++++++++++++++++++++------- compiler/src/compile_info.ts | 2 +- 3 files changed, 74 insertions(+), 20 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 96890290e..f24bea569 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -141,7 +141,6 @@ ets_loader_sources = [ "compiler/package.json", "compiler/tsconfig.json", "compiler/webpack.config.js", - ets_loader_component_config_file, ] ohos_copy("ets_loader") { @@ -155,6 +154,15 @@ ohos_copy("ets_loader") { module_install_name = "" } +ohos_copy("ets_loader_component_config") { + deps = [ ":build_ets_loader_library" ] + sources = [ ets_loader_component_config_file ] + + outputs = [ target_out_dir + "/$target_name/{{source_file_part}}" ] + module_source_dir = target_out_dir + "/$target_name" + module_install_name = "" +} + ohos_copy("ets_loader_library") { deps = [ ":build_ets_loader_library" ] sources = [ ets_loader_lib_dir ] diff --git a/compiler/build_declarations_file.js b/compiler/build_declarations_file.js index 3b016b636..48a73d5ea 100644 --- a/compiler/build_declarations_file.js +++ b/compiler/build_declarations_file.js @@ -43,10 +43,14 @@ function generateTargetFile(filePath, output) { * limitations under the License. */`; files.forEach((item) => { - const content = fs.readFileSync(item, 'utf8'); + let content = fs.readFileSync(item, 'utf8'); const fileName = path.resolve(output, path.basename(item)); - const newContent = license + '\n\n' + processsFile(content, fileName); - fs.writeFile(fileName, newContent, err => { + if (item === globalTsFile) { + content = license + '\n\n' + processsFile(content, fileName, true); + } else { + content = license + '\n\n' + processsFile(content, fileName, false); + } + fs.writeFile(fileName, content, err => { if (err) { console.error(err); return; @@ -76,28 +80,59 @@ function mkDir(filePath) { fs.mkdirSync(filePath); } -function processsFile(content, fileName) { +function processsFile(content, fileName, isGlobal) { let sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS); const newStatements = []; if (sourceFile.statements && sourceFile.statements.length) { - sourceFile.statements.forEach((node) => { - if (!ts.isImportDeclaration(node)) { - if (node.modifiers && node.modifiers.length && node.modifiers[0].kind === ts.SyntaxKind.ExportKeyword) { - node.modifiers.splice(0, 1); + if (isGlobal) { + sourceFile.statements.forEach((node) => { + if (!ts.isImportDeclaration(node)) { + if (node.modifiers && node.modifiers.length && node.modifiers[0].kind === ts.SyntaxKind.ExportKeyword) { + node.modifiers.splice(0, 1); + } + if (isVariable(node)) { + const name = node.declarationList.declarations[0].name.getText(); + const type = node.declarationList.declarations[0].type.getText(); + if (name.indexOf(type) !== -1) { + const declarationNode = ts.factory.updateVariableDeclaration(node.declarationList.declarations[0], + ts.factory.createIdentifier(type), node.declarationList.declarations[0].exclamationToken, + node.declarationList.declarations[0].type, node.declarationList.declarations[0].initializer); + node.declarationList = ts.factory.updateVariableDeclarationList(node.declarationList, [declarationNode]); + } + } + newStatements.push(node); } - if (isVariable(node)) { - const name = node.declarationList.declarations[0].name.getText(); - const type = node.declarationList.declarations[0].type.getText(); - if (name.indexOf(type) !== -1) { - const declarationNode = ts.factory.updateVariableDeclaration(node.declarationList.declarations[0], - ts.factory.createIdentifier(type), node.declarationList.declarations[0].exclamationToken, - node.declarationList.declarations[0].type, node.declarationList.declarations[0].initializer); - node.declarationList = ts.factory.updateVariableDeclarationList(node.declarationList, [declarationNode]); + }); + } else { + sourceFile.statements.forEach((node) => { + let extendNode = null; + if (isInterface(node)) { + const componentName = node.name.getText().replace(/Interface$/, ''); + let isComponentName = false; + if (node.members) { + for (let i = 0; i < node.members.length; i++) { + const callSignNode = node.members[i]; + if (isSignNode(callSignNode)) { + const callSignName = callSignNode.type.name.getText().repleace(/Attribute$/, ''); + if (componentName === callSignName) { + extendNode = callSignNode.type.typeName; + isComponentName = true; + break; + } + } + } + } + if (isComponentName) { + const heritageClause = ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, + [ts.factory.createExpressionWithTypeArguments(extendNode, undefined)]); + extendNode = null; + node = ts.factory.updateInterfaceDeclaration(node, node.decorators, node.modifiers, + node.name, node.typeParameters, [heritageClause], node.members); } } newStatements.push(node); - } - }); + }); + } } sourceFile = ts.factory.updateSourceFile(sourceFile, newStatements); const printer = ts.createPrinter({ removeComments: true, newLine: ts.NewLineKind.LineFeed }); @@ -114,6 +149,17 @@ function isVariable(node) { return false; } +function isInterface(node) { + return ts.isInterfaceDeclaration(node) && node.name && ts.isIdentifier(node.name) && + /Interface$/.test(node.name.getText()); +} + +function isSignNode(node) { + return (ts.isCallSignatureDeclaration(node) || ts.isConstructSignatureDeclaration(node)) && + node.type && ts.isTypeReferenceNode(node.type) && node.type.name && ts.isIdentifier(node.type.name) && + /Attribute$/.test(node.type.name.getText()); +} + generateComponentConfig(process.argv[4]); function generateComponentConfig(dir) { const configFile = path.resolve(dir, 'component_map.js'); diff --git a/compiler/src/compile_info.ts b/compiler/src/compile_info.ts index b2062a1f8..805cbd928 100644 --- a/compiler/src/compile_info.ts +++ b/compiler/src/compile_info.ts @@ -256,7 +256,7 @@ export class ResultStates { const componentNameReg: RegExp = /'typeof\s*(\$?[_a-zA-Z0-9]+)' is not callable/; const stateInfoReg: RegExp = /Property\s*'(\$[_a-zA-Z0-9]+)' does not exist on type/; const extendInfoReg: RegExp = - /Property\s*'([_a-zA-Z0-9]+)' does not exist on type\s*'([_a-zA-Z0-9]+)'\./; + /Property\s*'([_a-zA-Z0-9]+)' does not exist on type\s*'([_a-zA-Z0-9]+)(Attribute|Interface)'\./; if (this.matchMessage(message, props, propInfoReg) || this.matchMessage(message, [...componentCollection.customComponents], componentNameReg) || this.matchMessage(message, props, stateInfoReg) || -- Gitee From 13990d9aceb33080f2eb709f424bb6ff19422c85 Mon Sep 17 00:00:00 2001 From: lihong Date: Thu, 13 Jan 2022 15:58:08 +0800 Subject: [PATCH 2/2] lihong67@huawei.com update component declare file. Signed-off-by: lihong Change-Id: Ibd8299f12af9efc97203978d4a0f7422631b4b22 --- compiler/build_declarations_file.js | 66 +++++++++++++++++------------ 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/compiler/build_declarations_file.js b/compiler/build_declarations_file.js index 48a73d5ea..3e2e53dfc 100644 --- a/compiler/build_declarations_file.js +++ b/compiler/build_declarations_file.js @@ -105,32 +105,7 @@ function processsFile(content, fileName, isGlobal) { }); } else { sourceFile.statements.forEach((node) => { - let extendNode = null; - if (isInterface(node)) { - const componentName = node.name.getText().replace(/Interface$/, ''); - let isComponentName = false; - if (node.members) { - for (let i = 0; i < node.members.length; i++) { - const callSignNode = node.members[i]; - if (isSignNode(callSignNode)) { - const callSignName = callSignNode.type.name.getText().repleace(/Attribute$/, ''); - if (componentName === callSignName) { - extendNode = callSignNode.type.typeName; - isComponentName = true; - break; - } - } - } - } - if (isComponentName) { - const heritageClause = ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, - [ts.factory.createExpressionWithTypeArguments(extendNode, undefined)]); - extendNode = null; - node = ts.factory.updateInterfaceDeclaration(node, node.decorators, node.modifiers, - node.name, node.typeParameters, [heritageClause], node.members); - } - } - newStatements.push(node); + processComponent(node, newStatements); }); } } @@ -140,6 +115,41 @@ function processsFile(content, fileName, isGlobal) { return result; } +function processComponent(node, newStatements) { + let extendNode = null; + if (isInterface(node)) { + const componentName = node.name.getText().replace(/Interface$/, ''); + const result = validateComponentMembers(node, componentName); + if (result.isComponentName) { + const heritageClause = ts.factory.createHeritageClause(ts.SyntaxKind.ExtendsKeyword, + [ts.factory.createExpressionWithTypeArguments(result.extendNode, undefined)]); + extendNode = null; + node = ts.factory.updateInterfaceDeclaration(node, node.decorators, node.modifiers, + node.name, node.typeParameters, [heritageClause], node.members); + } + } + newStatements.push(node); +} + +function validateComponentMembers(node, componentName) { + let extendNode = null; + let isComponentName = false; + if (node.members) { + for (let i = 0; i < node.members.length; i++) { + const callSignNode = node.members[i]; + if (isSignNode(callSignNode)) { + const callSignName = callSignNode.type.typeName.getText().replace(/Attribute$/, ''); + if (componentName === callSignName) { + extendNode = callSignNode.type.typeName; + isComponentName = true; + break; + } + } + } + } + return { isComponentName, extendNode } +} + function isVariable(node) { if (ts.isVariableStatement(node) && node.declarationList && node.declarationList.declarations && node.declarationList.declarations.length && ts.isVariableDeclaration(node.declarationList.declarations[0]) && @@ -156,8 +166,8 @@ function isInterface(node) { function isSignNode(node) { return (ts.isCallSignatureDeclaration(node) || ts.isConstructSignatureDeclaration(node)) && - node.type && ts.isTypeReferenceNode(node.type) && node.type.name && ts.isIdentifier(node.type.name) && - /Attribute$/.test(node.type.name.getText()); + node.type && ts.isTypeReferenceNode(node.type) && node.type.typeName && ts.isIdentifier(node.type.typeName) && + /Attribute$/.test(node.type.typeName.getText()); } generateComponentConfig(process.argv[4]); -- Gitee