Skip to content

Commit e28462f

Browse files
committed
[javascript mode] Handle semicolon-less code better
Closes #898
1 parent 0f8715c commit e28462f

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

mode/javascript/javascript.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
9999
stream.skipToEnd();
100100
return ret("comment", "comment");
101101
}
102-
else if (state.reAllowed) {
102+
else if (state.lastType == "operator" || state.lastType == "keyword c" ||
103+
/^[\[{}\(,;:]$/.test(state.lastType)) {
103104
nextUntilUnescaped(stream, "/");
104105
stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
105106
return ret("regexp", "string-2");
@@ -120,7 +121,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
120121
else {
121122
stream.eatWhile(/[\w\$_]/);
122123
var word = stream.current(), known = keywords.propertyIsEnumerable(word) && keywords[word];
123-
return (known && state.kwAllowed) ? ret(known.type, known.style, word) :
124+
return (known && state.lastType != ".") ? ret(known.type, known.style, word) :
124125
ret("variable", "variable", word);
125126
}
126127
}
@@ -359,8 +360,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
359360
startState: function(basecolumn) {
360361
return {
361362
tokenize: jsTokenBase,
362-
reAllowed: true,
363-
kwAllowed: true,
363+
lastType: null,
364364
cc: [],
365365
lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
366366
localVars: parserConfig.localVars,
@@ -378,8 +378,7 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
378378
if (stream.eatSpace()) return null;
379379
var style = state.tokenize(stream, state);
380380
if (type == "comment") return style;
381-
state.reAllowed = !!(type == "operator" || type == "keyword c" || type.match(/^[\[{}\(,;:]$/));
382-
state.kwAllowed = type != '.';
381+
state.lastType = type;
383382
return parseJS(state, style, type, content, stream);
384383
},
385384

@@ -391,7 +390,9 @@ CodeMirror.defineMode("javascript", function(config, parserConfig) {
391390
var type = lexical.type, closing = firstChar == type;
392391
if (type == "vardef") return lexical.indented + 4;
393392
else if (type == "form" && firstChar == "{") return lexical.indented;
394-
else if (type == "stat" || type == "form") return lexical.indented + indentUnit;
393+
else if (type == "form") return lexical.indented + indentUnit;
394+
else if (type == "stat")
395+
return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? indentUnit : 0);
395396
else if (lexical.info == "switch" && !closing)
396397
return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit);
397398
else if (lexical.align) return lexical.column + (closing ? 0 : 1);

0 commit comments

Comments
 (0)