From 4965a790cb358e004a2ea7ffa8902391332194f2 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 4 Apr 2023 09:07:15 +0800 Subject: [PATCH] Forbid decorators on object property (#14661) --- src/language-js/parse/babel.js | 1 - .../traverse/visitor-keys.evaluate.js | 3 -- .../__snapshots__/jsfmt.spec.js.snap | 48 ++++++++----------- tests/format/js/decorators/jsfmt.spec.js | 2 +- tests/format/js/decorators/multiple.js | 11 +++-- tests/unit/__snapshots__/visitor-keys.js.snap | 1 - 6 files changed, 28 insertions(+), 38 deletions(-) diff --git a/src/language-js/parse/babel.js b/src/language-js/parse/babel.js index 432843004..8f2efee10 100644 --- a/src/language-js/parse/babel.js +++ b/src/language-js/parse/babel.js @@ -199,7 +199,6 @@ const allowedMessageCodes = new Set([ "LineTerminatorBeforeArrow", "InvalidEscapeSequenceTemplate", "NonAbstractClassHasAbstractMethod", - "UnsupportedPropertyDecorator", "OptionalTypeBeforeRequired", "PatternIsOptional", "OptionalBindingPattern", diff --git a/src/language-js/traverse/visitor-keys.evaluate.js b/src/language-js/traverse/visitor-keys.evaluate.js index cdd1ca54b..079dceb26 100644 --- a/src/language-js/traverse/visitor-keys.evaluate.js +++ b/src/language-js/traverse/visitor-keys.evaluate.js @@ -47,9 +47,6 @@ const additionalVisitorKeys = { NeverTypeAnnotation: [], UndefinedTypeAnnotation: [], UnknownTypeAnnotation: [], - - // Unknown - Property: ["decorators"], }; const excludeKeys = { diff --git a/tests/format/js/decorators/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/decorators/__snapshots__/jsfmt.spec.js.snap index 03dcdd68a..2206dd70a 100644 --- a/tests/format/js/decorators/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/decorators/__snapshots__/jsfmt.spec.js.snap @@ -579,32 +579,22 @@ class Foo { exports[`multiple.js [acorn] format 1`] = ` "Unexpected character '@' (2:3) - 1 | const dog = { + 1 | const dog = class { > 2 | @readonly | ^ 3 | @nonenumerable 4 | @doubledValue - 5 | legs: 4," + 5 | legs = 4;" `; exports[`multiple.js [espree] format 1`] = ` "Unexpected character '@' (2:3) - 1 | const dog = { + 1 | const dog = class { > 2 | @readonly | ^ 3 | @nonenumerable 4 | @doubledValue - 5 | legs: 4," -`; - -exports[`multiple.js [meriyah] format 1`] = ` -"Unexpected token: '@' (2:3) - 1 | const dog = { -> 2 | @readonly - | ^ - 3 | @nonenumerable - 4 | @doubledValue - 5 | legs: 4," + 5 | legs = 4;" `; exports[`multiple.js format 1`] = ` @@ -613,40 +603,44 @@ parsers: ["babel"] printWidth: 80 | printWidth =====================================input====================================== -const dog = { +const dog = class { @readonly @nonenumerable @doubledValue - legs: 4, + legs = 4; @readonly @nonenumerable @doubledValue - eyes: 2 + eyes() {return 2} }; -const foo = { - @multipleDecorators @inline @theyWontAllFitInOneline aVeryLongPropName: "A very long string as value" +const foo = class { + @multipleDecorators @inline @theyWontAllFitInOneline aVeryLongPropName = "A very long string as value" + @multipleDecorators @inline @theyWontAllFitInOneline aVeryLongPropName() { "A very long string as value"} }; =====================================output===================================== -const dog = { +const dog = class { @readonly @nonenumerable @doubledValue - legs: 4, + legs = 4; @readonly @nonenumerable @doubledValue - eyes: 2, + eyes() { + return 2; + } }; -const foo = { - @multipleDecorators - @inline - @theyWontAllFitInOneline - aVeryLongPropName: "A very long string as value", +const foo = class { + @multipleDecorators @inline @theyWontAllFitInOneline aVeryLongPropName = + "A very long string as value"; + @multipleDecorators @inline @theyWontAllFitInOneline aVeryLongPropName() { + "A very long string as value"; + } }; ================================================================================ diff --git a/tests/format/js/decorators/jsfmt.spec.js b/tests/format/js/decorators/jsfmt.spec.js index 5385acf75..14499d80c 100644 --- a/tests/format/js/decorators/jsfmt.spec.js +++ b/tests/format/js/decorators/jsfmt.spec.js @@ -1,3 +1,3 @@ run_spec(import.meta, ["babel"], { - errors: { acorn: true, espree: true, meriyah: ["multiple.js"] }, + errors: { acorn: true, espree: true }, }); diff --git a/tests/format/js/decorators/multiple.js b/tests/format/js/decorators/multiple.js index f35b7018f..0a83753c9 100644 --- a/tests/format/js/decorators/multiple.js +++ b/tests/format/js/decorators/multiple.js @@ -1,15 +1,16 @@ -const dog = { +const dog = class { @readonly @nonenumerable @doubledValue - legs: 4, + legs = 4; @readonly @nonenumerable @doubledValue - eyes: 2 + eyes() {return 2} }; -const foo = { - @multipleDecorators @inline @theyWontAllFitInOneline aVeryLongPropName: "A very long string as value" +const foo = class { + @multipleDecorators @inline @theyWontAllFitInOneline aVeryLongPropName = "A very long string as value" + @multipleDecorators @inline @theyWontAllFitInOneline aVeryLongPropName() { "A very long string as value"} }; diff --git a/tests/unit/__snapshots__/visitor-keys.js.snap b/tests/unit/__snapshots__/visitor-keys.js.snap index fdc17281f..c8654c7c2 100644 --- a/tests/unit/__snapshots__/visitor-keys.js.snap +++ b/tests/unit/__snapshots__/visitor-keys.js.snap @@ -613,7 +613,6 @@ exports[`visitor keys estree 1`] = ` "Property": [ "key", "value", - "decorators", ], "PropertyDefinition": [ "decorators",