Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast: directive-prologue #8219

Open
Boshen opened this issue Jan 2, 2025 · 1 comment
Open

ast: directive-prologue #8219

Boshen opened this issue Jan 2, 2025 · 1 comment
Assignees
Labels
C-bug Category - Bug

Comments

@Boshen
Copy link
Member

Boshen commented Jan 2, 2025

https://tc39.es/ecma262/#directive-prologue

A Directive Prologue is the longest sequence of ExpressionStatements occurring as the initial StatementListItems or ModuleItems of a FunctionBody, a ScriptBody, or a ModuleBody and where each ExpressionStatement in the sequence consists entirely of a StringLiteral token followed by a semicolon. The semicolon may appear explicitly or may be inserted by automatic semicolon insertion (12.10). A Directive Prologue may be an empty sequence.

Oxc AST does not conformance to this definition.

Oxc produces

directives: [ Directive { .. } ]
body: [  ]

it should be at least

directives: [ Directive { .. } ]
body: [ ExpressionStatement { .. } ]
@Boshen Boshen added the C-bug Category - Bug label Jan 2, 2025
@Boshen Boshen self-assigned this Jan 2, 2025
@overlookmotel
Copy link
Contributor

function f() {
  'use strict';
}

Are you saying that the AST for this function's body should look like this?

FunctionBody {
  directives: [
    Directive { StringLiteral("use strict") },
  ],
  statements: [
    ExpressionStatement {
      expression: Expression(StringLiteral("use strict")),
    },
  ],
}

If so, in my opinion, I don't think we need to do that in order to remain compliant with the spec.

My reading of the passage you linked to is that:

  • It describes the Directive Prologue as a thing which a parser needs to recognise.
  • It describes what code comprises the Directive Prologue.
  • It states that the engine needs to identify a Use Strict Directive within the Prologue, and process code within the function as strict mode code.

But what it doesn't say is how the Directive Prologue needs to be stored internally in the engine. All it says is:

A Use Strict Directive is an ExpressionStatement in a Directive Prologue

So I think we are compliant. FunctionBody::directives is the Directive Prologue, and we store directives in it. The spec doesn't say that directives also need to be stored elsewhere.

OK, we're storing directives as Directive rather than ExpressionStatement. But there seems little point in using ExpressionStatement(Expression(StringLiteral)) since the expression is statically known to be a StringLiteral.

Sorry if I've completely misinterpreted what you're saying and have got confused!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category - Bug
Projects
None yet
Development

No branches or pull requests

2 participants