Skip to content

Commit 9e1bced

Browse files
committed
Add a strict option to turn on strict mode for scripts
FEATURE: The new `strict` option can be used to start script sources in strict mode. Issue #1439
1 parent 8a47812 commit 9e1bced

4 files changed

Lines changed: 13 additions & 1 deletion

File tree

acorn/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ required):
8686
will be valid, even if `ecmaVersion` is less than 6. If set to `"commonjs"`,
8787
it is the same as `"script"` except that the top-level scope behaves like a function.
8888

89+
- **strict**: When set to true, enable strict parsing mode even if
90+
`sourceType` is `"script"`.
91+
8992
- **onInsertedSemicolon**: If given a callback, that callback will be
9093
called whenever a missing semicolon is inserted by the parser. The
9194
callback will be given the character offset of the point where the

acorn/src/acorn.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,12 @@ export interface Options {
619619
*/
620620
sourceType?: "script" | "module" | "commonjs"
621621

622+
/**
623+
* When set to true, enable strict parsing mode even if `sourceType`
624+
* is `"script"`.
625+
*/
626+
strict?: boolean
627+
622628
/**
623629
* a callback that will be called when a semicolon is automatically inserted.
624630
* @param lastTokEnd the position of the comma as an offset

acorn/src/options.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export const defaultOptions = {
1616
// Can be either `"script"`, `"module"` or `"commonjs"`. This influences global
1717
// strict mode and parsing of `import` and `export` declarations.
1818
sourceType: "script",
19+
// When set to true, enable strict parsing mode even if `sourceType`
20+
// is `"script"`.
21+
strict: false,
1922
// `onInsertedSemicolon` can be a callback that will be called when
2023
// a semicolon is automatically inserted. It will be passed the
2124
// position of the inserted semicolon as an offset, and if

acorn/src/state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class Parser {
6464

6565
// Figure out if it's a module code.
6666
this.inModule = options.sourceType === "module"
67-
this.strict = this.inModule || this.strictDirective(this.pos)
67+
this.strict = this.inModule || options.strict == true || this.strictDirective(this.pos)
6868

6969
// Used to signify the start of a potential arrow function
7070
this.potentialArrowAt = -1

0 commit comments

Comments
 (0)