misc: improve stylelint rule (#2415)

* 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
This commit is contained in:
Rémi Doreau 2020-03-21 05:42:10 +01:00 committed by GitHub
parent ce45413804
commit b4f4057d97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 201 additions and 594 deletions

View file

@ -20,6 +20,9 @@ module.exports = {
parserOptions: { parserOptions: {
allowImportExportEverywhere: true, allowImportExportEverywhere: true,
}, },
globals: {
testRule: true,
},
extends: ['airbnb', 'prettier', 'prettier/react'], extends: ['airbnb', 'prettier', 'prettier/react'],
plugins: ['react-hooks', 'header'], plugins: ['react-hooks', 'header'],
rules: { rules: {
@ -36,10 +39,17 @@ module.exports = {
'block', '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.',
},
' ', ' ',
], ],
], ],

135
jest-setup.js Normal file
View file

@ -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,
};
},
});
});
};

View file

@ -27,4 +27,5 @@ module.exports = {
transform: { transform: {
'^.+\\.[jt]sx?$': 'babel-jest', '^.+\\.[jt]sx?$': 'babel-jest',
}, },
setupFiles: ['./jest-setup.js'],
}; };

View file

@ -5,11 +5,13 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
const testRule = require('stylelint-test-rule-tape'); const rule = require('..');
const stylelintCopyright = require('..');
testRule(stylelintCopyright.rule, { const {ruleName, messages} = rule;
ruleName: stylelintCopyright.ruleName,
testRule(rule, {
ruleName,
fix: false,
accept: [ accept: [
{ {
code: ` code: `
@ -36,7 +38,7 @@ testRule(stylelintCopyright.rule, {
*/ */
.foo {}`, .foo {}`,
message: `Missing copyright in the header comment (${stylelintCopyright.ruleName})`, message: messages.rejected,
line: 2, line: 2,
column: 1, column: 1,
}, },
@ -50,7 +52,7 @@ testRule(stylelintCopyright.rule, {
* Copyright * Copyright
*/ */
.foo {}`, .foo {}`,
message: `Missing copyright in the header comment (${stylelintCopyright.ruleName})`, message: messages.rejected,
line: 2, line: 2,
column: 1, column: 1,
}, },

View file

@ -4,11 +4,7 @@
"description": "stylelint plugin to check css files for a copyright header", "description": "stylelint plugin to check css files for a copyright header",
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"scripts": {
"test": "node tests"
},
"dependencies": { "dependencies": {
"stylelint": "^13.2.0", "stylelint": "^13.2.0"
"stylelint-test-rule-tape": "^0.2.0"
} }
} }

623
yarn.lock

File diff suppressed because it is too large Load diff