Skip to content

Commit cb3f42d

Browse files
committed
TypeScript: Move certain TS expressions out of ECMAScript module
1 parent f8c61db commit cb3f42d

File tree

2 files changed

+104
-96
lines changed

2 files changed

+104
-96
lines changed

src/modules/ecmascript.js

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -917,101 +917,6 @@ export default {
917917
}
918918
},
919919

920-
TSAsExpression(node, state) {
921-
if (node.expression) {
922-
const needs_parens =
923-
EXPRESSIONS_PRECEDENCE[node.expression.type] < EXPRESSIONS_PRECEDENCE.TSAsExpression;
924-
925-
if (needs_parens) {
926-
state.commands.push('(');
927-
handle(node.expression, state);
928-
state.commands.push(')');
929-
} else {
930-
handle(node.expression, state);
931-
}
932-
}
933-
state.commands.push(' as ');
934-
handle(node.typeAnnotation, state);
935-
},
936-
937-
TSEnumDeclaration(node, state) {
938-
state.commands.push('enum ');
939-
handle(node.id, state);
940-
state.commands.push(' {', indent, newline);
941-
sequence(node.members, state, false, handle);
942-
state.commands.push(dedent, newline, '}', newline);
943-
},
944-
945-
TSModuleBlock(node, state) {
946-
state.commands.push(' {', indent, newline);
947-
handle_body(node.body, state);
948-
state.commands.push(dedent, newline, '}');
949-
},
950-
951-
TSModuleDeclaration(node, state) {
952-
if (node.declare) state.commands.push('declare ');
953-
else state.commands.push('namespace ');
954-
955-
handle(node.id, state);
956-
957-
if (!node.body) return;
958-
handle(node.body, state);
959-
},
960-
961-
TSNonNullExpression(node, state) {
962-
handle(node.expression, state);
963-
state.commands.push('!');
964-
},
965-
966-
TSInterfaceBody(node, state) {
967-
sequence(node.body, state, true, handle, ';');
968-
},
969-
970-
TSInterfaceDeclaration(node, state) {
971-
state.commands.push('interface ');
972-
handle(node.id, state);
973-
if (node.typeParameters) handle(node.typeParameters, state);
974-
if (node.extends) {
975-
state.commands.push(' extends ');
976-
sequence(node.extends, state, false, handle);
977-
}
978-
state.commands.push(' {');
979-
handle(node.body, state);
980-
state.commands.push('}');
981-
},
982-
983-
TSSatisfiesExpression(node, state) {
984-
if (node.expression) {
985-
const needs_parens =
986-
EXPRESSIONS_PRECEDENCE[node.expression.type] < EXPRESSIONS_PRECEDENCE.TSSatisfiesExpression;
987-
988-
if (needs_parens) {
989-
state.commands.push('(');
990-
handle(node.expression, state);
991-
state.commands.push(')');
992-
} else {
993-
handle(node.expression, state);
994-
}
995-
}
996-
state.commands.push(' satisfies ');
997-
handle(node.typeAnnotation, state);
998-
},
999-
1000-
TSTypeAliasDeclaration(node, state) {
1001-
state.commands.push('type ');
1002-
handle(node.id, state);
1003-
if (node.typeParameters) handle(node.typeParameters, state);
1004-
state.commands.push(' = ');
1005-
handle(node.typeAnnotation, state);
1006-
state.commands.push(';');
1007-
},
1008-
1009-
TSQualifiedName(node, state) {
1010-
handle(node.left, state);
1011-
state.commands.push('.');
1012-
handle(node.right, state);
1013-
},
1014-
1015920
UnaryExpression(node, state) {
1016921
state.commands.push(node.operator);
1017922

src/modules/typescript.js

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { sequence, handle } from '../handlers';
1+
import {
2+
sequence,
3+
handle,
4+
EXPRESSIONS_PRECEDENCE,
5+
dedent,
6+
newline,
7+
indent,
8+
handle_body
9+
} from '../handlers';
210
/** @import { Handlers } from '../types' */
311

412
/**
@@ -177,5 +185,100 @@ export default {
177185
state.commands.push('.');
178186
handle(node.qualifier, state);
179187
}
188+
},
189+
190+
TSAsExpression(node, state) {
191+
if (node.expression) {
192+
const needs_parens =
193+
EXPRESSIONS_PRECEDENCE[node.expression.type] < EXPRESSIONS_PRECEDENCE.TSAsExpression;
194+
195+
if (needs_parens) {
196+
state.commands.push('(');
197+
handle(node.expression, state);
198+
state.commands.push(')');
199+
} else {
200+
handle(node.expression, state);
201+
}
202+
}
203+
state.commands.push(' as ');
204+
handle(node.typeAnnotation, state);
205+
},
206+
207+
TSEnumDeclaration(node, state) {
208+
state.commands.push('enum ');
209+
handle(node.id, state);
210+
state.commands.push(' {', indent, newline);
211+
sequence(node.members, state, false, handle);
212+
state.commands.push(dedent, newline, '}', newline);
213+
},
214+
215+
TSModuleBlock(node, state) {
216+
state.commands.push(' {', indent, newline);
217+
handle_body(node.body, state);
218+
state.commands.push(dedent, newline, '}');
219+
},
220+
221+
TSModuleDeclaration(node, state) {
222+
if (node.declare) state.commands.push('declare ');
223+
else state.commands.push('namespace ');
224+
225+
handle(node.id, state);
226+
227+
if (!node.body) return;
228+
handle(node.body, state);
229+
},
230+
231+
TSNonNullExpression(node, state) {
232+
handle(node.expression, state);
233+
state.commands.push('!');
234+
},
235+
236+
TSInterfaceBody(node, state) {
237+
sequence(node.body, state, true, handle, ';');
238+
},
239+
240+
TSInterfaceDeclaration(node, state) {
241+
state.commands.push('interface ');
242+
handle(node.id, state);
243+
if (node.typeParameters) handle(node.typeParameters, state);
244+
if (node.extends) {
245+
state.commands.push(' extends ');
246+
sequence(node.extends, state, false, handle);
247+
}
248+
state.commands.push(' {');
249+
handle(node.body, state);
250+
state.commands.push('}');
251+
},
252+
253+
TSSatisfiesExpression(node, state) {
254+
if (node.expression) {
255+
const needs_parens =
256+
EXPRESSIONS_PRECEDENCE[node.expression.type] < EXPRESSIONS_PRECEDENCE.TSSatisfiesExpression;
257+
258+
if (needs_parens) {
259+
state.commands.push('(');
260+
handle(node.expression, state);
261+
state.commands.push(')');
262+
} else {
263+
handle(node.expression, state);
264+
}
265+
}
266+
state.commands.push(' satisfies ');
267+
handle(node.typeAnnotation, state);
268+
},
269+
270+
TSTypeAliasDeclaration(node, state) {
271+
state.commands.push('type ');
272+
handle(node.id, state);
273+
if (node.typeParameters) handle(node.typeParameters, state);
274+
state.commands.push(' = ');
275+
handle(node.typeAnnotation, state);
276+
state.commands.push(';');
277+
},
278+
279+
TSQualifiedName(node, state) {
280+
handle(node.left, state);
281+
state.commands.push('.');
282+
handle(node.right, state);
180283
}
181284
};

0 commit comments

Comments
 (0)