Skip to content

Commit

Permalink
chore(decorator): use getDeclarationParameters to parse prop options
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Aug 12, 2020
1 parent e11bb52 commit 3b4211e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 38 deletions.
Expand Up @@ -47,7 +47,7 @@ export const visitClassDeclaration = (config: d.Config, diagnostics: d.Diagnosti
const watchable = new Set<string>();
if (decoratedMembers.length > 0) {
propDecoratorsToStatic(diagnostics, decoratedMembers, typeChecker, watchable, newMembers);
stateDecoratorsToStatic(diagnostics, decoratedMembers, typeChecker, watchable, newMembers);
stateDecoratorsToStatic(decoratedMembers, watchable, newMembers);
eventDecoratorsToStatic(diagnostics, decoratedMembers, typeChecker, newMembers);
methodDecoratorsToStatic(config, diagnostics, classNode, decoratedMembers, typeChecker, newMembers);
elementDecoratorsToStatic(diagnostics, decoratedMembers, typeChecker, newMembers);
Expand Down
Expand Up @@ -22,24 +22,23 @@ const parseEventDecorator = (diagnostics: d.Diagnostic[], typeChecker: ts.TypeCh
return null;
}

const [opts] = getDeclarationParameters<d.EventOptions>(eventDecorator);

const memberName = prop.name.getText();
if (!memberName) {
return null;
}

const [eventOpts] = getDeclarationParameters<d.EventOptions>(eventDecorator);
const symbol = typeChecker.getSymbolAtLocation(prop.name);
const name = getEventName(opts, memberName);
const eventName = getEventName(eventOpts, memberName);

validateEventName(diagnostics, prop.name, name);
validateEventName(diagnostics, prop.name, eventName);

const eventMeta = {
method: memberName,
name,
bubbles: opts && typeof opts.bubbles === 'boolean' ? opts.bubbles : true,
cancelable: opts && typeof opts.cancelable === 'boolean' ? opts.cancelable : true,
composed: opts && typeof opts.composed === 'boolean' ? opts.composed : true,
name: eventName,
bubbles: eventOpts && typeof eventOpts.bubbles === 'boolean' ? eventOpts.bubbles : true,
cancelable: eventOpts && typeof eventOpts.cancelable === 'boolean' ? eventOpts.cancelable : true,
composed: eventOpts && typeof eventOpts.composed === 'boolean' ? eventOpts.composed : true,
docs: serializeSymbol(typeChecker, symbol),
complexType: getComplexType(typeChecker, prop),
};
Expand Down
26 changes: 5 additions & 21 deletions src/compiler/transformers/decorators-to-static/prop-decorator.ts
@@ -1,5 +1,5 @@
import type * as d from '../../../declarations';
import { augmentDiagnosticWithNode, buildError, buildWarn, catchError, toDashCase } from '@utils';
import { augmentDiagnosticWithNode, buildError, buildWarn, toDashCase } from '@utils';
import {
convertValueToLiteral,
createStaticGetter,
Expand All @@ -10,7 +10,7 @@ import {
typeToString,
validateReferences,
} from '../transform-utils';
import { isDecoratorNamed } from './decorator-utils';
import { isDecoratorNamed, getDeclarationParameters } from './decorator-utils';
import { validatePublicName } from '../reserved-public-members';
import ts from 'typescript';

Expand All @@ -37,8 +37,10 @@ const parsePropDecorator = (diagnostics: d.Diagnostic[], typeChecker: ts.TypeChe
return null;
}

const decoratorParms = getDeclarationParameters<d.PropOptions>(propDecorator);
const propOptions: d.PropOptions = decoratorParms[0] || {};

const propName = prop.name.getText();
const propOptions = getPropOptions(propDecorator, diagnostics);

if (isMemberPrivate(prop)) {
const err = buildError(diagnostics);
Expand Down Expand Up @@ -111,24 +113,6 @@ const getReflect = (diagnostics: d.Diagnostic[], propDecorator: ts.Decorator, pr
return false;
};

const getPropOptions = (propDecorator: ts.Decorator, diagnostics: d.Diagnostic[]) => {
if (propDecorator.expression == null) {
return {};
}

const suppliedOptions = (propDecorator.expression as ts.CallExpression).arguments.map(arg => {
try {
const fnStr = `return ${arg.getText()};`;
return new Function(fnStr)();
} catch (e) {
catchError(diagnostics, e, `parse prop options: ${e}`);
}
});

const propOptions: d.PropOptions = suppliedOptions[0];
return propOptions || {};
};

const getComplexType = (typeChecker: ts.TypeChecker, node: ts.PropertyDeclaration, type: ts.Type): d.ComponentCompilerPropertyComplexType => {
const nodeType = node.type;
return {
Expand Down
@@ -1,15 +1,8 @@
import type * as d from '../../../declarations';
import { createStaticGetter } from '../transform-utils';
import { isDecoratorNamed } from './decorator-utils';
import ts from 'typescript';

export const stateDecoratorsToStatic = (
_diagnostics: d.Diagnostic[],
decoratedProps: ts.ClassElement[],
_typeChecker: ts.TypeChecker,
watchable: Set<string>,
newMembers: ts.ClassElement[],
) => {
export const stateDecoratorsToStatic = (decoratedProps: ts.ClassElement[], watchable: Set<string>, newMembers: ts.ClassElement[]) => {
const states = decoratedProps
.filter(ts.isPropertyDeclaration)
.map(prop => stateDecoratorToStatic(prop, watchable))
Expand Down
1 change: 1 addition & 0 deletions src/compiler/transformers/test/transpile.ts
Expand Up @@ -83,6 +83,7 @@ export function transpileModule(
currentDirectory: '/',
proxy: null,
style: 'static',
styleImportData: 'queryparams',
};

tsProgram.emit(undefined, undefined, undefined, undefined, {
Expand Down

0 comments on commit 3b4211e

Please sign in to comment.