Skip to content

Commit

Permalink
Add deprecated rule warnings (#6561)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Jan 26, 2023
1 parent e488111 commit 005ed45
Show file tree
Hide file tree
Showing 17 changed files with 1,813 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-llamas-pump.md
@@ -0,0 +1,5 @@
---
"stylelint": minor
---

Added: deprecated rule warnings
2 changes: 1 addition & 1 deletion lib/__tests__/integration.test.js
Expand Up @@ -60,7 +60,7 @@ describe('integration test expecting warnings', () => {
});

it('number and type', () => {
expect(result.messages).toHaveLength(5);
expect(result.messages).toHaveLength(6);
expect(result.messages.every((m) => m.type === 'warning')).toBeTruthy();
expect(result.messages.every((m) => m.plugin === 'stylelint')).toBeTruthy();
});
Expand Down
4 changes: 2 additions & 2 deletions lib/formatters/__tests__/githubFormatter.test.js
Expand Up @@ -47,13 +47,13 @@ test('githubFormatter', () => {
const returnValue = {
ruleMetadata: {
foo: { url: 'https://stylelint.io/rules/foo' },
bar: { fixable: true },
bar: { url: 'https://stylelint.io/rules/bar', fixable: true, deprecated: true },
},
};

expect(githubFormatter(results, returnValue))
.toBe(`::error file=path/to/file.css,line=1,col=2,endLine=1,endColumn=5,title=Stylelint problem::Unexpected "foo" (foo) - https://stylelint.io/rules/foo
::warning file=a.css,line=10,col=20,title=Stylelint problem::Unexpected "bar" (bar) [maybe fixable]
::warning file=a.css,line=10,col=20,title=Stylelint problem::Unexpected "bar" (bar) [maybe fixable, deprecated] - https://stylelint.io/rules/bar
::error file=a.css,line=20,col=1,title=Stylelint problem::Cannot parse foo (foo-error)
::error file=a.css,line=20,col=3,title=Stylelint problem::Anonymous error`);
});
7 changes: 4 additions & 3 deletions lib/formatters/__tests__/stringFormatter.test.js
Expand Up @@ -145,7 +145,7 @@ path/to/file.css
source: 'file.css',
deprecations: [
{
text: 'Deprecated foo',
text: 'Deprecated foo.',
reference: 'bar',
},
],
Expand All @@ -160,7 +160,7 @@ path/to/file.css
source: 'file2.css',
deprecations: [
{
text: 'Deprecated foo',
text: 'Deprecated foo.',
reference: 'bar',
},
],
Expand All @@ -178,7 +178,8 @@ path/to/file.css
expect(output).toBe(stripIndent`
Invalid Option: Unexpected option for baz
Deprecation Warning: Deprecated foo See: bar`);
Deprecation warnings:
- Deprecated foo. See: bar`);
});

it('handles ignored file', () => {
Expand Down
28 changes: 24 additions & 4 deletions lib/formatters/__tests__/verboseFormatter.test.js
Expand Up @@ -337,14 +337,30 @@ describe('verboseFormatter', () => {
severity: 'error',
text: 'Unexpected foo',
},
{
line: 1,
column: 2,
rule: 'no-bar',
severity: 'error',
text: 'Unexpected bar',
},
{
line: 1,
column: 2,
rule: 'no-baz',
severity: 'error',
text: 'Unexpected baz',
},
],
deprecations: [],
invalidOptionWarnings: [],
},
];
const returnValue = {
ruleMetadata: {
'no-foo': { url: 'https://stylelint.io', fixable: true },
'no-foo': { url: 'https://stylelint.io', fixable: true, deprecated: true },
'no-bar': { url: 'https://stylelint.io', fixable: true },
'no-baz': { url: 'https://stylelint.io', deprecated: true },
},
};

Expand All @@ -353,14 +369,18 @@ describe('verboseFormatter', () => {
expect(output).toBe(stripIndent`
file.css
1:2 × Unexpected foo no-foo
1:2 × Unexpected bar no-bar
1:2 × Unexpected baz no-baz
1 problem (1 error, 0 warnings)
3 problems (3 errors, 0 warnings)
1 source checked
file.css
1 error found
no-foo: 1 (maybe fixable)
3 errors found
no-foo: 1 (maybe fixable, deprecated)
no-bar: 1 (maybe fixable)
no-baz: 1 (deprecated)
You may fix some problems with the "--fix" option.`);
});
Expand Down
12 changes: 10 additions & 2 deletions lib/formatters/githubFormatter.js
Expand Up @@ -35,7 +35,15 @@ function buildMessage(msg, metadata) {
if (!metadata) return msg;

const url = metadata.url ? ` - ${metadata.url}` : '';
const fixable = metadata.fixable ? ' [maybe fixable]' : '';

return `${msg}${fixable}${url}`;
let additional = [
metadata.fixable ? 'maybe fixable' : '',
metadata.deprecated ? 'deprecated' : '',
]
.filter(Boolean)
.join(', ');

additional = additional ? ` [${additional}]` : '';

return `${msg}${additional}${url}`;
}
21 changes: 11 additions & 10 deletions lib/formatters/stringFormatter.js
Expand Up @@ -47,22 +47,23 @@ function deprecationsFormatter(results) {
}

const seenText = new Set();
const lines = [];

return allDeprecationWarnings.reduce((output, warning) => {
if (seenText.has(warning.text)) return output;
for (const { text, reference } of allDeprecationWarnings) {
if (seenText.has(text)) continue;

seenText.add(warning.text);
seenText.add(text);

output += yellow('Deprecation Warning: ');
output += warning.text;
let line = ` ${dim('-')} ${text}`;

if (warning.reference) {
output += dim(' See: ');
output += dim(underline(warning.reference));
if (reference) {
line += dim(` See: ${underline(reference)}`);
}

return `${output}\n`;
}, '\n');
lines.push(line);
}

return ['', yellow('Deprecation warnings:'), ...lines, ''].join('\n');
}

/**
Expand Down
13 changes: 9 additions & 4 deletions lib/formatters/verboseFormatter.js
Expand Up @@ -72,12 +72,17 @@ module.exports = function verboseFormatter(results, returnValue) {
const metadata = returnValue.ruleMetadata;

for (const [rule, list] of Object.entries(problemsByRule)) {
const meta = metadata[rule];
const fixable = meta && meta.fixable ? ' (maybe fixable)' : '';
const meta = metadata[rule] || {};

output += dim(` ${ruleLink(rule, meta)}: ${list.length}${fixable}\n`);
let additional = [meta.fixable ? 'maybe fixable' : '', meta.deprecated ? 'deprecated' : '']
.filter(Boolean)
.join(', ');

if (!fixableProblemsFound && meta && meta.fixable) {
additional = additional ? ` (${additional})` : '';

output += dim(` ${ruleLink(rule, meta)}: ${list.length}${additional}\n`);

if (!fixableProblemsFound && meta.fixable) {
fixableProblemsFound = true;
}
}
Expand Down
17 changes: 14 additions & 3 deletions lib/lintPostcssResult.js
Expand Up @@ -16,7 +16,7 @@ const getStylelintRule = require('./utils/getStylelintRule');
* @param {StylelintConfig} config
* @returns {Promise<any>}
*/
function lintPostcssResult(stylelintOptions, postcssResult, config) {
module.exports = function lintPostcssResult(stylelintOptions, postcssResult, config) {
postcssResult.stylelint.ruleSeverities = {};
postcssResult.stylelint.customMessages = {};
postcssResult.stylelint.ruleMetadata = {};
Expand Down Expand Up @@ -77,6 +77,10 @@ function lintPostcssResult(stylelintOptions, postcssResult, config) {
continue;
}

if (ruleFunction.meta && ruleFunction.meta.deprecated) {
warnDeprecatedRule(postcssResult, ruleName);
}

const ruleSettings = config.rules && config.rules[ruleName];

if (ruleSettings === null || ruleSettings[0] === null) {
Expand Down Expand Up @@ -114,7 +118,7 @@ function lintPostcssResult(stylelintOptions, postcssResult, config) {
}

return Promise.all(performRules);
}
};

/**
* There are currently some bugs in the autofixer of Stylelint.
Expand All @@ -131,4 +135,11 @@ function isFixCompatible({ stylelint }) {
return true;
}

module.exports = lintPostcssResult;
/**
* @param {PostcssResult} result
* @param {string} ruleName
* @returns {void}
*/
function warnDeprecatedRule(result, ruleName) {
result.warn(`The "${ruleName}" rule is deprecated.`, { stylelintType: 'deprecation' });
}
4 changes: 3 additions & 1 deletion scripts/visual-config.json
Expand Up @@ -10,6 +10,8 @@
"severity": "warning"
}
],
"import-notation": "string"
"import-notation": "string",
"unit-case": null,
"string-quotes": "single"
}
}

0 comments on commit 005ed45

Please sign in to comment.