Skip to content

Commit 1c52e27

Browse files
committed
NEW: @W-19053333@ Add CSS Rules
1 parent ce4ddfd commit 1c52e27

File tree

20 files changed

+447
-64
lines changed

20 files changed

+447
-64
lines changed

package-lock.json

Lines changed: 72 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/code-analyzer-eslint-engine/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@salesforce/code-analyzer-eslint-engine",
33
"description": "Plugin package that adds 'eslint' as an engine into Salesforce Code Analyzer",
4-
"version": "0.32.0",
4+
"version": "0.33.0-SNAPSHOT",
55
"author": "The Salesforce Code Analyzer Team",
66
"license": "BSD-3-Clause",
77
"homepage": "https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/overview",
@@ -16,7 +16,7 @@
1616
"@eslint/js": "^9.35.0",
1717
"@lwc/eslint-plugin-lwc": "^3.2.0",
1818
"@lwc/eslint-plugin-lwc-platform": "^6.1.0",
19-
"@salesforce-ux/eslint-plugin-slds": "^0.5.3",
19+
"@salesforce-ux/eslint-plugin-slds": "^1.0.2",
2020
"@salesforce/code-analyzer-engine-api": "0.30.0",
2121
"@salesforce/code-analyzer-eslint8-engine": "0.7.0",
2222
"@salesforce/eslint-config-lwc": "^4.0.0",

packages/code-analyzer-eslint-engine/src/base-config.ts

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import globals from "globals";
99

1010
export class BaseConfigFactory {
1111
private readonly engineConfig: ESLintEngineConfig;
12-
12+
1313
constructor(engineConfig: ESLintEngineConfig) {
1414
this.engineConfig = engineConfig;
1515
}
16-
16+
1717
createBaseConfigArray(): Linter.Config[] {
1818
const configArray: Linter.Config[] = [{
1919
linterOptions: {
@@ -28,7 +28,7 @@ export class BaseConfigFactory {
2828
"$Label": "readonly", // ^
2929
"$Locale": "readonly", // ^
3030
"$Resource": "readonly", // ^
31-
31+
3232
// ESLint doesn't natively know about various browser and node globals. So we add them here to
3333
// remove false positives for our users.
3434
... globals.node,
@@ -37,85 +37,99 @@ export class BaseConfigFactory {
3737
}
3838
}
3939
}];
40-
40+
4141
if (this.useJsBaseConfig() && this.useLwcBaseConfig()) {
4242
configArray.push(...this.createJavascriptPlusLwcConfigArray());
4343
} else if (this.useJsBaseConfig()) {
4444
configArray.push(...this.createJavascriptConfigArray());
4545
} else if (this.useLwcBaseConfig()) {
4646
configArray.push(...this.createLwcConfigArray());
4747
}
48-
if (this.useSldsBaseConfig()) {
49-
configArray.push(...this.createSldsConfigArray());
48+
if (this.useSldsCSSBaseConfig()) {
49+
configArray.push(...this.createSldsCSSConfigArray());
50+
}
51+
if (this.useSldsHTMLBaseConfig()) {
52+
configArray.push(...this.createSldsHTMLConfigArray());
5053
}
5154
if (this.useTsBaseConfig()) {
5255
configArray.push(...this.createTypescriptConfigArray());
5356
}
5457
return configArray;
5558
}
56-
59+
5760
private createJavascriptPlusLwcConfigArray(): Linter.Config[] {
5861
let configs: Linter.Config[] = validateAndGetRawLwcConfigArray();
59-
62+
6063
// TODO: Remove the For the following 2 updates when https://github.com/salesforce/eslint-config-lwc/issues/158 is fixed
6164
// 1) Turn off the babel parser's configFile option from the lwc base plugin
6265
configs[0].languageOptions!.parserOptions!.babelOptions.configFile = false;
6366
// 2) For some reason babel doesn't like .cjs files unless we explicitly set this to undefined because I think
6467
// ESLint 9 is setting it to "commonjs" automatically when the field doesn't exist in the parserOptions (and for
6568
// babel "commonjs" isn't a valid option)
6669
configs[0].languageOptions!.parserOptions!.sourceType = undefined;
67-
70+
6871
// Swap out eslintJs.configs.recommended with eslintJs.configs.all
6972
configs[1] = eslintJs.configs.all;
70-
73+
7174
// This one rule is broken and thus, we need to turn it off for now.
7275
// See https://git.soma.salesforce.com/lwc/eslint-plugin-lwc-platform/issues/152
7376
configs[5].rules = {
7477
...configs[5].rules,
7578
'@lwc/lwc-platform/valid-offline-wire': 'off'
7679
}
77-
80+
7881
// Restrict these configs to just javascript files
7982
configs = configs.map(config => {
8083
return {
8184
...config,
8285
files: this.engineConfig.file_extensions.javascript.map(ext => `**/*${ext}`)
8386
}
8487
});
85-
88+
8689
return configs;
8790
}
88-
91+
8992
private createLwcConfigArray(): Linter.Config[] {
9093
const configs: Linter.Config[] = this.createJavascriptPlusLwcConfigArray();
91-
94+
9295
// Remove any explicitly listed rule that is a base javascript rule from the recommended LWC/Lightning rules.
9396
// Note the modified base rules don't have namespace like jest/*, @lwc/*, etc (and thus has no '/').
9497
configs[4].rules = Object.fromEntries(
9598
Object.entries(configs[4].rules as Linter.RulesRecord).filter(([key]) => key.includes('/'))
9699
);
97-
100+
98101
// Remove the eslintJs.configs.all (at element 1). Note that this delete is after the configs[4] update above so
99102
// we can work with the original index [4] instead of [3] to avoid confusion.
100103
configs.splice(1, 1);
101-
104+
102105
return configs;
103106
}
104-
107+
105108
private createJavascriptConfigArray(): Linter.Config[] {
106109
return [{
107110
... eslintJs.configs.all,
108111
files: this.engineConfig.file_extensions.javascript.map(ext => `**/*${ext}`)
109112
}];
110113
}
111-
112-
private createSldsConfigArray(): Linter.Config[] {
113-
return sldsEslintPlugin.configs['flat/recommended'].map(conf => ({
114-
...conf,
115-
files: this.engineConfig.file_extensions.html.map(ext => `**/*${ext}`)
116-
}));
114+
115+
private createSldsHTMLConfigArray(): Linter.Config[] {
116+
return sldsEslintPlugin.configs['flat/recommended-html'].map((htmlConfig: Linter.Config) => {
117+
return {
118+
...htmlConfig,
119+
files: this.engineConfig.file_extensions.html.map(ext => `**/*${ext}`)
120+
};
121+
});
117122
}
118-
123+
124+
private createSldsCSSConfigArray(): Linter.Config[] {
125+
return sldsEslintPlugin.configs['flat/recommended-css'].map((cssConfig: Linter.Config) => {
126+
return {
127+
...cssConfig,
128+
files: this.engineConfig.file_extensions.css.map(ext => `**/*${ext}`)
129+
};
130+
});
131+
}
132+
119133
private createTypescriptConfigArray(): Linter.Config[] {
120134
const configs: Linter.Config[] = [];
121135
for (const conf of ([eslintJs.configs.all, ...eslintTs.configs.all] as Linter.Config[])) {
@@ -126,7 +140,7 @@ export class BaseConfigFactory {
126140
... (conf.languageOptions ?? {}),
127141
parserOptions: {
128142
... (conf.languageOptions?.parserOptions ?? {}),
129-
143+
130144
// Finds the tsconfig.json file nearest to each source file. This should work for most users.
131145
// If not, then we may consider letting user specify this via config or alternatively users can
132146
// just set disable_typescript_base_config=true and configure typescript in their own eslint
@@ -138,19 +152,23 @@ export class BaseConfigFactory {
138152
}
139153
return configs;
140154
}
141-
155+
142156
private useJsBaseConfig(): boolean {
143157
return !this.engineConfig.disable_javascript_base_config && this.engineConfig.file_extensions.javascript.length > 0;
144158
}
145-
159+
146160
private useLwcBaseConfig(): boolean {
147161
return !this.engineConfig.disable_lwc_base_config && this.engineConfig.file_extensions.javascript.length > 0;
148162
}
149-
150-
private useSldsBaseConfig(): boolean {
163+
164+
private useSldsCSSBaseConfig(): boolean {
165+
return !this.engineConfig.disable_slds_base_config && this.engineConfig.file_extensions.css.length > 0;
166+
}
167+
168+
private useSldsHTMLBaseConfig(): boolean {
151169
return !this.engineConfig.disable_slds_base_config && this.engineConfig.file_extensions.html.length > 0;
152170
}
153-
171+
154172
private useTsBaseConfig(): boolean {
155173
return !this.engineConfig.disable_typescript_base_config && this.engineConfig.file_extensions.typescript.length > 0;
156174
}
@@ -181,7 +199,7 @@ function validateAndGetRawLwcConfigArray(): Linter.Config[] {
181199
// - and lib/configs/recommended.js of https://www.npmjs.com/package/@lwc/eslint-plugin-lwc-platform?activeTab=code
182200
throw new Error("INTERNAL ERROR: The recommended config for @salesforce/eslint-config-lwc or @lwc/eslint-plugin-lwc-platform must have changed.");
183201
}
184-
202+
185203
// Return a shallow copy since we will be making modifications
186204
return rawLwcConfigs.map(config => { return {... config}});
187205
}

packages/code-analyzer-eslint-engine/src/config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type ESLintEngineConfig = {
2828
// Default: false
2929
disable_lwc_base_config: boolean
3030

31-
// If true then the base configuration that supplies the slds rules for html and cmp files will not be applied.
31+
// If true then the base configuration that supplies the slds rules for html, cmp, and css files will not be applied.
3232
// Default: false
3333
disable_slds_base_config: boolean
3434

@@ -41,8 +41,9 @@ export type ESLintEngineConfig = {
4141
// rules, add them under the 'javascript' language. To associate file extensions to the standard TypeScript
4242
// rules or custom TypeScript-based rules, add them under the 'typescript' language. To associate file extensions
4343
// to standard LWC HTML rules, Component (CMP) rules, or custom HTML rules, add them under the 'html' language.
44-
// To allow for the discovery of custom rules that are associated with any other language, then add the associated
45-
// file extensions under the 'other' language.
44+
// To associate file extensions to CSS or SCSS rules, add them under the 'css' language. To allow for the
45+
// discovery of custom rules that are associated with any other language, then add the associated file extensions
46+
// under the 'other' language.
4647
file_extensions: FileExtensionsObject
4748

4849
// (INTERNAL USE ONLY) Copy of the code analyzer config root.
@@ -53,6 +54,7 @@ export type FileExtensionsObject = {
5354
javascript: string[],
5455
typescript: string[],
5556
html: string[],
57+
css: string[],
5658
other: string[]
5759
};
5860

@@ -68,6 +70,7 @@ export const DEFAULT_CONFIG: ESLintEngineConfig = {
6870
javascript: ['.js', '.cjs', '.mjs'],
6971
typescript: ['.ts'],
7072
html: ['.html', '.htm', '.cmp'],
73+
css: ['.css', '.scss'],
7174
other: []
7275
},
7376
config_root: process.cwd() // INTERNAL USE ONLY

packages/code-analyzer-eslint-engine/src/messages.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ const MESSAGE_CATALOG : { [key: string]: string } = {
5151
`rules, add them under the 'javascript' language. To associate file extensions to the standard TypeScript\n` +
5252
`rules or custom TypeScript-based rules, add them under the 'typescript' language.\n` +
5353
`To associate file extensions to standard LWC HTML rules, Component (CMP) rules, or custom HTML rules, add them\n` +
54-
`under the 'html' language. To allow for the discovery of custom rules that are associated with any other language,\n` +
54+
`under the 'html' language. To associate file extensions to CSS or SCSS rules, add them under the 'css' language.\n `+
55+
`To allow for the discovery of custom rules that are associated with any other language,\n` +
5556
`then add the associated file extensions under the 'other' language.`,
5657

5758
UnsupportedEngineName:

0 commit comments

Comments
 (0)