Skip to content
This repository
Browse code

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
Rick Waldron rwaldron authored valueof committed

Showing 2 changed files with 57 additions and 0 deletions. Show diff stats Hide diff stats

  1. +19 0 jshint.js
  2. +38 0 tests/unit/core.js
19 jshint.js
@@ -2009,6 +2009,25 @@ loop: for (;;) {
2009 2009 while (rbp < nexttoken.lbp) {
2010 2010 isArray = token.value === 'Array';
2011 2011 isObject = token.value === 'Object';
  2012 +
  2013 + // #527, new Foo.Array(), Foo.Array(), new Foo.Object(), Foo.Object()
  2014 + // Line breaks in IfStatement heads exist to satisfy the checkJSHint
  2015 + // "Line too long." error.
  2016 + if (left && (left.value || (left.first && left.first.value))) {
  2017 + // If the left.value is not "new", or the left.first.value is a "."
  2018 + // then safely assume that this is not "new Array()" and possibly
  2019 + // not "new Object()"...
  2020 + if (left.value !== 'new' ||
  2021 + (left.first && left.first.value && left.first.value === '.')) {
  2022 + isArray = false;
  2023 + // ...In the case of Object, if the left.value and token.value
  2024 + // are not equal, then safely assume that this not "new Object()"
  2025 + if (left.value !== token.value) {
  2026 + isObject = false;
  2027 + }
  2028 + }
  2029 + }
  2030 +
2012 2031 advance();
2013 2032 if (isArray && token.id === '(' && nexttoken.id === ')')
2014 2033 warning("Use the array literal notation [].", token);
38 tests/unit/core.js
@@ -130,6 +130,30 @@ exports.testNewArray = function () {
130 130 .test('new Array();');
131 131 };
132 132
  133 +/** Test that JSHint recognizes `new foo.Array(<expr>)` as a valid expression #527 **/
  134 +exports.testNewNonNativeArray = function () {
  135 + var code = 'new foo.Array();',
  136 + code1 = 'new foo.Array(1);',
  137 + code2 = 'new foo.Array(v + 1);',
  138 + code3 = 'new foo.Array("hello", "there", "chaps");';
  139 +
  140 + TestRun().test(code);
  141 + TestRun().test(code1);
  142 + TestRun().test(code2);
  143 + TestRun().test(code3);
  144 +};
  145 +
  146 +exports.testNonNativeArray = function () {
  147 + var code1 = 'foo.Array();',
  148 + code2 = 'foo.Array(v + 1);',
  149 + code3 = 'foo.Array("hello", "there", "chaps");';
  150 +
  151 + TestRun().test(code1);
  152 + TestRun().test(code2);
  153 + TestRun().test(code3);
  154 +};
  155 +
  156 +
133 157 /** Test that JSHint recognizes `new Object(<expr>)` as a valid expression */
134 158 exports.testNewObject = function () {
135 159 var code = 'Object(1);',
@@ -147,6 +171,20 @@ exports.testNewObject = function () {
147 171 .test('new Object();');
148 172 };
149 173
  174 +/** Test that JSHint recognizes `new foo.Object(<expr>)` as a valid expression #527 **/
  175 +exports.testNewNonNativeObject = function () {
  176 + var code = 'new foo.Object();',
  177 + code1 = 'new foo.Object(1);',
  178 + code2 = 'foo.Object();',
  179 + code3 = 'foo.Object(1);';
  180 +
  181 + TestRun().test(code);
  182 + TestRun().test(code1);
  183 + TestRun().test(code2);
  184 + TestRun().test(code3);
  185 +};
  186 +
  187 +
150 188 /**
151 189 * Test that JSHint allows `undefined` to be a function parameter.
152 190 * It is a common pattern to protect against the case when somebody

0 comments on commit 26fd384

Please sign in to comment.
Something went wrong with that request. Please try again.