Skip to content

Commit

Permalink
Add support for testing computeEditInfo (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke authored Jan 25, 2025
1 parent bd2f546 commit 192de43
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 58 deletions.
18 changes: 11 additions & 7 deletions lib/__tests__/fixtures/plugin-foo.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const messages = ruleMessages(ruleName, {
const isString = (value) => typeof value === 'string';

/** @type {import('stylelint').Rule} */
const ruleFunction = (primary, secondaryOptions, { fix }) => {
const ruleFunction = (primary, secondaryOptions) => {
return (root, result) => {
const validOptions = validateOptions(
result,
Expand Down Expand Up @@ -59,24 +59,28 @@ const ruleFunction = (primary, secondaryOptions, { fix }) => {

if (primary === selector) return;

if (fix) {
rule.selector = primary;

return;
}

report({
result,
ruleName,
message: messages.rejected(selector),
node: rule,
word: selector,
fix: {
apply: () => {
rule.selector = primary;
},
node: rule,
},
});
});
};
};

ruleFunction.ruleName = ruleName;
ruleFunction.messages = messages;
ruleFunction.meta = {
url: 'plugin/foo',
fixable: true,
};

export default createPlugin(ruleName, ruleFunction);
29 changes: 29 additions & 0 deletions lib/__tests__/testRule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,32 @@ testRule({
},
],
});

testRule({
plugins,
ruleName,
config: ['.a'],
computeEditInfo: true,

accept: [],

reject: [
{
code: '#a {}',
message: messages.rejected('#a'),
fix: {
range: [0, 1],
text: '.',
},
},
{
code: '.a {} #a {}',
message: messages.rejected('#a'),
description: 'with description',
fix: {
range: [6, 7],
text: '.',
},
},
],
});
14 changes: 14 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export type Warning = {
* Expected end column number of the warning.
*/
endColumn?: number | undefined;

/**
* Expected `EditInfo` of the warning.
*
* @experimental
*/
fix?: { range: [number, number]; text: string };
};

/**
Expand Down Expand Up @@ -120,6 +127,13 @@ export type TestSchema = {
*/
fix?: boolean | undefined;

/**
* Turn on computing `EditInfo`. Default: `false`.
*
* @experimental
*/
computeEditInfo?: boolean;

/**
* Maps to Stylelint's `plugins` configuration property.
*
Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export function testRule(schema) {
config: stylelintConfig,
customSyntax,
codeFilename: testCase.codeFilename || codeFilename,
computeEditInfo: schema.computeEditInfo,
};

const { results } = await lint(stylelintOptions);
Expand Down Expand Up @@ -122,6 +123,10 @@ export function testRule(schema) {
'Warning "endColumn" does not match',
);
}

if ('fix' in actualWarning && 'fix' in expected) {
assert.deepStrictEqual(actualWarning.fix, expected.fix, 'Warning "fix" does not match');
}
}

if (!fix) return;
Expand Down
Loading

0 comments on commit 192de43

Please sign in to comment.