Skip to content

Commit 054a94e

Browse files
committed
Always pass through object literal decorators on methods
1 parent 6f9e491 commit 054a94e

10 files changed

Lines changed: 38 additions & 14 deletions

transforms/ember-object/__testfixtures__/object-literal-with-decorators-invalid.input.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ const Foo2 = EmberObject.extend({
1212
const Foo3 = EmberObject.extend({
1313
@tracked arr: [1, 2, 3],
1414
});
15+
16+
// Do not function expression if not on allowlist
17+
const Foo4 = EmberObject.extend({
18+
@userAdded methodish: () => {},
19+
});

transforms/ember-object/__testfixtures__/object-literal-with-decorators-invalid.output.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ const Foo2 = EmberObject.extend({
1111
// Do not transform array
1212
const Foo3 = EmberObject.extend({
1313
@tracked arr: [1, 2, 3],
14+
});
15+
16+
// Do not function expression if not on allowlist
17+
const Foo4 = EmberObject.extend({
18+
@userAdded methodish: () => {},
1419
});

transforms/ember-object/__testfixtures__/object-literal-with-decorators.input.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,9 @@ const Foo = EmberObject.extend({
9797
bar() {
9898
// Executes whenever barEvent is emitted
9999
},
100+
101+
@userAdded
102+
yolo() {
103+
// methods always pass through decorators, even if not on allow-list
104+
}
100105
});

transforms/ember-object/__testfixtures__/object-literal-with-decorators.output.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,9 @@ class Foo extends EmberObject {
157157
bar() {
158158
// Executes whenever barEvent is emitted
159159
}
160+
161+
@userAdded
162+
yolo() {
163+
// methods always pass through decorators, even if not on allow-list
164+
}
160165
}

transforms/ember-object/__testfixtures__/options/object-literal-decorators.input.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@ import EmberObject from '@ember/object';
22

33
const Foo = EmberObject.extend({
44
@userAdded
5-
toggleShowing() {
6-
set(this, 'isShowing', !this.isShowing);
7-
}
5+
whatever: undefined
86
});

transforms/ember-object/__testfixtures__/options/object-literal-decorators.output.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@ import EmberObject from '@ember/object';
44
@classic
55
class Foo extends EmberObject {
66
@userAdded
7-
toggleShowing() {
8-
set(this, 'isShowing', !this.isShowing);
9-
}
7+
whatever;
108
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ export default abstract class AbstractEOProp<
3838

3939
protected readonly runtimeData: RuntimeData;
4040

41-
/** Override to `true` if the property type supports object literal decorators. */
42-
protected readonly supportsObjectLiteralDecorators: boolean = false;
41+
/**
42+
* Override to `true` if the property type supports object literal decorators
43+
* or to `'withVerification'` if the property type supports object literal
44+
* decorators as long as they are on the allow-list.
45+
*/
46+
protected readonly objectLiteralDecoratorSupport:
47+
| boolean
48+
| 'withVerification' = false;
4349

4450
constructor(
4551
protected readonly rawProp: P & {
@@ -95,7 +101,7 @@ export default abstract class AbstractEOProp<
95101
const { decorators, objectLiteralDecorators } = this.options;
96102

97103
if (this.existingDecorators) {
98-
if (!this.supportsObjectLiteralDecorators) {
104+
if (!this.objectLiteralDecoratorSupport) {
99105
errors.push(
100106
this.makeError(
101107
'can only transform object literal decorators on methods or properties with literal values (string, number, boolean, null, undefined)'
@@ -116,12 +122,12 @@ export default abstract class AbstractEOProp<
116122
this.makeError('decorator expression type not supported')
117123
);
118124
} else if (
119-
// TODO: Don't check this for EOMethodProp
125+
this.objectLiteralDecoratorSupport === 'withVerification' &&
120126
!allowObjectLiteralDecorator(decoratorName, objectLiteralDecorators)
121127
) {
122128
errors.push(
123129
this.makeError(
124-
"decorator '@${decoratorName}' not included in ALLOWED_OBJECT_LITERAL_DECORATORS or option '--objectLiteralDecorators'"
130+
`decorator '@${decoratorName}' not included in ALLOWED_OBJECT_LITERAL_DECORATORS or option '--objectLiteralDecorators'`
125131
)
126132
);
127133
}

transforms/helpers/eo-prop/private/function-expression.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export default class EOFunctionExpressionProp extends AbstractEOProp<
3939

4040
protected readonly value = this.rawProp.value;
4141

42-
protected override readonly supportsObjectLiteralDecorators = true;
42+
protected override readonly objectLiteralDecoratorSupport =
43+
'withVerification' as const;
4344

4445
build(): AST.ClassMethod {
4546
return replaceMethodSuperExpressions(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default class EOMethod extends AbstractEOProp<
4343

4444
protected readonly value = this.rawProp;
4545

46-
protected override readonly supportsObjectLiteralDecorators = true;
46+
protected override readonly objectLiteralDecoratorSupport = true;
4747

4848
build(): AST.ClassMethod {
4949
return replaceMethodSuperExpressions(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export default class EOSimpleProp extends AbstractEOProp<
4545

4646
protected readonly value = this.rawProp.value;
4747

48-
protected override readonly supportsObjectLiteralDecorators = true;
48+
protected override readonly objectLiteralDecoratorSupport =
49+
'withVerification' as const;
4950

5051
build(): AST.ClassProperty {
5152
const classProp = j.classProperty.from({

0 commit comments

Comments
 (0)