Part of #9705.
TS-ESTree presents a `TSTypeName` which is an identifier `this` as a `ThisExpression`.
e.g. `type T = typeof this;` or `type T = typeof this.foo;`
Refactor `wrap.cjs` and `wrap.mjs` in `napi/parser` package.
* Update outdated comments.
* Move JSON transform function to top level to avoid creating a new closure each time `jsonParseAst` is called.
* Make the 2 files as similar as possible so it's easier to keep them both in sync.
Part of #9705.
Our AST represents `module X.Y.Z {}` as 3 x nested `TSModuleDeclaration`s. TS-ESTree represents it as a single `TSModuleDeclaration`, with a nested tree of `TSQualifiedName`s as `id`.
Additionally, TS-ESTree skips the `body` field if the module declaration has no body e.g. `module "foo";`.
Align our TS-ESTree AST with these oddities.
Follow-on after #10534. Fix the raw deser implementation for
`FormalParameter` to handle `TSParameterProperty`.
This involves moving some of the logic into a converter for
`FormalParameter`, instead of doing it all in `FormalParameters`
converter.
It wasn't possible for `extends` field to be `Some` containing an empty `Vec` because this is not legal code:
```ts
interface Foo extends {}
```
So use `Vec` for `extends` field, instead of `Option<Vec>`. This prevents creating an invalid AST, and simplifies code in various places.
#10438 aligned the spans of `@dec export class C {}` and `@dec export default class {}` with TS-ESLint.
```ts
@dec export class C {}
^^^^^^^^^^^^^^^^^ ExportNamedDeclaration
^^^^^^^^^^ Class
^^^^ Decorator
@dec export default class {}
^^^^^^^^^^^^^^^^^^^^^^^ ExportDefaultDeclaration
^^^^^^^^ Class
^^^^ Decorator
```
However, this causes a problem where one of these is the first statement in the file. In TS-ESTree, `Program` start is the start of the first token (excluding whitespace and comments). So we need to set `Program` start to the start of the decorator in these cases.
closes#10419
This change reduces the size of these AST nodes, saving a bit of memory.
`ArrayExpression`: 56 -> 40
`ObjectExpression`: 56 -> 40
`ArrayAssignmentTarget`: 80 -> 64
Also removes the annoying `None` argument from these AST node builders.
Add `optional` field to `AssignmentTargetPropertyIdentifier` and `AssignmentTargetPropertyProperty` in TS-ESTree AST.
This doesn't pass any more tests because those test cases all also include mismatches on `ArrayPattern` or `ObjectPattern`, but it does reduce the size of mismatch diffs.
Part of #9705
In this PR, I've tried to align `TSMappedType` with TS-ESLint.
>
https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/ast-spec/src/type/TSMappedType/spec.ts
```js
// BEFORE
{
type: "TSMappedType",
typeParameter: {
name: Identifier,
constraint: TypeNode,
},
// ...
}
// AFTER
{
type: "TSMappedType",
key: Identifier,
constraint: TypeNode,
// ...
}
```
And also fixed serializer for `optional` and `readonly` types.
They are defined as `boolean | '+' | '-' | undefined`, but we use `true
| '+' | '-' | null`.
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
Part of #9705
Just renamed them, no difference appeared in the coverage ATM.
From my understanding, there is a problem with the parser logic for now.
I'll try to fix it later.
Fixes#10087
---
Follow up for #10017 .
This PR eliminates the part where nodes were manually generated for
JS-land AST.
NOTE: This node was originally present, but was removed at #2509 and is
now being reintroduced to get the correct `Span` position for #9705 .
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Fix `JSXOpeningFragment` node estree TS serialization by skipping
`selfClosing` and `attributes` fields when AST is TS.
Part of our broader work to align our AST's ESTree output with that of
TS-ESLint's. Relates to #9705
---------
Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
Part of #9705.
TS-ESLint differs from Acorn in the `span` of `TemplateElement`.
TS-ESLint includes the preceding `` ` `` or `}` and following `${` or `` ` `` in the span.
```js
const template = `abc${x}def${x}ghi`;
// Acorn: ^^^ ^^^ ^^^
// TS-ESLint: ^^^^^^ ^^^^^^ ^^^^^
```
Make the span follow TS-ESLint in the TS AST.
Remove `self_closing` field from `JSXOpeningElement`. Whether the tag is self-closing can be determined by whether the `JSXElement` has a `closing_element` or not.
This is advantageous for 2 reasons:
1. Reduce size of `JSXOpeningElement` by 8 bytes.
2. Make it impossible for `self_closing` and `closing_element` fields to get out of sync. Only one source of truth.
close: #10139
The `ModuleKind::Unambiguous` can only occur in the parser stage, after being parsed, the `ModuleKind` certainly changes to `ModuleKind::Script` or `ModuleKind::Module`; otherwise is a bug in the parser.
Skips the `isTypeOf` field for the Estree serialiser on the
`TSImportType` node.
Part of our broader work to align our AST's ESTree output with that of
TS-ESLint's. Relates to #9705
Adds the `optional` field to the Estree serialiser for the
`BindingProperty` node.
Part of our broader work to align our AST's ESTree output with that of
TS-ESLint's. Relates to #9705
---------
Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>
#10134 fixed the start span of `Program` in TS AST to align with TS-ESLint.
Change the method this is achieved by - use a custom converter instead of mutating the AST in `to_estree_ts_json` and `to_pretty_estree_ts_json`. This method is more convoluted, but I think it's preferable if serialization does not have side effect of mutating the AST.
Encode lone surrogates in `cooked` property of `TemplateElementValue` using same encoding scheme as for `StringLiteral`s.
In fact, they were already being encoded like this after #10041, but add a `lone_surrogates` flag to `TemplateLiteral` to decode them correctly in ESTree AST.
`oxc_codegen` ignores `cooked` and just prints `raw`, so needs no alteration.
Fix 2 edge cases when parsing lone surrogates in `StringLiteral`s:
* Lone surrogate followed by `\u{...}` escape e.g. `"\uD800\u{41}"`.
* Escaped lossy replacement character after lone surrogate e.g. `"\uD800 \u{FFFD}"`.
Create new `TsValue` struct and use in new `exportKind` field on the
`ExportDefaultDeclaration` node.
Align our AST's ESTree output with that of TS-ESLint's for the
`ExportDefaultDeclaration` node. Relates to #9705
Align our AST's ESTree output with that of TS-ESLint's for
the`AccessorProperty` node. Relates to #9705
Add `optional`, `override`, `readonly`, and `declare` fields to
`AccessorProperty` when serializing to TS estree.
---------
Co-authored-by: overlookmotel <theoverlookmotel@gmail.com>