Skip to content

Commit c3454cb

Browse files
authored
Merge pull request microsoft#23 from kingdaro/semi-interpolation-fix
Fix for microsoft#22
2 parents e9f7443 + 43a70d1 commit c3454cb

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

e2e/tests/errors.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const { openMockFile, getFirstResponseOfType } = require('./_helpers');
44

55
const mockFileName = 'main.ts';
66

7-
87
describe('Errors', () => {
98
it('should return error for unknown property', () => {
109
const server = createServer();
@@ -115,4 +114,44 @@ describe('Errors', () => {
115114
assert.strictEqual(error.end.offset, 8);
116115
});
117116
});
117+
118+
it('should not error with interpolation at start, followed by semicolon #22', () => {
119+
const server = createServer();
120+
121+
const lines = [
122+
"function css(...args){}",
123+
"const mixin = ''",
124+
125+
// test single-line
126+
"css`${mixin}; color: blue;`",
127+
128+
// test multi-line (normal case)
129+
"css`",
130+
" ${mixin};",
131+
" color: blue;",
132+
"`",
133+
134+
// test multiple spaces after semi
135+
"css`",
136+
" ${mixin} ;",
137+
" color: blue;",
138+
"`",
139+
140+
// test hella semis - will this ever pop up? probably not, but screw it
141+
"css`",
142+
" ${mixin};;; ;; ;",
143+
" color: blue;",
144+
"`",
145+
];
146+
147+
openMockFile(server, mockFileName, lines.join('\n'));
148+
149+
server.send({ command: 'semanticDiagnosticsSync', arguments: { file: mockFileName } });
150+
151+
return server.close().then(() => {
152+
const errorResponse = getFirstResponseOfType('semanticDiagnosticsSync', server);
153+
assert.isTrue(errorResponse.success);
154+
assert.strictEqual(errorResponse.body.length, 0);
155+
});
156+
})
118157
})

src/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,22 @@ export = (mod: { typescript: typeof ts }) => {
3636
end: number
3737
): string {
3838
const placeholder = templateString.slice(start, end);
39+
40+
// check to see if it's an in-property interplation, or a mixin,
41+
// and determine which character to use in either case
42+
// if in-property, replace with "xxxxxx"
43+
// if a mixin, replace with " "
3944
const pre = templateString.slice(0, start);
4045
const replacementChar = pre.match(/(^|\n)\s*$/g) ? ' ' : 'x';
41-
return placeholder.replace(/./gm, c => c === '\n' ? '\n' : replacementChar);
46+
47+
let result = placeholder.replace(/./gm, c => c === '\n' ? '\n' : replacementChar);
48+
49+
// check if it's a mixin and if followed by a semicolon
50+
// if so, replace with a dummy variable declaration, so scss server doesn't complain about rogue semicolon
51+
if (replacementChar === ' ' && templateString.slice(end).match(/^\s*;/)) {
52+
result = '$a:0' + result.slice(4);
53+
}
54+
return result;
4255
},
4356
}, { logger });
4457
},

0 commit comments

Comments
 (0)