Skip to content

Commit 6dd0ef9

Browse files
feat: similified reduce-annotations Eslint v9 (#250)
* feat: similified AST based reduce-annotations Eslint v9
1 parent c3efcd0 commit 6dd0ef9

File tree

6 files changed

+223
-13
lines changed

6 files changed

+223
-13
lines changed

packages/eslint-plugin-slds/src/config/rule-messages.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,10 @@ enforce-component-hook-naming-convention:
118118
messages:
119119
replace: "Replace the deprecated {{oldValue}} component styling hook with {{suggestedMatch}}."
120120

121+
reduce-annotations:
122+
description: "Remove your annotations and update your code."
123+
url: "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#reduce-annotations"
124+
type: "problem"
125+
messages:
126+
removeAnnotation: "Remove this annotation and update the code to SLDS best practices. For help, file an issue at https://github.com/salesforce-ux/slds-linter/"
127+

packages/eslint-plugin-slds/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import noUnsupportedHooksSlds2 from './rules/v9/no-unsupported-hooks-slds2';
1313
import noSldsVarWithoutFallback from './rules/v9/no-slds-var-without-fallback';
1414
import noSldsNamespaceForCustomHooks from './rules/v9/no-slds-namespace-for-custom-hooks';
1515
import enforceComponentHookNamingConvention from './rules/v9/enforce-component-hook-naming-convention';
16+
import reduceAnnotations from './rules/v9/reduce-annotations';
1617
import noSldsPrivateVar from './rules/v9/no-slds-private-var';
1718

1819

@@ -33,6 +34,7 @@ const rules = {
3334
"no-slds-var-without-fallback": noSldsVarWithoutFallback,
3435
"no-slds-namespace-for-custom-hooks": noSldsNamespaceForCustomHooks,
3536
"enforce-component-hook-naming-convention": enforceComponentHookNamingConvention,
37+
"reduce-annotations": reduceAnnotations,
3638
"no-slds-private-var": noSldsPrivateVar
3739
};
3840

@@ -89,6 +91,7 @@ Object.assign(plugin.configs, {
8991
"@salesforce-ux/slds/no-slds-var-without-fallback": "warn",
9092
"@salesforce-ux/slds/no-slds-namespace-for-custom-hooks": "warn",
9193
"@salesforce-ux/slds/enforce-component-hook-naming-convention": "error",
94+
"@salesforce-ux/slds/reduce-annotations": "warn",
9295
"@salesforce-ux/slds/no-slds-private-var": "warn"
9396
}
9497
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Rule } from 'eslint';
2+
import ruleMessages from '../../config/rule-messages.yml';
3+
4+
const ruleConfig = ruleMessages['reduce-annotations'];
5+
const { type, description, url, messages } = ruleConfig;
6+
7+
// SLDS validator annotations to detect and flag for removal
8+
const SLDS_ANNOTATIONS = [
9+
"@sldsValidatorIgnoreNextLine",
10+
"@sldsValidatorAllow",
11+
"@sldsValidatorIgnore"
12+
];
13+
14+
export default {
15+
meta: {
16+
type,
17+
docs: {
18+
description,
19+
recommended: true,
20+
url,
21+
},
22+
messages,
23+
},
24+
25+
create(context) {
26+
return {
27+
StyleSheet(node) {
28+
const sourceCode = context.sourceCode;
29+
30+
let comments = (sourceCode as any)?.comments || [];
31+
32+
comments.forEach(comment => {
33+
const commentContent = comment.value.trim();
34+
35+
const matchingAnnotation = SLDS_ANNOTATIONS.find(annotation =>
36+
commentContent.startsWith(annotation)
37+
);
38+
39+
if (matchingAnnotation) {
40+
context.report({
41+
node: comment,
42+
messageId: 'removeAnnotation'
43+
});
44+
}
45+
});
46+
},
47+
};
48+
},
49+
} as Rule.RuleModule;
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
const rule = require('../../src/rules/v9/reduce-annotations').default;
2+
const { RuleTester } = require('eslint');
3+
4+
let cssPlugin;
5+
try {
6+
cssPlugin = require('@eslint/css').default || require('@eslint/css');
7+
} catch (e) {
8+
cssPlugin = require('@eslint/css');
9+
}
10+
11+
const ruleTester = new RuleTester({
12+
plugins: {
13+
css: cssPlugin,
14+
},
15+
language: 'css/css',
16+
});
17+
18+
ruleTester.run('reduce-annotations', rule, {
19+
valid: [
20+
{
21+
code: `
22+
.valid-class {
23+
color: green;
24+
}
25+
`,
26+
filename: 'test.css',
27+
},
28+
{
29+
code: `
30+
/* Regular comment */
31+
.my-other-class {
32+
background: orange;
33+
}
34+
`,
35+
filename: 'test.css',
36+
},
37+
{
38+
code: `
39+
/* stylelint-disable */
40+
.example-class {
41+
color: red;
42+
}
43+
`,
44+
filename: 'test.css',
45+
},
46+
{
47+
code: `
48+
/* Some other comment */
49+
/* Not an annotation */
50+
.another-class {
51+
margin: 10px;
52+
}
53+
`,
54+
filename: 'test.css',
55+
},
56+
],
57+
58+
invalid: [
59+
// @sldsValidatorAllow annotation
60+
{
61+
code: `
62+
/* @sldsValidatorAllow */
63+
.my-class {
64+
color: red;
65+
}
66+
`,
67+
filename: 'test.css',
68+
errors: [{
69+
messageId: 'removeAnnotation'
70+
}]
71+
},
72+
73+
// @sldsValidatorIgnore annotation
74+
{
75+
code: `
76+
/* @sldsValidatorIgnore */
77+
.another-class {
78+
background: blue;
79+
}
80+
`,
81+
filename: 'test.css',
82+
errors: [{
83+
messageId: 'removeAnnotation'
84+
}]
85+
},
86+
87+
// @sldsValidatorIgnoreNextLine annotation
88+
{
89+
code: `
90+
/* @sldsValidatorIgnoreNextLine */
91+
.another-class {
92+
background: blue;
93+
}
94+
`,
95+
filename: 'test.css',
96+
errors: [{
97+
messageId: 'removeAnnotation'
98+
}]
99+
},
100+
101+
// Multiple annotations in same file
102+
{
103+
code: `
104+
/* @sldsValidatorAllow */
105+
/* @sldsValidatorIgnore */
106+
.valid-class {
107+
color: yellow;
108+
}
109+
`,
110+
filename: 'test.css',
111+
errors: [
112+
{
113+
messageId: 'removeAnnotation'
114+
},
115+
{
116+
messageId: 'removeAnnotation'
117+
}
118+
]
119+
},
120+
121+
// Annotation with additional text
122+
{
123+
code: `
124+
/* @sldsValidatorAllow some extra text */
125+
.test-class {
126+
padding: 5px;
127+
}
128+
`,
129+
filename: 'test.css',
130+
errors: [{
131+
messageId: 'removeAnnotation'
132+
}]
133+
},
134+
135+
// Mixed valid and invalid comments
136+
{
137+
code: `
138+
/* Regular comment */
139+
/* @sldsValidatorIgnoreNextLine */
140+
.mixed-example {
141+
border: 1px solid red;
142+
}
143+
`,
144+
filename: 'test.css',
145+
errors: [{
146+
messageId: 'removeAnnotation'
147+
}]
148+
},
149+
150+
// Single-line comment style
151+
{
152+
code: `
153+
/* @sldsValidatorIgnore */
154+
.single-line-annotation {
155+
width: 100%;
156+
}
157+
`,
158+
filename: 'test.css',
159+
errors: [{
160+
messageId: 'removeAnnotation'
161+
}]
162+
}
163+
]
164+
});

packages/stylelint-plugin-slds/stylelint.rules.internal.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,5 @@
1111
{
1212
"severity": "warning"
1313
}
14-
],
15-
"slds/reduce-annotations": [
16-
true,
17-
{
18-
"severity": "warning"
19-
}
2014
]
21-
2215
}

packages/stylelint-plugin-slds/stylelint.rules.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@
44
{
55
"severity": "warning"
66
}
7-
],
8-
"slds/reduce-annotations": [
9-
true,
10-
{
11-
"severity": "warning"
12-
}
137
]
148
}

0 commit comments

Comments
 (0)