Skip to content

Commit 586f821

Browse files
committed
Replace unnecessary map by array
1 parent dd795bd commit 586f821

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

lib/dummy_replacements_map.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
/**
2-
* Map that tracks the replacements that have been made in the processed text
3-
* alongside their unique id. This id is just a counter that is incremented
4-
* for each replacement, see preprocess.js.
2+
* Array that tracks the replacements that have been made in the processed text,
3+
* so that we can reverse them later.
54
*/
65
class DummyReplacementsMap {
76
constructor() {
8-
this.map = new Map();
7+
this.replacements = [];
98
}
109

11-
addMapping(id, originalText, dummyText) {
12-
this.map.set(id, [originalText, dummyText]);
10+
addMapping(originalText, dummyText) {
11+
this.replacements.push([originalText, dummyText]);
1312
}
1413

1514
/**
16-
* Applies the replacements from the map to the given text.
15+
* Tries to apply all stored replacements to the given text.
1716
* @param {string} text - The text to apply the replacements to.
1817
* @returns {string} - The text with the replacements applied.
1918
*/
2019
apply(text) {
2120
let lintableText = text;
22-
for (const [_id, [originalText, dummyText]] of this.map) {
21+
for (const [originalText, dummyText] of this.replacements) {
2322
lintableText = lintableText.replace(dummyText, originalText);
2423
}
2524
return lintableText;

lib/preprocess.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ const HASH = "566513c5d83ac26e15414f2754"; // to avoid collisions with user code
2020
* @param {string} text text of the file
2121
* @param {string} filename filename of the file
2222
* @param {(id) => string} dummyString dummy string to replace ERB tags with
23-
* (this is language-specific)
23+
* (this is language-specific). Should be
24+
* a function that takes an id and returns
25+
* a UNIQUE dummy string.
2426
* @returns {Array<{ filename: string, text: string }>} source code blocks to lint
2527
*/
2628
function preprocess(text, filename, dummyString) {
@@ -54,7 +56,7 @@ function preprocess(text, filename, dummyString) {
5456
replaceTextWithDummy(lintableTextArr, startIndex, matchLength - 1, dummy);
5557

5658
const textWithErbSyntax = text.slice(startIndex, endIndex);
57-
dummyReplacementsMap.addMapping(matchedId, textWithErbSyntax, dummy);
59+
dummyReplacementsMap.addMapping(textWithErbSyntax, dummy);
5860

5961
// Store in map
6062
const lineAfter = coordEndIndex.line - numAddLines;

0 commit comments

Comments
 (0)