Files
developtools_ace-ets2bundle/compiler/src/process_ui_syntax.ts
T
lihong d3adf823db fixed 8d77388 from https://gitee.com/lihong67/developtools_ace-ets2bundle/pulls/324
lihong67@huawei.com

optimize ets-loader.

Signed-off-by: lihong <lihong67@huawei.com>
Change-Id: I7358bb5c838c392eb58ad23a06714adf7c56d5cb
2022-03-11 07:26:38 +00:00

330 lines
13 KiB
TypeScript

/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import ts from 'typescript';
import path from 'path';
import { componentCollection } from './validate_ui_syntax';
import { processComponentClass } from './process_component_class';
import processImport from './process_import';
import {
PAGE_ENTRY_FUNCTION_NAME,
COMPONENT_CONSTRUCTOR_UNDEFINED,
BUILD_ON,
COMPONENT_BUILDER_DECORATOR,
COMPONENT_EXTEND_DECORATOR,
COMPONENT_STYLES_DECORATOR,
RESOURCE,
RESOURCE_TYPE,
WORKER_OBJECT,
RESOURCE_NAME_ID,
RESOURCE_NAME_TYPE,
RESOURCE_NAME_PARAMS,
RESOURCE_RAWFILE,
ATTRIBUTE_ANIMATETO,
GLOBAL_CONTEXT,
CHECK_COMPONENT_EXTEND_DECORATOR,
INSTANCE
} from './pre_define';
import {
componentInfo,
LogInfo,
LogType,
hasDecorator,
FileLog
} from './utils';
import {
processComponentBlock,
bindComponentAttr
} from './process_component_build';
import {
BUILDIN_STYLE_NAMES,
CUSTOM_BUILDER_METHOD,
EXTEND_ATTRIBUTE,
INNER_STYLE_FUNCTION,
GLOBAL_STYLE_FUNCTION,
INTERFACE_NODE_SET
} from './component_map';
import { resources } from '../main';
export const transformLog: FileLog = new FileLog();
export let contextGlobal: ts.TransformationContext;
export function processUISyntax(program: ts.Program, ut = false): Function {
return (context: ts.TransformationContext) => {
contextGlobal = context;
let pagesDir: string;
return (node: ts.SourceFile) => {
pagesDir = path.resolve(path.dirname(node.fileName));
if (process.env.compiler === BUILD_ON) {
if (!ut && (path.basename(node.fileName) === 'app.ets' || /\.ts$/.test(node.fileName))) {
node = ts.visitEachChild(node, processResourceNode, context);
return node;
}
transformLog.sourceFile = node;
node = createEntryNode(node, context);
node = ts.visitEachChild(node, processAllNodes, context);
GLOBAL_STYLE_FUNCTION.forEach((block, styleName) => {
BUILDIN_STYLE_NAMES.delete(styleName);
});
GLOBAL_STYLE_FUNCTION.clear();
const statements: ts.Statement[] = Array.from(node.statements);
INTERFACE_NODE_SET.forEach(item => {
statements.unshift(item);
});
node = ts.factory.updateSourceFile(node, statements);
INTERFACE_NODE_SET.clear();
return node;
} else {
return node;
}
};
function processAllNodes(node: ts.Node): ts.Node {
if (ts.isImportDeclaration(node) || ts.isImportEqualsDeclaration(node)) {
processImport(node, pagesDir, transformLog.errors);
} else if (ts.isStructDeclaration(node)) {
componentCollection.currentClassName = node.name.getText();
node = processComponentClass(node, context, transformLog.errors, program);
componentCollection.currentClassName = null;
INNER_STYLE_FUNCTION.forEach((block, styleName) => {
BUILDIN_STYLE_NAMES.delete(styleName);
});
INNER_STYLE_FUNCTION.clear();
} else if (ts.isFunctionDeclaration(node)) {
if (hasDecorator(node, COMPONENT_EXTEND_DECORATOR)) {
node = processExtend(node, transformLog.errors);
} else if (hasDecorator(node, COMPONENT_BUILDER_DECORATOR) && node.name && node.body &&
ts.isBlock(node.body)) {
CUSTOM_BUILDER_METHOD.add(node.name.getText());
node = ts.factory.updateFunctionDeclaration(node, undefined, node.modifiers,
node.asteriskToken, node.name, node.typeParameters, node.parameters, node.type,
processComponentBlock(node.body, false, transformLog.errors));
} else if (hasDecorator(node, COMPONENT_STYLES_DECORATOR)) {
if (node.parameters.length === 0) {
node = undefined;
} else {
transformLog.errors.push({
type: LogType.ERROR,
message: `@Styles can't have parameters.`,
pos: node.getStart()
});
}
}
} else if (isResource(node)) {
node = processResourceData(node as ts.CallExpression);
} else if (isWorker(node)) {
node = processWorker(node as ts.NewExpression);
} else if (isAnimateTo(node)) {
node = processAnimateTo(node as ts.CallExpression);
}
return ts.visitEachChild(node, processAllNodes, context);
}
function processResourceNode(node: ts.Node): ts.Node {
if (isResource(node)) {
node = processResourceData(node as ts.CallExpression);
}
return ts.visitEachChild(node, processResourceNode, context);
}
};
}
function isResource(node: ts.Node): boolean {
return ts.isCallExpression(node) && ts.isIdentifier(node.expression) &&
(node.expression.escapedText.toString() === RESOURCE ||
node.expression.escapedText.toString() === RESOURCE_RAWFILE) && node.arguments.length > 0;
}
function isAnimateTo(node: ts.Node): boolean {
return ts.isCallExpression(node) && ts.isIdentifier(node.expression) &&
node.expression.escapedText.toString() === ATTRIBUTE_ANIMATETO;
}
function processResourceData(node: ts.CallExpression): ts.Node {
if (ts.isStringLiteral(node.arguments[0])) {
if (node.expression.getText() === RESOURCE_RAWFILE) {
return createResourceParam(0, RESOURCE_TYPE.rawfile, [node.arguments[0]]);
} else {
// @ts-ignore
const resourceData: string[] = node.arguments[0].text.trim().split('.');
if (validateResourceData(resourceData, resources, node.arguments[0].getStart())) {
const resourceType: number = RESOURCE_TYPE[resourceData[1]];
const resourceValue: number = resources[resourceData[0]][resourceData[1]][resourceData[2]];
return createResourceParam(resourceValue, resourceType,
Array.from(node.arguments).slice(1));
}
}
}
return node;
}
function createResourceParam(resourceValue: number, resourceType: number, argsArr: ts.Expression[]):
ts.ObjectLiteralExpression {
const resourceParams: ts.ObjectLiteralExpression = ts.factory.createObjectLiteralExpression(
[
ts.factory.createPropertyAssignment(
ts.factory.createStringLiteral(RESOURCE_NAME_ID),
ts.factory.createNumericLiteral(resourceValue)
),
ts.factory.createPropertyAssignment(
ts.factory.createStringLiteral(RESOURCE_NAME_TYPE),
ts.factory.createNumericLiteral(resourceType)
),
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier(RESOURCE_NAME_PARAMS),
ts.factory.createArrayLiteralExpression(
argsArr,
false
)
)
],
false
);
return resourceParams;
}
function validateResourceData(resourceData: string[], resources: object, pos: number): boolean {
if (resourceData.length !== 3) {
transformLog.errors.push({
type: LogType.ERROR,
message: 'The input parameter is not supported.',
pos: pos
});
} else if (!resources[resourceData[0]]) {
transformLog.errors.push({
type: LogType.ERROR,
message: `The value of '${resourceData[0]}' is invalid.`,
pos: pos
});
} else if (!resources[resourceData[0]][resourceData[1]]) {
transformLog.errors.push({
type: LogType.ERROR,
message: `Value '${resourceData[1]}' does not exist on type 'typeof ${resourceData[0]}'.`,
pos: pos
});
} else if (!resources[resourceData[0]][resourceData[1]][resourceData[2]]) {
transformLog.errors.push({
type: LogType.ERROR,
message: `Value '${resourceData[2]}' does not exist on type 'typeof ${resourceData[1]}'.`,
pos: pos
});
} else {
return true;
}
return false;
}
function isWorker(node: ts.Node): boolean {
return ts.isNewExpression(node) && ts.isPropertyAccessExpression(node.expression) &&
ts.isIdentifier(node.expression.name) &&
node.expression.name.escapedText.toString() === WORKER_OBJECT;
}
function processWorker(node: ts.NewExpression): ts.Node {
if (node.arguments.length && ts.isStringLiteral(node.arguments[0])) {
const args: ts.Expression[] = Array.from(node.arguments);
// @ts-ignore
const workerPath: string = node.arguments[0].text;
const stringNode: ts.StringLiteral = ts.factory.createStringLiteral(
workerPath.replace(/\.ts$/, '.js'));
args.splice(0, 1, stringNode);
return ts.factory.updateNewExpression(node, node.expression, node.typeArguments, args);
}
return node;
}
function processAnimateTo(node: ts.CallExpression): ts.CallExpression {
return ts.factory.updateCallExpression(node, ts.factory.createPropertyAccessExpression(
ts.factory.createIdentifier(GLOBAL_CONTEXT), ts.factory.createIdentifier(ATTRIBUTE_ANIMATETO)),
node.typeArguments, node.arguments);
}
function processExtend(node: ts.FunctionDeclaration, log: LogInfo[]): ts.FunctionDeclaration {
const componentName: string = isExtendFunction(node);
if (componentName && node.body && node.body.statements.length) {
const statementArray: ts.Statement[] = [];
const attrSet: ts.CallExpression = node.body.statements[0].expression;
const changeCompName: ts.ExpressionStatement = ts.factory.createExpressionStatement(processExtendBody(attrSet));
bindComponentAttr(changeCompName as ts.ExpressionStatement,
ts.factory.createIdentifier(componentName), statementArray, log);
let extendFunctionName: string;
if (node.name.getText().startsWith('__' + componentName + '__')) {
extendFunctionName = node.name.getText();
} else {
extendFunctionName = '__' + componentName + '__' + node.name.getText();
collectExtend(EXTEND_ATTRIBUTE, componentName, node.name.escapedText.toString());
}
return ts.factory.updateFunctionDeclaration(node, undefined, node.modifiers, node.asteriskToken,
ts.factory.createIdentifier(extendFunctionName), node.typeParameters,
node.parameters, node.type, ts.factory.updateBlock(node.body, statementArray));
}
}
function processExtendBody(node: ts.Node): ts.Expression {
switch (node.kind) {
case ts.SyntaxKind.CallExpression:
return ts.factory.createCallExpression(processExtendBody(node.expression), undefined, node.arguments);
case ts.SyntaxKind.PropertyAccessExpression:
return ts.factory.createPropertyAccessExpression(processExtendBody(node.expression), node.name);
case ts.SyntaxKind.Identifier:
return ts.factory.createIdentifier(node.escapedText.toString().replace(INSTANCE, ''));
}
}
export function collectExtend(collectionSet: Map<string, Set<string>>, component: string, attribute: string): void {
if (collectionSet.has(component)) {
collectionSet.get(component).add(attribute);
} else {
collectionSet.set(component, new Set([attribute]));
}
}
function isExtendFunction(node: ts.FunctionDeclaration): string {
if (node.decorators && node.decorators[0].expression &&
node.decorators[0].expression.expression.escapedText.toString() === CHECK_COMPONENT_EXTEND_DECORATOR &&
node.decorators[0].expression.arguments) {
return node.decorators[0].expression.arguments[0].escapedText.toString();
} else {
return null;
}
}
function createEntryNode(node: ts.SourceFile, context: ts.TransformationContext): ts.SourceFile {
if (componentCollection.entryComponent) {
const entryNode: ts.ExpressionStatement =
createEntryFunction(componentCollection.entryComponent, context);
return context.factory.updateSourceFile(node, [...node.statements, entryNode]);
} else if (componentCollection.previewComponent) {
const entryNode: ts.ExpressionStatement =
createEntryFunction(componentCollection.previewComponent, context);
return context.factory.updateSourceFile(node, [...node.statements, entryNode]);
} else {
return node;
}
}
function createEntryFunction(name: string, context: ts.TransformationContext)
: ts.ExpressionStatement {
return context.factory.createExpressionStatement(context.factory.createCallExpression(
context.factory.createIdentifier(PAGE_ENTRY_FUNCTION_NAME), undefined,
[context.factory.createNewExpression(context.factory.createIdentifier(name), undefined,
[context.factory.createStringLiteral((++componentInfo.id).toString()),
context.factory.createIdentifier(COMPONENT_CONSTRUCTOR_UNDEFINED),
context.factory.createObjectLiteralExpression([], false)])]));
}
export function resetLog(): void {
transformLog.errors = [];
}