@@ -98,4 +98,184 @@ describe("'use strict' directives", () => {
9898 assertIsStrictRecursively ( globalScope . childScopes [ 1 ] , false ) ; // function e() { ... }
9999 } ) ;
100100 } ) ;
101+
102+ it ( "can be with single quotes at the top level" , ( ) => {
103+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
104+ const ast = espree . parse ( `
105+ 'use strict';
106+ ` , { ecmaVersion, range : true } ) ;
107+
108+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
109+
110+ assert . strictEqual ( globalScope . isStrict , true ) ;
111+ } ) ;
112+ } ) ;
113+
114+ it ( "can be without the semicolon at the top level" , ( ) => {
115+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
116+ const ast = espree . parse ( `
117+ "use strict"
118+ foo()
119+ ` , { ecmaVersion, range : true } ) ;
120+
121+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
122+
123+ assert . strictEqual ( globalScope . isStrict , true ) ;
124+ } ) ;
125+ } ) ;
126+
127+ it ( "can be anywhere in the directive prologue at the top level" , ( ) => {
128+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
129+ const ast = espree . parse ( `
130+ "foo";
131+ "use strict";
132+ ` , { ecmaVersion, range : true } ) ;
133+
134+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
135+
136+ assert . strictEqual ( globalScope . isStrict , true ) ;
137+ } ) ;
138+ } ) ;
139+
140+ it ( "cannot be after the directive prologue at the top level" , ( ) => {
141+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
142+ const ast = espree . parse ( `
143+ foo();
144+ "use strict";
145+ ` , { ecmaVersion, range : true } ) ;
146+
147+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
148+
149+ assert . strictEqual ( globalScope . isStrict , false ) ;
150+ } ) ;
151+ } ) ;
152+
153+ it ( "cannot contain escapes at the top level" , ( ) => {
154+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
155+ const ast = espree . parse ( `
156+ "use \\strict";
157+ ` , { ecmaVersion, range : true } ) ;
158+
159+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
160+
161+ assert . strictEqual ( globalScope . isStrict , false ) ;
162+ } ) ;
163+ } ) ;
164+
165+ it ( "cannot be parenthesized at the top level" , ( ) => {
166+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
167+ const ast = espree . parse ( `
168+ ("use strict");
169+ ` , { ecmaVersion, range : true } ) ;
170+
171+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
172+
173+ assert . strictEqual ( globalScope . isStrict , false ) ;
174+ } ) ;
175+ } ) ;
176+
177+ it ( "can be with single quotes in a function" , ( ) => {
178+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
179+ const ast = espree . parse ( `
180+ function foo() {
181+ 'use strict';
182+ }
183+ ` , { ecmaVersion, range : true } ) ;
184+
185+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
186+
187+ assert . strictEqual ( globalScope . isStrict , false ) ;
188+ assert . strictEqual ( globalScope . childScopes . length , 1 ) ;
189+ assert . strictEqual ( globalScope . childScopes [ 0 ] . type , "function" ) ;
190+ assert . strictEqual ( globalScope . childScopes [ 0 ] . isStrict , true ) ;
191+ } ) ;
192+ } ) ;
193+
194+ it ( "can be without the semicolon in a function" , ( ) => {
195+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
196+ const ast = espree . parse ( `
197+ function foo() {
198+ "use strict"
199+ bar()
200+ }
201+ ` , { ecmaVersion, range : true } ) ;
202+
203+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
204+
205+ assert . strictEqual ( globalScope . isStrict , false ) ;
206+ assert . strictEqual ( globalScope . childScopes . length , 1 ) ;
207+ assert . strictEqual ( globalScope . childScopes [ 0 ] . type , "function" ) ;
208+ assert . strictEqual ( globalScope . childScopes [ 0 ] . isStrict , true ) ;
209+ } ) ;
210+ } ) ;
211+
212+ it ( "can be anywhere in the directive prologue in a function" , ( ) => {
213+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
214+ const ast = espree . parse ( `
215+ function foo() {
216+ "foo";
217+ "use strict";
218+ }
219+ ` , { ecmaVersion, range : true } ) ;
220+
221+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
222+
223+ assert . strictEqual ( globalScope . isStrict , false ) ;
224+ assert . strictEqual ( globalScope . childScopes . length , 1 ) ;
225+ assert . strictEqual ( globalScope . childScopes [ 0 ] . type , "function" ) ;
226+ assert . strictEqual ( globalScope . childScopes [ 0 ] . isStrict , true ) ;
227+ } ) ;
228+ } ) ;
229+
230+ it ( "cannot be after the directive prologue in a function" , ( ) => {
231+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
232+ const ast = espree . parse ( `
233+ function foo() {
234+ bar();
235+ "use strict";
236+ }
237+ ` , { ecmaVersion, range : true } ) ;
238+
239+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
240+
241+ assert . strictEqual ( globalScope . isStrict , false ) ;
242+ assert . strictEqual ( globalScope . childScopes . length , 1 ) ;
243+ assert . strictEqual ( globalScope . childScopes [ 0 ] . type , "function" ) ;
244+ assert . strictEqual ( globalScope . childScopes [ 0 ] . isStrict , false ) ;
245+ } ) ;
246+ } ) ;
247+
248+ it ( "cannot contain escapes in a function" , ( ) => {
249+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
250+ const ast = espree . parse ( `
251+ function foo() {
252+ "use \\strict";
253+ }
254+ ` , { ecmaVersion, range : true } ) ;
255+
256+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
257+
258+ assert . strictEqual ( globalScope . isStrict , false ) ;
259+ assert . strictEqual ( globalScope . childScopes . length , 1 ) ;
260+ assert . strictEqual ( globalScope . childScopes [ 0 ] . type , "function" ) ;
261+ assert . strictEqual ( globalScope . childScopes [ 0 ] . isStrict , false ) ;
262+ } ) ;
263+ } ) ;
264+
265+ it ( "cannot be parenthesized in a function" , ( ) => {
266+ getSupportedEcmaVersions ( { min : 5 } ) . forEach ( ecmaVersion => {
267+ const ast = espree . parse ( `
268+ function foo() {
269+ ("use strict");
270+ }
271+ ` , { ecmaVersion, range : true } ) ;
272+
273+ const { globalScope } = analyze ( ast , { ecmaVersion, childVisitorKeys : KEYS } ) ;
274+
275+ assert . strictEqual ( globalScope . isStrict , false ) ;
276+ assert . strictEqual ( globalScope . childScopes . length , 1 ) ;
277+ assert . strictEqual ( globalScope . childScopes [ 0 ] . type , "function" ) ;
278+ assert . strictEqual ( globalScope . childScopes [ 0 ] . isStrict , false ) ;
279+ } ) ;
280+ } ) ;
101281} ) ;
0 commit comments