Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[[CHORE]] Refactor internal function #3601

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 29 additions & 32 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3458,13 +3458,13 @@ var JSHINT = (function() {
}

prefix("[", function(context) {
var blocktype = lookupBlockType();
if (blocktype.isCompArray) {
var blocktype = lookupBlockType(true);
if (blocktype === "array comprehension") {
if (!state.option.esnext && !state.inMoz()) {
warning("W118", state.tokens.curr, "array comprehension");
}
return comprehensiveArrayExpression(context);
} else if (blocktype.isDestAssign) {
} else if (blocktype === "destructuring pattern") {
this.destructAssign = destructuringPattern(context, {
openingParsed: true,
assignment: true
Expand Down Expand Up @@ -4058,8 +4058,7 @@ var JSHINT = (function() {
}
}

var blocktype = lookupBlockType();
if (blocktype.isDestAssign) {
if (lookupBlockType(true) === "destructuring pattern") {
this.destructAssign = destructuringPattern(context, {
openingParsed: true,
assignment: true
Expand Down Expand Up @@ -5979,17 +5978,20 @@ var JSHINT = (function() {
FutureReservedWord("transient");
FutureReservedWord("volatile");

// this function is used to determine whether a squarebracket or a curlybracket
// expression is a comprehension array, destructuring assignment or a json value.

var lookupBlockType = function() {
/**
* Determine whether a bracket or brace denotes a Mozilla comprehension
* array, a destructuring pattern, a property reference, a JSON-serializable
* literal value, or a non-JSON-serializable literal value.
*
* @param {bool} parsedOpening Whether the opening delimiter has already been
* parsed.
*/
var lookupBlockType = function(parsedOpening) {
var pn, pn1, prev;
var i = -1;
var bracketStack = 0;
var ret = {};
if (checkPunctuators(state.tokens.curr, ["[", "{"])) {
bracketStack += 1;
}
var bracketStack = parsedOpening ? 1 : 0;
var serializable = true;

do {
prev = i === -1 ? state.tokens.curr : pn;
pn = i === -1 ? state.tokens.next : peek(i);
Expand All @@ -6002,25 +6004,21 @@ var JSHINT = (function() {
}
if (bracketStack === 1 && pn.identifier && pn.value === "for" &&
!checkPunctuator(prev, ".")) {
ret.isCompArray = true;
ret.notJson = true;
break;
return "array comprehension";
}
if (bracketStack === 0 && checkPunctuators(pn, ["}", "]"])) {
if (pn1.value === "=") {
ret.isDestAssign = true;
ret.notJson = true;
break;
return "destructuring pattern";
} else if (pn1.value === ".") {
ret.notJson = true;
break;
return "property reference";
}
}
if (checkPunctuator(pn, ";")) {
ret.notJson = true;
serializable = false;
}
} while (bracketStack > 0 && pn.id !== "(end)");
return ret;

return serializable ? "JSON serializable" : "not JSON serializable";
};

/**
Expand Down Expand Up @@ -6171,18 +6169,17 @@ var JSHINT = (function() {
// if it has semicolons, it is a block, so go parse it as a block
// or it's not a block, but there are assignments, check for undeclared variables

var block = lookupBlockType();
if (block.notJson) {
if (!state.inES6() && block.isDestAssign) {
var block = lookupBlockType(false);
if (block === "JSON serializable") {
state.option.laxbreak = true;
state.jsonMode = true;
jsonValue();
} else {
if (!state.inES6() && block === "destructuring pattern") {
/* istanbul ignore next */
warning("W104", state.tokens.curr, "destructuring assignment", "6");
}
statements(context);
// otherwise parse json value
} else {
state.option.laxbreak = true;
state.jsonMode = true;
jsonValue();
}
}

Expand Down