Skip to content
This repository

Improve lookupBlockType() #1193

Closed
wants to merge 2 commits into from

3 participants

Wolfgang Kluge Anton Kovalyov Nick Schonning
Wolfgang Kluge
Collaborator

For discussion first!
This PR fixes #1132 by testing for punctuation more precisely.

Unfortunately, there are not many tests according to the function lookupBlockType.

I'm pretty sure with the addition of the (punctuation)-tests.
But I'm not that sure with the additional break. However, it makes sense to add the break there - otherwise it can lead to a return value of e.g.

{
    isBlock: true,
    notJson: true,
    isDestAssign: true,
}

Both of the additions would fix 1132 for itself - both of them make sense.

Wolfgang Kluge Improve lookupBlockType()
Fixes #1132 by testing for punctuation more precisely
5b99fae
src/stable/jshint.js
((8 lines not shown))
4035 4033
 			bracketStack += 1;
4036  
-		if (_.contains(["]", "}"], state.tokens.next.value))
4037  
-			bracketStack -= 1;
  4034
+		if (state.tokens.next.type === '(punctuator)') {
1
Nick Schonning
nschonni added a note

Double quotes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/stable/jshint.js
((17 lines not shown))
4038 4040
 		do {
4039 4041
 			pn = peek(i);
4040 4042
 			pn1 = peek(i + 1);
4041 4043
 			i = i + 1;
4042  
-			if (_.contains(["[", "{"], pn.value)) {
4043  
-				bracketStack += 1;
4044  
-			} else if (_.contains(["]", "}"], pn.value)) {
4045  
-				bracketStack -= 1;
  4044
+
  4045
+			if (pn.type === '(punctuator)') {
1
Nick Schonning
nschonni added a note

Double quotes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/stable/jshint.js
((32 lines not shown))
4046 4051
 			}
4047 4052
 			if (pn.identifier && pn.value === "for" && bracketStack === 1) {
4048 4053
 				ret.isCompArray = true;
4049 4054
 				ret.notJson = true;
4050 4055
 				break;
4051 4056
 			}
4052  
-			if (_.contains(["}", "]"], pn.value) && pn1.value === "=") {
4053  
-				ret.isDestAssign = true;
4054  
-				ret.notJson = true;
4055  
-				break;
4056  
-			}
4057  
-			if (pn.value === ";") {
4058  
-				ret.isBlock = true;
4059  
-				ret.notJson = true;
  4057
+			if (pn.type === '(punctuator)') {
  4058
+				if (_.contains(["}", "]"], pn.value) && pn1.type === '(punctuator)' && pn1.value === "=") {
1
Nick Schonning
nschonni added a note

pn1.type === "(punctuator)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/stable/jshint.js
((32 lines not shown))
4046 4051
 			}
4047 4052
 			if (pn.identifier && pn.value === "for" && bracketStack === 1) {
4048 4053
 				ret.isCompArray = true;
4049 4054
 				ret.notJson = true;
4050 4055
 				break;
4051 4056
 			}
4052  
-			if (_.contains(["}", "]"], pn.value) && pn1.value === "=") {
4053  
-				ret.isDestAssign = true;
4054  
-				ret.notJson = true;
4055  
-				break;
4056  
-			}
4057  
-			if (pn.value === ";") {
4058  
-				ret.isBlock = true;
4059  
-				ret.notJson = true;
  4057
+			if (pn.type === '(punctuator)') {
1
Nick Schonning
nschonni added a note

Double quotes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Wolfgang Kluge
Collaborator

Seems like this one fixes #1174, too

Wolfgang Kluge Format
Sorry, too many different style guides ;)
61cf651
Anton Kovalyov
Owner

Sorry, missed that. Could you rebase your patch again?

Anton Kovalyov
Owner

Closing for now. Feel free to re-open if it is still valid.

Anton Kovalyov valueof closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Jul 23, 2013
Wolfgang Kluge Improve lookupBlockType()
Fixes #1132 by testing for punctuation more precisely
5b99fae
Wolfgang Kluge Format
Sorry, too many different style guides ;)
61cf651
This page is out of date. Refresh to see the latest.
46  src/stable/jshint.js
@@ -3951,7 +3951,7 @@ var JSHINT = (function () {
3951 3951
 			this.exportee = expression(10);
3952 3952
 
3953 3953
 			return this;
3954  
-		} 
  3954
+		}
3955 3955
 		if (state.tokens.next.value === "{") {
3956 3956
 			advance("{");
3957 3957
 			for (;;) {
@@ -4029,34 +4029,44 @@ var JSHINT = (function () {
4029 4029
 		var i = 0;
4030 4030
 		var bracketStack = 0;
4031 4031
 		var ret = {};
4032  
-		if (_.contains(["[", "{"], state.tokens.curr.value))
4033  
-			bracketStack += 1;
4034  
-		if (_.contains(["[", "{"], state.tokens.next.value))
  4032
+		if (state.tokens.curr.type === "(punctuator)" &&
  4033
+			_.contains(["[", "{"], state.tokens.curr.value))
4035 4034
 			bracketStack += 1;
4036  
-		if (_.contains(["]", "}"], state.tokens.next.value))
4037  
-			bracketStack -= 1;
  4035
+		if (state.tokens.next.type === "(punctuator)") {
  4036
+			if (_.contains(["[", "{"], state.tokens.next.value))
  4037
+				bracketStack += 1;
  4038
+			if (_.contains(["]", "}"], state.tokens.next.value))
  4039
+				bracketStack -= 1;
  4040
+		}
4038 4041
 		do {
4039 4042
 			pn = peek(i);
4040 4043
 			pn1 = peek(i + 1);
4041 4044
 			i = i + 1;
4042  
-			if (_.contains(["[", "{"], pn.value)) {
4043  
-				bracketStack += 1;
4044  
-			} else if (_.contains(["]", "}"], pn.value)) {
4045  
-				bracketStack -= 1;
  4045
+
  4046
+			if (pn.type === "(punctuator)") {
  4047
+				if (_.contains(["[", "{"], pn.value)) {
  4048
+					bracketStack += 1;
  4049
+				} else if (_.contains(["]", "}"], pn.value)) {
  4050
+					bracketStack -= 1;
  4051
+				}
4046 4052
 			}
4047 4053
 			if (pn.identifier && pn.value === "for" && bracketStack === 1) {
4048 4054
 				ret.isCompArray = true;
4049 4055
 				ret.notJson = true;
4050 4056
 				break;
4051 4057
 			}
4052  
-			if (_.contains(["}", "]"], pn.value) && pn1.value === "=") {
4053  
-				ret.isDestAssign = true;
4054  
-				ret.notJson = true;
4055  
-				break;
4056  
-			}
4057  
-			if (pn.value === ";") {
4058  
-				ret.isBlock = true;
4059  
-				ret.notJson = true;
  4058
+			if (pn.type === "(punctuator)") {
  4059
+				if (_.contains(["}", "]"], pn.value) &&
  4060
+					pn1.type === "(punctuator)" && pn1.value === "=") {
  4061
+					ret.isDestAssign = true;
  4062
+					ret.notJson = true;
  4063
+					break;
  4064
+				}
  4065
+				if (pn.value === ";") {
  4066
+					ret.isBlock = true;
  4067
+					ret.notJson = true;
  4068
+					break;
  4069
+				}
4060 4070
 			}
4061 4071
 		} while (bracketStack > 0 && pn.id !== "(end)" && i < 15);
4062 4072
 		return ret;
5  tests/stable/unit/fixtures/gh1132.js
... ...
@@ -0,0 +1,5 @@
  1
+function b() {
  2
+	var foo = ["["];
  3
+	for (var i = 0; i < 2; i++) {
  4
+	}
  5
+}
10  tests/stable/unit/options.js
@@ -1589,3 +1589,13 @@ exports.unignored = function (test) {
1589 1589
 
1590 1590
 	test.done();
1591 1591
 };
  1592
+
  1593
+// Regressions for "var foo = ['{'];" (GH-1132)
  1594
+exports['var foo = ["{"];'] = function (test) {
  1595
+	var src = fs.readFileSync(__dirname + "/fixtures/gh1132.js", "utf-8");
  1596
+
  1597
+	TestRun(test)
  1598
+		.test(src);
  1599
+
  1600
+	test.done();
  1601
+};
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.