Skip to content
This repository was archived by the owner on Apr 16, 2023. It is now read-only.

Commit e45ae8a

Browse files
committed
fix: first rule error
1 parent fa369ae commit e45ae8a

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/multireplacer/Intermediate.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Replacement } from './index';
1+
import { Replacement } from './Replacement';
22

33
export class Intermediate<ContextClass = unknown> {
44
public readonly value: string;
@@ -37,7 +37,7 @@ export class Intermediate<ContextClass = unknown> {
3737
}
3838

3939
absorb(other: Intermediate<ContextClass>): this {
40-
if (this.value !== other.value) {
40+
if (this.equals(other)) {
4141
throw new Error(
4242
`Cannot merge different intermediates: ${this.value} and ${other.value}`,
4343
);
@@ -57,15 +57,12 @@ export class Intermediate<ContextClass = unknown> {
5757
return this;
5858
}
5959

60-
*replacements(): IterableIterator<Replacement<ContextClass>> {
60+
*chain(): IterableIterator<Intermediate<ContextClass>> {
6161
// eslint-disable-next-line @typescript-eslint/no-this-alias
6262
let item: Intermediate<ContextClass> | null = this;
6363

6464
while (item) {
65-
if (item.via) {
66-
yield item.via;
67-
}
68-
65+
yield item;
6966
item = item.parent;
7067
}
7168
}

src/multireplacer/Multireplacer.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,29 @@ export class Multireplacer<Context = unknown> {
1313
): MultireplacerOutput<Context> {
1414
const intermediatesCache = new IntermediatesCache<Context>();
1515

16-
let intermediates = values.map((v) => new Intermediate(v, record));
16+
let intermediates = values.map((v) => {
17+
return intermediatesCache.resolve(new Intermediate(v, record));
18+
});
1719
let nextIntermediates: Intermediate<Context>[] = [];
18-
let applied = false;
1920
let rule: MultireplacerRule<Context>;
2021
let value: Intermediate<Context>;
2122

2223
for (rule of this.rules) {
2324
rule.intermediatesCache = intermediatesCache;
25+
2426
nextIntermediates = [];
25-
applied = false;
2627

2728
for (value of intermediates) {
2829
if (rule.appliesTo(value)) {
2930
rule.apply(value, nextIntermediates);
30-
applied = true;
3131
}
3232
}
3333

34-
if (applied) {
34+
if (nextIntermediates.length > 0) {
3535
intermediates = nextIntermediates;
3636
}
37+
38+
rule.intermediatesCache = null;
3739
}
3840

3941
return { variants: intermediates };

src/multireplacer/MultireplacerRule.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@ export class MultireplacerRule<Context>
4242
? intermediate.value.replace(this.splitter, replacerFn)
4343
: replacerFn(intermediate.value);
4444

45-
let newIntermediate = new Intermediate<Context>(
46-
newValue,
47-
intermediate,
48-
variant,
49-
);
50-
51-
if (this.intermediatesCache) {
52-
newIntermediate = this.intermediatesCache.resolve(newIntermediate);
53-
}
45+
if (newValue !== intermediate.value) {
46+
let newIntermediate = new Intermediate<Context>(
47+
newValue,
48+
intermediate,
49+
variant,
50+
);
51+
52+
if (this.intermediatesCache) {
53+
newIntermediate = this.intermediatesCache.resolve(newIntermediate);
54+
}
5455

55-
if (!results.includes(newIntermediate)) {
56-
results.push(newIntermediate);
56+
if (!results.includes(newIntermediate)) {
57+
results.push(newIntermediate);
58+
}
5759
}
5860
}
5961

0 commit comments

Comments
 (0)