Skip to content

Commit

Permalink
noLabeledExpressionStatements plugin added
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Oct 3, 2017
1 parent 6ff25b9 commit 210de1f
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 48 deletions.
3 changes: 3 additions & 0 deletions src/parser/statement.js
Expand Up @@ -599,6 +599,9 @@ pp.parseLabeledStatement = function (node, maybeName, expr) {
this.state.labels.push({ name: maybeName, kind: kind, statementStart: this.state.start });
node.body = this.parseStatement(true);
this.state.labels.pop();
if (node.body.type === "ExpressionStatement" && this.hasPlugin("noLabeledExpressionStatements")) {
this.raise(expr.start, "Labeled expressions are illegal.");
}
node.label = expr;
return this.finishNode(node, "LabeledStatement");
};
Expand Down
2 changes: 2 additions & 0 deletions src/registerPlugins.js
Expand Up @@ -98,4 +98,6 @@ export default function registerPlugins(plugins, metadata) {
registerPlugin("whiteblockPreferred", noncePlugin, {
dependencies: ["lightscript"]
});

registerPlugin("noLabeledExpressionStatements", noncePlugin);
}
2 changes: 1 addition & 1 deletion test/fixtures/comments/options.json
Expand Up @@ -7,7 +7,7 @@
},
"noLsc": {
"allPlugins": true,
"excludePlugins": ["lightscript", "match", "splatComprehension", "whiteblockOnly", "whiteblockPreferred"]
"excludePlugins": ["lightscript", "noLabeledExpressionStatements"]
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/core/options.json
Expand Up @@ -8,7 +8,7 @@
},
"noLsc": {
"allPlugins": true,
"excludePlugins": ["lightscript"]
"excludePlugins": ["lightscript", "noLabeledExpressionStatements"]
}
}
}
7 changes: 7 additions & 0 deletions test/fixtures/core/uncategorised/323/options.json
@@ -0,0 +1,7 @@
{
"alternatives": {
"all": {
"throws": "Labeled expressions are illegal. (1:0)"
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/es2015/options.json
Expand Up @@ -8,7 +8,7 @@
},
"noLsc": {
"allPlugins": true,
"excludePlugins": ["lightscript"]
"excludePlugins": ["lightscript", "noLabeledExpressionStatements"]
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/es2017/options.json
Expand Up @@ -8,7 +8,7 @@
},
"noLsc": {
"allPlugins": true,
"excludePlugins": ["lightscript"]
"excludePlugins": ["lightscript", "noLabeledExpressionStatements"]
}
}
}
2 changes: 1 addition & 1 deletion test/fixtures/esprima/options.json
Expand Up @@ -8,7 +8,7 @@
},
"noLsc": {
"allPlugins": true,
"excludePlugins": ["lightscript"]
"excludePlugins": ["lightscript", "noLabeledExpressionStatements"]
}
}
}
@@ -0,0 +1,7 @@
{
"alternatives": {
"all": {
"throws": "Labeled expressions are illegal. (1:0)"
}
}
}
@@ -1,3 +1,3 @@
a?
b:
c
while (true) {}
@@ -1,29 +1,29 @@
{
"type": "File",
"start": 0,
"end": 7,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
"column": 15
}
},
"program": {
"type": "Program",
"start": 0,
"end": 7,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
"column": 15
}
},
"sourceType": "script",
Expand Down Expand Up @@ -78,47 +78,78 @@
{
"type": "LabeledStatement",
"start": 3,
"end": 7,
"end": 21,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 3,
"column": 1
"column": 15
}
},
"body": {
"type": "ExpressionStatement",
"type": "WhileStatement",
"start": 6,
"end": 7,
"end": 21,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 1
"column": 15
}
},
"expression": {
"type": "Identifier",
"start": 6,
"end": 7,
"test": {
"type": "BooleanLiteral",
"start": 13,
"end": 17,
"loc": {
"start": {
"line": 3,
"column": 0
"column": 7
},
"end": {
"line": 3,
"column": 1
"column": 11
}
},
"value": true,
"extra": {}
},
"body": {
"type": "ExpressionStatement",
"start": 19,
"end": 21,
"loc": {
"start": {
"line": 3,
"column": 13
},
"identifierName": "c"
"end": {
"line": 3,
"column": 15
}
},
"name": "c"
"expression": {
"type": "ObjectExpression",
"start": 19,
"end": 21,
"loc": {
"start": {
"line": 3,
"column": 13
},
"end": {
"line": 3,
"column": 15
}
},
"properties": []
}
}
},
"label": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/flow/options.json
Expand Up @@ -10,7 +10,7 @@
},
"noLsc": {
"allPlugins": true,
"excludePlugins": ["lightscript"]
"excludePlugins": ["lightscript", "noLabeledExpressionStatements"]
}
}
}
@@ -0,0 +1,6 @@
{
a
b: c
d
if true: e
}
@@ -0,0 +1,13 @@
{
"alternatives": {
"all": {
"throws": "Unexpected token, expected , (5:5)"
},
"whiteblockOnly": {
"throws": "Unexpected token, expected , (5:5)"
},
"braceblock": {
"throws": "Unexpected token, expected , (5:5)"
}
}
}
@@ -1,2 +1,2 @@
match x:
| if guard: Atom: 1
| if guard: Atom: while(true) {}
@@ -1,45 +1,45 @@
{
"type": "File",
"start": 0,
"end": 30,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 21
"column": 34
}
},
"program": {
"type": "Program",
"start": 0,
"end": 30,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 21
"column": 34
}
},
"sourceType": "script",
"body": [
{
"type": "MatchStatement",
"start": 0,
"end": 30,
"end": 43,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 21
"column": 34
}
},
"discriminant": {
Expand All @@ -63,15 +63,15 @@
{
"type": "MatchCase",
"start": 11,
"end": 30,
"end": 43,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 21
"column": 34
}
},
"outerGuard": {
Expand All @@ -94,50 +94,67 @@
"consequent": {
"type": "LabeledStatement",
"start": 23,
"end": 30,
"end": 43,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 21
"column": 34
}
},
"body": {
"type": "ExpressionStatement",
"type": "WhileStatement",
"start": 29,
"end": 30,
"end": 43,
"loc": {
"start": {
"line": 2,
"column": 20
},
"end": {
"line": 2,
"column": 21
"column": 34
}
},
"expression": {
"type": "NumericLiteral",
"start": 29,
"end": 30,
"test": {
"type": "BooleanLiteral",
"start": 35,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 20
"column": 26
},
"end": {
"line": 2,
"column": 21
"column": 30
}
},
"extra": {
"rawValue": 1,
"raw": "1"
"value": true,
"extra": {}
},
"body": {
"type": "BlockStatement",
"start": 41,
"end": 43,
"loc": {
"start": {
"line": 2,
"column": 32
},
"end": {
"line": 2,
"column": 34
}
},
"value": 1
"body": [],
"directives": [],
"extra": {
"curly": true
}
}
},
"label": {
Expand Down

0 comments on commit 210de1f

Please sign in to comment.