mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-29 18:27:56 +02:00
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:
parent
ce45413804
commit
b4f4057d97
6 changed files with 201 additions and 594 deletions
16
.eslintrc.js
16
.eslintrc.js
|
@ -20,6 +20,9 @@ module.exports = {
|
|||
parserOptions: {
|
||||
allowImportExportEverywhere: true,
|
||||
},
|
||||
globals: {
|
||||
testRule: true,
|
||||
},
|
||||
extends: ['airbnb', 'prettier', 'prettier/react'],
|
||||
plugins: ['react-hooks', 'header'],
|
||||
rules: {
|
||||
|
@ -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.',
|
||||
},
|
||||
' ',
|
||||
],
|
||||
],
|
||||
|
|
135
jest-setup.js
Normal file
135
jest-setup.js
Normal 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,
|
||||
};
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
|
@ -27,4 +27,5 @@ module.exports = {
|
|||
transform: {
|
||||
'^.+\\.[jt]sx?$': 'babel-jest',
|
||||
},
|
||||
setupFiles: ['./jest-setup.js'],
|
||||
};
|
||||
|
|
|
@ -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: `
|
||||
|
@ -36,7 +38,7 @@ testRule(stylelintCopyright.rule, {
|
|||
*/
|
||||
|
||||
.foo {}`,
|
||||
message: `Missing copyright in the header comment (${stylelintCopyright.ruleName})`,
|
||||
message: messages.rejected,
|
||||
line: 2,
|
||||
column: 1,
|
||||
},
|
||||
|
@ -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,
|
||||
},
|
|
@ -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…
Add table
Reference in a new issue