Skip to content

Commit bb3540d

Browse files
committed
Guard against processor re-entry
Changing filenames in the return value of `preprocess()` automatically triggers the processor again for those "new files". This adds a guard to bail in processing if those temporary files come in for new processing.
1 parent ae44e59 commit bb3540d

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

lib/processor.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
'use strict';
99

1010
const { extname } = require('path');
11-
const { setLwcBundleCacheEntry, removeLwcBundleCacheEntry } = require('./util/helper');
11+
const {
12+
setLwcBundleCacheEntry,
13+
removeLwcBundleCacheEntry,
14+
extractBundleKey
15+
} = require('./util/helper');
1216
const LwcBundle = require('./lwc-bundle');
17+
const bundleStateManager = require('./util/bundle-state-manager');
1318

1419
const SUPPORTS_AUTOFIX = true;
1520

@@ -83,6 +88,13 @@ class BundleAnalyzer {
8388
* @returns {{ text: string, filename: string }[]} An array of files and their content, to be processed.
8489
*/
8590
preprocess = (text, filename) => {
91+
// Check if this is a file we generated ourselves with `preprocess()` return values.
92+
const bundleKey = extractBundleKey(filename);
93+
if (bundleStateManager.hasBundleWithKey(bundleKey)) {
94+
// Skip processing for files we generated
95+
return [{ text, filename }];
96+
}
97+
8698
const fileExtension = extname(filename);
8799

88100
// If this.#lwcBundle has already been set, use that data. Otherwise, create

lib/util/bundle-state-manager.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ class BundleStateManager {
6565
clear() {
6666
this.#bundleMap.clear();
6767
}
68+
69+
/**
70+
* Checks if a bundle exists with the given key
71+
*
72+
* @param {string} key - The unique key to check for
73+
* @returns {boolean} True if a bundle exists with the given key, false otherwise
74+
*/
75+
hasBundleWithKey(key) {
76+
return this.#bundleMap.has(key);
77+
}
6878
}
6979

7080
// Export a singleton instance

lib/util/helper.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,6 @@ function getKomaciReport(ruleName, filename) {
142142
module.exports = {
143143
setLwcBundleCacheEntry,
144144
removeLwcBundleCacheEntry,
145-
analyzeLWC
145+
analyzeLWC,
146+
extractBundleKey
146147
};

test/lib/processor.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
const { expect } = require('chai');
1111
const bundleAnalyzer = require('../../lib/processor');
1212
const LwcBundle = require('../../lib/lwc-bundle');
13+
const bundleStateManager = require('../../lib/util/bundle-state-manager');
1314

1415
describe('BundleAnalyzer', () => {
1516
beforeEach(() => {
@@ -96,6 +97,36 @@ describe('BundleAnalyzer', () => {
9697
expect(bundleAnalyzer.lwcBundle.primaryFile).to.equal(bundleAnalyzer.lwcBundle.js);
9798
expect(result[0].filename).to.equal(bundleAnalyzer.lwcBundle.getBundleKey());
9899
});
100+
101+
it('should skip processing for files that have already been processed', () => {
102+
// First create a bundle and add it to the state manager
103+
const jsContent = 'export default class Test {}';
104+
const bundle = LwcBundle.lwcBundleFromContent('test', jsContent);
105+
// Set the primary file before getting the bundle key
106+
bundle.setPrimaryFileByContent(jsContent);
107+
const bundleKey = bundle.getBundleKey();
108+
bundleStateManager.addBundleState(bundle);
109+
110+
// Now try to process a file with the same bundle key
111+
const result = bundleAnalyzer.preprocess(jsContent, `/some/path/0_${bundleKey}`);
112+
113+
// Should return the original text without further processing
114+
expect(result).to.have.length(1);
115+
expect(result[0].text).to.equal(jsContent);
116+
expect(result[0].filename).to.equal(`/some/path/0_${bundleKey}`);
117+
118+
// Clean up
119+
bundleStateManager.removeBundleState(bundle);
120+
});
121+
122+
it('should process files that have not been processed before', () => {
123+
const jsContent = 'export default class Test {}';
124+
const result = bundleAnalyzer.preprocess(jsContent, 'test.js');
125+
126+
expect(result).to.have.length(1);
127+
expect(result[0].text).to.equal(jsContent);
128+
expect(bundleAnalyzer.lwcBundle).to.be.instanceof(LwcBundle);
129+
});
99130
});
100131

101132
describe('postprocess', () => {

0 commit comments

Comments
 (0)