ladybird/Libraries/LibJS/Tests/use-strict-directive.js
Linus Groh 9e80c67608 LibJS: Fix "use strict" directive false positives
By having the "is this a use strict directive?" logic in
parse_string_literal() we would apply it to *any* string literal, which
is incorrect and would lead to false positives - e.g.:

    "use strict" + 1
    `"use strict"`
    "\123"; ({"use strict": ...})

Relevant part from the spec which is now implemented properly:

[...] and where each ExpressionStatement in the sequence consists
entirely of a StringLiteral token [...]

I also got rid of UseStrictDirectiveState which is not needed anymore.

Fixes #3903.
2020-11-02 13:13:54 +01:00

60 lines
1.2 KiB
JavaScript

test("valid 'use strict; directive", () => {
expect(
(() => {
"use strict";
return isStrictMode();
})()
).toBeTrue();
expect(
(() => {
'use strict';
return isStrictMode();
})()
).toBeTrue();
});
test("invalid 'use strict; directive", () => {
expect(
(() => {
" use strict ";
return isStrictMode();
})()
).toBeFalse();
expect(
(() => {
`use strict`;
return isStrictMode();
})()
).toBeFalse();
expect(
(() => {
"use\
strict";
return isStrictMode();
})()
).toBeFalse();
expect(
(() => {
"use\ strict";
return isStrictMode();
})()
).toBeFalse();
expect(
(() => {
"use \163trict";
return isStrictMode();
})()
).toBeFalse();
expect(
(() => {
`"use strict"`;
return isStrictMode();
})()
).toBeFalse();
expect(
(() => {
"use strict" + 1;
return isStrictMode();
})()
).toBeFalse();
});