Skip to content

Commit

Permalink
refactor(compiler): use constant decorator variable
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisCindy committed May 6, 2022
1 parent ba90189 commit 5dd3adf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/pwc-compiler/src/transform/autoAddAccessor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { File } from '@babel/types';
import * as t from '@babel/types';
import babelTraverse from '@babel/traverse';
import { REACTIVE_DECORATOR } from './autoAddReactiveDecorator';

function isIncludeReactiveDecorator(decorators: Array<t.Decorator>): boolean {
return decorators.some(decorator => t.isIdentifier(decorator.expression) && decorator.expression.name === '__reactive');
return decorators.some(decorator => t.isIdentifier(decorator.expression) && decorator.expression.name === REACTIVE_DECORATOR);
}

function createClassAccessorPropertyFromClassProperty(node: t.ClassProperty | t.ClassPrivateProperty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as t from '@babel/types';
import babelTraverse from '@babel/traverse';
import { toDash } from '../utils';

const CUSTOM_ELEMENT_DECORATOR = '__customElement';

// e.g. @customElement('__custom-component')
function createCallExpressionDecorator(decorator: string, argument) {
return t.decorator(t.callExpression(t.identifier(decorator), [t.stringLiteral(argument)]));
Expand All @@ -19,7 +21,7 @@ export default function autoAddCustomElementDecorator(ast: File): boolean {
if (!declaration.decorators || declaration.decorators.length === 0) {
const { id } = declaration;
// TODO:component name
declaration.decorators = [createCallExpressionDecorator('__customElement', toDash(id.name))];
declaration.decorators = [createCallExpressionDecorator(CUSTOM_ELEMENT_DECORATOR, toDash(id.name))];
} else {
shouldAutoAddCustomElementDecorator = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as babelParser from '@babel/parser';
import type { ValueDescriptor } from '../compileTemplate';
import { isEventName } from '../utils';

export const REACTIVE_DECORATOR = '__reactive';

const THIS_EXPRESSION_REG = /^this[.|[]/;

function isThisExpression(expression: string): boolean {
Expand Down Expand Up @@ -88,7 +90,7 @@ export default function autoAddReactiveDecorator(ast: File, values: ValueDescrip
const { node } = path;
if (t.isIdentifier(node.key) && classPropertyUsedInTemplate.includes(node.key.name)) {
if (!node.decorators || node.decorators.length === 0) {
node.decorators = [createIdentifierDecorator('__reactive')];
node.decorators = [createIdentifierDecorator(REACTIVE_DECORATOR)];
hasReactiveVariableInTemplate = true;
}
}
Expand All @@ -98,7 +100,7 @@ export default function autoAddReactiveDecorator(ast: File, values: ValueDescrip
const { node } = path;
if (t.isPrivateName(node.key) && classPropertyUsedInTemplate.includes(`#${node.key.id.name}`)) {
if (!node.decorators || node.decorators.length === 0) {
node.decorators = [createIdentifierDecorator('__reactive')];
node.decorators = [createIdentifierDecorator(REACTIVE_DECORATOR)];
hasReactiveVariableInTemplate = true;
}
}
Expand Down

0 comments on commit 5dd3adf

Please sign in to comment.