Skip to content

Commit e823847

Browse files
committed
Clean up EOProp.decorators
1 parent 78d3abf commit e823847

4 files changed

Lines changed: 20 additions & 29 deletions

File tree

transforms/helpers/decorator-info.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ export interface DecoratorImportInfo {
99
isMetaDecorator?: boolean;
1010
isMethodDecorator?: boolean;
1111
localName?: string;
12+
args?: Array<string | boolean | number | null>;
1213
}
14+
1315
export type DecoratorImportInfoMap = Map<
1416
/** local name */ string,
1517
DecoratorImportInfo

transforms/helpers/eo-prop/private/abstract.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ import {
1515
allowObjectLiteralDecorator,
1616
} from '../../util/index';
1717

18-
interface EODecoratorArgs {
19-
unobserves?: Array<string | boolean | number | null> | undefined;
20-
off?: Array<string | boolean | number | null> | undefined;
21-
}
22-
2318
type EOPropValue = EOProperty['value'] | EOMethod;
2419

2520
/**
@@ -34,7 +29,6 @@ export default abstract class AbstractEOProp<P extends EOExpressionProp, B> {
3429
};
3530

3631
protected decorators: DecoratorImportInfo[] = [];
37-
readonly decoratorArgs: EODecoratorArgs = {};
3832

3933
/** Runtime Data */
4034
readonly runtimeData: RuntimeData;
@@ -47,14 +41,17 @@ export default abstract class AbstractEOProp<P extends EOExpressionProp, B> {
4741
if (this.runtimeData.type) {
4842
const { type, offProperties, unobservedProperties } = this.runtimeData;
4943

50-
const name = this.name;
51-
if (name in unobservedProperties) {
52-
this.decorators.push({ name: 'unobserves' });
53-
this.decoratorArgs.unobserves = unobservedProperties[name];
44+
const unobservedArgs = unobservedProperties[this.name];
45+
if (unobservedArgs) {
46+
this.decorators.push({
47+
name: 'unobserves',
48+
args: unobservedArgs,
49+
});
5450
}
55-
if (name in offProperties) {
56-
this.decorators.push({ name: 'off' });
57-
this.decoratorArgs.off = offProperties[name];
51+
52+
const offArgs = offProperties[this.name];
53+
if (offArgs) {
54+
this.decorators.push({ name: 'off', args: offArgs });
5855
}
5956
this.runtimeType = type;
6057
}
@@ -124,20 +121,16 @@ export default abstract class AbstractEOProp<P extends EOExpressionProp, B> {
124121
return this._prop.decorators ?? null;
125122
}
126123

127-
get decoratorNames(): string[] {
128-
return this.decorators.map((d) => d.name);
129-
}
130-
131124
get hasDecorators(): boolean {
132125
return this.decorators.length > 0;
133126
}
134127

135128
get hasUnobservesDecorator(): boolean {
136-
return this.decoratorNames.includes('unobserves');
129+
return this.decorators.some((d) => d.name === 'unobserves');
137130
}
138131

139132
get hasOffDecorator(): boolean {
140-
return this.decoratorNames.includes('off');
133+
return this.decorators.some((d) => d.name === 'off');
141134
}
142135

143136
get hasMetaDecorator(): boolean {

transforms/helpers/eo-prop/private/call-expression/abstract.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export default abstract class AbstractEOCallExpressionProp<
1818
> extends AbstractEOProp<EOPropertyWithCallExpression, B> {
1919
protected buildDecorators(): Decorator[] {
2020
const decorators: Decorator[] = [];
21-
for (const decoratorName of this.decoratorNames) {
21+
for (const decorator of this.decorators) {
22+
const decoratorName = decorator.name;
2223
if (this.isVolatileReadOnly) {
2324
logger.info(`[${this.name}] Ignored decorator ${decoratorName}`);
2425
} else {

transforms/helpers/eo-prop/private/simple.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { default as j } from 'jscodeshift';
22
import type { ClassProperty, Decorator, EOPropertySimple } from '../../ast';
33
import { createDecoratorWithArgs } from '../../decorator-helper';
44
import logger from '../../log-helper';
5-
import { defined } from '../../util/types';
65
import AbstractEOProp from './abstract';
76

87
export default class EOSimpleProp extends AbstractEOProp<
@@ -33,14 +32,10 @@ export default class EOSimpleProp extends AbstractEOProp<
3332

3433
protected buildDecorators(): Decorator[] {
3534
const decorators: Decorator[] = [];
36-
for (const decoratorName of this.decoratorNames) {
37-
if (decoratorName === 'off' || decoratorName === 'unobserves') {
38-
decorators.push(
39-
createDecoratorWithArgs(
40-
decoratorName,
41-
defined(this.decoratorArgs[decoratorName])
42-
)
43-
);
35+
for (const decorator of this.decorators) {
36+
const decoratorName = decorator.name;
37+
if ('args' in decorator) {
38+
decorators.push(createDecoratorWithArgs(decoratorName, decorator.args));
4439
} else {
4540
logger.info(`[${this.name}] Ignored decorator ${decoratorName}`);
4641
}

0 commit comments

Comments
 (0)