Skip to content

Instantly share code, notes, and snippets.

@zenparsing
Last active December 30, 2015 00:59
Show Gist options
  • Save zenparsing/7753590 to your computer and use it in GitHub Desktop.
Save zenparsing/7753590 to your computer and use it in GitHub Desktop.
Simplified algorithm for detection of strict-mode errors in ECMAScript 6.

A context object is associated with any AST node which may have "strictness". Each context has a strictness state: UNKOWN, STRICT, and NOT_STRICT. In order to account for arrow functions, parenthesized expressions are also assigned a context.

When a context C is pushed:

  • set C.parent to the current context object
  • set C.strictError to null
  • if C.parent.strict is STRICT
    • set C.strict to STRICT
  • else
    • set C.strict to UNKNOWN

When a strict mode error is encountered:

  • if C.strict is STRICT
    • throw a SyntaxError
  • else if C.strictError is null
    • set C.strictError to the error

When a context C is popped:

  • if C.strict is UNKNOWN
    • if C.parent.strict is UNKNOWN and C.parent.strictError is null
      • set C.parent.strictError = C.strictError
  • else if C.strict is STRICT and C.strictError is not null
    • throw a SyntaxError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment