Skip to content

Commit 1b85ae9

Browse files
committed
More changes to interoperate with other ESLint custom processors
1 parent 2dbc319 commit 1b85ae9

File tree

5 files changed

+117
-75
lines changed

5 files changed

+117
-75
lines changed

lib/configs/base.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
module.exports = {
1111
plugins: ['@salesforce/lwc-graph-analyzer'],
12-
processor: '@salesforce/lwc-graph-analyzer/bundleAnalyzer',
1312
parser: '@babel/eslint-parser',
1413
parserOptions: {
1514
ecmaVersion: 'latest',
@@ -20,10 +19,5 @@ module.exports = {
2019
plugins: [['decorators', { decoratorsBeforeExport: false }]]
2120
}
2221
}
23-
},
24-
overrides: [
25-
{
26-
files: ['**/*.html']
27-
}
28-
]
22+
}
2923
};

lib/configs/recommended.js

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,76 @@
99

1010
module.exports = {
1111
extends: ['./configs/base'],
12-
rules: {
13-
'@salesforce/lwc-graph-analyzer/no-getter-contains-more-than-return-statement': 'warn',
14-
'@salesforce/lwc-graph-analyzer/no-assignment-expression-assigns-value-to-member-variable':
15-
'warn',
16-
'@salesforce/lwc-graph-analyzer/no-wire-config-references-non-local-property-reactive-value':
17-
'warn',
18-
'@salesforce/lwc-graph-analyzer/no-private-wire-config-property': 'warn',
19-
'@salesforce/lwc-graph-analyzer/no-unresolved-parent-class-reference': 'warn',
20-
'@salesforce/lwc-graph-analyzer/no-class-refers-to-parent-class-from-unsupported-namespace':
21-
'warn',
22-
'@salesforce/lwc-graph-analyzer/no-reference-to-unsupported-namespace-reference': 'warn',
23-
'@salesforce/lwc-graph-analyzer/no-wire-config-property-uses-getter-function-returning-inaccessible-import':
24-
'warn',
25-
'@salesforce/lwc-graph-analyzer/no-wire-config-property-uses-getter-function-returning-non-literal':
26-
'warn',
27-
'@salesforce/lwc-graph-analyzer/no-wire-config-property-circular-wire-dependency': 'warn',
28-
'@salesforce/lwc-graph-analyzer/no-wire-configuration-property-using-output-of-non-primeable-wire':
29-
'warn',
30-
'@salesforce/lwc-graph-analyzer/no-missing-resource-cannot-prime-wire-adapter': 'warn',
31-
'@salesforce/lwc-graph-analyzer/no-wire-config-property-uses-imported-artifact-from-unsupported-namespace':
32-
'warn',
33-
'@salesforce/lwc-graph-analyzer/no-wire-adapter-of-resource-cannot-be-primed': 'warn',
34-
'@salesforce/lwc-graph-analyzer/no-unsupported-member-variable-in-member-expression':
35-
'warn',
36-
'@salesforce/lwc-graph-analyzer/no-multiple-template-files': 'warn',
37-
'@salesforce/lwc-graph-analyzer/no-assignment-expression-for-external-components': 'warn',
38-
'@salesforce/lwc-graph-analyzer/no-tagged-template-expression-contains-unsupported-namespace':
39-
'warn',
40-
'@salesforce/lwc-graph-analyzer/no-expression-contains-module-level-variable-ref': 'warn',
41-
'@salesforce/lwc-graph-analyzer/no-call-expression-references-unsupported-namespace':
42-
'warn',
43-
'@salesforce/lwc-graph-analyzer/no-eval-usage': 'warn',
44-
'@salesforce/lwc-graph-analyzer/no-reference-to-class-functions': 'warn',
45-
'@salesforce/lwc-graph-analyzer/no-reference-to-module-functions': 'warn',
46-
'@salesforce/lwc-graph-analyzer/no-functions-declared-within-getter-method': 'warn',
47-
'@salesforce/lwc-graph-analyzer/no-member-expression-reference-to-non-existent-member-variable':
48-
'warn',
49-
'@salesforce/lwc-graph-analyzer/no-member-expression-reference-to-unsupported-namespace-reference':
50-
'warn',
51-
'@salesforce/lwc-graph-analyzer/no-member-expression-contains-non-portable-identifier':
52-
'warn',
53-
'@salesforce/lwc-graph-analyzer/no-member-expression-reference-to-super-class': 'warn',
54-
'@salesforce/lwc-graph-analyzer/no-member-expression-reference-to-unsupported-global':
55-
'warn',
56-
'@salesforce/lwc-graph-analyzer/no-composition-on-unanalyzable-getter-property': 'warn',
57-
'@salesforce/lwc-graph-analyzer/no-composition-on-unanalyzable-property-from-unresolvable-wire':
58-
'warn',
59-
'@salesforce/lwc-graph-analyzer/no-composition-on-unanalyzable-property-missing': 'warn',
60-
'@salesforce/lwc-graph-analyzer/no-composition-on-unanalyzable-property-non-public': 'warn',
61-
'@salesforce/lwc-graph-analyzer/no-render-function-contains-more-than-return-statement':
62-
'warn',
63-
'@salesforce/lwc-graph-analyzer/no-render-function-return-statement-not-returning-imported-template':
64-
'warn',
65-
'@salesforce/lwc-graph-analyzer/no-render-function-return-statement': 'warn'
66-
}
12+
overrides: [
13+
{
14+
files: ['*.html', '**/*.html', '*.js', '**/*.js'],
15+
processor: '@salesforce/lwc-graph-analyzer/bundleAnalyzer',
16+
rules: {
17+
'@salesforce/lwc-graph-analyzer/no-getter-contains-more-than-return-statement':
18+
'warn',
19+
'@salesforce/lwc-graph-analyzer/no-assignment-expression-assigns-value-to-member-variable':
20+
'warn',
21+
'@salesforce/lwc-graph-analyzer/no-wire-config-references-non-local-property-reactive-value':
22+
'warn',
23+
'@salesforce/lwc-graph-analyzer/no-private-wire-config-property': 'warn',
24+
'@salesforce/lwc-graph-analyzer/no-unresolved-parent-class-reference': 'warn',
25+
'@salesforce/lwc-graph-analyzer/no-class-refers-to-parent-class-from-unsupported-namespace':
26+
'warn',
27+
'@salesforce/lwc-graph-analyzer/no-reference-to-unsupported-namespace-reference':
28+
'warn',
29+
'@salesforce/lwc-graph-analyzer/no-wire-config-property-uses-getter-function-returning-inaccessible-import':
30+
'warn',
31+
'@salesforce/lwc-graph-analyzer/no-wire-config-property-uses-getter-function-returning-non-literal':
32+
'warn',
33+
'@salesforce/lwc-graph-analyzer/no-wire-config-property-circular-wire-dependency':
34+
'warn',
35+
'@salesforce/lwc-graph-analyzer/no-wire-configuration-property-using-output-of-non-primeable-wire':
36+
'warn',
37+
'@salesforce/lwc-graph-analyzer/no-missing-resource-cannot-prime-wire-adapter':
38+
'warn',
39+
'@salesforce/lwc-graph-analyzer/no-wire-config-property-uses-imported-artifact-from-unsupported-namespace':
40+
'warn',
41+
'@salesforce/lwc-graph-analyzer/no-wire-adapter-of-resource-cannot-be-primed':
42+
'warn',
43+
'@salesforce/lwc-graph-analyzer/no-unsupported-member-variable-in-member-expression':
44+
'warn',
45+
'@salesforce/lwc-graph-analyzer/no-multiple-template-files': 'warn',
46+
'@salesforce/lwc-graph-analyzer/no-assignment-expression-for-external-components':
47+
'warn',
48+
'@salesforce/lwc-graph-analyzer/no-tagged-template-expression-contains-unsupported-namespace':
49+
'warn',
50+
'@salesforce/lwc-graph-analyzer/no-expression-contains-module-level-variable-ref':
51+
'warn',
52+
'@salesforce/lwc-graph-analyzer/no-call-expression-references-unsupported-namespace':
53+
'warn',
54+
'@salesforce/lwc-graph-analyzer/no-eval-usage': 'warn',
55+
'@salesforce/lwc-graph-analyzer/no-reference-to-class-functions': 'warn',
56+
'@salesforce/lwc-graph-analyzer/no-reference-to-module-functions': 'warn',
57+
'@salesforce/lwc-graph-analyzer/no-functions-declared-within-getter-method': 'warn',
58+
'@salesforce/lwc-graph-analyzer/no-member-expression-reference-to-non-existent-member-variable':
59+
'warn',
60+
'@salesforce/lwc-graph-analyzer/no-member-expression-reference-to-unsupported-namespace-reference':
61+
'warn',
62+
'@salesforce/lwc-graph-analyzer/no-member-expression-contains-non-portable-identifier':
63+
'warn',
64+
'@salesforce/lwc-graph-analyzer/no-member-expression-reference-to-super-class':
65+
'warn',
66+
'@salesforce/lwc-graph-analyzer/no-member-expression-reference-to-unsupported-global':
67+
'warn',
68+
'@salesforce/lwc-graph-analyzer/no-composition-on-unanalyzable-getter-property':
69+
'warn',
70+
'@salesforce/lwc-graph-analyzer/no-composition-on-unanalyzable-property-from-unresolvable-wire':
71+
'warn',
72+
'@salesforce/lwc-graph-analyzer/no-composition-on-unanalyzable-property-missing':
73+
'warn',
74+
'@salesforce/lwc-graph-analyzer/no-composition-on-unanalyzable-property-non-public':
75+
'warn',
76+
'@salesforce/lwc-graph-analyzer/no-render-function-contains-more-than-return-statement':
77+
'warn',
78+
'@salesforce/lwc-graph-analyzer/no-render-function-return-statement-not-returning-imported-template':
79+
'warn',
80+
'@salesforce/lwc-graph-analyzer/no-render-function-return-statement': 'warn'
81+
}
82+
}
83+
]
6784
};

lib/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ declare class LwcBundle {
8787

8888
/**
8989
* ESLint processor that analyzes LWC bundles. This will set up the LWC bundle to be processed
90-
* by Komaci.
90+
* by Komaci. The processor is configured to only process .js and .html files through the
91+
* plugin's configuration.
9192
*/
9293
export class BundleAnalyzer implements Linter.Processor {
9394
/** Gets the current LWC bundle being processed */

lib/util/helper.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
'use strict';
99

10-
const { basename } = require('path');
10+
const { basename, extname } = require('path');
1111
const bundleStateManager = require('./bundle-state-manager');
1212
const StaticAnalyzerProvider = require('./static-analyzer-provider');
1313
// eslint-disable-next-line no-unused-vars
@@ -74,7 +74,7 @@ function removeLwcBundleCacheEntry(bundle) {
7474
}
7575

7676
function analyzeLWC(context) {
77-
const eslintReports = getKomaciReport(context.id, context.filename);
77+
const eslintReports = getKomaciReport(context.id, context.filename, context.sourceCode.text);
7878

7979
for (const report of eslintReports) {
8080
context.report(report);
@@ -109,14 +109,16 @@ function extractBundleKey(eslintFilename) {
109109
*
110110
* @param {string} ruleName - The full ESLint rule name (e.g. '@salesforce/lwc-graph-analyzer/rule-name')
111111
* @param {string} filename - The filename being processed by ESLint
112+
* @param {string} sourceCode - The source code of the file being processed by ESLint
112113
* @returns {Array<Object>} An array of ESLint report objects, each containing a message and ___location information
113114
*/
114-
function getKomaciReport(ruleName, filename) {
115+
function getKomaciReport(ruleName, filename, sourceCode) {
115116
const bundleKey = extractBundleKey(filename);
116-
const lwcBundle = bundleStateManager.getBundleByKey(bundleKey);
117+
let lwcBundle = bundleStateManager.getBundleByKey(bundleKey);
117118
if (!lwcBundle) {
118-
console.warn('getKomaciReport(): LWC bundle not configured. Nothing to do.');
119-
return [];
119+
// We didn't stash a bundle for this file. Our processor may not have been invoked, due
120+
// to a clash with another processor. Just create a bundle for this file.
121+
lwcBundle = LwcBundle.lwcBundleFromFile(sourceCode, filename, extname(filename));
120122
}
121123

122124
const lwcBundleFiles = lwcBundle.filesRecord();

test/lib/util/helper.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ describe('helper', () => {
7272
const bundleKey = bundle.getBundleKey();
7373
const reports = getKomaciReport(
7474
'@salesforce/lwc-graph-analyzer/test-rule',
75-
`0_${bundleKey}`
75+
`0_${bundleKey}`,
76+
jsContent
7677
);
7778

7879
// Should only include the diagnostic for the primary file (test.js)
@@ -114,21 +115,47 @@ describe('helper', () => {
114115
const bundleKey = bundle.getBundleKey();
115116
const reports = getKomaciReport(
116117
'@salesforce/lwc-graph-analyzer/test-rule',
117-
`0_${bundleKey}`
118+
`0_${bundleKey}`,
119+
jsContent
118120
);
119121

120122
// Should return empty array since no diagnostics target the primary file
121123
expect(reports).to.have.length(0);
122124
});
123125

124-
it('should return empty array when no bundle is found', () => {
126+
it('should create bundle on the fly when no bundle is found but source code is provided', () => {
125127
// Don't add any bundles to the state manager
128+
const jsContent = 'export default class Test {}';
129+
const filename = 'test.js';
130+
const mockDiagnostics = [
131+
{
132+
code: {
133+
value: 'TEST_RULE',
134+
target: { path: filename }
135+
},
136+
message: 'Test message for JS file',
137+
range: {
138+
start: { line: 0, character: 0 },
139+
end: { line: 0, character: 10 }
140+
}
141+
}
142+
];
143+
mockStaticAnalyzer.setDiagnostics(mockDiagnostics);
144+
mockStaticAnalyzer.setDiagnosticMessages({
145+
TEST_RULE: {
146+
code: { value: 'TEST_RULE' }
147+
}
148+
});
149+
126150
const reports = getKomaciReport(
127151
'@salesforce/lwc-graph-analyzer/test-rule',
128-
'0_nonexistent-bundle'
152+
filename,
153+
jsContent
129154
);
130155

131-
expect(reports).to.be.an('array').that.is.empty;
156+
// Should include the diagnostic since we created a bundle on the fly
157+
expect(reports).to.have.length(1);
158+
expect(reports[0].message).to.equal('Test message for JS file');
132159
});
133160

134161
it('should return empty array when no matching diagnostic message is found', () => {
@@ -161,7 +188,8 @@ describe('helper', () => {
161188
const bundleKey = bundle.getBundleKey();
162189
const reports = getKomaciReport(
163190
'@salesforce/lwc-graph-analyzer/test-rule',
164-
`0_${bundleKey}`
191+
`0_${bundleKey}`,
192+
jsContent
165193
);
166194

167195
expect(reports).to.be.an('array').that.is.empty;

0 commit comments

Comments
 (0)