Skip to content

Commit bcc3783

Browse files
committed
FIX: Smart parser selection to support decorators when LWC base config disabled
Problem: When users set 'disable_lwc_base_config: true', JavaScript files containing LWC decorators (@api, @track, @wire) failed to parse with error: 'Unexpected character @' Root Cause: - createJavascriptConfigArray() used Espree parser (ESLint default) - Espree does not support decorators, only Babel does - Users disable LWC base config to avoid LWC-specific rules, but still have LWC files with decorators that need to be parsed Solution: Implement smart parser selection based on file extensions: - If .js extension is included → Use Babel parser (supports decorators) - If only .jsx, .mjs, .cjs → Use Espree parser (faster, no decorators needed) Benefits: - Fixes decorator parsing when disable_lwc_base_config: true - Preserves Espree performance optimization for React-only projects - Both parsers remain useful (no dead code) - Automatic detection (no new config needed) - Backwards compatible Test Cases: - .js files with LWC decorators: Works (Babel) - .jsx files with React JSX: Works (Espree for performance) - Mixed .js + .jsx: Works (Babel handles both)
1 parent 3922bc2 commit bcc3783

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
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.40.0",
4+
"version": "0.41.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",

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,39 @@ export class BaseConfigFactory {
148148
}
149149

150150
private createJavascriptConfigArray(): Linter.Config[] {
151-
return [{
152-
... eslintJs.configs.all,
153-
files: this.engineConfig.file_extensions.javascript.map(ext => `**/*${ext}`),
154-
languageOptions: {
155-
parserOptions: {
156-
ecmaFeatures: {
157-
jsx: true // Enable JSX parsing for React/JSX files
151+
// Smart parser selection based on file extensions:
152+
// - .js files may contain LWC decorators (@api, @track, @wire) → need Babel
153+
// - .jsx, .mjs, .cjs are typically React or modules without decorators → can use Espree (faster)
154+
const hasJsExtension = this.engineConfig.file_extensions.javascript.includes('.js');
155+
156+
if (hasJsExtension) {
157+
// .js files might have LWC decorators - use Babel parser with decorator support
158+
const lwcConfig = validateAndGetRawLwcConfigArray()[0];
159+
const babelParser = lwcConfig.languageOptions?.parser;
160+
const babelParserOptions = lwcConfig.languageOptions?.parserOptions;
161+
162+
return [{
163+
... eslintJs.configs.all,
164+
files: this.engineConfig.file_extensions.javascript.map(ext => `**/*${ext}`),
165+
languageOptions: {
166+
parser: babelParser,
167+
parserOptions: babelParserOptions
168+
}
169+
}];
170+
} else {
171+
// Only .jsx, .mjs, .cjs (no .js) - use Espree for better performance
172+
return [{
173+
... eslintJs.configs.all,
174+
files: this.engineConfig.file_extensions.javascript.map(ext => `**/*${ext}`),
175+
languageOptions: {
176+
parserOptions: {
177+
ecmaFeatures: {
178+
jsx: true // Enable JSX parsing for React/JSX files
179+
}
158180
}
159181
}
160-
}
161-
}];
182+
}];
183+
}
162184
}
163185

164186
private createSldsHTMLConfigArray(): Linter.Config[] {

0 commit comments

Comments
 (0)