Skip to content

Commit

Permalink
misc: improve stylelint rule (#2415)
Browse files Browse the repository at this point in the history
* feat(header-lint): allow for rule configuration

* test(stylelint-copyright): moove implementation to jest

* test(stylelint-copyright): add missing check

* test(jest-setup): add linter
  • Loading branch information
ayshiff authored Mar 21, 2020
1 parent ce45413 commit b4f4057
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 594 deletions.
16 changes: 13 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ module.exports = {
parserOptions: {
allowImportExportEverywhere: true,
},
globals: {
testRule: true,
},
extends: ['airbnb', 'prettier', 'prettier/react'],
plugins: ['react-hooks', 'header'],
rules: {
Expand All @@ -36,10 +39,17 @@ module.exports = {
'block',
[
'*',
' * Copyright (c) Facebook, Inc. and its affiliates.',
{
pattern: ' * Copyright \\(c\\) Facebook, Inc. and its affiliates.',
},
' *',
' * This source code is licensed under the MIT license found in the',
' * LICENSE file in the root directory of this source tree.',
{
pattern:
' * This source code is licensed under the MIT license found in the',
},
{
pattern: ' * LICENSE file in the root directory of this source tree.',
},
' ',
],
],
Expand Down
135 changes: 135 additions & 0 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// eslint-disable-next-line import/no-extraneous-dependencies
const stylelint = require('stylelint');

function getOutputCss(output) {
const result = output.results[0]._postcssResult;
const css = result.root.toString(result.opts.syntax);

return css;
}

global.testRule = (rule, schema) => {
describe(schema.ruleName, () => {
const stylelintConfig = {
plugins: ['./packages/stylelint-copyright'],
rules: {
[schema.ruleName]: schema.config,
},
};

const checkTestCaseContent = testCase =>
testCase.description || testCase.code || 'no description';

if (schema.accept && schema.accept.length) {
describe('accept', () => {
schema.accept.forEach(testCase => {
const spec = testCase.only ? it.only : it;

spec(checkTestCaseContent(testCase), () => {
const options = {
code: testCase.code,
config: stylelintConfig,
syntax: schema.syntax,
};

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

if (!schema.fix) {
return;
}

// Check the fix
// eslint-disable-next-line consistent-return
return stylelint
.lint({...options, fix: true})
.then(fixedOutput => {
const fixedCode = getOutputCss(fixedOutput);

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

if (schema.reject && schema.reject.length) {
describe('reject', () => {
schema.reject.forEach(testCase => {
// eslint-disable-next-line no-nested-ternary
const spec = testCase.only ? it.only : testCase.skip ? it.skip : it;

spec(checkTestCaseContent(testCase), () => {
const options = {
code: testCase.code,
config: stylelintConfig,
syntax: schema.syntax,
};

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

expect(warnings.length).toBeGreaterThanOrEqual(1);
expect(testCase).toHaveMessage();

if (testCase.message !== undefined) {
expect(warning.text).toBe(testCase.message);
}

if (testCase.line !== undefined) {
expect(warning.line).toBe(testCase.line);
}

if (testCase.column !== undefined) {
expect(warning.column).toBe(testCase.column);
}

if (!schema.fix) {
return;
}

if (!testCase.fixed) {
throw new Error(
'If using { fix: true } in test schema, all reject cases must have { fixed: .. }',
);
}
// Check the fix
// eslint-disable-next-line consistent-return
return stylelint
.lint({...options, fix: true})
.then(fixedOutput => {
const fixedCode = getOutputCss(fixedOutput);

expect(fixedCode).toBe(testCase.fixed);
});
});
});
});
});
}
expect.extend({
toHaveMessage(testCase) {
if (testCase.message === undefined) {
return {
message: () =>
'Expected "reject" test case to have a "message" property',
pass: false,
};
}

return {
pass: true,
};
},
});
});
};
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ module.exports = {
transform: {
'^.+\\.[jt]sx?$': 'babel-jest',
},
setupFiles: ['./jest-setup.js'],
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

const testRule = require('stylelint-test-rule-tape');
const stylelintCopyright = require('..');
const rule = require('..');

testRule(stylelintCopyright.rule, {
ruleName: stylelintCopyright.ruleName,
const {ruleName, messages} = rule;

testRule(rule, {
ruleName,
fix: false,
accept: [
{
code: `
Expand All @@ -36,7 +38,7 @@ testRule(stylelintCopyright.rule, {
*/
.foo {}`,
message: `Missing copyright in the header comment (${stylelintCopyright.ruleName})`,
message: messages.rejected,
line: 2,
column: 1,
},
Expand All @@ -50,7 +52,7 @@ testRule(stylelintCopyright.rule, {
* Copyright
*/
.foo {}`,
message: `Missing copyright in the header comment (${stylelintCopyright.ruleName})`,
message: messages.rejected,
line: 2,
column: 1,
},
Expand Down
6 changes: 1 addition & 5 deletions packages/stylelint-copyright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
"description": "stylelint plugin to check css files for a copyright header",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "node tests"
},
"dependencies": {
"stylelint": "^13.2.0",
"stylelint-test-rule-tape": "^0.2.0"
"stylelint": "^13.2.0"
}
}
Loading

0 comments on commit b4f4057

Please sign in to comment.