Skip to content

Conversation

@hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Mar 17, 2025

spec: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/ast-spec/src/expression/Identifier/spec.ts

I'm not sure whether this can be fixed on struct side BindingIdentifier instead of field side Class.id. BindingIdentifier is used inside flatten BindingPatternKind enum, so changing it might be complicated.

There are probably many instances like this, so I'm just trying to see the pattern first.

Copy link
Contributor Author

hi-ogawa commented Mar 17, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 17, 2025

CodSpeed Instrumentation Performance Report

Merging #9822 will not alter performance

Comparing 03-17-fix_ast_estree_fix_class.id_ (d69cc34) with main (d3d7d98)

Summary

✅ 33 untouched benchmarks

@github-actions github-actions bot added A-ast Area - AST A-ast-tools Area - AST tools C-bug Category - Bug labels Mar 17, 2025
@hi-ogawa hi-ogawa force-pushed the 03-17-fix_ast_estree_fix_class.id_ branch 4 times, most recently from fb88238 to 4628d1e Compare March 18, 2025 00:57
@hi-ogawa hi-ogawa force-pushed the 03-17-fix_ast_extree_fix_class.implements_ branch from c402430 to 9da220e Compare March 18, 2025 00:57
@graphite-app graphite-app bot force-pushed the 03-17-fix_ast_extree_fix_class.implements_ branch 2 times, most recently from f50cef0 to 223e16f Compare March 18, 2025 01:17
@graphite-app graphite-app bot force-pushed the 03-17-fix_ast_estree_fix_class.id_ branch from 4628d1e to ce97302 Compare March 18, 2025 01:17
@graphite-app graphite-app bot changed the base branch from 03-17-fix_ast_extree_fix_class.implements_ to graphite-base/9822 March 18, 2025 01:47
@graphite-app graphite-app bot force-pushed the graphite-base/9822 branch from 223e16f to cd18358 Compare March 18, 2025 01:53
@graphite-app graphite-app bot force-pushed the 03-17-fix_ast_estree_fix_class.id_ branch from ce97302 to b91d6c8 Compare March 18, 2025 01:54
@graphite-app graphite-app bot changed the base branch from graphite-base/9822 to main March 18, 2025 01:54
@graphite-app graphite-app bot force-pushed the 03-17-fix_ast_estree_fix_class.id_ branch from b91d6c8 to d252425 Compare March 18, 2025 01:54
@overlookmotel
Copy link
Member

overlookmotel commented Mar 18, 2025

Personally I would like to rewrite BindingPattern entirely to get rid of the flattened enum BindingPatternKind. That's the only place we have to flatten an enum in the entire AST, and this oddity complicates our code in various places. Also there's no Span for the entire node including the type annotation.

Something like:

enum BindingPattern {
    BindingIdentifier(Box<'a, BindingIdentifierWithType<'a>>),
    ObjectPattern(Box<'a, ObjectPatternWithType<'a>>),
    ArrayPattern(Box<'a, ArrayPatternWithType<'a>>),
    AssignmentPattern(Box<'a, AssignmentPatternWithType<'a>>),
}

struct BindingIdentifierWithType<'a> {
    pub span: Span,
    pub ident: BindingIdentifier<'a>,
    pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
    pub optional: bool,
}

struct ObjectPatternWithType<'a> {
    pub span: Span,
    // `ObjectPattern` is a separate type because it also has a `Span`
    pub pattern: ObjectPattern<'a>,
    pub type_annotation: Option<Box<'a, TSTypeAnnotation<'a>>>,
    pub optional: bool,
}

// Same for `ArrayPatternWithType` and `AssignmentPatternWithType`

This has the downside of requiring more AST types. But the advantage is that we could make illegal syntax impossible to represent in the AST. e.g. the type annotation is not legal here:

[x: Foo] = array;

TS playground

But... problem is that BindingPattern is so commonly used, it'd require tons of changes (including downstream in Rolldown). So we had decided to leave it.

Anyway... TLDR... if BindingPattern has to change to enable TS-ESLint alignment, then doing that would not be out of the question, in my opinion.

@hi-ogawa hi-ogawa force-pushed the 03-17-fix_ast_estree_fix_class.id_ branch from d252425 to c79728f Compare March 18, 2025 08:57
@hi-ogawa hi-ogawa force-pushed the 03-17-fix_ast_estree_fix_class.id_ branch from c79728f to 7940c54 Compare March 19, 2025 05:46
graphite-app bot pushed a commit that referenced this pull request Mar 19, 2025
Binding side is tricky #9822, so I went with chipping away these two first.
@hi-ogawa hi-ogawa force-pushed the 03-17-fix_ast_estree_fix_class.id_ branch from 7940c54 to d1802c8 Compare March 20, 2025 04:41
@hi-ogawa hi-ogawa changed the title fix(ast/estree): fix Class.id fix(ast/estree): fix BindingIdentifier Mar 20, 2025
@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Mar 20, 2025

It looks like BindingIdentifier can also do same as IdentifierName and IdentifierReference. The catch is that, when BindingIdentifier is inside BindingPattern, raw json string has duplicate keys for typeAnnotation and optional, but JSON.parse picks up the last (which is the correct value from BindingPattern), so parse result in js world looks correct.

Here is an example:

let x: string = "foo"
$ node napi/parser/example.mjs test.ts
          "id": {
            "type": "Identifier",
            "start": 45,
            "end": 54,
            "name": "x",
            "decorators": [],
            "optional": false,  // 👈
            "typeAnnotation": null,  // 👈
            "typeAnnotation": {  // 👈
              "type": "TSTypeAnnotation",
              "start": 46,
              "end": 54,
              "typeAnnotation": {
                "type": "TSStringKeyword",
                "start": 48,
                "end": 54
              }
            },
            "optional": false  // 👈
          },

I'm not sure if we should concern with this right now or proceed with this until we see issues with this approach.

Copy link
Member

@overlookmotel overlookmotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplicate fields in JSON are not ideal, and we'll need to fix at some point. But let's go with it for now, and fix it later once we have achieved alignment of the whole AST.

If it turns out we have to change the shape of BindingPattern anyway, then that'll make it easier to fix.

@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Mar 21, 2025
Copy link
Member

overlookmotel commented Mar 21, 2025

Merge activity

  • Mar 21, 1:44 AM EDT: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Mar 21, 1:44 AM EDT: A user added this pull request to the Graphite merge queue.
  • Mar 21, 1:54 AM EDT: A user merged this pull request with the Graphite merge queue.

spec: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/ast-spec/src/expression/Identifier/spec.ts

I'm not sure whether this can be fixed on struct side `BindingIdentifier` instead of field side `Class.id`. `BindingIdentifier` is used inside flatten `BindingPatternKind` enum, so changing it might be complicated.

There are probably many instances like this, so I'm just trying to see the pattern first.
@graphite-app graphite-app bot force-pushed the 03-17-fix_ast_estree_fix_class.id_ branch from 00642cd to d69cc34 Compare March 21, 2025 05:47
@graphite-app graphite-app bot merged commit d69cc34 into main Mar 21, 2025
24 checks passed
@graphite-app graphite-app bot deleted the 03-17-fix_ast_estree_fix_class.id_ branch March 21, 2025 05:54
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Mar 21, 2025
export interface BindingIdentifier extends Span {
type: 'Identifier';
name: string;
decorators?: [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this types be generated as non optional?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast Area - AST A-ast-tools Area - AST tools C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants