Skip to content

Commit

Permalink
Add support for testing computeEditInfo (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke authored Jan 25, 2025
1 parent 0b55066 commit 1c25867
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 96 deletions.
10 changes: 10 additions & 0 deletions __tests__/fixtures/plugin-foo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ const ruleFunction = (primary, secondaryOptions) => {
ruleName,
message: messages.rejected(selector),
node: rule,
fix: {
apply: () => {
rule.selector = primary;
},
node: rule,
},
});
}
});
Expand All @@ -71,5 +77,9 @@ const ruleFunction = (primary, secondaryOptions) => {

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

export default createPlugin(ruleName, ruleFunction);
29 changes: 29 additions & 0 deletions __tests__/getTestRule.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,32 @@ testRuleWithLoadLint({
config: ['.a'],
accept: [{ code: '.a {}' }],
});

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: '.',
},
},
],
});
2 changes: 2 additions & 0 deletions getTestRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module.exports = function getTestRule(options = {}) {
config: stylelintConfig,
customSyntax: schema.customSyntax,
codeFilename: testCase.codeFilename || schema.codeFilename,
computeEditInfo: schema.computeEditInfo,
};

const outputAfterLint = await lint(stylelintOptions).catch(formatExceptions);
Expand All @@ -92,6 +93,7 @@ module.exports = function getTestRule(options = {}) {
column: expected.column,
endLine: expected.endLine,
endColumn: expected.endColumn,
fix: expected.fix,
};

for (const [key, value] of Object.entries(expectedWarning)) {
Expand Down
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ export type Warning = {
* Expected end column number of the warning.
*/
endColumn?: number;

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

/**
Expand Down Expand Up @@ -108,6 +115,13 @@ export type TestSchema = {
*/
fix?: boolean;

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

/**
* Maps to Stylelint's `plugins` configuration property.
*
Expand Down
Loading

0 comments on commit 1c25867

Please sign in to comment.