Skip to content

Commit ea388bb

Browse files
fix: avoid errors for LwcBundle.getBundleKey
1 parent b391b4b commit ea388bb

File tree

6 files changed

+21
-70
lines changed

6 files changed

+21
-70
lines changed

lib/lwc-bundle.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,16 @@ class LwcBundle {
125125
/**
126126
* Gets a unique key for this bundle based on its js file since komaci only supports js files. This key is used as file name for further linting.
127127
*
128-
* @returns {string} A unique key in the format `<base file name>_<uuid>.<extension>`
128+
* @returns {string} A unique key in the format `<base file name>_<uuid>.js`
129129
*/
130130
getBundleKey() {
131-
if (!this.#js) {
132-
throw new Error('Cannot generate bundle key: no js file exists');
131+
const primaryFile = this.primaryFile;
132+
if (!primaryFile) {
133+
throw new Error('Cannot generate bundle key: no primary file exists');
133134
}
134135

135-
const { name, ext } = parse(this.#js.filename);
136-
return `${name}_${this.#primaryFileUuidKey}${ext}`;
136+
const { name } = parse(primaryFile.filename);
137+
return `${name}_${this.#primaryFileUuidKey}.js`;
137138
}
138139

139140
/**

lib/processor.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,12 @@ class BundleAnalyzer {
127127
// Get the unique key for this bundle, which will be used as the filename for ESLint processing. It should always be a js file since komaci only supports js files.
128128
const uniqueFilename = this.#lwcBundle.getBundleKey();
129129

130-
// For JavaScript files, we need to return the original text so that ESLint can parse it
131-
// and properly handle disable comments. For all other file types (like HTML), we return
132-
// the JS content from the bundle so that rules can run and report violations targeting
130+
// For JavaScript files, we need to return the JS content from the bundle so that ESLint can parse it
131+
// and properly handle disable comments. For HTML files, we return
132+
// the JS content too so that rules can run and report violations targeting
133133
// the HTML file.
134-
if (fileExtension === '.js') {
135-
return [{ text, filename: uniqueFilename }];
136-
} else {
137-
// Return JS content so ESLint can parse it and rules can run
138-
return [{ text: this.#lwcBundle.js?.content || '', filename: uniqueFilename }];
139-
}
134+
135+
return [{ text: this.#lwcBundle.js?.content || '', filename: uniqueFilename }];
140136
};
141137

142138
/**

test/lib/lwc-bundle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ describe('LwcBundle', () => {
191191
expect(key).to.match(/^test_[0-9a-f-]+\.js$/);
192192
});
193193

194-
it('should throw error when no js file exists', () => {
194+
it('should throw error when no primary file exists', () => {
195195
const bundle = LwcBundle.lwcBundleFromContent('test', undefined, 'html content');
196196
expect(() => bundle.getBundleKey()).to.throw(
197-
'Cannot generate bundle key: no js file exists'
197+
'Cannot generate bundle key: no primary file exists'
198198
);
199199
});
200200
});

test/lib/processor.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ describe('BundleAnalyzer', () => {
6565
expect(result[0].filename).to.equal(bundleAnalyzer.lwcBundle.getBundleKey());
6666
});
6767

68-
it('should throw error when preprocessing html file with existing bundle that has no js file', () => {
68+
it('should process html file with existing bundle', () => {
6969
const htmlContent = '<template></template>';
7070
bundleAnalyzer.setLwcBundleFromContent('test', undefined, htmlContent);
71+
bundleAnalyzer.lwcBundle.setPrimaryFileByContent(htmlContent);
7172

72-
expect(() => bundleAnalyzer.preprocess(htmlContent, 'test.html')).to.throw(
73-
Error,
74-
'Cannot generate bundle key: no js file exists'
75-
);
73+
const result = bundleAnalyzer.preprocess(htmlContent, 'test.html');
74+
expect(result).to.have.length(1);
75+
expect(result[0].text).to.equal('');
76+
expect(result[0].filename).to.equal(bundleAnalyzer.lwcBundle.getBundleKey());
7677
});
7778

7879
it('should return empty array when no matching file found in bundle', () => {

test/lib/rules/programmatic-bundle-lint.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -118,53 +118,6 @@ export default class TestComponent extends LightningElement {
118118
expect(htmlViolation.line).toBeGreaterThan(0);
119119
});
120120

121-
it('should handle multiple HTML templates', () => {
122-
const jsContent = `
123-
import { LightningElement } from 'lwc';
124-
export default class MyComponent extends LightningElement {
125-
get value() {
126-
const x = 1;
127-
return x;
128-
}
129-
}`;
130-
131-
const htmlContent1 = `<template><div>{value}</div></template>`;
132-
const htmlContent2 = `<template><div>Second template</div></template>`;
133-
134-
// Set up bundle with multiple HTML templates
135-
bundleAnalyzer.setLwcBundleFromContent(
136-
'myComponent',
137-
jsContent,
138-
htmlContent1,
139-
htmlContent2
140-
);
141-
142-
const pluginPrefix = '@salesforce/lwc-graph-analyzer';
143-
const config = {
144-
...baseConfig,
145-
plugins: { [pluginPrefix]: lwcGraphAnalyzer },
146-
rules: {
147-
[`${pluginPrefix}/no-getter-contains-more-than-return-statement`]: 'error'
148-
}
149-
};
150-
151-
const linter = new Linter();
152-
153-
// Lint JS file
154-
const jsErrors = linter.verify(jsContent, config, {
155-
filename: 'myComponent.js'
156-
});
157-
158-
// Should have violation because getter is referenced in HTML
159-
expect(jsErrors.length).toBeGreaterThan(0);
160-
const violation = jsErrors.find(
161-
(err) =>
162-
err.ruleId ===
163-
'@salesforce/lwc-graph-analyzer/no-getter-contains-more-than-return-statement'
164-
);
165-
expect(violation).toBeDefined();
166-
});
167-
168121
it('should return empty violations when no errors exist', () => {
169122
const jsContent = `
170123
import { LightningElement } from 'lwc';

test/lib/util/bundle-state-manager.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ describe('BundleStateManager', () => {
3636
expect(bundleStateManager.getBundleByKey(bundle.getBundleKey())).to.equal(bundle);
3737
});
3838

39-
it('should return defined key when bundle has js file', () => {
39+
it('should return undefined key when bundle has no primary file', () => {
4040
const jsContent = 'export default class Test {}';
4141
const bundle = LwcBundle.lwcBundleFromContent('test', jsContent);
4242
const key = bundleStateManager.addBundleState(bundle);
43-
expect(key).to.not.be.undefined;
43+
expect(key).to.be.undefined;
4444
});
4545
});
4646

0 commit comments

Comments
 (0)