tree-sitter-typescript icon indicating copy to clipboard operation
tree-sitter-typescript copied to clipboard

feat: allow global declarations in modules

Open crawford opened this issue 1 year ago • 2 comments

TypeScript allows declarations in the global namespace from within ambient module declarations. This form is used in several of the declaration files for Node.js (e.g. buffer.d.ts, console.d.ts).

crawford avatar Jun 24 '24 21:06 crawford

Hey, thanks for the PR! After taking a look at the TS handbook and AST output, I think a better name might be global_module, what do you think? Also, throw it under internal_module to group it a little more nicely, and rebase if you can, I just pushed some changes

amaanq avatar Jul 06 '24 06:07 amaanq

@amaanq I agree, global_module makes more sense. And I'll rebase your changes. I want to make sure I understand your comment about internal_module though. How do you expect the following snippet to parse?

declare module "foo" {
  global {
  }
}

Like this?

...
      (statement_block
        (expression_statement
          (internal_module
            (identifier)
            (statement_block
...

Or like this?

...
      (statement_block
        (expression_statement
          (global_module
            (statement_block
...

I experimented with a few different options but I noticed ambiguity with the existing support for global namespace declarations. The following isn't understood to be a global module currently:

declare global {
}

It just parses as follows:

(program
  (ambient_declaration
    (statement_block)))

Is it going to cause downstream trouble if we change this to parse as something like this?

(program
  (ambient_declaration
    (global_module
      (statement_block))))

Ultimately, what I'm trying to do is identify objects defined in the global namespace so I can track them separately.

crawford avatar Jul 08 '24 18:07 crawford