-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
6 changed files
with
201 additions
and
594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}, | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,4 +27,5 @@ module.exports = { | |
transform: { | ||
'^.+\\.[jt]sx?$': 'babel-jest', | ||
}, | ||
setupFiles: ['./jest-setup.js'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.