Skip to content

Commit

Permalink
fix: do not throw when defining a global named __defineSetter__
Browse files Browse the repository at this point in the history
It replaced {} with `Object.create(null)` to avoid accessing
properties on the Object prototype.

fixes #18363
  • Loading branch information
aladdin-add committed Apr 19, 2024
1 parent 751b518 commit e0ec017
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/source-code/source-code.js
Expand Up @@ -1063,7 +1063,7 @@ class SourceCode extends TokenStore {
* https://github.com/eslint/eslint/issues/16302
*/
const configGlobals = Object.assign(
{},
Object.create(null), // https://github.com/eslint/eslint/issues/18363
getGlobalsForEcmaVersion(languageOptions.ecmaVersion),
languageOptions.sourceType === "commonjs" ? globals.commonjs : void 0,
languageOptions.globals
Expand Down
32 changes: 32 additions & 0 deletions tests/lib/linter/linter.js
Expand Up @@ -10310,6 +10310,38 @@ describe("Linter with FlatConfigArray", () => {
linter.verify(code, config);
assert(spy && spy.calledOnce);
});

// https://github.com/eslint/eslint/issues/18363
it("not throw when defining a global named __defineSetter__", () => {
const code = "/*global __defineSetter__*/";
let spy;

const config = {
plugins: {
test: {
rules: {
checker: {
create(context) {
spy = sinon.spy(node => {
const scope = context.sourceCode.getScope(node);
const def = getVariable(scope, "__defineSetter__");

assert.strictEqual(def.name, "__defineSetter__");
assert.strictEqual(def.writeable, false);
});

return { Program: spy };
}
}
}
}
},
rules: { "test/checker": "error" }
};

linter.verify(code, config);
assert(spy && spy.calledOnce);
});
});

describe("when evaluating code containing a /*global */ block with sloppy whitespace", () => {
Expand Down

0 comments on commit e0ec017

Please sign in to comment.