Skip to content

Commit

Permalink
[[FIX]] Do not warn about non-ambiguous linebreaks
Browse files Browse the repository at this point in the history
Warning W014 was implemented to alert users of potentially-confusing
formatting resulting from reliance on ASI.

The original heuristic was applied for every expression and simply
referenced token values. It assumed that when the previous token was a
closing parenthesis or bracket, this always signified a line of code
where the newline could be interpreted as a statement boundary, e.g.

    x = 3 * (2 + 1)
    [0]

However, this heuristic was susceptible to false positives, where the
closing punctuator would not be interpreted as the end of a statement:

    if (true)
      [x] = []

Re-implement the code that detects the warning condition from the
general "expression" function to the specific "infix" parsing logic
(used only when the "(" and "[" tokens have been identified as
components of an exiting expression).
  • Loading branch information
jugglinmike authored and rwaldron committed Jul 30, 2018
1 parent eaca85b commit ab3ab85
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,15 +885,6 @@ var JSHINT = (function() {
if (state.tokens.next.id === "(end)")
error("E006", state.tokens.curr);

var isDangerous =
state.option.asi &&
state.tokens.prev.line !== startLine(state.tokens.curr) &&
_.includes(["]", ")"], state.tokens.prev.id) &&
_.includes(["[", "("], state.tokens.curr.id);

if (isDangerous)
warning("W014", state.tokens.curr, state.tokens.curr.id);

advance();

if (initial) {
Expand Down Expand Up @@ -2362,6 +2353,11 @@ var JSHINT = (function() {
warning("W062");
}

if (state.option.asi && checkPunctuators(state.tokens.prev, [")", "]"]) &&
state.tokens.prev.line !== startLine(state.tokens.curr)) {
warning("W014", state.tokens.curr, state.tokens.curr.id);
}

var n = 0;
var p = [];

Expand Down Expand Up @@ -2558,8 +2554,14 @@ var JSHINT = (function() {
application("=>");

infix("[", function(left, that) {
var e = expression(10);
var s, canUseDot;
var e, s, canUseDot;

if (state.option.asi && checkPunctuators(state.tokens.prev, [")", "]"]) &&
state.tokens.prev.line !== startLine(state.tokens.curr)) {
warning("W014", state.tokens.curr, state.tokens.curr.id);
}

e = expression(10);

if (e && e.type === "(string)") {
if (!state.option.evil && (e.value === "eval" || e.value === "execScript")) {
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,24 @@ exports.safeasi = function (test) {
.addError(4, 1, "Misleading line break before '('; readers may interpret this as an expression boundary.")
.test(afterBracket, { asi: true });

var asClause = [
'if (true)',
' ({x} = {});',
'if (true)',
' [x] = [0];',
'while (false)',
' ({x} = {});',
'while (false)',
' [x] = [0];'
];

// Regression tests for gh-3304
TestRun(test, 'as clause (asi: false)')
.test(asClause, { esversion: 6 });

TestRun(test, 'as clause (asi: true)')
.test(asClause, { esversion: 6, asi: true });

test.done();
};

Expand Down

0 comments on commit ab3ab85

Please sign in to comment.