Skip to content

Commit 528edb9

Browse files
committed
handle declare keyword
1 parent 2558e72 commit 528edb9

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

src/languages/ts.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ export default (options = {}) => {
497497
* @param {Context} context
498498
*/
499499
'ClassDeclaration|ClassExpression': (node, context) => {
500+
if (node.declare) {
501+
context.write('declare ');
502+
}
503+
500504
context.write('class ');
501505

502506
if (node.id) {
@@ -1356,6 +1360,39 @@ export default (options = {}) => {
13561360
}
13571361
},
13581362

1363+
TSDeclareFunction(node, context) {
1364+
context.write('declare ');
1365+
1366+
if (node.async) {
1367+
context.write('async ');
1368+
}
1369+
1370+
context.write('function');
1371+
1372+
if (node.generator) {
1373+
context.write('*');
1374+
}
1375+
1376+
if (node.id) {
1377+
context.write(' ');
1378+
context.visit(node.id);
1379+
}
1380+
1381+
if (node.typeParameters) {
1382+
context.visit(node.typeParameters);
1383+
}
1384+
1385+
context.write('(');
1386+
sequence(context, node.params, node.returnType?.loc?.start ?? node.loc?.end ?? null, false);
1387+
context.write(')');
1388+
1389+
if (node.returnType) {
1390+
context.visit(node.returnType);
1391+
}
1392+
1393+
context.write(';');
1394+
},
1395+
13591396
TSNumberKeyword(node, context) {
13601397
context.write('number', node);
13611398
},
@@ -1758,6 +1795,10 @@ function handle_var_declaration(node, context) {
17581795

17591796
context.append(child_context);
17601797

1798+
if (node.declare) {
1799+
child_context.write('declare ');
1800+
}
1801+
17611802
child_context.write(`${node.kind} `);
17621803
child_context.append(open);
17631804

test/samples/ts-declare/expected.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare function a(): void;
2+
3+
declare const b: number;
4+
5+
declare class c {}
6+
7+
export {};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": 3,
3+
"names": [],
4+
"sources": [
5+
"input.js"
6+
],
7+
"sourcesContent": [
8+
"declare function a(): void;\ndeclare const b: number;\ndeclare class c {}\n\nexport {};\n"
9+
],
10+
"mappings": "iBAAiB,CAAC,IAAI,IAAI;;cACZ,CAAS,EAAN,MAAM;;cACT,CAAC,CAAC,CAAC,AAAA,CAAC;;"
11+
}

test/samples/ts-declare/input.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare function a(): void;
2+
declare const b: number;
3+
declare class c {}
4+
5+
export {};

0 commit comments

Comments
 (0)