Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Don't show unnecessary warnings for new my.Array()

There is no need in showing "Use the _ literal notation." warning
when Array is simply a property of some custom object. Same with
Object.

Fixes GH-527.
  • Loading branch information...
commit 26fd3842e2acd4285de327bbe2a32984a75e613e 1 parent 023cca6
@rwaldron rwaldron authored valueof committed
Showing with 57 additions and 0 deletions.
  1. +19 −0 jshint.js
  2. +38 −0 tests/unit/core.js
View
19 jshint.js
@@ -2009,6 +2009,25 @@ loop: for (;;) {
while (rbp < nexttoken.lbp) {
isArray = token.value === 'Array';
isObject = token.value === 'Object';
+
+ // #527, new Foo.Array(), Foo.Array(), new Foo.Object(), Foo.Object()
+ // Line breaks in IfStatement heads exist to satisfy the checkJSHint
+ // "Line too long." error.
+ if (left && (left.value || (left.first && left.first.value))) {
+ // If the left.value is not "new", or the left.first.value is a "."
+ // then safely assume that this is not "new Array()" and possibly
+ // not "new Object()"...
+ if (left.value !== 'new' ||
+ (left.first && left.first.value && left.first.value === '.')) {
+ isArray = false;
+ // ...In the case of Object, if the left.value and token.value
+ // are not equal, then safely assume that this not "new Object()"
+ if (left.value !== token.value) {
+ isObject = false;
+ }
+ }
+ }
+
advance();
if (isArray && token.id === '(' && nexttoken.id === ')')
warning("Use the array literal notation [].", token);
View
38 tests/unit/core.js
@@ -130,6 +130,30 @@ exports.testNewArray = function () {
.test('new Array();');
};
+/** Test that JSHint recognizes `new foo.Array(<expr>)` as a valid expression #527 **/
+exports.testNewNonNativeArray = function () {
+ var code = 'new foo.Array();',
+ code1 = 'new foo.Array(1);',
+ code2 = 'new foo.Array(v + 1);',
+ code3 = 'new foo.Array("hello", "there", "chaps");';
+
+ TestRun().test(code);
+ TestRun().test(code1);
+ TestRun().test(code2);
+ TestRun().test(code3);
+};
+
+exports.testNonNativeArray = function () {
+ var code1 = 'foo.Array();',
+ code2 = 'foo.Array(v + 1);',
+ code3 = 'foo.Array("hello", "there", "chaps");';
+
+ TestRun().test(code1);
+ TestRun().test(code2);
+ TestRun().test(code3);
+};
+
+
/** Test that JSHint recognizes `new Object(<expr>)` as a valid expression */
exports.testNewObject = function () {
var code = 'Object(1);',
@@ -147,6 +171,20 @@ exports.testNewObject = function () {
.test('new Object();');
};
+/** Test that JSHint recognizes `new foo.Object(<expr>)` as a valid expression #527 **/
+exports.testNewNonNativeObject = function () {
+ var code = 'new foo.Object();',
+ code1 = 'new foo.Object(1);',
+ code2 = 'foo.Object();',
+ code3 = 'foo.Object(1);';
+
+ TestRun().test(code);
+ TestRun().test(code1);
+ TestRun().test(code2);
+ TestRun().test(code3);
+};
+
+
/**
* Test that JSHint allows `undefined` to be a function parameter.
* It is a common pattern to protect against the case when somebody
Please sign in to comment.
Something went wrong with that request. Please try again.