Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wild-jokes-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'esrap': patch
---

fix: preserve type-level syntax that was being dropped when printing
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ test/sandbox/_output.js

expected.ts
expected.jsx
expected.tsx
12 changes: 12 additions & 0 deletions src/languages/ts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,11 @@ export default (options = {}) => {
TSTypeQuery(node, context) {
context.write('typeof ');
context.visit(node.exprName);

// instantiation expression (`typeof foo<string>`)
if (node.typeArguments) {
context.visit(node.typeArguments);
}
},

TSClassImplements(node, context) {
Expand Down Expand Up @@ -2169,6 +2174,11 @@ export default (options = {}) => {
},

TSMethodSignature(node, context) {
// accessor signatures (`get x(): string`, `set x(value: string)`)
if (node.kind === 'get' || node.kind === 'set') {
write_keyword(context, node, node.kind, ' ');
}

if (node.computed) context.write('[', token_before(node.key.loc?.start));
context.visit(node.key);
if (node.computed) context.write(']', token_at(node.key.loc?.end));
Expand Down Expand Up @@ -2204,6 +2214,8 @@ export default (options = {}) => {

TSNamedTupleMember(node, context) {
context.visit(node.label);
// optional element (`[a?: string]`)
if (node.optional) context.write('?');
context.write(': ');
context.visit(node.elementType);
},
Expand Down
5 changes: 5 additions & 0 deletions src/languages/tsx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export default (options) => ({

context.visit(node.name);

// explicit type arguments (`<Comp<string> ... />`)
if (node.typeArguments) {
context.visit(node.typeArguments);
}

for (const attribute of node.attributes) {
context.write(' ');
context.visit(attribute);
Expand Down
5 changes: 5 additions & 0 deletions test/samples/ts-type-level-syntax/expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Tuple = [required: string, optional?: number, ...rest: boolean[]];

interface Accessors { get value(): string; set value(next: string); method(): void }

type Instantiated = typeof identity<string>;
11 changes: 11 additions & 0 deletions test/samples/ts-type-level-syntax/expected.ts.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": 3,
"names": [],
"sources": [
"input.js"
],
"sourcesContent": [
"type Tuple = [required: string, optional?: number, ...rest: boolean[]];\n\ninterface Accessors {\n\tget value(): string;\n\tset value(next: string);\n\tmethod(): void;\n}\n\ntype Instantiated = typeof identity<string>;\n"
],
"mappings": "KAAK,KAAK,IAAI,QAAQ,EAAE,MAAM,EAAE,QAAQ,GAAG,MAAM,KAAK,IAAI,EAAE,OAAO;;UAEzD,SAAS,GAClB,GAAG,CAAC,KAAK,IAAI,MAAM,EACnB,GAAG,CAAC,KAAK,CAAC,IAAY,EAAN,MAAM,GACtB,MAAM,IAAI,IAAI;;KAGV,YAAY,UAAU,QAAQ,CAAC,MAAM"
}
9 changes: 9 additions & 0 deletions test/samples/ts-type-level-syntax/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Tuple = [required: string, optional?: number, ...rest: boolean[]];

interface Accessors {
get value(): string;
set value(next: string);
method(): void;
}

type Instantiated = typeof identity<string>;
3 changes: 3 additions & 0 deletions test/samples/tsx-element-type-arguments/expected.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const list = <List<string> items={items} />;
const nested = <Table<Row, Column>>{children}</Table>;
const plain = <div className="x" />;
11 changes: 11 additions & 0 deletions test/samples/tsx-element-type-arguments/expected.tsx.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": 3,
"names": [],
"sources": [
"input.js"
],
"sourcesContent": [
"const list = <List<string> items={items} />;\nconst nested = <Table<Row, Column>>{children}</Table>;\nconst plain = <div className=\"x\" />;\n"
],
"mappings": "AAAA,MAAM,AAAA,IAAI,IAAI,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK;AACvC,MAAM,AAAA,MAAM,IAAI,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,GAAG,KAAK;AACpD,MAAM,AAAA,KAAK,IAAI,GAAG,CAAC,SAAS,CAAC,GAAG"
}
3 changes: 3 additions & 0 deletions test/samples/tsx-element-type-arguments/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const list = <List<string> items={items} />;
const nested = <Table<Row, Column>>{children}</Table>;
const plain = <div className="x" />;
Loading