oxc-project / oxc-project/backlog

AST: Correct ESTree spans for `ImportMeta` and `NewTarget`

Open
#227 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
7
Forks
0
PR merge metrics
No merged PRs in 30d

Description

Parent: #210

oxc-project/oxc#24557 replaced MetaProperty with 2 new types ImportMeta and NewTarget.

oxc-project/oxc#24775 followed up with a fix to spans, aiming to handle programmatically-created nodes with empty spans (0, 0).

The idea is that it's redundant to store individual spans for the import and meta keywords in import.meta, because they can be derived from the span of the whole "phrase" import.meta.

@camc314 and I discussed an unintended downside of this change which is it doesn't handle well when some other code is transformed to import.meta e.g.:

  • x.y() is transformed to import.meta.
  • The ImportMeta node has the original span of the x.y() expression.
  • The spans for import and meta keywords in ESTree AST are nonsense because calculating the keyword spans from the span of x.y() makes no sense.
  • This could screw up source maps if a JS-side library producing a source map looks at the spans on import and meta keywords.

This is an edge case, but:

  1. We're trying to stabilize the AST and move towards locking it (1.0), so we should IMO try to get it exactly right.
  2. This kind of problem will likely come up again in more consequential ways in other parts of AST, so we may do well to figure it out now.

One way to handle this without bloating ImportMeta with 2 x extra Spans:

pub struct ImportMeta {
    pub node_id: Cell<NodeId>,
    pub span: Span,
    pub is_raw: bool,
}
  • Parser sets is_raw: true -> start & end set on import and meta keywords in ESTree AST.
  • Transformer sets is_raw: false (when creating a new ImportMeta) -> keywords receive no span (0, 0) in ESTree AST.

We don't give people option to set individual spans for the keywords - but that's likely not required because import.meta is only meaningful as a complete "unit". But we do get correct spans in ESTree AST, and it only costs 1 bool (free, since ImportMeta has 4 padding bytes spare anyway).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the ImportMeta and NewTarget definitions, then find the parser and transformer construction sites mentioned in the issue. Trace how these nodes become ESTree AST spans. Done means parser-created import.meta keywords retain their spans, while transformed ImportMeta keywords receive (0, 0), without adding individual keyword spans.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.