refactor: fix more type-aware linting errors (#7479)

This commit is contained in:
Joshua Chen 2022-05-24 19:19:24 +08:00 committed by GitHub
parent bf1513a3e3
commit 624735bd92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 192 additions and 189 deletions

View file

@ -43,68 +43,64 @@ function getOutputCss(output: stylelint.LinterResult) {
function testStylelintRule(config: stylelint.Config, tests: TestSuite) {
describe(`${tests.ruleName}`, () => {
const checkTestCaseContent = (testCase: TestCase) =>
testCase.description || testCase.code;
testCase.description ?? testCase.code;
if (tests.accept?.length) {
describe('accept cases', () => {
tests.accept.forEach((testCase) => {
it(`${checkTestCaseContent(testCase)}`, async () => {
const options: stylelint.LinterOptions = {
code: testCase.code,
config,
};
describe('accept cases', () => {
tests.accept.forEach((testCase) => {
it(`${checkTestCaseContent(testCase)}`, async () => {
const options: stylelint.LinterOptions = {
code: testCase.code,
config,
};
const output = await stylelint.lint(options);
expect(output.results[0]!.warnings).toEqual([]);
if (!tests.fix) {
return;
}
const fixedOutput = await stylelint.lint({...options, fix: true});
const fixedCode = getOutputCss(fixedOutput);
expect(fixedCode).toBe(testCase.code);
});
const output = await stylelint.lint(options);
expect(output.results[0]!.warnings).toEqual([]);
if (!tests.fix) {
return;
}
const fixedOutput = await stylelint.lint({...options, fix: true});
const fixedCode = getOutputCss(fixedOutput);
expect(fixedCode).toBe(testCase.code);
});
});
}
});
if (tests.reject?.length) {
describe('reject cases', () => {
tests.reject.forEach((testCase) => {
it(`${checkTestCaseContent(testCase)}`, async () => {
const options = {
code: testCase.code,
config,
};
describe('reject cases', () => {
tests.reject.forEach((testCase) => {
it(`${checkTestCaseContent(testCase)}`, async () => {
const options = {
code: testCase.code,
config,
};
const output = await stylelint.lint(options);
const {warnings} = output.results[0]!;
const warning = warnings[0]!;
expect(warnings.length).toBeGreaterThanOrEqual(1);
expect(testCase).toHaveMessage();
if (testCase.message != null) {
expect(warning.text).toBe(testCase.message);
}
if (testCase.line != null) {
expect(warning.line).toBe(testCase.line);
}
if (testCase.column != null) {
expect(warning.column).toBe(testCase.column);
}
if (!tests.fix) {
return;
}
if (!testCase.fixed) {
throw new Error(
'If using { fix: true } in test tests, all reject cases must have { fixed: .. }',
);
}
const fixedOutput = await stylelint.lint({...options, fix: true});
const fixedCode = getOutputCss(fixedOutput);
expect(fixedCode).toBe(testCase.fixed);
});
const output = await stylelint.lint(options);
const {warnings} = output.results[0]!;
const warning = warnings[0]!;
expect(warnings.length).toBeGreaterThanOrEqual(1);
expect(testCase).toHaveMessage();
if (testCase.message != null) {
expect(warning.text).toBe(testCase.message);
}
if (testCase.line != null) {
expect(warning.line).toBe(testCase.line);
}
if (testCase.column != null) {
expect(warning.column).toBe(testCase.column);
}
if (!tests.fix) {
return;
}
if (!testCase.fixed) {
throw new Error(
'If using { fix: true } in test tests, all reject cases must have { fixed: .. }',
);
}
const fixedOutput = await stylelint.lint({...options, fix: true});
const fixedCode = getOutputCss(fixedOutput);
expect(fixedCode).toBe(testCase.fixed);
});
});
}
});
expect.extend({
toHaveMessage(testCase: TestCase) {