Skip to content

Commit 04b26b3

Browse files
committed
Fix flase error when placeholder used as value with unit outside of placeholder
Fixes microsoft#48
1 parent f91e38a commit 04b26b3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

e2e/tests/errors.js

+14
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,19 @@ describe('Errors', () => {
282282
});
283283
});
284284

285+
it('should not return an error for a placeholder value followed by unit (#48)', () => {
286+
const server = createServer();
287+
openMockFile(server, mockFileName, `function css(strings, ...) { return ""; }; const value = 1; const q = css\`
288+
width: $\{value}%;
289+
\``)
290+
server.send({ command: 'semanticDiagnosticsSync', arguments: { file: mockFileName } });
291+
292+
return server.close().then(() => {
293+
const errorResponse = getFirstResponseOfType('semanticDiagnosticsSync', server);
294+
assert.isTrue(errorResponse.success);
295+
assert.strictEqual(errorResponse.body.length, 0);
296+
});
297+
});
298+
285299
});
286300

src/index.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ export = (mod: { typescript: typeof ts }) => {
3333
// if a mixin, replace with " "
3434
const pre = templateString.slice(0, start);
3535
const post = templateString.slice(end);
36-
const replacementChar = pre.match(/(^|\n)\s*$/g) && !post.match(/^\s*\{/) /* ${'button'} */ ? ' ' : 'x';
37-
36+
const replacementChar = getReplacementCharacter(pre, post, logger);
3837
const result = placeholder.replace(/./gm, c => c === '\n' ? '\n' : replacementChar);
3938

4039
// If followed by a semicolon, we may have to eat the semi colon using a false property
@@ -63,3 +62,20 @@ export = (mod: { typescript: typeof ts }) => {
6362
},
6463
};
6564
};
65+
66+
function getReplacementCharacter(pre: string, post: string, logger: LanguageServiceLogger) {
67+
if (pre.match(/(^|\n)\s*$/g)) {
68+
if (!post.match(/^\s*\{/)) { // ${'button'} {
69+
return ' ';
70+
}
71+
}
72+
73+
// If the placeholder looks like a unit that would not work when replaced with an identifier,
74+
// try replaceing with a number.
75+
if (post.match(/^(%)/)) {
76+
return '0';
77+
}
78+
79+
// Otherwise replace with an identifier
80+
return 'x';
81+
}

0 commit comments

Comments
 (0)