Skip to content

Commit

Permalink
Add support for JS labels.
Browse files Browse the repository at this point in the history
Issue #409
Fails on labels followed by braces
a: {
 statements...
}
as this may be a flow object def.
  • Loading branch information
Graham Clark committed Sep 3, 2017
1 parent 584e6c3 commit 9b85933
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 71 deletions.
41 changes: 28 additions & 13 deletions grammars/Babel Language.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
{ "include": "#multiline-arrow-function-generics"},
{ "include": "#jsx" },
{ "include": "#es7-decorators" },
{ "include": "#literal-js-label" },
{ "include": "#support" },
{ "include": "#literal-function" },
{ "include": "#literal-arrow-function" },
Expand Down Expand Up @@ -244,6 +245,7 @@
{ "include": "#expression"}
]
},
{ "include": "#literal-js-label" },
{
"match": "(?<!\\.|\\?)\\s*+(((?:[$_\\p{L}\\p{Nl}]|\\\\u\\h{4}|\\\\u{\\h+})(?:[$_\\p{L}\\p{Mn}\\p{Mc}\\p{Nd}\\p{Nl}\\p{Pc}\\x{200C}\\x{200D}]|\\\\u\\h{4}|\\\\u{\\h+})*+))\\s*+(:)((?=::)|(?!:))",
"captures": {
Expand Down Expand Up @@ -283,10 +285,25 @@
}
},
{
"match": "(?<!\\.)\\s*+\\b(break|continue|do|goto|while)\\b",
"match": "(?<!\\.)\\s*+\\b(do|goto|while)\\b",
"captures": {
"1": { "name": "keyword.control.loop.js" }
}
},
{
"begin": "(?<!\\.)\\s*+\\b(break|continue)\\b",
"end": "\\s*(?=;|$|\\b(if|switch|case|break|default|try|var|let|const|static|function|return|class|do|for|while|debugger|export|import|yield|type|declare|interface)\\b)",
"beginCaptures": {
"1": { "name": "keyword.control.loop.js" }
},
"patterns": [
{
"match": "\\s*([$_[:alpha:]][$_[:alnum:]]*)(?=\\s|$|;|/\\*)",
"captures": {
"1": { "name": "constant.other.label.js"}
}
}
]
}
]
},
Expand Down Expand Up @@ -782,22 +799,12 @@
"patterns": [
{
"begin": "(?<!\\.)\\s*+\\b(const|let|var)\\b",
"end": "\\s*(?=;|\\*/|\\b(if|switch|case|break|default|try|var|let|const|static|function|return|class|do|for|while|debugger|export|import|yield|type|declare|interface)\\b|\\)|})",
"end": "(?=^\\s*[$_[:alpha:]][$_[:alnum:]]*:\\s*($|\\b(do|for|while)\\b))|\\s*(?=;|\\*/|\\b(if|switch|case|break|default|try|var|let|const|static|function|return|class|do|for|while|debugger|export|import|yield|type|declare|interface)\\b|\\)|})",
"beginCaptures": {
"1": { "name": "storage.type.js" }
},
"patterns": [
{
"comment": "other variables",
"begin": "\\s*",
"end": "\\s*((,)|(?=(;)|\\*/|\\b(if|switch|case|break|default|try|var|let|const|static|function|return|class|do|for|while|debugger|export|import|yield|type|declare|interface)\\b|\\)|}))",
"endCaptures": {
"2": { "name": "punctuation.terminator.statement.js" }
},
"patterns": [
{ "include": "#flowtype-variable" }
]
}
{ "include": "#flowtype-variable" }
]
}
]
Expand Down Expand Up @@ -1109,6 +1116,14 @@
{ "include": "#expression" }
]
},
"literal-js-label": {
"comment": "jslabel: $|for|while|do",
"match": "(?:^|;]|(?<=;))\\s*([$_[:alpha:]][$_[:alnum:]]*)(:)\\s*(?=($|;|\\b(for|while|do)\\b))",
"captures": {
"1": { "name": "constant.other.label.js"},
"2": { "name": "punctuation.separator.label.js"}
}
},
"literal-object": {
"comment": "obj lteral ({ or ,{ or [{ or ={ or return { or default {",
"begin": "(?:(?<=\\(|\\[|,)|(?:\\s*(?:(=)|\\b(default)\\b|\\b(return)\\b|(,))))\\s*({)",
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/grammar/babel-sublime/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -1965,7 +1965,7 @@ var a: number = 123,
// ^^^^^^ support.type.builtin.primitive.flowtype
// ^ keyword.operator.assignment.js
// ^^^ constant.numeric.js
// ^ punctuation.terminator.statement.js
// ^ meta.delimiter.comma.js
b: void = (x,y,z) => {};
// ^ ^ ^ ^ variable.other.readwrite.js
// ^ punctuation.type.flowtype
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/grammar/es6module.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export let name1, name2, nameN
//^^^^ keyword.control.module.js
// ^^^ storage.type.js
// ^^^^^ ^^^^^ ^^^^^ variable.other.readwrite.js
// ^ ^ punctuation.terminator.statement.js
// ^ ^ meta.delimiter.comma.js
export let name1: {a: string} = {a: "aaa"}
// <- keyword.control.module.js
// <- keyword.control.module.js
Expand Down
104 changes: 48 additions & 56 deletions spec/fixtures/grammar/everythingJs/es2015-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,20 @@ export var e5, e6 = 0;
//^^^^ keyword.control.module.js
// ^^^ storage.type.js
// ^^ ^^ variable.other.readwrite.js
// ^ ^ punctuation.terminator.statement.js
// ^ meta.delimiter.comma.js
// ^ keyword.operator.assignment.js
// ^ constant.numeric.js
// ^ punctuation.terminator.statement.js
export let e7, e8 = 0;
// <- keyword.control.module.js
// <- keyword.control.module.js
//^^^^ keyword.control.module.js
// ^^^ storage.type.js
// ^^ ^^ variable.other.readwrite.js
// ^ ^ punctuation.terminator.statement.js
// ^ meta.delimiter.comma.js
// ^ keyword.operator.assignment.js
// ^ constant.numeric.js
// ^ punctuation.terminator.statement.js
export const e9 = 0, e10 = 0;
// <- keyword.control.module.js
// <- keyword.control.module.js
Expand All @@ -167,7 +169,8 @@ export const e9 = 0, e10 = 0;
// ^^ ^^^ variable.other.readwrite.js
// ^ ^ keyword.operator.assignment.js
// ^ ^ constant.numeric.js
// ^ ^ punctuation.terminator.statement.js
// ^ meta.delimiter.comma.js
// ^ punctuation.terminator.statement.js
export function e11(){}
// <- keyword.control.module.js
// <- keyword.control.module.js
Expand Down Expand Up @@ -228,71 +231,59 @@ export default function e16(){}
// <- comment.line.double-slash.js punctuation.definition.comment.js
// ^^^^^^^^^^ comment.line.double-slash.js
tab:for(;;)break tab;
// <- constant.other.object.key.js string.unquoted.js
// <- constant.other.object.key.js string.unquoted.js
//^ constant.other.object.key.js
//^ string.unquoted.js
// ^ punctuation.separator.key-value.js
// <- constant.other.label.js
// <- constant.other.label.js
//^ ^^^ constant.other.label.js
// ^ punctuation.separator.label.js
// ^^^^^^^ meta.for.js
// ^^^ ^^^^^ keyword.control.loop.js
// ^ ^ meta.brace.round.js
// ^^ ^ punctuation.terminator.statement.js
// ^^^ variable.other.readwrite.js
verticalTab:for(;;)break verticalTab;
// <- constant.other.object.key.js string.unquoted.js
// <- constant.other.object.key.js string.unquoted.js
//^^^^^^^^^ constant.other.object.key.js
//^^^^^^^^^ string.unquoted.js
// ^ punctuation.separator.key-value.js
// <- constant.other.label.js
// <- constant.other.label.js
//^^^^^^^^^ ^^^^^^^^^^^ constant.other.label.js
// ^ punctuation.separator.label.js
// ^^^^^^^ meta.for.js
// ^^^ ^^^^^ keyword.control.loop.js
// ^ ^ meta.brace.round.js
// ^^ ^ punctuation.terminator.statement.js
// ^^^^^^^^^^^ variable.other.readwrite.js
formFeed:for(;;)break formFeed;
// <- constant.other.object.key.js string.unquoted.js
// <- constant.other.object.key.js string.unquoted.js
//^^^^^^ constant.other.object.key.js
//^^^^^^ string.unquoted.js
// ^ punctuation.separator.key-value.js
// <- constant.other.label.js
// <- constant.other.label.js
//^^^^^^ ^^^^^^^^ constant.other.label.js
// ^ punctuation.separator.label.js
// ^^^^^^^ meta.for.js
// ^^^ ^^^^^ keyword.control.loop.js
// ^ ^ meta.brace.round.js
// ^^ ^ punctuation.terminator.statement.js
// ^^^^^^^^ variable.other.readwrite.js
space:for(;;)break space;
// <- constant.other.object.key.js string.unquoted.js
// <- constant.other.object.key.js string.unquoted.js
//^^^ constant.other.object.key.js
//^^^ string.unquoted.js
// ^ punctuation.separator.key-value.js
// <- constant.other.label.js
// <- constant.other.label.js
//^^^ ^^^^^ constant.other.label.js
// ^ punctuation.separator.label.js
// ^^^^^^^ meta.for.js
// ^^^ ^^^^^ keyword.control.loop.js
// ^ ^ meta.brace.round.js
// ^^ ^ punctuation.terminator.statement.js
// ^^^^^ variable.other.readwrite.js
nbsp:for(;;)break nbsp;
// <- constant.other.object.key.js string.unquoted.js
// <- constant.other.object.key.js string.unquoted.js
//^^ constant.other.object.key.js
//^^ string.unquoted.js
// ^ punctuation.separator.key-value.js
// <- constant.other.label.js
// <- constant.other.label.js
//^^ ^^^^ constant.other.label.js
// ^ punctuation.separator.label.js
// ^^^^^^^ meta.for.js
// ^^^ ^^^^^ keyword.control.loop.js
// ^ ^ meta.brace.round.js
// ^^ ^ punctuation.terminator.statement.js
// ^^^^ variable.other.readwrite.js
bom:for(;;)breakbom;
// <- constant.other.object.key.js string.unquoted.js
// <- constant.other.object.key.js string.unquoted.js
//^ constant.other.object.key.js
//^ string.unquoted.js
// ^ punctuation.separator.key-value.js
// <- constant.other.label.js
// <- constant.other.label.js
//^ ^^^ constant.other.label.js
// ^ punctuation.separator.label.js
// ^^^^^^^ meta.for.js
// ^^^ ^^^^^ keyword.control.loop.js
// ^ ^ meta.brace.round.js
// ^^ ^ punctuation.terminator.statement.js
// ^^^ variable.other.readwrite.js

// line terminators
// <- comment.line.double-slash.js punctuation.definition.comment.js
Expand Down Expand Up @@ -352,12 +343,13 @@ var $, _, \u0078, \u{2F9F9}, x$, x_, x\u0030, x\u{e01d5}, xa, x0, x0a,
// <- storage.type.js
//^ storage.type.js
// ^ variable.other.dollar.js
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ punctuation.terminator.statement.js
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ meta.delimiter.comma.js
// ^ ^^^^^^ ^^^^^^^^^ ^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^ ^^ ^^^ variable.other.readwrite.js
x0123456789, qwertyuiopasdfghjklzxcvbnm, QWERTYUIOPASDFGHJKLZXCVBNM;
//^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^ variable.other.readwrite.js
// ^ ^ ^ punctuation.terminator.statement.js
// ^ ^ meta.delimiter.comma.js
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ variable.other.constant.js
// ^ punctuation.terminator.statement.js

// a representative sample of ID_Start and ID_Continue
// <- comment.line.double-slash.js punctuation.definition.comment.js
Expand All @@ -369,7 +361,8 @@ var 䩶, x󠇕, œ一, ǻ둘, ɤ〩, φ, fiⅷ, ユニコード, x‌‍;
// <- storage.type.js
//^ storage.type.js
// ^ ^^^ ^^ ^^ ^^ ^ ^^ ^^^^^ ^^^ variable.other.readwrite.js
// ^ ^ ^ ^ ^ ^ ^ ^ ^ punctuation.terminator.statement.js
// ^ ^ ^ ^ ^ ^ ^ ^ meta.delimiter.comma.js
// ^ punctuation.terminator.statement.js
let letx; let[x\u0078] = 0; const constx = 0;
// <- storage.type.js
// <- storage.type.js
Expand Down Expand Up @@ -1086,15 +1079,17 @@ var x0; var x1,y2; var x3,y4,z5;
// <- storage.type.js
//^ ^^^ ^^^ storage.type.js
// ^^ ^^ ^^ ^^ ^^ ^^ variable.other.readwrite.js
// ^ ^ ^ ^ ^ ^ punctuation.terminator.statement.js
// ^ ^ ^ punctuation.terminator.statement.js
// ^ ^ ^ meta.delimiter.comma.js
var x6=0; var x7=0,y8; var x9,y10=0; var x11=0,y12=0;
// <- storage.type.js
// <- storage.type.js
//^ ^^^ ^^^ ^^^ storage.type.js
// ^^ ^^ ^^ ^^ ^^^ ^^^ ^^^ variable.other.readwrite.js
// ^ ^ ^ ^ ^ keyword.operator.assignment.js
// ^ ^ ^ ^ ^ constant.numeric.js
// ^ ^ ^ ^ ^ ^ ^ punctuation.terminator.statement.js
// ^ ^ ^ ^ punctuation.terminator.statement.js
// ^ ^ ^ meta.delimiter.comma.js

;
// <- punctuation.terminator.statement.js
Expand Down Expand Up @@ -1153,7 +1148,8 @@ for(var a0;;)break; for(var a1,b2;0;0);
// ^ ^ ^ ^ meta.brace.round.js
// ^^^ ^^^ storage.type.js
// ^^ ^^ ^^ variable.other.readwrite.js
// ^^ ^ ^ ^ ^ ^ punctuation.terminator.statement.js
// ^^ ^ ^ ^ ^ punctuation.terminator.statement.js
// ^ meta.delimiter.comma.js
// ^ ^ constant.numeric.js
for(var a3=0;;)break; for(var a4=(0 in[]);0;);
// <- meta.for.js keyword.control.loop.js
Expand Down Expand Up @@ -1199,10 +1195,8 @@ for(;0;)continue; x:for(;0;)continue x;
// ^ ^ ^ ^ meta.brace.round.js
// ^ ^ ^ ^ ^ ^ punctuation.terminator.statement.js
// ^ ^ constant.numeric.js
// ^ constant.other.object.key.js
// ^ string.unquoted.js
// ^ punctuation.separator.key-value.js
// ^ variable.other.readwrite.js
// ^ ^ constant.other.label.js
// ^ punctuation.separator.label.js

for(;;)break; x:for(;;)break x;
// <- meta.for.js keyword.control.loop.js
Expand All @@ -1211,10 +1205,8 @@ for(;;)break; x:for(;;)break x;
//^ ^^^^^ ^^^ ^^^^^ keyword.control.loop.js
// ^ ^ ^ ^ meta.brace.round.js
// ^^ ^ ^^ ^ punctuation.terminator.statement.js
// ^ constant.other.object.key.js
// ^ string.unquoted.js
// ^ punctuation.separator.key-value.js
// ^ variable.other.readwrite.js
// ^ ^ constant.other.label.js
// ^ punctuation.separator.label.js
switch(0){case 0:break;default:}
// <- meta.switch.js keyword.control.switch.js
// <- meta.switch.js keyword.control.switch.js
Expand Down Expand Up @@ -1297,12 +1289,12 @@ switch(0){default:;} switch(0){default:;;}
// ^ ^ meta.brace.curly.switchEnd.js

x:; x:y:;
// <- constant.other.object.key.js string.unquoted.js
// <- punctuation.separator.key-value.js
// <- constant.other.label.js
// <- punctuation.separator.label.js
//^ ^ punctuation.terminator.statement.js
// ^ ^ constant.other.object.key.js
// ^ ^ string.unquoted.js
// ^ ^ punctuation.separator.key-value.js
//^ ^ punctuation.terminator.statement.js

try { throw 0; }catch(x){}
// <- keyword.control.trycatch.js
Expand Down

0 comments on commit 9b85933

Please sign in to comment.