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

Commit b9f01bc

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

File tree

5 files changed

+231
-7
lines changed

5 files changed

+231
-7
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: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,13 +1574,20 @@ module.exports = function convert(config) {
15741574
});
15751575
break;
15761576

1577-
case SyntaxKind.NullKeyword:
1578-
Object.assign(result, {
1579-
type: AST_NODE_TYPES.Literal,
1580-
value: null,
1581-
raw: "null"
1582-
});
1577+
case SyntaxKind.NullKeyword: {
1578+
if (nodeUtils.isWithinTypeAnnotation(node)) {
1579+
Object.assign(result, {
1580+
type: AST_NODE_TYPES.TSNullKeyword
1581+
});
1582+
} else {
1583+
Object.assign(result, {
1584+
type: AST_NODE_TYPES.Literal,
1585+
value: null,
1586+
raw: "null"
1587+
});
1588+
}
15831589
break;
1590+
}
15841591

15851592
case SyntaxKind.EmptyStatement:
15861593
case SyntaxKind.DebuggerStatement:

lib/node-utils.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ module.exports = {
197197
getTokenType,
198198
convertToken,
199199
convertTokens,
200-
getNodeContainer
200+
getNodeContainer,
201+
isWithinTypeAnnotation
201202
};
202203
/* eslint-enable no-use-before-define */
203204

@@ -472,6 +473,16 @@ function isOptional(node) {
472473
? (node.questionToken.kind === SyntaxKind.QuestionToken) : false;
473474
}
474475

476+
/**
477+
* Returns true if the given TSNode is within the context of a "typeAnnotation",
478+
* which effectively means - is it coming from its parent's `type` or `types` property
479+
* @param {TSNode} node TSNode to be checked
480+
* @returns {boolean} is within "typeAnnotation context"
481+
*/
482+
function isWithinTypeAnnotation(node) {
483+
return node.parent.type === node || (node.parent.types && node.parent.types.indexOf(node) > -1);
484+
}
485+
475486
/**
476487
* Fixes the exports of the given TSNode
477488
* @param {TSNode} node the TSNode
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)