Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 0673ea4

Browse files
committed
Fix: Use TSNullKeyword for null type instead of Literal
1 parent 9037dc5 commit 0673ea4

File tree

4 files changed

+217
-1
lines changed

4 files changed

+217
-1
lines changed

lib/ast-node-types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ module.exports = {
118118
TSModuleDeclaration: "TSModuleDeclaration",
119119
TSNamespaceFunctionDeclaration: "TSNamespaceFunctionDeclaration",
120120
TSNonNullExpression: "TSNonNullExpression",
121+
TSNullKeyword: "TSNullKeyword",
121122
TSNumberKeyword: "TSNumberKeyword",
122123
TSParameterProperty: "TSParameterProperty",
123124
TSPropertySignature: "TSPropertySignature",

lib/convert.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,22 @@ module.exports = function convert(config) {
8585
*/
8686
function convertTypeAnnotation(child) {
8787
const annotation = convertChild(child);
88-
return {
88+
const converted = {
8989
type: AST_NODE_TYPES.TypeAnnotation,
9090
loc: annotation.loc,
9191
range: annotation.range,
9292
typeAnnotation: annotation
9393
};
94+
/**
95+
* A typeAnnotation of "null" will be interpreted as a Literal within convertChild(),
96+
* so we simply post-process it to work around that
97+
*/
98+
if (child.kind === SyntaxKind.NullKeyword) {
99+
converted.typeAnnotation.type = AST_NODE_TYPES.TSNullKeyword;
100+
delete converted.typeAnnotation.value;
101+
delete converted.typeAnnotation.raw;
102+
}
103+
return converted;
94104
}
95105

96106
/**
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let x: null;
2+
let y: undefined;

tests/lib/__snapshots__/typescript.js.snap

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15659,6 +15659,209 @@ Object {
1565915659
}
1566015660
`;
1566115661

15662+
exports[`typescript fixtures/basics/null-and-undefined-type-annotations.src 1`] = `
15663+
Object {
15664+
"body": Array [
15665+
Object {
15666+
"declarations": Array [
15667+
Object {
15668+
"id": Object {
15669+
"loc": Object {
15670+
"end": Object {
15671+
"column": 5,
15672+
"line": 1,
15673+
},
15674+
"start": Object {
15675+
"column": 4,
15676+
"line": 1,
15677+
},
15678+
},
15679+
"name": "x",
15680+
"range": Array [
15681+
4,
15682+
5,
15683+
],
15684+
"type": "Identifier",
15685+
"typeAnnotation": Object {
15686+
"loc": Object {
15687+
"end": Object {
15688+
"column": 11,
15689+
"line": 1,
15690+
},
15691+
"start": Object {
15692+
"column": 7,
15693+
"line": 1,
15694+
},
15695+
},
15696+
"range": Array [
15697+
7,
15698+
11,
15699+
],
15700+
"type": "TypeAnnotation",
15701+
"typeAnnotation": Object {
15702+
"loc": Object {
15703+
"end": Object {
15704+
"column": 11,
15705+
"line": 1,
15706+
},
15707+
"start": Object {
15708+
"column": 7,
15709+
"line": 1,
15710+
},
15711+
},
15712+
"range": Array [
15713+
7,
15714+
11,
15715+
],
15716+
"type": "TSNullKeyword",
15717+
},
15718+
},
15719+
},
15720+
"init": null,
15721+
"loc": Object {
15722+
"end": Object {
15723+
"column": 11,
15724+
"line": 1,
15725+
},
15726+
"start": Object {
15727+
"column": 4,
15728+
"line": 1,
15729+
},
15730+
},
15731+
"range": Array [
15732+
4,
15733+
11,
15734+
],
15735+
"type": "VariableDeclarator",
15736+
},
15737+
],
15738+
"kind": "let",
15739+
"loc": Object {
15740+
"end": Object {
15741+
"column": 12,
15742+
"line": 1,
15743+
},
15744+
"start": Object {
15745+
"column": 0,
15746+
"line": 1,
15747+
},
15748+
},
15749+
"range": Array [
15750+
0,
15751+
12,
15752+
],
15753+
"type": "VariableDeclaration",
15754+
},
15755+
Object {
15756+
"declarations": Array [
15757+
Object {
15758+
"id": Object {
15759+
"loc": Object {
15760+
"end": Object {
15761+
"column": 5,
15762+
"line": 2,
15763+
},
15764+
"start": Object {
15765+
"column": 4,
15766+
"line": 2,
15767+
},
15768+
},
15769+
"name": "y",
15770+
"range": Array [
15771+
17,
15772+
18,
15773+
],
15774+
"type": "Identifier",
15775+
"typeAnnotation": Object {
15776+
"loc": Object {
15777+
"end": Object {
15778+
"column": 16,
15779+
"line": 2,
15780+
},
15781+
"start": Object {
15782+
"column": 7,
15783+
"line": 2,
15784+
},
15785+
},
15786+
"range": Array [
15787+
20,
15788+
29,
15789+
],
15790+
"type": "TypeAnnotation",
15791+
"typeAnnotation": Object {
15792+
"loc": Object {
15793+
"end": Object {
15794+
"column": 16,
15795+
"line": 2,
15796+
},
15797+
"start": Object {
15798+
"column": 7,
15799+
"line": 2,
15800+
},
15801+
},
15802+
"range": Array [
15803+
20,
15804+
29,
15805+
],
15806+
"type": "TSUndefinedKeyword",
15807+
},
15808+
},
15809+
},
15810+
"init": null,
15811+
"loc": Object {
15812+
"end": Object {
15813+
"column": 16,
15814+
"line": 2,
15815+
},
15816+
"start": Object {
15817+
"column": 4,
15818+
"line": 2,
15819+
},
15820+
},
15821+
"range": Array [
15822+
17,
15823+
29,
15824+
],
15825+
"type": "VariableDeclarator",
15826+
},
15827+
],
15828+
"kind": "let",
15829+
"loc": Object {
15830+
"end": Object {
15831+
"column": 17,
15832+
"line": 2,
15833+
},
15834+
"start": Object {
15835+
"column": 0,
15836+
"line": 2,
15837+
},
15838+
},
15839+
"range": Array [
15840+
13,
15841+
30,
15842+
],
15843+
"type": "VariableDeclaration",
15844+
},
15845+
],
15846+
"loc": Object {
15847+
"end": Object {
15848+
"column": 17,
15849+
"line": 2,
15850+
},
15851+
"start": Object {
15852+
"column": 0,
15853+
"line": 1,
15854+
},
15855+
},
15856+
"range": Array [
15857+
0,
15858+
30,
15859+
],
15860+
"sourceType": "script",
15861+
"type": "Program",
15862+
}
15863+
`;
15864+
1566215865
exports[`typescript fixtures/basics/type-alias-declaration.src 1`] = `
1566315866
Object {
1566415867
"body": Array [

0 commit comments

Comments
 (0)