Skip to content

Commit 35be8fe

Browse files
committed
[javascript mode] Eagerly insert semicolons after return/break/cont
Issue codemirror#5016
1 parent 9d9cb0e commit 35be8fe

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

mode/javascript/javascript.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
2323

2424
var keywords = function(){
2525
function kw(type) {return {type: type, style: "keyword"};}
26-
var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
26+
var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c"), D = kw("keyword d");
2727
var operator = kw("operator"), atom = {type: "atom", style: "atom"};
2828

2929
var jsKeywords = {
3030
"if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
31-
"return": C, "break": C, "continue": C, "new": kw("new"), "delete": C, "void": C, "throw": C, "debugger": C,
32-
"var": kw("var"), "const": kw("var"), "let": kw("var"),
31+
"return": D, "break": D, "continue": D, "new": kw("new"), "delete": C, "void": C, "throw": C,
32+
"debugger": kw("debugger"), "var": kw("var"), "const": kw("var"), "let": kw("var"),
3333
"function": kw("function"), "catch": kw("catch"),
3434
"for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
3535
"in": operator, "typeof": operator, "instanceof": operator,
@@ -357,6 +357,8 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
357357
if (type == "var") return cont(pushlex("vardef", value.length), vardef, expect(";"), poplex);
358358
if (type == "keyword a") return cont(pushlex("form"), parenExpr, statement, poplex);
359359
if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
360+
if (type == "keyword d") return cx.stream.match(/^\s*$/, false) ? cont() : cont(pushlex("stat"), maybeexpression, expect(";"), poplex);
361+
if (type == "debugger") return cont(expect(";"));
360362
if (type == "{") return cont(pushlex("}"), block, poplex);
361363
if (type == ";") return cont();
362364
if (type == "if") {
@@ -412,7 +414,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
412414
if (atomicTypes.hasOwnProperty(type)) return cont(maybeop);
413415
if (type == "function") return cont(functiondef, maybeop);
414416
if (type == "class") return cont(pushlex("form"), classExpression, poplex);
415-
if (type == "keyword c" || type == "async") return cont(noComma ? maybeexpressionNoComma : maybeexpression);
417+
if (type == "keyword c" || type == "async") return cont(noComma ? expressionNoComma : expression);
416418
if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop);
417419
if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression);
418420
if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop);

mode/javascript/test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@
245245
"[string-2 `${][variable async][operator ++][string-2 }//`];",
246246
"[string-2 `${]{} [operator /] [string-2 /\\//}`];")
247247

248+
MT("return_eol",
249+
"[keyword return]",
250+
"{} [string-2 /5/]")
251+
248252
var ts_mode = CodeMirror.getMode({indentUnit: 2}, "application/typescript")
249253
function TS(name) {
250254
test.mode(name, ts_mode, Array.prototype.slice.call(arguments, 1))

0 commit comments

Comments
 (0)