Add more tests; fix plugin conflict code

This commit is contained in:
Peeyush Kushwaha 2017-06-19 23:45:57 +05:30
parent b60ac52ae6
commit 04b40868f9
12 changed files with 472 additions and 4 deletions

View File

@ -47,6 +47,11 @@ const parserClassCache: { [key: string]: Class<Parser> } = {};
/** Get a Parser class with plugins applied. */
function getParserClass(pluginsFromOptions: $ReadOnlyArray<string>): Class<Parser> {
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<string>): Class<Parse
pluginList.unshift("estree");
}
if (pluginList.indexOf("decorators") >= 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) {

View File

@ -0,0 +1,6 @@
var foo = @dec class Bar {
@baz
bam() {
f();
}
}

View File

@ -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": []
}
}

View File

@ -0,0 +1,2 @@
export default
@bar class Foo { }

View File

@ -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": []
}
}

View File

@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@ -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

View File

@ -0,0 +1,4 @@
{
"plugins": ["decorators", "decoratorsStage2"],
"throws": "Cannot use decorators and decoratorsStage2 plugin together"
}

View File

@ -0,0 +1,2 @@
@(bar.baz)
class Foo {}

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token (1:1)"
}

View File

@ -0,0 +1,2 @@
@foo().bar
class Baz {}

View File

@ -0,0 +1,3 @@
{
"throws": "Leading decorators must be attached to a class declaration (1:6)"
}