Skip to content

Commit

Permalink
Update eslint-config-stylelint (#3733)
Browse files Browse the repository at this point in the history
* Update eslint and eslint-config-stylelint

* Format files with new ESLint rules
  • Loading branch information
hudochenkov committed Oct 20, 2018
1 parent 8fa9b87 commit 0fb8b6d
Show file tree
Hide file tree
Showing 304 changed files with 1,429 additions and 62 deletions.
20 changes: 19 additions & 1 deletion jest-setup.js
Expand Up @@ -32,6 +32,7 @@ global.testRule = (rule, schema) => {
};

let passingTestCases = schema.accept || [];

if (!schema.skipBasicChecks) {
passingTestCases = passingTestCases.concat(basicChecks);
}
Expand All @@ -40,6 +41,7 @@ global.testRule = (rule, schema) => {
describe("accept", () => {
passingTestCases.forEach(testCase => {
const spec = testCase.only ? it.only : it;

describe(JSON.stringify(schema.config, replacer), () => {
describe(JSON.stringify(testCase.code), () => {
spec(testCase.description || "no description", () => {
Expand All @@ -48,15 +50,18 @@ global.testRule = (rule, schema) => {
config: stylelintConfig,
syntax: schema.syntax
};

return stylelint(options).then(output => {
expect(output.results[0].warnings).toEqual([]);
expect(output.results[0].parseErrors).toEqual([]);

if (!schema.fix) return;

// Check the fix
return stylelint(Object.assign({ fix: true }, options)).then(
output => {
const fixedCode = getOutputCss(output);

expect(fixedCode).toBe(testCase.code);
}
);
Expand All @@ -72,6 +77,7 @@ global.testRule = (rule, schema) => {
describe("reject", () => {
schema.reject.forEach(testCase => {
const spec = testCase.only ? it.only : it;

describe(JSON.stringify(schema.config, replacer), () => {
describe(JSON.stringify(testCase.code), () => {
spec(testCase.description || "no description", () => {
Expand All @@ -80,6 +86,7 @@ global.testRule = (rule, schema) => {
config: stylelintConfig,
syntax: schema.syntax
};

return stylelint(options).then(output => {
const warning = output.results[0].warnings[0];

Expand All @@ -89,16 +96,22 @@ global.testRule = (rule, schema) => {
if (testCase.message !== undefined) {
expect(_.get(warning, "text")).toBe(testCase.message);
}

if (testCase.line !== undefined) {
expect(_.get(warning, "line")).toBe(testCase.line);
}

if (testCase.column !== undefined) {
expect(_.get(warning, "column")).toBe(testCase.column);
}

if (!schema.fix) return;

if (!testCase.fixed && testCase.fixed !== "" && !testCase.unfixable) {
if (
!testCase.fixed &&
testCase.fixed !== "" &&
!testCase.unfixable
) {
throw new Error(
"If using { fix: true } in test schema, all reject cases must have { fixed: .. }"
);
Expand All @@ -108,6 +121,7 @@ global.testRule = (rule, schema) => {
return stylelint(Object.assign({ fix: true }, options))
.then(output => {
const fixedCode = getOutputCss(output);

if (!testCase.unfixable) {
expect(fixedCode).toBe(testCase.fixed);
expect(fixedCode).not.toBe(testCase.code);
Expand All @@ -116,8 +130,10 @@ global.testRule = (rule, schema) => {
if (testCase.fixed) {
expect(fixedCode).toBe(testCase.fixed);
}

expect(fixedCode).toBe(testCase.code);
}

return {
fixedCode,
warnings: output.results[0].warnings
Expand Down Expand Up @@ -151,10 +167,12 @@ global.testRule = (rule, schema) => {
function getOutputCss(output) {
const result = output.results[0]._postcssResult;
const css = result.root.toString(result.opts.syntax);

if (result.opts.syntax === less) {
// Less needs us to manually strip whitespace at the end of single-line comments ¯\_(ツ)_/¯
return css.replace(/(\n?\s*\/\/.*?)[ \t]*(\r?\n)/g, "$1$2");
}

return css;
}

Expand Down
1 change: 1 addition & 0 deletions lib/__tests__/createLinter.test.js
Expand Up @@ -10,6 +10,7 @@ it("createLinter().getConfigForFile augmented config loads", () => {
__dirname,
"fixtures/getConfigForFile/a/b/foo.css"
);

return linter.getConfigForFile(filepath).then(result => {
expect(result).toEqual({
config: {
Expand Down
2 changes: 2 additions & 0 deletions lib/__tests__/defaultSeverity.test.js
Expand Up @@ -9,10 +9,12 @@ it("`defaultSeverity` option set to warning", () => {
"block-no-empty": true
}
};

return postcssPlugin
.process("a {}", { from: undefined }, config)
.then(result => {
const warnings = result.warnings();

expect(warnings).toHaveLength(1);
expect(warnings[0].text.indexOf("block-no-empty")).not.toBe(-1);
expect(warnings[0].severity).toBe("warning");
Expand Down
2 changes: 2 additions & 0 deletions lib/__tests__/disableRanges.test.js
Expand Up @@ -233,6 +233,7 @@ it("SCSS // line-disabling comment", () => {
const scssSource = `a {
color: pink !important; // stylelint-disable-line declaration-no-important
}`;

return postcss()
.use(assignDisabledRanges)
.process(scssSource, { syntax: scss, from: undefined })
Expand All @@ -253,6 +254,7 @@ it("Less // line-disabling comment", () => {
const lessSource = `a {
color: pink !important; // stylelint-disable-line declaration-no-important
}`;

return postcss()
.use(assignDisabledRanges)
.process(lessSource, { syntax: less, from: undefined })
Expand Down
Expand Up @@ -14,8 +14,10 @@ module.exports = stylelint.createPlugin(ruleName, function(
options,
context
);

return (root, result) => {
if (root.toString().indexOf("@@check-color-hex-case") === -1) return;

colorHexCaseRule(root, result);
};
});
2 changes: 2 additions & 0 deletions lib/__tests__/fixtures/processor-fenced-blocks.js
Expand Up @@ -7,10 +7,12 @@ module.exports = function() {
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

const specialMessage = options.specialMessage || "was processed";

return {
code(input) {
const blocks = execall(/```start([\s\S]+?)```end/g, input);
const toLint = blocks.map(match => match.sub[0]).join("\n\n");

return toLint;
},
result(stylelintResult) {
Expand Down
3 changes: 3 additions & 0 deletions lib/__tests__/fixtures/processor-triple-question-marks.js
Expand Up @@ -4,13 +4,16 @@ const execall = require("execall");

module.exports = function() {
let found = false;

return {
code(input) {
const blocks = execall(/\?\?\?start([\s\S]+?)\?\?\?end/g, input);
const toLint = blocks.map(match => match.sub[0]).join("\n\n");

if (toLint.length > 0) {
found = true;
}

return toLint;
},
result(stylelintResult) {
Expand Down
1 change: 1 addition & 0 deletions lib/__tests__/needlessDisables.test.js
Expand Up @@ -35,6 +35,7 @@ it("needlessDisables simple case", () => {
ignoreDisables: true
}).then(linted => {
const report = needlessDisables(linted.results);

expect(report).toHaveLength(1);
expect(report[0].ranges).toEqual([
{ start: 7, end: 9 },
Expand Down
23 changes: 19 additions & 4 deletions lib/__tests__/normalizeRuleSettings.test.js
Expand Up @@ -4,22 +4,24 @@ const normalizeRuleSettings = require("../normalizeRuleSettings");

describe("rules whose primary option IS NOT an array", () => {
it("solo null returns null", () => {
expect(normalizeRuleSettings(null, "foo")).toBe(null);
expect(normalizeRuleSettings(null, "foo")).toBeNull();
});

it("arrayed null returns null", () => {
expect(normalizeRuleSettings([null], "foo")).toBe(null);
expect(normalizeRuleSettings([null], "foo")).toBeNull();
});

it("solo number returns arrayed number", () => {
const actual = normalizeRuleSettings(2, "foo");
const expected = [2];

expect(actual).toEqual(expected);
});

it("arrayed number returns arrayed number if rule is not special", () => {
const actual = normalizeRuleSettings([2], "foo");
const expected = [2];

expect(actual).toEqual(expected);
});

Expand All @@ -29,18 +31,21 @@ describe("rules whose primary option IS NOT an array", () => {
"block-no-empty"
);
const expected = [2, { severity: "warning" }];

expect(actual).toEqual(expected);
});

it("solo string returns arrayed string", () => {
const actual = normalizeRuleSettings("always", "foo");
const expected = ["always"];

expect(actual).toEqual(expected);
});

it("arrayed string returns arrayed string", () => {
const actual = normalizeRuleSettings(["always"], "foo");
const expected = ["always"];

expect(actual).toEqual(expected);
});

Expand All @@ -50,18 +55,21 @@ describe("rules whose primary option IS NOT an array", () => {
"foo"
);
const expected = ["always", { severity: "warning" }];

expect(actual).toEqual(expected);
});

it("solo boolean returns arrayed boolean", () => {
const actual = normalizeRuleSettings(true, "foo");
const expected = [true];

expect(actual).toEqual(expected);
});

it("arrayed boolean returns arrayed boolean if rule is not special", () => {
const actual = normalizeRuleSettings([false], "foo");
const expected = [false];

expect(actual).toEqual(expected);
});

Expand All @@ -71,17 +79,18 @@ describe("rules whose primary option IS NOT an array", () => {
"block-no-empty"
);
const expected = [true, { severity: "warning" }];

expect(actual).toEqual(expected);
});
});

describe("rules whose primary option CAN BE an array", () => {
it("solo null returns null", () => {
expect(normalizeRuleSettings(null, "foo")).toBe(null);
expect(normalizeRuleSettings(null, "foo")).toBeNull();
});

it("arrayed null returns null", () => {
expect(normalizeRuleSettings([null], "foo")).toBe(null);
expect(normalizeRuleSettings([null], "foo")).toBeNull();
});

it("solo primary option array is nested within an array", () => {
Expand All @@ -91,6 +100,7 @@ describe("rules whose primary option CAN BE an array", () => {
true
);
const expected = [["calc", "rgba"]];

expect(actual).toEqual(expected);
});

Expand All @@ -101,6 +111,7 @@ describe("rules whose primary option CAN BE an array", () => {
true
);
const expected = [["calc", "rgba"]];

expect(actual).toEqual(expected);
});

Expand All @@ -111,6 +122,7 @@ describe("rules whose primary option CAN BE an array", () => {
true
);
const expected = [["calc", "rgba"], { severity: "warning" }];

expect(actual).toEqual(expected);
});

Expand All @@ -120,6 +132,7 @@ describe("rules whose primary option CAN BE an array", () => {
"rulename-bar"
);
const expected = ["alphabetical", { severity: "warning" }];

expect(actual).toEqual(expected);
});

Expand All @@ -130,6 +143,7 @@ describe("rules whose primary option CAN BE an array", () => {
true
);
const expected = [[{ foo: 1 }, { foo: 2 }]];

expect(actual).toEqual(expected);
});

Expand All @@ -140,6 +154,7 @@ describe("rules whose primary option CAN BE an array", () => {
true
);
const expected = [[{ foo: 1 }, { foo: 2 }], { severity: "warning" }];

expect(actual).toEqual(expected);
});
});
3 changes: 3 additions & 0 deletions lib/__tests__/plugins.test.js
Expand Up @@ -260,6 +260,7 @@ it("plugin with primary option array", () => {
"plugin/primary-array": ["foo", "bar"]
}
};

return postcss()
.use(stylelint(config))
.process("a {}", { from: undefined })
Expand All @@ -275,6 +276,7 @@ it("plugin with primary option array within options array", () => {
"plugin/primary-array": [["foo", "bar"], { something: true }]
}
};

return postcss()
.use(stylelint(config))
.process("a {}", { from: undefined })
Expand All @@ -290,6 +292,7 @@ it("plugin with async rule", () => {
"plugin/async": true
}
};

return postcss()
.use(stylelint(config))
.process("a {}", { from: undefined })
Expand Down

0 comments on commit 0fb8b6d

Please sign in to comment.