Skip to content

Commit 966204a

Browse files
committed
Handle adjacent variables case (fix microsoft#62)
By using `contents.slice(listIndex, span.start)` as `pre`, we couldn't tell the different between ` ${'color: red'};` and the second variable of `margin: ${'1px'} ${'1px'};`. Both of their `pre` are ` `, hence making both of them got replaced by `$a:0 `, which cause an error in the second code (`margin: xxxxxxxx $a:0 ;`). To tell their different, I decided to use a whole line as a context. Use the start of the line to the placeholder as `pre`. This way the second code's `pre` would be `margin: ${'1px'} `, which is now different from the `pre` of ` ${'color: red'};`.
1 parent 79111dd commit 966204a

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

e2e/tests/errors.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,5 +294,16 @@ describe('Errors', () => {
294294
assert.strictEqual(errorResponse.body.length, 0);
295295
});
296296
});
297+
298+
it('should not return an error on adjacent variables (#62)', () => {
299+
return getSemanticDiagnosticsForFile(
300+
`let css: any = {}; const margin1 = "3px"; const margin2 = "3px"; const q = css.a\`
301+
margin: $\{margin1} $\{margin2};
302+
\``
303+
).then(errorResponse => {
304+
assert.isTrue(errorResponse.success);
305+
assert.strictEqual(errorResponse.body.length, 0);
306+
});
307+
});
297308
});
298309

src/_substituter.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ export function getSubstitutions(
77
): string {
88
const parts: string[] = [];
99
let lastIndex = 0;
10+
const lineStarts = contents
11+
.split('\n')
12+
.map(line => line.length)
13+
.reduce((previousValue, currentValue, currentIndex) => [...previousValue, currentValue + previousValue[currentIndex] + 1], [0]);
14+
let lineStartIndex = 0;
1015
for (const span of spans) {
11-
const pre = contents.slice(lastIndex, span.start);
16+
while (lineStarts[lineStartIndex] <= span.start) {
17+
lineStartIndex++;
18+
}
19+
const pre = contents.slice(lineStarts[lineStartIndex - 1], span.start);
1220
const post = contents.slice(span.end);
1321
const placeholder = contents.slice(span.start, span.end);
1422

15-
parts.push(pre);
23+
parts.push(contents.slice(lastIndex, span.start));
1624
parts.push(getSubstitution({ pre, placeholder, post }));
1725
lastIndex = span.end;
1826
}

src/test/substituter.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ describe('substituter', () => {
132132
'color: #000 ;'
133133
);
134134
});
135+
136+
it('should replace adjacent variables with x (#62)', () => {
137+
assert.deepEqual(
138+
performSubstitutions([
139+
`margin: \${'1px'}\${'1px'};`,
140+
`padding: \${'1px'} \${'1px'};`,
141+
].join('\n')),
142+
[
143+
`margin: xxxxxxxxxxxxxxxx;`,
144+
`padding: xxxxxxxx xxxxxxxx;`,
145+
].join('\n')
146+
);
147+
});
135148
});
136149

137150
function performSubstitutions(value: string) {

0 commit comments

Comments
 (0)