Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: clone config before validating (fixes #12592) #13034

Merged
merged 16 commits into from Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/cli-engine/config-array-factory.js
Expand Up @@ -623,7 +623,9 @@ class ConfigArrayFactory {
* @private
*/
_normalizeConfigData(configData, ctx) {

validateConfigSchema(configData, ctx.name || ctx.filePath);

anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
return this._normalizeObjectConfigData(configData, ctx);
}

Expand Down Expand Up @@ -697,6 +699,8 @@ class ConfigArrayFactory {
ctx.matchBasePath
);

const clonedRulesConfig = rules && JSON.parse(JSON.stringify((rules)));
mysticatea marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately i never saw this PR; JSON cloning should never be used, especially since eslint config values can be regexes, Infinity, NaN, and other things that don't survive JSON serialization.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb We did try to avoid json cloning using this. But we tried using lodash cloneDeepWith with customizer but that had different issues like circular ref issue,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BREAKING CHANGE for a lot of plugins which support options via

regexes, Infinity, NaN, and other things that don't survive JSON serialization

For example: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/docs/rules/filename-case.md#ignore


// Flatten `extends`.
for (const extendName of extendList.filter(Boolean)) {
yield* this._loadExtends(extendName, ctx);
Expand Down Expand Up @@ -731,7 +735,7 @@ class ConfigArrayFactory {
processor,
reportUnusedDisableDirectives,
root,
rules,
rules: clonedRulesConfig,
settings
};

Expand Down
@@ -0,0 +1,15 @@
let errorConfig = ["error", {}];
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved

class CircularRef {
constructor() {
this.self = this;
}
}

module.exports = {
settings: {
sharedData: new CircularRef()
},

rules: { camelcase: errorConfig, "default-case": errorConfig }
};
4 changes: 4 additions & 0 deletions tests/fixtures/config-file/cloned-config/eslintConfig.js
@@ -0,0 +1,4 @@
let errorConfig = ["error", {}];
module.exports = {
rules: { camelcase: errorConfig, "default-case": errorConfig }
aladdin-add marked this conversation as resolved.
Show resolved Hide resolved
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
};
13 changes: 13 additions & 0 deletions tests/fixtures/config-file/cloned-config/eslintConfigFail.js
@@ -0,0 +1,13 @@
let errorConfig = ["error", {}];
module.exports = {
rules: {
camelcase: errorConfig,
"default-case": errorConfig ,
"camelcase" : [
'error',
{
"ignoreDestructuring": new Date().getUTCFullYear // returns a function
}
]
}
};
1 change: 1 addition & 0 deletions tests/fixtures/config-file/cloned-config/index.js
@@ -0,0 +1 @@
var someValue = "bar";
3 changes: 3 additions & 0 deletions tests/fixtures/config-file/cloned-config/inlineText.js
@@ -0,0 +1,3 @@
/*eslint default-case: ["error", {}]*/
/*eslint camelcase: ["error", {}]*/
var someValue = "bar";
42 changes: 42 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -187,6 +187,7 @@ describe("cli", () => {
});
});


describe("when given a config with environment set to Node.js", () => {
it("should execute without any errors", async () => {
const configPath = getFixturePath("configurations", "env-node.json");
Expand Down Expand Up @@ -1160,4 +1161,45 @@ describe("cli", () => {
});
});

describe("testing the cloned config", () => {
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
describe("config file and input file", () => {
it("should not modify original configuration object", async () => {
const configPath = getFixturePath("config-file", "cloned-config", "eslintConfig.js");
const filePath = getFixturePath("config-file", "cloned-config", "index.js");
const args = `--config ${configPath} ${filePath}`;

const exit = await cli.execute(args);

assert.strictEqual(exit, 0);
});
});

describe("config file and input file", () => {
it("should exit with 1 as camelcase has wrong property type", async () => {
const configPath = getFixturePath("config-file", "cloned-config", "eslintConfigFail.js");
const filePath = getFixturePath("config-file", "cloned-config", "index.js");
const args = `--config ${configPath} ${filePath}`;

try {
await cli.execute(args);
} catch (error) {
assert.strictEqual(/Configuration for rule "camelcase" is invalid:/u.test(error), true);
}

});
});

describe("inline config and input file", () => {
it("should not modify original configuration object", async () => {
const filePath = getFixturePath("config-file", "cloned-config", "inlineText.js");
const args = `${filePath}`;

const exit = await cli.execute(args);

assert.strictEqual(exit, 0);
});
});

});

});