!832 @Builder @Extend @Styles混用代码回退

Merge pull request !832 from houhaoyu/master9
This commit is contained in:
openharmony_ci
2022-07-18 09:26:18 +00:00
committed by Gitee
8 changed files with 38 additions and 268 deletions
-3
View File
@@ -77,9 +77,6 @@ export interface ExtendParamterInterfance {
}
export const EXTEND_ATTRIBUTE: Map<string, Set<string>> = new Map();
export const STYLES_ATTRIBUTE: Set<string> = new Set();
export const BUILDER_MIX_EXTEND_RESPECTIVE: Map<string, Set<string>> = new Map();
export const BUILDER_MIX_EXTEND: Set<string> = new Set();
export const BUILDER_MIX_STYLES: Set<string> = new Set();
export const INTERFACE_NODE_SET: Set<ts.InterfaceDeclaration> = new Set();
+7 -96
View File
@@ -74,10 +74,7 @@ import {
INNER_STYLE_FUNCTION,
GLOBAL_STYLE_FUNCTION,
COMMON_ATTRS,
CUSTOM_BUILDER_PROPERTIES,
BUILDER_MIX_EXTEND,
BUILDER_MIX_EXTEND_RESPECTIVE,
BUILDER_MIX_STYLES
CUSTOM_BUILDER_PROPERTIES
} from './component_map';
import {
componentCollection,
@@ -135,17 +132,8 @@ export function processComponentBlock(node: ts.Block, isLazy: boolean, log: LogI
function validateRootNode(node: ts.MethodDeclaration, log: LogInfo[]): boolean {
let isValid: boolean = false;
const bodyStatements: ts.NodeArray<ts.Statement> = node.body.statements;
let length: number = bodyStatements.length;
bodyStatements.map(node => {
const nodeName: string = node.expression && node.expression.expression &&
node.expression.expression.escapedText || undefined;
if (BUILDER_MIX_STYLES.has(nodeName) || BUILDER_MIX_EXTEND.has(nodeName)) {
length--;
}
});
if (length === 1) {
const statement: ts.Statement = bodyStatements[0];
if (node.body.statements.length === 1) {
const statement: ts.Statement = node.body.statements[0];
if (ts.isIfStatement(statement) || validateFirstNode(statement)) {
isValid = true;
}
@@ -156,7 +144,7 @@ function validateRootNode(node: ts.MethodDeclaration, log: LogInfo[]): boolean {
log.push({
type: LogType.ERROR,
message: `There should have a root container component.`,
pos: bodyStatements.pos
pos: node.body.statements.pos
});
}
return isValid;
@@ -232,17 +220,13 @@ export function processComponentChild(node: ts.Block | ts.SourceFile, newStateme
sourceNode = ts.createSourceFile('', node.getText(), ts.ScriptTarget.Latest, true, ts.ScriptKind.ETS, compilerOptions);
}
if (node.statements.length) {
let lastName: string;
let lastExpression: ts.Statement;
node.statements.forEach((item, index) => {
node.statements.forEach((item) => {
if (ts.isExpressionStatement(item)) {
checkEtsComponent(item, log);
const name: string = getName(item);
switch (getComponentType(item, log, name)) {
case ComponentType.innerComponent:
processInnerComponent(item, newStatements, log);
lastName = name;
lastExpression = item;
break;
case ComponentType.customComponent:
if (!newsupplement.isAcceleratePreview) {
@@ -254,34 +238,18 @@ export function processComponentChild(node: ts.Block | ts.SourceFile, newStateme
}
}
processCustomComponent(item as ts.ExpressionStatement, newStatements, log, name);
lastName = name;
lastExpression = item;
}
break;
case ComponentType.forEachComponent:
processForEachComponent(item, newStatements, log);
lastName = name;
lastExpression = item;
break;
case ComponentType.customBuilderMethod:
case ComponentType.builderParamMethod:
newStatements.push(item);
lastName = name;
lastExpression = item;
break;
case ComponentType.builderMixExtendMethod:
processMixBuilder(lastName, name, index, lastExpression, log, newStatements, item,
ComponentType.builderMixExtendMethod);
break;
case ComponentType.builderMixStylesMethod:
processMixBuilder(lastName, name, index, lastExpression, log, newStatements, item,
ComponentType.builderMixStylesMethod);
break;
}
} else if (ts.isIfStatement(item)) {
processIfStatement(item, newStatements, log);
lastName = 'if';
lastExpression = undefined;
} else if (!ts.isBlock(item)) {
log.push({
type: LogType.ERROR,
@@ -299,51 +267,6 @@ export function processComponentChild(node: ts.Block | ts.SourceFile, newStateme
};
}
function processMixBuilder(lastName: string, name: string, index: number, lastExpression: ts.Statement, log: LogInfo[],
newStatements: ts.Statement[], item: ts.Statement, type: ComponentType): void {
let isMixExtend: boolean;
if (type === ComponentType.builderMixExtendMethod) {
isMixExtend = true;
} else {
isMixExtend = false;
}
if (lastName) {
if (index && (isMixExtend ? BUILDER_MIX_EXTEND_RESPECTIVE.get(lastName) &&
BUILDER_MIX_EXTEND_RESPECTIVE.get(lastName).has(name) : true) &&
(getComponentType(lastExpression as ts.ExpressionStatement, log, lastName) ===
ComponentType.innerComponent || (isMixExtend ? undefined : getComponentType(
lastExpression as ts.ExpressionStatement, log, lastName) === ComponentType.customComponent))) {
if (newStatements.length && checkPop(newStatements[newStatements.length - 1])) {
newStatements.splice(newStatements.length - 1, 0, item);
} else {
newStatements.push(item);
}
} else {
log.push({
type: LogType.ERROR,
message: `@Builder & ${isMixExtend ? '@Extend' : '@Styles'} function '${name}' cannot decorate '${lastName}'`,
pos: item.getStart()
});
}
} else {
log.push({
type: LogType.ERROR,
message: `@Builder & ${isMixExtend ? '@Extend' : '@Styles'} function '${name}' should decorate a Component`,
pos: item.getStart()
});
}
}
function checkPop(node: ts.Statement): boolean {
if (ts.isExpressionStatement(node) && node.expression && ts.isCallExpression(node.expression) &&
node.expression.expression && ts.isPropertyAccessExpression(node.expression.expression) &&
node.expression.expression.name && ts.isIdentifier(node.expression.expression.name) &&
node.expression.expression.name.escapedText.toString() === COMPONENT_POP_FUNCTION) {
return true;
}
return false;
}
function processExpressionStatementChange(node: ts.ExpressionStatement, nextNode: ts.Block,
log: LogInfo[]): ts.ExpressionStatement {
let name: string;
@@ -1034,12 +957,6 @@ function addComponentAttr(temp: any, node: ts.Identifier, lastStatement: any,
statements.push(ts.factory.createExpressionStatement(
createFunction(identifierNode, node, argumentsArr)));
lastStatement.kind = true;
} else if (BUILDER_MIX_EXTEND.has(propName) || BUILDER_MIX_STYLES.has(propName)) {
log.push({
type: LogType.ERROR,
message: `'${propName}' is a function, not attribute.`,
pos: node.getStart()
});
} else {
if (isStylesAttr) {
if (!COMMON_ATTRS.has(propName)) {
@@ -1321,9 +1238,7 @@ enum ComponentType {
customComponent,
forEachComponent,
customBuilderMethod,
builderParamMethod,
builderMixExtendMethod,
builderMixStylesMethod
builderParamMethod
}
function isEtsComponent(node: ts.ExpressionStatement): boolean {
@@ -1352,10 +1267,6 @@ function getComponentType(node: ts.ExpressionStatement, log: LogInfo[],
return ComponentType.forEachComponent;
} else if (CUSTOM_BUILDER_METHOD.has(name)) {
return ComponentType.customBuilderMethod;
} else if (BUILDER_MIX_EXTEND.has(name)) {
return ComponentType.builderMixExtendMethod;
} else if (BUILDER_MIX_STYLES.has(name)) {
return ComponentType.builderMixStylesMethod;
} else if (builderParamObjectCollection.get(componentCollection.currentClassName) &&
builderParamObjectCollection.get(componentCollection.currentClassName).has(name)) {
return ComponentType.builderParamMethod;
@@ -1398,4 +1309,4 @@ function checkEtsComponent(node: ts.ExpressionStatement, log: LogInfo[]): void {
log
);
}
}
}
+5 -53
View File
@@ -27,9 +27,7 @@ import {
CUSTOM_COMPONENT_DEFAULT,
CUSTOM_DECORATOR_NAME,
COMPONENT_DECORATOR_ENTRY,
COMPONENT_BUILDER_DECORATOR,
COMPONENT_EXTEND_DECORATOR,
COMPONENT_STYLES_DECORATOR
COMPONENT_BUILDER_DECORATOR
} from './pre_define';
import {
propertyCollection,
@@ -50,13 +48,7 @@ import {
import { hasDecorator, LogInfo, LogType, repeatLog } from './utils';
import { projectConfig } from '../main';
import { isOhmUrl, resolveSourceFile } from './resolve_ohm_url';
import {
BUILDER_MIX_STYLES,
BUILDER_MIX_EXTEND,
BUILDER_MIX_EXTEND_RESPECTIVE,
CUSTOM_BUILDER_METHOD
} from './component_map';
import { isExtendFunction } from './process_ui_syntax';
import { CUSTOM_BUILDER_METHOD } from './component_map';
const IMPORT_FILE_ASTCACHE: Map<string, ts.SourceFile> = new Map();
@@ -170,16 +162,8 @@ function visitAllNode(node: ts.Node, sourceFile: ts.SourceFile, defaultNameFromP
}
}
if (ts.isFunctionDeclaration(node) && hasDecorator(node, COMPONENT_BUILDER_DECORATOR)) {
if (hasDecorator(node, COMPONENT_EXTEND_DECORATOR)) {
collectSpecialFunctionNode(node, asNameFromParent, defaultNameFromParent, defaultCollection,
asExportCollection, BUILDER_MIX_EXTEND, BUILDER_MIX_EXTEND_RESPECTIVE);
} else if (hasDecorator(node, COMPONENT_STYLES_DECORATOR)) {
collectSpecialFunctionNode(node, asNameFromParent, defaultNameFromParent, defaultCollection,
asExportCollection, BUILDER_MIX_STYLES);
} else {
collectSpecialFunctionNode(node, asNameFromParent, defaultNameFromParent, defaultCollection,
asExportCollection, CUSTOM_BUILDER_METHOD);
}
collectSpecialFunctionNode(node, asNameFromParent, defaultNameFromParent, defaultCollection,
asExportCollection, CUSTOM_BUILDER_METHOD);
}
if (ts.isExportAssignment(node) && node.expression && ts.isIdentifier(node.expression) &&
hasCollection(node.expression)) {
@@ -276,52 +260,20 @@ function visitAllNode(node: ts.Node, sourceFile: ts.SourceFile, defaultNameFromP
function collectSpecialFunctionNode(node: ts.FunctionDeclaration, asNameFromParent: Map<string, string>,
defaultNameFromParent: string, defaultCollection: Set<string>, asExportCollection: Map<string, string>,
collection: Set<string>, specialCollection?: Map<string, Set<string>>): void {
collection: Set<string>): void {
const name: string = node.name.getText();
let componentName: string;
if (asNameFromParent.has(name)) {
collection.add(asNameFromParent.get(name));
if (specialCollection) {
componentName = isExtendFunction(node);
if (specialCollection.get(componentName)) {
specialCollection.get(componentName).add(asNameFromParent.get(name));
} else {
specialCollection.set(componentName, new Set([asNameFromParent.get(name)]));
}
}
} else if (node.modifiers && node.modifiers.length >= 2 && node.modifiers[0] &&
node.modifiers[0].kind === ts.SyntaxKind.ExportKeyword && node.modifiers[1] &&
node.modifiers[1].kind === ts.SyntaxKind.DefaultKeyword && defaultNameFromParent &&
asNameFromParent.has(defaultNameFromParent)) {
collection.add(asNameFromParent.get(defaultNameFromParent));
if (specialCollection) {
componentName = isExtendFunction(node);
if (specialCollection.get(componentName)) {
specialCollection.get(componentName).add(asNameFromParent.get(name));
} else {
specialCollection.set(componentName, new Set([asNameFromParent.get(name)]));
}
}
} else if (defaultCollection.has(name)) {
collection.add(CUSTOM_COMPONENT_DEFAULT);
if (specialCollection) {
componentName = isExtendFunction(node);
if (specialCollection.get(componentName)) {
specialCollection.get(componentName).add(CUSTOM_COMPONENT_DEFAULT);
} else {
specialCollection.set(componentName, new Set([CUSTOM_COMPONENT_DEFAULT]));
}
}
} else if (asExportCollection.has(name)) {
collection.add(asExportCollection.get(name));
if (specialCollection) {
componentName = isExtendFunction(node);
if (specialCollection.get(componentName)) {
specialCollection.get(componentName).add(asExportCollection.get(name));
} else {
specialCollection.set(componentName, new Set([asExportCollection.get(name)]));
}
}
}
}
+15 -50
View File
@@ -134,21 +134,7 @@ export function processUISyntax(program: ts.Program, ut = false): Function {
});
INNER_STYLE_FUNCTION.clear();
} else if (ts.isFunctionDeclaration(node)) {
if (hasDecorator(node, COMPONENT_STYLES_DECORATOR)) {
if (!hasDecorator(node, COMPONENT_BUILDER_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 {
node = processStyles(node, transformLog.errors);
}
} else if (hasDecorator(node, COMPONENT_EXTEND_DECORATOR)) {
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)) {
@@ -156,6 +142,16 @@ export function processUISyntax(program: ts.Program, ut = false): Function {
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);
@@ -425,32 +421,6 @@ function processAnimateTo(node: ts.CallExpression): ts.CallExpression {
node.typeArguments, node.arguments);
}
function processStyles(node: ts.FunctionDeclaration, log: LogInfo[]): ts.FunctionDeclaration {
const componentName: string = COMPONENT_COMMON;
if (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(processStylesBody(attrSet));
bindComponentAttr(changeCompName as ts.ExpressionStatement,
ts.factory.createIdentifier(componentName), statementArray, log);
const stylesFunctionName: string = node.name.getText();
return ts.factory.updateFunctionDeclaration(node, undefined, node.modifiers, node.asteriskToken,
ts.factory.createIdentifier(stylesFunctionName), node.typeParameters,
node.parameters, node.type, ts.factory.updateBlock(node.body, statementArray));
}
}
function processStylesBody(node: ts.Node): ts.Expression {
switch (node.kind) {
case ts.SyntaxKind.CallExpression:
return ts.factory.createCallExpression(processStylesBody(node.expression), undefined, node.arguments);
case ts.SyntaxKind.PropertyAccessExpression:
return ts.factory.createPropertyAccessExpression(processStylesBody(node.expression), node.name);
case ts.SyntaxKind.Identifier:
return ts.factory.createIdentifier(node.escapedText.toString().replace(INSTANCE, ''));
}
}
function processExtend(node: ts.FunctionDeclaration, log: LogInfo[]): ts.FunctionDeclaration {
const componentName: string = isExtendFunction(node);
if (componentName && node.body && node.body.statements.length) {
@@ -460,16 +430,11 @@ function processExtend(node: ts.FunctionDeclaration, log: LogInfo[]): ts.Functio
bindComponentAttr(changeCompName as ts.ExpressionStatement,
ts.factory.createIdentifier(componentName), statementArray, log);
let extendFunctionName: string;
const prefixCompName: string = '__' + componentName + '__';
if (!hasDecorator(node, COMPONENT_BUILDER_DECORATOR)) {
if (node.name.getText().startsWith(prefixCompName)) {
extendFunctionName = node.name.getText();
} else {
extendFunctionName = prefixCompName + node.name.getText();
collectExtend(EXTEND_ATTRIBUTE, componentName, node.name.escapedText.toString());
}
} else {
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,
+7 -34
View File
@@ -61,10 +61,7 @@ import {
EXTEND_ATTRIBUTE,
GLOBAL_STYLE_FUNCTION,
STYLES_ATTRIBUTE,
CUSTOM_BUILDER_METHOD,
BUILDER_MIX_EXTEND,
BUILDER_MIX_EXTEND_RESPECTIVE,
BUILDER_MIX_STYLES
CUSTOM_BUILDER_METHOD
} from './component_map';
import {
LogType,
@@ -338,16 +335,9 @@ function visitAllNode(node: ts.Node, sourceFileNode: ts.SourceFile, allComponent
}
if ((ts.isMethodDeclaration(node) || ts.isFunctionDeclaration(node)) &&
hasDecorator(node, COMPONENT_BUILDER_DECORATOR)) {
if (hasDecorator(node, COMPONENT_EXTEND_DECORATOR) && ts.isFunctionDeclaration(node)) {
const componentName: string = isExtendFunction(node);
BUILDER_MIX_EXTEND.add(node.name.getText());
collectExtend(BUILDER_MIX_EXTEND_RESPECTIVE, componentName, node.name.getText());
} else if (hasDecorator(node, COMPONENT_STYLES_DECORATOR)) {
BUILDER_MIX_STYLES.add(node.name.getText());
} else {
CUSTOM_BUILDER_METHOD.add(node.name.getText());
}
} else if (ts.isFunctionDeclaration(node) && isExtendFunction(node)) {
}
if (ts.isFunctionDeclaration(node) && isExtendFunction(node)) {
const componentName: string = isExtendFunction(node);
collectExtend(EXTEND_ATTRIBUTE, componentName, node.name.getText());
} else if (ts.isFunctionDeclaration(node) && hasDecorator(node, COMPONENT_STYLES_DECORATOR)) {
@@ -453,19 +443,10 @@ function hasNonSingleChild(node: ts.EtsComponentExpression, allComponentNames: S
return false;
}
if (BlockNode && BlockNode.statements) {
let length: number = BlockNode.statements.length;
const length: number = BlockNode.statements.length;
if (!length) {
return false;
}
BlockNode.statements.map(node => {
const nodeName: string = ts.isExpressionStatement(node) &&
ts.isCallExpression(node.expression) &&
ts.isIdentifier(node.expression.expression) ?
node.expression.expression.escapedText.toString() : '';
if (BUILDER_MIX_STYLES.has(nodeName) || BUILDER_MIX_EXTEND.has(nodeName)) {
length--;
}
});
if (length > 3) {
return true;
}
@@ -597,13 +578,13 @@ function isNonspecificChildBlock(blockNode: ts.Block, specificChildSet: Set<stri
if (ts.isBlock(item) && isNonspecificChildBlock(item, specificChildSet, allComponentNames)) {
return true;
}
if (ts.isExpressionStatement(item) && ts.isEtsComponentExpression(item.expression)) {
if (ts.isExpressionStatement(item)) {
let newNode: any = item.expression;
while (newNode.expression) {
if (ts.isEtsComponentExpression(newNode) && ts.isIdentifier(newNode.expression) &&
!isForEachComponent(newNode) && isComponent(newNode, allComponentNames)) {
const isNonspecific: boolean =
isNonspecificChildNonForEach(item.expression, specificChildSet);
isNonspecificChildNonForEach(newNode, specificChildSet);
if (isNonspecific) {
return isNonspecific;
}
@@ -849,20 +830,12 @@ export function preprocessExtend(content: string, extendCollection?: Set<string>
export function preprocessNewExtend(content: string, extendCollection?: Set<string>): string {
const REG_EXTEND: RegExp = /@Extend\s*\([^\)]+\)\s*function\s+([^\(\s]+)\s*\(/gm;
content.replace(REG_EXTEND, (item, item1) => {
return content.replace(REG_EXTEND, (item, item1) => {
if (extendCollection) {
extendCollection.add(item1);
}
return item;
});
const REG_BUILDER_EXTEND: RegExp = /@Builder\s*@Extend\s*\([^\)]+\)\s*function\s+([^\(\s]+)\s*\(/gm;
content.replace(REG_BUILDER_EXTEND, (item, item1) => {
if (extendCollection) {
extendCollection.delete(item1);
}
return item;
});
return content;
}
function getPackageInfo(configFile: string): Array<string> {
+1 -2
View File
@@ -14,9 +14,8 @@
*/
@Builder
@Extend(Text)
function textExtend(fontsize: number){
.fontSize(fontsize)
Text('Builder').fontSize(fontsize)
}
@Component
+1 -2
View File
@@ -14,9 +14,8 @@
*/
@Builder
@Styles
function textStyles(){
.backgroundColor(Color.Red)
Text('Builder').backgroundColor(Color.Red)
}
@Component
+2 -28
View File
@@ -38,10 +38,8 @@ struct ImportTest {
Text(this.testText2)
tStyles()
Button(this.testText3)
globalExtend(Color.Blue)
Text(this.testText4)
.fontSize(50)
globalStyles()
Base({
testStr: $testState1,
@@ -55,20 +53,6 @@ struct ImportTest {
}
}
}
@Builder
@Extend(Button)
function globalExtend(color: Color | string){
.backgroundColor(color)
.width(100)
.height(30)
}
@Builder
@Styles
function globalStyles(){
.backgroundColor('#abcdef')
}
`
exports.expectResult =
@@ -188,17 +172,15 @@ class ImportTest extends View {
Column.create();
Text.create(this.testText1);
Text.fontSize(50);
Text.pop();
ImportNestAll_1.tExtend(20);
Text.pop();
Text.create(this.testText2);
ImportNestAll_1.tStyles();
Text.pop();
ImportNestAll_1.tStyles();
Button.createWithLabel(this.testText3);
globalExtend(Color.Blue);
Button.pop();
Text.create(this.testText4);
Text.fontSize(50);
globalStyles();
Text.pop();
let earlierCreatedChild_2 = this.findChildById("2");
if (earlierCreatedChild_2 == undefined) {
@@ -226,13 +208,5 @@ class ImportTest extends View {
Column.pop();
}
}
function globalExtend(color) {
Button.backgroundColor(color);
Button.width(100);
Button.height(30);
}
function globalStyles() {
__Common__.backgroundColor('#abcdef');
}
loadDocument(new ImportTest("1", undefined, {}));
`