Skip to content

Commit 65a7789

Browse files
committed
use a constant marker
1 parent f0772a4 commit 65a7789

File tree

2 files changed

+17
-29
lines changed

2 files changed

+17
-29
lines changed

packages/lit-analyzer/src/analyze/parse/document/virtual-document/virtual-ast-document.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,26 @@ import { DocumentOffset, DocumentRange, Range, SourceFilePosition, SourceFileRan
44
import { intersects, makeSourceFileRange } from "../../../util/range-util";
55
import { VirtualDocument } from "./virtual-document";
66

7+
const marker = `lit$analyzer$`;
8+
79
export class VirtualAstDocument implements VirtualDocument {
810
readonly fileName: string;
911
readonly location: SourceFileRange;
1012
private readonly parts: (Expression | string)[];
11-
protected readonly substitutions: WeakMap<Expression, string> = new WeakMap();
12-
protected substitutionIndex: number = 0;
1313

1414
private _text?: string;
1515

1616
get text(): string {
1717
if (this._text == null) {
1818
let str = "";
1919

20-
let prevPart = "";
2120
this.parts.forEach((part, i) => {
2221
const isLastPart = i >= this.parts.length - 1;
2322

2423
if (typeof part === "string") {
2524
str += part.substring(i === 0 ? 0 : 1, part.length - (isLastPart ? 0 : 2));
26-
prevPart = part;
2725
} else {
28-
const substitution = this.substituteExpression(part, prevPart, this.parts[i + 1] as string);
29-
this.substitutions.set(part, substitution);
30-
str += substitution;
26+
str += marker;
3127
}
3228
});
3329

@@ -72,19 +68,15 @@ export class VirtualAstDocument implements VirtualDocument {
7268
resultParts.push(substr);
7369
}
7470
} else {
75-
const sub = this.substitutions.get(part);
76-
77-
if (sub) {
78-
offset += sub.length;
71+
offset += marker.length;
7972

80-
const expressionPartRange: Range = {
81-
start: startOffset,
82-
end: offset
83-
};
73+
const expressionPartRange: Range = {
74+
start: startOffset,
75+
end: offset
76+
};
8477

85-
if (intersects(expressionPartRange, range)) {
86-
resultParts.push(part);
87-
}
78+
if (intersects(expressionPartRange, range)) {
79+
resultParts.push(part);
8880
}
8981
}
9082
});
@@ -129,10 +121,6 @@ export class VirtualAstDocument implements VirtualDocument {
129121
this.fileName = this.fileName = astNodeOrParts.getSourceFile().fileName;
130122
}
131123
}
132-
133-
protected substituteExpression(expression: Expression, prev: string, next: string | undefined): string {
134-
return `la_expr_${this.substitutionIndex++}`;
135-
}
136124
}
137125

138126
function getPartsFromTaggedTemplate(astNode: TaggedTemplateExpression): { expressionParts: Expression[]; literalParts: Node[] } {

packages/lit-analyzer/test/parser/css-document/css-substitutions.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ function isTemplateText(t: ExecutionContext, text: string, testFile: string) {
1717
}
1818

1919
tsTest("Substitute for template followed by percent", t => {
20-
isTemplateText(t, "{ div { transform-origin: 00% 01%; } }", "css`{ div { transform-origin: ${x}% ${y}%; } }`");
20+
isTemplateText(t, "{ div { transform-origin: lit$analyzer$% lit$analyzer$%; } }", "css`{ div { transform-origin: ${x}% ${y}%; } }`");
2121
});
2222

2323
tsTest("Substitute for template last in css list", t => {
24-
isTemplateText(t, "{ div { border: 2px solid _0; } }", "css`{ div { border: 2px solid ${COLOR}; } }`");
24+
isTemplateText(t, "{ div { border: 2px solid lit$analyzer$; } }", "css`{ div { border: 2px solid ${COLOR}; } }`");
2525
});
2626

2727
tsTest("Substitute for template first in css list", t => {
28-
isTemplateText(t, "{ div { border: _0 solid #ffffff; } }", "css`{ div { border: ${WIDTH} solid #ffffff; } }`");
28+
isTemplateText(t, "{ div { border: lit$analyzer$ solid #ffffff; } }", "css`{ div { border: ${WIDTH} solid #ffffff; } }`");
2929
});
3030

3131
tsTest("Substitute for template middle in css list", t => {
32-
isTemplateText(t, "{ div { border: 2px _0 #ffffff; } }", "css`{ div { border: 2px ${STYLE} #ffffff; } }`");
32+
isTemplateText(t, "{ div { border: 2px lit$analyzer$ #ffffff; } }", "css`{ div { border: 2px ${STYLE} #ffffff; } }`");
3333
});
3434

3535
tsTest("Substitute for template css key-value pair", t => {
36-
isTemplateText(t, "{ div { $_:_0; } }", "css`{ div { ${unsafeCSS('color: red')}; } }`");
36+
isTemplateText(t, "{ div { lit$analyzer$; } }", "css`{ div { ${unsafeCSS('color: red')}; } }`");
3737
});
3838

3939
tsTest("Substitute for template css value only", t => {
40-
isTemplateText(t, "{ div { color: _0; } }", "css`{ div { color: ${unsafeCSS('red')}; } }`");
40+
isTemplateText(t, "{ div { color: lit$analyzer$; } }", "css`{ div { color: ${unsafeCSS('red')}; } }`");
4141
});
4242

4343
tsTest("Substitute for template css key only", t => {
44-
isTemplateText(t, "{ div { $_0: red; } }", "css`{ div { ${unsafeCSS('color')}: red; } }`");
44+
isTemplateText(t, "{ div { lit$analyzer$: red; } }", "css`{ div { ${unsafeCSS('color')}: red; } }`");
4545
});

0 commit comments

Comments
 (0)