Skip to content

Commit

Permalink
[[FIX]] Honor globals config in JavaScript API
Browse files Browse the repository at this point in the history
Prior to this commit, the `globals` configuration value was not
considered for invocations of the JavaScript API. Ensure that option is
consistently interpreted according to the project documentation. Ensure
that globals defined by the more specific `predef` option of the API
take precedence over globals defined using this configuration option.
  • Loading branch information
jugglinmike authored and rwaldron committed Jan 25, 2019
1 parent 1ef9cc6 commit 0278731
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5345,18 +5345,21 @@ var JSHINT = (function() {
}

if (o) {
each(o.predef || null, function(item) {
var slice, prop;

if (item[0] === "-") {
slice = item.slice(1);
JSHINT.blacklist[slice] = slice;
// remove from predefined if there
delete predefined[slice];
} else {
prop = Object.getOwnPropertyDescriptor(o.predef, item);
predefined[item] = prop ? prop.value : false;
}

each([o.predef, o.globals], function(dict) {
each(dict, function(item) {
var slice, prop;

if (item[0] === "-") {
slice = item.slice(1);
JSHINT.blacklist[slice] = slice;
// remove from predefined if there
delete predefined[slice];
} else {
prop = Object.getOwnPropertyDescriptor(dict, item);
predefined[item] = prop ? prop.value : false;
}
});
});

each(o.exported || null, function(item) {
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ exports.testCustomGlobals = function (test) {
.addError(3, 1, "Read only.")
.test(code, { es3: true, unused: true, predef: { foo: false }});

JSHINT("x = null;", { undef: true, globals: { x: true } });

test.ok(!JSHINT.data().errors);

JSHINT("x = null;", { undef: true, globals: { x: false } });

test.equal(JSHINT.data().errors.length, 1);

JSHINT("parseInt('');", { undef: true, globals: { "-parseInt": true } });

test.equal(JSHINT.data().errors.length, 1);

JSHINT('x = null;', { undef: true, globals: { x: false } }, { x: true });

test.ok(
!JSHINT.data().errors,
"`predef` parameter takes precedence over `globals` option"
);

JSHINT("x = null;", { undef: true, globals: { x: true } }, { x: false });

test.equal(
JSHINT.data().errors.length,
1,
"`predef` parameter takes precedence over `globals` option"
);

test.done();
};

Expand Down

0 comments on commit 0278731

Please sign in to comment.