Disallow in object literals, params, class props

And add tests to reflect the same
This commit is contained in:
Peeyush Kushwaha 2017-06-17 17:49:33 +05:30
parent 162bc905a6
commit 9c639743dd
33 changed files with 727 additions and 178 deletions

View File

@ -847,8 +847,16 @@ export default class ExpressionParser extends LValParser {
if (this.eat(tt.braceR)) break;
}
while (this.match(tt.at)) {
decorators.push(this.parseDecorator());
if (this.match(tt.at)) {
if (this.hasPlugin("decorators-stage-2")) {
this.raise(this.state.start, "decorators-stage-2 disallows object literal property decorators");
} else {
// we needn't check if decorators (stage 0) plugin is enabled since it's checked by
// the call to this.parseDecorator
while (this.match(tt.at)) {
decorators.push(this.parseDecorator());
}
}
}
let prop = this.startNode(), isGenerator = false, isAsync = false, startPos, startLoc;

View File

@ -187,6 +187,9 @@ export default class LValParser extends NodeUtils {
break;
} else {
const decorators = [];
if (this.match(tt.at) && this.hasPlugin("decorators-stage-2")) {
this.raise(this.state.start, "Stage 2 decorators cannot be used to decorate parameters");
}
while (this.match(tt.at)) {
decorators.push(this.parseDecorator());
}

View File

@ -709,6 +709,10 @@ export default class StatementParser extends ExpressionParser {
}
this.parseClassMember(classBody, member, state);
if (this.hasPlugin("decorators-stage-2") && member.kind != "method" && member.decorators && member.decorators.length > 0) {
this.raise(member.start, "Stage 2 decorators may only be used with a class or a class method");
}
}
if (decorators.length) {
@ -776,6 +780,7 @@ export default class StatementParser extends ExpressionParser {
if (!methodOrProp.computed && methodOrProp.static && (methodOrProp.key.name === "prototype" || methodOrProp.key.value === "prototype")) {
this.raise(methodOrProp.key.start, "Classes may not have static property named prototype");
}
if (this.isClassMethod()) {
// a normal method
if (this.isNonstaticConstructor(method)) {

View File

@ -0,0 +1,2 @@
@foo('bar')
class Foo {}

View File

@ -0,0 +1,154 @@
{
"type": "File",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"program": {
"type": "Program",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 12,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"decorators": [
{
"type": "Decorator",
"start": 0,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 11
}
},
"expression": {
"type": "CallExpression",
"start": 1,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 11
}
},
"callee": {
"type": "Identifier",
"start": 1,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "foo"
},
"name": "foo"
},
"arguments": [
{
"type": "StringLiteral",
"start": 5,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 10
}
},
"extra": {
"rawValue": "bar",
"raw": "'bar'"
},
"value": "bar"
}
]
}
}
],
"id": {
"type": "Identifier",
"start": 18,
"end": 21,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 22,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 12
}
},
"body": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
@abc
class Foo {
}

View File

@ -0,0 +1,117 @@
{
"type": "File",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 5,
"end": 19,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"decorators": [
{
"type": "Decorator",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"expression": {
"type": "Identifier",
"start": 1,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "abc"
},
"name": "abc"
}
}
],
"id": {
"type": "Identifier",
"start": 11,
"end": 14,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "Foo"
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 15,
"end": 19,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 4,
"column": 1
}
},
"body": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
class Foo {
constructor(@foo x) {}
}

View File

@ -0,0 +1,3 @@
{
"throws": "Stage 2 decorators cannot be used to decorate parameters (2:14)"
}

View File

@ -0,0 +1,3 @@
class Foo {
@foo[bar] a = 1;
}

View File

@ -0,0 +1,4 @@
{
"plugins": ["decorators-stage-2", "classProperties"],
"throws": "Unexpected token (2:12)"
}

View File

@ -0,0 +1,6 @@
class Foo {
@bar[bizz]
abc() {
}
}

View File

@ -0,0 +1,4 @@
{
"plugins": ["decorators-stage-2", "classProperties"],
"throws": "Stage 2 decorators may only be used with a class or a class method (2:6)"
}

View File

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

View File

@ -0,0 +1,116 @@
{
"type": "File",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 23
}
},
"program": {
"type": "Program",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 23
}
},
"sourceType": "module",
"body": [
{
"type": "ExportDefaultDeclaration",
"start": 5,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 23
}
},
"declaration": {
"type": "ClassDeclaration",
"start": 20,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 23
}
},
"decorators": [
{
"type": "Decorator",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
}
},
"expression": {
"type": "Identifier",
"start": 1,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "foo"
},
"name": "foo"
}
}
],
"id": null,
"superClass": null,
"body": {
"type": "ClassBody",
"start": 26,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 21
},
"end": {
"line": 2,
"column": 23
}
},
"body": []
}
}
}
],
"directives": []
}
}

View File

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

View File

@ -0,0 +1,7 @@
@ParentDecorator
export default class ParentClass {
makeNestedClass() {
class NestedClass {
}
}
}

View File

@ -0,0 +1,240 @@
{
"type": "File",
"start": 0,
"end": 109,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 7,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 109,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 7,
"column": 1
}
},
"sourceType": "module",
"body": [
{
"type": "ExportDefaultDeclaration",
"start": 17,
"end": 109,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 7,
"column": 1
}
},
"declaration": {
"type": "ClassDeclaration",
"start": 32,
"end": 109,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 7,
"column": 1
}
},
"decorators": [
{
"type": "Decorator",
"start": 0,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 16
}
},
"expression": {
"type": "Identifier",
"start": 1,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 16
},
"identifierName": "ParentDecorator"
},
"name": "ParentDecorator"
}
}
],
"id": {
"type": "Identifier",
"start": 38,
"end": 49,
"loc": {
"start": {
"line": 2,
"column": 21
},
"end": {
"line": 2,
"column": 32
},
"identifierName": "ParentClass"
},
"name": "ParentClass"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 50,
"end": 109,
"loc": {
"start": {
"line": 2,
"column": 33
},
"end": {
"line": 7,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 54,
"end": 107,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 6,
"column": 3
}
},
"static": false,
"computed": false,
"key": {
"type": "Identifier",
"start": 54,
"end": 69,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 17
},
"identifierName": "makeNestedClass"
},
"name": "makeNestedClass"
},
"kind": "method",
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 72,
"end": 107,
"loc": {
"start": {
"line": 3,
"column": 20
},
"end": {
"line": 6,
"column": 3
}
},
"body": [
{
"type": "ClassDeclaration",
"start": 78,
"end": 103,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 5,
"column": 5
}
},
"id": {
"type": "Identifier",
"start": 84,
"end": 95,
"loc": {
"start": {
"line": 4,
"column": 10
},
"end": {
"line": 4,
"column": 21
},
"identifierName": "NestedClass"
},
"name": "NestedClass"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 96,
"end": 103,
"loc": {
"start": {
"line": 4,
"column": 22
},
"end": {
"line": 5,
"column": 5
}
},
"body": []
}
}
],
"directives": []
}
}
]
}
}
}
],
"directives": []
}
}

View File

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

View File

@ -0,0 +1 @@
function func(@foo x) {}

View File

@ -0,0 +1,3 @@
{
"throws": "Stage 2 decorators cannot be used to decorate parameters (1:14)"
}

View File

@ -0,0 +1,4 @@
class Foo {
@abc
constructor(){}
}

View File

@ -0,0 +1,3 @@
{
"throws": "You can't attach decorators to a class constructor (3:2)"
}

View File

@ -0,0 +1,2 @@
@foo
export default function f(){};

View File

@ -0,0 +1,4 @@
{
"throws": "You can only use decorators on an export when exporting a class (2:0)",
"sourceType": "module"
}

View File

@ -0,0 +1,2 @@
@foo
export default 0;

View File

@ -0,0 +1,4 @@
{
"throws": "You can only use decorators on an export when exporting a class (2:0)",
"sourceType": "module"
}

View File

@ -0,0 +1,4 @@
class A {
@a;
m(){}
}

View File

@ -0,0 +1,3 @@
{
"throws": "Decorators must not be followed by a semicolon (2:5)"
}

View File

@ -0,0 +1,3 @@
var obj = {
method(@foo x) {}
};

View File

@ -0,0 +1,3 @@
{
"throws": "Stage 2 decorators cannot be used to decorate parameters (2:9)"
}

View File

@ -1,175 +0,0 @@
{
"type": "File",
"start": 0,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "A"
},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 31,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassProperty",
"start": 17,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 19
}
},
"decorators": [
{
"type": "Decorator",
"start": 12,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 6
}
},
"expression": {
"type": "Identifier",
"start": 13,
"end": 16,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 6
},
"identifierName": "dec"
},
"name": "dec"
}
}
],
"static": false,
"computed": true,
"key": {
"type": "StringLiteral",
"start": 18,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 14
}
},
"extra": {
"rawValue": "name",
"raw": "'name'"
},
"value": "name"
},
"value": {
"type": "NumericLiteral",
"start": 28,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 18
},
"end": {
"line": 2,
"column": 19
}
},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
]
}
}
],
"directives": []
}
}

View File

@ -1,3 +1,4 @@
{
"plugins": ["classProperties", "decorators-stage-2"]
"plugins": ["classProperties", "decorators-stage-2"],
"throws": "Stage 2 decorators may only be used with a class or a class method (2:7)"
}