1+ // @ts -check
12const assert = require ( 'chai' ) . assert ;
23const createServer = require ( '../server-fixture' ) ;
34const { openMockFile, getFirstResponseOfType } = require ( './_helpers' ) ;
45
56const mockFileName = 'main.ts' ;
67
8+ const getSemanticDiagnosticsForFile = ( fileContents ) => {
9+ const server = createServer ( ) ;
10+ openMockFile ( server , mockFileName , fileContents ) ;
11+ server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
12+
13+ return server . close ( ) . then ( errorResponse => {
14+ return getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
15+ } ) ;
16+ }
17+
718describe ( 'Errors' , ( ) => {
819 it ( 'should return error for unknown property' , ( ) => {
9- const server = createServer ( ) ;
10- openMockFile ( server , mockFileName , 'function css(x) { return x; }; const q = css`boarder: 1px solid black;`' ) ;
11- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
12-
13- return server . close ( ) . then ( ( ) => {
14- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
20+ return getSemanticDiagnosticsForFile (
21+ 'function css(x) { return x; }; const q = css`boarder: 1px solid black;`'
22+ ) . then ( errorResponse => {
1523 assert . isTrue ( errorResponse . success ) ;
1624 assert . strictEqual ( errorResponse . body . length , 1 ) ;
1725 const error = errorResponse . body [ 0 ] ;
@@ -24,63 +32,47 @@ describe('Errors', () => {
2432 } ) ;
2533
2634 it ( 'should not return errors for empty rulesets' , ( ) => {
27- const server = createServer ( ) ;
28- openMockFile ( server , mockFileName , 'function css(x) { return x; }; const q = css``' ) ;
29- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
30-
31- return server . close ( ) . then ( ( ) => {
32- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
35+ return getSemanticDiagnosticsForFile (
36+ 'function css(x) { return x; }; const q = css``'
37+ ) . then ( errorResponse => {
3338 assert . isTrue ( errorResponse . success ) ;
3439 assert . strictEqual ( errorResponse . body . length , 0 ) ;
3540 } ) ;
3641 } ) ;
3742
3843 it ( 'should not return errors for nested rulesets' , ( ) => {
39- const server = createServer ( ) ;
40- openMockFile ( server , mockFileName , 'function css(x) { return x; }; const q = css`&:hover { border: 1px solid black; }`' ) ;
41- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
42-
43- return server . close ( ) . then ( ( ) => {
44- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
44+ return getSemanticDiagnosticsForFile (
45+ 'function css(x) { return x; }; const q = css`&:hover { border: 1px solid black; }`'
46+ ) . then ( errorResponse => {
4547 assert . isTrue ( errorResponse . success ) ;
4648 assert . strictEqual ( errorResponse . body . length , 0 ) ;
4749 } ) ;
4850 } ) ;
4951
5052 it ( 'should not return an error for a placeholder in a property' , ( ) => {
51- const server = createServer ( ) ;
52- openMockFile ( server , mockFileName , 'function css(strings, ...) { return ""; }; const q = css`color: ${"red"};`' )
53- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
54-
55- return server . close ( ) . then ( ( ) => {
56- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
53+ return getSemanticDiagnosticsForFile (
54+ 'function css(strings, ...) { return ""; }; const q = css`color: ${"red"};`'
55+ ) . then ( errorResponse => {
5756 assert . isTrue ( errorResponse . success ) ;
5857 assert . strictEqual ( errorResponse . body . length , 0 ) ;
5958 } ) ;
6059 } ) ;
6160
6261 it ( 'should not return an error for a placeholder in a property with a multiline string' , ( ) => {
63- const server = createServer ( ) ;
64- openMockFile ( server , mockFileName , [
62+ return getSemanticDiagnosticsForFile ( [
6563 'function css(strings, ...) { return ""; }; const q = css`' ,
6664 ' color: ${"red"};' ,
67- '`' ] . join ( '\n' ) ) ;
68- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
69-
70- return server . close ( ) . then ( ( ) => {
71- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
65+ '`' ] . join ( '\n' )
66+ ) . then ( errorResponse => {
7267 assert . isTrue ( errorResponse . success ) ;
7368 assert . strictEqual ( errorResponse . body . length , 0 ) ;
7469 } ) ;
7570 } ) ;
7671
7772 it ( 'should return errors when error occures in last position' , ( ) => {
78- const server = createServer ( ) ;
79- openMockFile ( server , mockFileName , 'function css(strings, ...) { return ""; }; const q = css`;`' )
80- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
81-
82- return server . close ( ) . then ( ( ) => {
83- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
73+ return getSemanticDiagnosticsForFile (
74+ 'function css(strings, ...) { return ""; }; const q = css`;`'
75+ ) . then ( errorResponse => {
8476 assert . isTrue ( errorResponse . success ) ;
8577 assert . strictEqual ( errorResponse . body . length , 1 ) ;
8678 const error = errorResponse . body [ 0 ] ;
@@ -93,17 +85,13 @@ describe('Errors', () => {
9385 } ) ;
9486
9587 it ( 'should return error for multiline unknown property #20' , ( ) => {
96- const server = createServer ( ) ;
97- openMockFile ( server , mockFileName , [
88+ return getSemanticDiagnosticsForFile ( [
9889 'function css(x) { return x; };' ,
9990 'const q = css`' ,
10091 'boarder: 1px solid black;' ,
10192 '`'
102- ] . join ( '\n' ) ) ;
103- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
104-
105- return server . close ( ) . then ( ( ) => {
106- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
93+ ] . join ( '\n' )
94+ ) . then ( errorResponse => {
10795 assert . isTrue ( errorResponse . success ) ;
10896 assert . strictEqual ( errorResponse . body . length , 1 ) ;
10997 const error = errorResponse . body [ 0 ] ;
@@ -116,9 +104,7 @@ describe('Errors', () => {
116104 } ) ;
117105
118106 it ( 'should not error with interpolation at start, followed by semicolon #22' , ( ) => {
119- const server = createServer ( ) ;
120-
121- const lines = [
107+ return getSemanticDiagnosticsForFile ( [
122108 "function css(...args){}" ,
123109 "const mixin = ''" ,
124110
@@ -142,34 +128,24 @@ describe('Errors', () => {
142128 " ${mixin};;; ;; ;" ,
143129 " color: blue;" ,
144130 "`" ,
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 ) ;
131+ ] . join ( '\n' )
132+ ) . then ( errorResponse => {
153133 assert . isTrue ( errorResponse . success ) ;
154134 assert . strictEqual ( errorResponse . body . length , 0 ) ;
155135 } ) ;
156136 } ) ;
157137
158138 it ( 'should not return an error for a placeholder used as a selector (#30)' , ( ) => {
159- const server = createServer ( ) ;
160- openMockFile ( server , mockFileName , 'function css(strings, ...) { return ""; }; const q = css`${"button"} { color: red; }`' )
161- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
162-
163- return server . close ( ) . then ( ( ) => {
164- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
139+ return getSemanticDiagnosticsForFile (
140+ 'function css(strings, ...) { return ""; }; const q = css`${"button"} { color: red; }`'
141+ ) . then ( errorResponse => {
165142 assert . isTrue ( errorResponse . success ) ;
166143 assert . strictEqual ( errorResponse . body . length , 0 ) ;
167144 } ) ;
168145 } ) ;
169146
170147 it ( 'should not return an error for a placeholder used as a complex selector (#30)' , ( ) => {
171- const server = createServer ( ) ;
172- openMockFile ( server , mockFileName , `
148+ return getSemanticDiagnosticsForFile ( `
173149 function css(strings, ...) { return ""; };
174150 function fullWidth() { };
175151 const Button = {};
@@ -186,51 +162,42 @@ describe('Errors', () => {
186162 border-radius: 0;
187163 }
188164 }
189- \`` )
190- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
191-
192- return server . close ( ) . then ( ( ) => {
193- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
165+ \``
166+ ) . then ( errorResponse => {
194167 assert . isTrue ( errorResponse . success ) ;
195168 assert . strictEqual ( errorResponse . body . length , 0 ) ;
196169 } ) ;
197170 } ) ;
198171
199172 it ( 'should not return an error for a placeholder used as selector part (#39)' , ( ) => {
200- const server = createServer ( ) ;
201- openMockFile ( server , mockFileName , 'function css(strings, ...) { return ""; }; const Content = "button"; const q = css`& > ${Content} { margin-left: 1px; }`' )
202- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
203-
204- return server . close ( ) . then ( ( ) => {
205- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
173+ return getSemanticDiagnosticsForFile (
174+ 'function css(strings, ...) { return ""; }; const Content = "button"; const q = css`& > ${Content} { margin-left: 1px; }`'
175+ ) . then ( errorResponse => {
206176 assert . isTrue ( errorResponse . success ) ;
207177 assert . strictEqual ( errorResponse . body . length , 0 ) ;
208178 } ) ;
209179 } ) ;
210180
211181 it ( 'should not return an error for a placeholder in multiple properties (#39)' , ( ) => {
212- const server = createServer ( ) ;
213- openMockFile ( server , mockFileName , `function css(strings, ...) { return ""; }; const Content = "button"; const q = css\`
182+ return getSemanticDiagnosticsForFile (
183+ `function css(strings, ...) { return ""; }; const Content = "button"; const q = css\`
214184 & > $\{'content'} {
215185 color: 1px;
216186 }
217187
218188 & > $\{'styledNavBar'} {
219189 margin-left: $\{1};
220190 }
221- \`` )
222- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
223-
224- return server . close ( ) . then ( ( ) => {
225- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
191+ \``
192+ ) . then ( errorResponse => {
226193 assert . isTrue ( errorResponse . success ) ;
227194 assert . strictEqual ( errorResponse . body . length , 0 ) ;
228195 } ) ;
229196 } ) ;
230197
231198 it ( 'should not return an error for a placeholder that spans multiple lines (#44)' , ( ) => {
232- const server = createServer ( ) ;
233- openMockFile ( server , mockFileName , `let css: any = {}; const q = css.a\`
199+ return getSemanticDiagnosticsForFile (
200+ `let css: any = {}; const q = css.a\`
234201 color:
235202 $\{'transparent'};
236203 border-bottom: 1px;
@@ -239,20 +206,17 @@ describe('Errors', () => {
239206 text-decoration: none;
240207 }
241208}
242- \`` )
243- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
244-
245- return server . close ( ) . then ( ( ) => {
246- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
209+ \``
210+ ) . then ( errorResponse => {
247211 assert . isTrue ( errorResponse . success ) ;
248212 assert . strictEqual ( errorResponse . body . length , 0 ) ;
249213 } ) ;
250214 } ) ;
251215
252216
253217 it ( 'should not return an error for complicated style (#44)' , ( ) => {
254- const server = createServer ( ) ;
255- openMockFile ( server , mockFileName , `let css: any = {}; const q = css.a\`
218+ return getSemanticDiagnosticsForFile (
219+ `let css: any = {}; const q = css.a\`
256220 display: flex;
257221 width: 6rem;
258222 height: 5rem;
@@ -272,29 +236,22 @@ describe('Errors', () => {
272236 color: inherit;
273237 text-decoration: none;
274238 }
275- \`` )
276- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
277-
278- return server . close ( ) . then ( ( ) => {
279- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
239+ \``
240+ ) . then ( errorResponse => {
280241 assert . isTrue ( errorResponse . success ) ;
281242 assert . strictEqual ( errorResponse . body . length , 0 ) ;
282243 } ) ;
283244 } ) ;
284245
285246 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\`
247+ return getSemanticDiagnosticsForFile (
248+ `function css(strings, ...) { return ""; }; const value = 1; const q = css\`
288249 width: $\{value}%;
289- \`` )
290- server . send ( { command : 'semanticDiagnosticsSync' , arguments : { file : mockFileName } } ) ;
291-
292- return server . close ( ) . then ( ( ) => {
293- const errorResponse = getFirstResponseOfType ( 'semanticDiagnosticsSync' , server ) ;
250+ \``
251+ ) . then ( errorResponse => {
294252 assert . isTrue ( errorResponse . success ) ;
295253 assert . strictEqual ( errorResponse . body . length , 0 ) ;
296254 } ) ;
297255 } ) ;
298-
299256} ) ;
300257
0 commit comments