Skip to content

Commit c3efcd0

Browse files
riteshiitbbs007sf-nithappu
authored andcommitted
feat: similified no-slds-private-var Eslint v9
1 parent 97e1809 commit c3efcd0

File tree

6 files changed

+200
-14
lines changed

6 files changed

+200
-14
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
@@ -104,6 +104,13 @@ no-slds-namespace-for-custom-hooks:
104104
messages:
105105
customHookNamespace: "Using the --slds namespace for {{token}} isn't supported. Create the custom styling hook in your namespace. Example: --myapp-{{tokenWithoutNamespace}}"
106106

107+
no-slds-private-var:
108+
description: "Some SLDS styling hooks are private and reserved only for internal Salesforce use. Private SLDS styling hooks have prefixes --_slds- and --slds-s-. For more information, look up private CSS in lightningdesignsystem.com."
109+
url: "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#no-slds-private-var"
110+
type: "problem"
111+
messages:
112+
privateVar: "This styling hook is reserved for internal Salesforce use. Remove the --_slds- or –slds-s private variable within selector {{prop}}. For more information, look up private CSS in lightningdesignsystem.com."
113+
107114
enforce-component-hook-naming-convention:
108115
description: "Replace component styling hooks that use a deprecated naming convention."
109116
url: "https://developer.salesforce.com/docs/platform/slds-linter/guide/reference-rules.html#enforce-component-hook-naming-convention"

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

Lines changed: 5 additions & 2 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 noSldsPrivateVar from './rules/v9/no-slds-private-var';
1617

1718

1819
import htmlParser from "@html-eslint/parser";
@@ -31,7 +32,8 @@ const rules = {
3132
"no-unsupported-hooks-slds2": noUnsupportedHooksSlds2,
3233
"no-slds-var-without-fallback": noSldsVarWithoutFallback,
3334
"no-slds-namespace-for-custom-hooks": noSldsNamespaceForCustomHooks,
34-
"enforce-component-hook-naming-convention": enforceComponentHookNamingConvention
35+
"enforce-component-hook-naming-convention": enforceComponentHookNamingConvention,
36+
"no-slds-private-var": noSldsPrivateVar
3537
};
3638

3739
const plugin = {
@@ -86,7 +88,8 @@ Object.assign(plugin.configs, {
8688
"@salesforce-ux/slds/no-unsupported-hooks-slds2": "warn",
8789
"@salesforce-ux/slds/no-slds-var-without-fallback": "warn",
8890
"@salesforce-ux/slds/no-slds-namespace-for-custom-hooks": "warn",
89-
"@salesforce-ux/slds/enforce-component-hook-naming-convention": "error"
91+
"@salesforce-ux/slds/enforce-component-hook-naming-convention": "error",
92+
"@salesforce-ux/slds/no-slds-private-var": "warn"
9093
}
9194
}
9295
],
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Rule } from 'eslint';
2+
import ruleMessages from '../../config/rule-messages.yml';
3+
4+
const ruleConfig = ruleMessages['no-slds-private-var'];
5+
const { type, description, url, messages } = ruleConfig;
6+
7+
export default {
8+
meta: {
9+
type,
10+
docs: {
11+
description,
12+
recommended: true,
13+
url,
14+
},
15+
fixable: 'code',
16+
messages
17+
},
18+
19+
create(context) {
20+
return {
21+
// Handle CSS custom properties (declarations starting with --)
22+
"Declaration"(node) {
23+
// Check if this is a custom property declaration
24+
if (node.property && typeof node.property === 'string' && node.property.startsWith('--_slds-')) {
25+
context.report({
26+
node,
27+
messageId: 'privateVar',
28+
data: { prop: node.property },
29+
fix(fixer) {
30+
// Auto-fix: replace --_slds- with --slds-
31+
const newProperty = node.property.replace('--_slds-', '--slds-');
32+
const sourceCode = context.sourceCode.getText(node);
33+
const fixedCode = sourceCode.replace(node.property, newProperty);
34+
return fixer.replaceText(node, fixedCode);
35+
}
36+
});
37+
}
38+
},
39+
};
40+
},
41+
} as Rule.RuleModule;
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
const rule = require('../../src/rules/v9/no-slds-private-var').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('no-slds-private-var', rule, {
19+
valid: [
20+
{
21+
code: `.example { --slds-valid-var: #fff; }`,
22+
filename: 'test.css',
23+
},
24+
{
25+
code: `.example { color: red; }`,
26+
filename: 'test.css',
27+
},
28+
{
29+
code: `.example { background: #123456; }`,
30+
filename: 'test.css',
31+
},
32+
// Custom properties that don't match the private pattern
33+
{
34+
code: `.example { --my-custom-var: blue; }`,
35+
filename: 'test.css',
36+
},
37+
{
38+
code: `.example { --slds-regular-hook: var(--lwc-brand-primary); }`,
39+
filename: 'test.css',
40+
},
41+
// Complex selectors with valid properties
42+
{
43+
code: `.container .example:hover { --slds-button-color: #0176d3; }`,
44+
filename: 'test.css',
45+
},
46+
// Regular CSS properties
47+
{
48+
code: `.example { margin: 0; padding: 8px; }`,
49+
filename: 'test.css',
50+
},
51+
],
52+
invalid: [
53+
// Basic private variable usage
54+
{
55+
code: `.example { --_slds-deprecated-var: #fff; }`,
56+
output: `.example { --slds-deprecated-var: #fff; }`,
57+
filename: 'test.css',
58+
errors: [{
59+
messageId: 'privateVar',
60+
type: 'Declaration'
61+
}]
62+
},
63+
64+
// Private variable in complex selector
65+
{
66+
code: `.container .example { --_slds-private-hook: #123456; }`,
67+
output: `.container .example { --slds-private-hook: #123456; }`,
68+
filename: 'test.css',
69+
errors: [{
70+
messageId: 'privateVar',
71+
type: 'Declaration'
72+
}]
73+
},
74+
75+
// Multiple private variables in same file
76+
{
77+
code: `
78+
.example1 {
79+
--_slds-deprecated-var: #fff;
80+
}
81+
.example2 {
82+
--_slds-another-private: #000;
83+
}
84+
`,
85+
output: `
86+
.example1 {
87+
--slds-deprecated-var: #fff;
88+
}
89+
.example2 {
90+
--slds-another-private: #000;
91+
}
92+
`,
93+
filename: 'test.css',
94+
errors: [
95+
{
96+
messageId: 'privateVar',
97+
type: 'Declaration'
98+
},
99+
{
100+
messageId: 'privateVar',
101+
type: 'Declaration'
102+
}
103+
]
104+
},
105+
106+
// Private variable with pseudo-selector
107+
{
108+
code: `.example:hover { --_slds-hover-state: #0176d3; }`,
109+
output: `.example:hover { --slds-hover-state: #0176d3; }`,
110+
filename: 'test.css',
111+
errors: [{
112+
messageId: 'privateVar',
113+
type: 'Declaration'
114+
}]
115+
},
116+
117+
// Private variable in media query
118+
{
119+
code: `
120+
@media (min-width: 768px) {
121+
.example { --_slds-responsive-var: 20px; }
122+
}
123+
`,
124+
output: `
125+
@media (min-width: 768px) {
126+
.example { --slds-responsive-var: 20px; }
127+
}
128+
`,
129+
filename: 'test.css',
130+
errors: [{
131+
messageId: 'privateVar',
132+
type: 'Declaration'
133+
}]
134+
},
135+
136+
// Private variable with complex value
137+
{
138+
code: `.example { --_slds-complex-var: var(--lwc-brand-primary, #0176d3); }`,
139+
output: `.example { --slds-complex-var: var(--lwc-brand-primary, #0176d3); }`,
140+
filename: 'test.css',
141+
errors: [{
142+
messageId: 'privateVar',
143+
type: 'Declaration'
144+
}]
145+
},
146+
]
147+
});

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
"severity": "warning"
1313
}
1414
],
15-
"slds/no-slds-private-var": [
16-
true,
17-
{
18-
"severity": "warning"
19-
}
20-
],
2115
"slds/reduce-annotations": [
2216
true,
2317
{

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
"severity": "warning"
66
}
77
],
8-
"slds/no-slds-private-var": [
9-
true,
10-
{
11-
"severity": "warning"
12-
}
13-
],
148
"slds/reduce-annotations": [
159
true,
1610
{

0 commit comments

Comments
 (0)