Skip to content

Commit

Permalink
add tests with functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Jan 3, 2024
1 parent 6f1ac93 commit 62683f1
Showing 1 changed file with 111 additions and 6 deletions.
117 changes: 111 additions & 6 deletions tests/use-strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("'use strict' directives", () => {
});
});

it("can be with single quotes", () => {
it("can be with single quotes at the top level", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
'use strict';
Expand All @@ -111,7 +111,7 @@ describe("'use strict' directives", () => {
});
});

it("can be without the semicolon", () => {
it("can be without the semicolon at the top level", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
"use strict"
Expand All @@ -124,7 +124,7 @@ describe("'use strict' directives", () => {
});
});

it("can be anywhere in the directive prologue", () => {
it("can be anywhere in the directive prologue at the top level", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
"foo";
Expand All @@ -137,7 +137,7 @@ describe("'use strict' directives", () => {
});
});

it("cannot be after the directive prologue", () => {
it("cannot be after the directive prologue at the top level", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
foo();
Expand All @@ -150,7 +150,7 @@ describe("'use strict' directives", () => {
});
});

it("cannot contain escapes", () => {
it("cannot contain escapes at the top level", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
"use \\strict";
Expand All @@ -162,7 +162,7 @@ describe("'use strict' directives", () => {
});
});

it("cannot be parenthesized", () => {
it("cannot be parenthesized at the top level", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
("use strict");
Expand All @@ -173,4 +173,109 @@ describe("'use strict' directives", () => {
assert.strictEqual(globalScope.isStrict, false);
});
});

it("can be with single quotes in a function", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
function foo() {
'use strict';
}
`, { ecmaVersion, range: true });

const { globalScope } = analyze(ast, { ecmaVersion, childVisitorKeys: KEYS });

assert.strictEqual(globalScope.isStrict, false);
assert.strictEqual(globalScope.childScopes.length, 1);
assert.strictEqual(globalScope.childScopes[0].type, "function");
assert.strictEqual(globalScope.childScopes[0].isStrict, true);
});
});

it("can be without the semicolon in a function", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
function foo() {
"use strict"
bar()
}
`, { ecmaVersion, range: true });

const { globalScope } = analyze(ast, { ecmaVersion, childVisitorKeys: KEYS });

assert.strictEqual(globalScope.isStrict, false);
assert.strictEqual(globalScope.childScopes.length, 1);
assert.strictEqual(globalScope.childScopes[0].type, "function");
assert.strictEqual(globalScope.childScopes[0].isStrict, true);
});
});

it("can be anywhere in the directive prologue in a function", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
function foo() {
"foo";
"use strict";
}
`, { ecmaVersion, range: true });

const { globalScope } = analyze(ast, { ecmaVersion, childVisitorKeys: KEYS });

assert.strictEqual(globalScope.isStrict, false);
assert.strictEqual(globalScope.childScopes.length, 1);
assert.strictEqual(globalScope.childScopes[0].type, "function");
assert.strictEqual(globalScope.childScopes[0].isStrict, true);
});
});

it("cannot be after the directive prologue in a function", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
function foo() {
bar();
"use strict";
}
`, { ecmaVersion, range: true });

const { globalScope } = analyze(ast, { ecmaVersion, childVisitorKeys: KEYS });

assert.strictEqual(globalScope.isStrict, false);
assert.strictEqual(globalScope.childScopes.length, 1);
assert.strictEqual(globalScope.childScopes[0].type, "function");
assert.strictEqual(globalScope.childScopes[0].isStrict, false);
});
});

it("cannot contain escapes in a function", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
function foo() {
"use \\strict";
}
`, { ecmaVersion, range: true });

const { globalScope } = analyze(ast, { ecmaVersion, childVisitorKeys: KEYS });

assert.strictEqual(globalScope.isStrict, false);
assert.strictEqual(globalScope.childScopes.length, 1);
assert.strictEqual(globalScope.childScopes[0].type, "function");
assert.strictEqual(globalScope.childScopes[0].isStrict, false);
});
});

it("cannot be parenthesized in a function", () => {
getSupportedEcmaVersions({ min: 5 }).forEach(ecmaVersion => {
const ast = espree.parse(`
function foo() {
("use strict");
}
`, { ecmaVersion, range: true });

const { globalScope } = analyze(ast, { ecmaVersion, childVisitorKeys: KEYS });

assert.strictEqual(globalScope.isStrict, false);
assert.strictEqual(globalScope.childScopes.length, 1);
assert.strictEqual(globalScope.childScopes[0].type, "function");
assert.strictEqual(globalScope.childScopes[0].isStrict, false);
});
});
});

0 comments on commit 62683f1

Please sign in to comment.