From 04b40868f9b84969c49369d1b93666f820a6ddd4 Mon Sep 17 00:00:00 2001 From: Peeyush Kushwaha Date: Mon, 19 Jun 2017 23:45:57 +0530 Subject: [PATCH] Add more tests; fix plugin conflict code --- src/index.js | 9 +- .../class-expression/actual.js | 6 + .../class-expression/expected.json | 308 ++++++++++++++++++ .../export-decorated-class/actual.js | 2 + .../export-decorated-class/expected.json | 132 ++++++++ .../export-decorated-class/options.json | 3 + .../plugin-conflict/actual.js | 2 + .../plugin-conflict/options.json | 4 + .../decorators-stage-2/restricted-1/actual.js | 2 + .../restricted-1/options.json | 3 + .../decorators-stage-2/restricted-2/actual.js | 2 + .../restricted-2/options.json | 3 + 12 files changed, 472 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/experimental/decorators-stage-2/class-expression/actual.js create mode 100644 test/fixtures/experimental/decorators-stage-2/class-expression/expected.json create mode 100644 test/fixtures/experimental/decorators-stage-2/export-decorated-class/actual.js create mode 100644 test/fixtures/experimental/decorators-stage-2/export-decorated-class/expected.json create mode 100644 test/fixtures/experimental/decorators-stage-2/export-decorated-class/options.json create mode 100644 test/fixtures/experimental/decorators-stage-2/plugin-conflict/actual.js create mode 100644 test/fixtures/experimental/decorators-stage-2/plugin-conflict/options.json create mode 100644 test/fixtures/experimental/decorators-stage-2/restricted-1/actual.js create mode 100644 test/fixtures/experimental/decorators-stage-2/restricted-1/options.json create mode 100644 test/fixtures/experimental/decorators-stage-2/restricted-2/actual.js create mode 100644 test/fixtures/experimental/decorators-stage-2/restricted-2/options.json diff --git a/src/index.js b/src/index.js index 8ebcb0c7be..0ebe931266 100755 --- a/src/index.js +++ b/src/index.js @@ -47,6 +47,11 @@ const parserClassCache: { [key: string]: Class } = {}; /** Get a Parser class with plugins applied. */ function getParserClass(pluginsFromOptions: $ReadOnlyArray): Class { + + if (pluginsFromOptions.indexOf("decorators") >= 0 && pluginsFromOptions.indexOf("decoratorsStage2") >= 0) { + throw new Error("Cannot use decorators and decoratorsStage2 plugin together"); + } + // Filter out just the plugins that have an actual mixin associated with them. let pluginList = pluginsFromOptions.filter((p) => p === "estree" || p === "flow" || p === "jsx"); @@ -62,10 +67,6 @@ function getParserClass(pluginsFromOptions: $ReadOnlyArray): Class= 0 && pluginList.indexOf("decoratorsStage2") >= 0) { - throw new Error("Cannot use decorators and decoratorsStage2 plugin together"); - } - const key = pluginList.join("/"); let cls = parserClassCache[key]; if (!cls) { diff --git a/test/fixtures/experimental/decorators-stage-2/class-expression/actual.js b/test/fixtures/experimental/decorators-stage-2/class-expression/actual.js new file mode 100644 index 0000000000..b8695c19cd --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/class-expression/actual.js @@ -0,0 +1,6 @@ +var foo = @dec class Bar { + @baz + bam() { + f(); + } +} diff --git a/test/fixtures/experimental/decorators-stage-2/class-expression/expected.json b/test/fixtures/experimental/decorators-stage-2/class-expression/expected.json new file mode 100644 index 0000000000..338ad4e54c --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/class-expression/expected.json @@ -0,0 +1,308 @@ +{ + "type": "File", + "start": 0, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "init": { + "type": "ClassExpression", + "start": 15, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "decorators": [ + { + "type": "Decorator", + "start": 10, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "expression": { + "type": "Identifier", + "start": 11, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "dec" + }, + "name": "dec" + } + } + ], + "id": { + "type": "Identifier", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "Bar" + }, + "name": "Bar" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 25, + "end": 58, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 36, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "decorators": [ + { + "type": "Decorator", + "start": 29, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "expression": { + "type": "Identifier", + "start": 30, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "baz" + }, + "name": "baz" + } + } + ], + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 36, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "bam" + }, + "name": "bam" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 42, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 48, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 8 + } + }, + "expression": { + "type": "CallExpression", + "start": 48, + "end": 51, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 7 + } + }, + "callee": { + "type": "Identifier", + "start": 48, + "end": 49, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + } + } + ], + "directives": [] + } + } + ] + } + } + } + ], + "kind": "var" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/decorators-stage-2/export-decorated-class/actual.js b/test/fixtures/experimental/decorators-stage-2/export-decorated-class/actual.js new file mode 100644 index 0000000000..80ba64f71a --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/export-decorated-class/actual.js @@ -0,0 +1,2 @@ +export default +@bar class Foo { } diff --git a/test/fixtures/experimental/decorators-stage-2/export-decorated-class/expected.json b/test/fixtures/experimental/decorators-stage-2/export-decorated-class/expected.json new file mode 100644 index 0000000000..a480a4682d --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/export-decorated-class/expected.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportDefaultDeclaration", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "declaration": { + "type": "ClassExpression", + "start": 21, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "decorators": [ + { + "type": "Decorator", + "start": 16, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 4 + } + }, + "expression": { + "type": "Identifier", + "start": 17, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 4 + }, + "identifierName": "bar" + }, + "name": "bar" + } + } + ], + "id": { + "type": "Identifier", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 31, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "body": [] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/experimental/decorators-stage-2/export-decorated-class/options.json b/test/fixtures/experimental/decorators-stage-2/export-decorated-class/options.json new file mode 100644 index 0000000000..2104ca4328 --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/export-decorated-class/options.json @@ -0,0 +1,3 @@ +{ + "sourceType": "module" +} diff --git a/test/fixtures/experimental/decorators-stage-2/plugin-conflict/actual.js b/test/fixtures/experimental/decorators-stage-2/plugin-conflict/actual.js new file mode 100644 index 0000000000..68f513e2af --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/plugin-conflict/actual.js @@ -0,0 +1,2 @@ +// we don't need any source code for this error to trigger, +// it should trigger just because both old and new decorator plugins exist together diff --git a/test/fixtures/experimental/decorators-stage-2/plugin-conflict/options.json b/test/fixtures/experimental/decorators-stage-2/plugin-conflict/options.json new file mode 100644 index 0000000000..b0e2270b4a --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/plugin-conflict/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["decorators", "decoratorsStage2"], + "throws": "Cannot use decorators and decoratorsStage2 plugin together" +} diff --git a/test/fixtures/experimental/decorators-stage-2/restricted-1/actual.js b/test/fixtures/experimental/decorators-stage-2/restricted-1/actual.js new file mode 100644 index 0000000000..cf35085f86 --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/restricted-1/actual.js @@ -0,0 +1,2 @@ +@(bar.baz) +class Foo {} diff --git a/test/fixtures/experimental/decorators-stage-2/restricted-1/options.json b/test/fixtures/experimental/decorators-stage-2/restricted-1/options.json new file mode 100644 index 0000000000..b5b75f4187 --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/restricted-1/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:1)" +} diff --git a/test/fixtures/experimental/decorators-stage-2/restricted-2/actual.js b/test/fixtures/experimental/decorators-stage-2/restricted-2/actual.js new file mode 100644 index 0000000000..8402c20e30 --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/restricted-2/actual.js @@ -0,0 +1,2 @@ +@foo().bar +class Baz {} diff --git a/test/fixtures/experimental/decorators-stage-2/restricted-2/options.json b/test/fixtures/experimental/decorators-stage-2/restricted-2/options.json new file mode 100644 index 0000000000..9fc4104fb3 --- /dev/null +++ b/test/fixtures/experimental/decorators-stage-2/restricted-2/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Leading decorators must be attached to a class declaration (1:6)" +}