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

Fix: Use TSNullKeyword for null type instead of Literal #313

Merged
merged 1 commit into from
Jun 6, 2017
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
1 change: 1 addition & 0 deletions lib/ast-node-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ module.exports = {
TSModuleDeclaration: "TSModuleDeclaration",
TSNamespaceFunctionDeclaration: "TSNamespaceFunctionDeclaration",
TSNonNullExpression: "TSNonNullExpression",
TSNullKeyword: "TSNullKeyword",
TSNumberKeyword: "TSNumberKeyword",
TSParameterProperty: "TSParameterProperty",
TSPropertySignature: "TSPropertySignature",
Expand Down
19 changes: 13 additions & 6 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1574,13 +1574,20 @@ module.exports = function convert(config) {
});
break;

case SyntaxKind.NullKeyword:
Object.assign(result, {
type: AST_NODE_TYPES.Literal,
value: null,
raw: "null"
});
case SyntaxKind.NullKeyword: {
if (nodeUtils.isWithinTypeAnnotation(node)) {
Object.assign(result, {
type: AST_NODE_TYPES.TSNullKeyword
});
} else {
Object.assign(result, {
type: AST_NODE_TYPES.Literal,
value: null,
raw: "null"
});
}
break;
}

case SyntaxKind.EmptyStatement:
case SyntaxKind.DebuggerStatement:
Expand Down
13 changes: 12 additions & 1 deletion lib/node-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ module.exports = {
getTokenType,
convertToken,
convertTokens,
getNodeContainer
getNodeContainer,
isWithinTypeAnnotation
};
/* eslint-enable no-use-before-define */

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

/**
* Returns true if the given TSNode is within the context of a "typeAnnotation",
* which effectively means - is it coming from its parent's `type` or `types` property
* @param {TSNode} node TSNode to be checked
* @returns {boolean} is within "typeAnnotation context"
*/
function isWithinTypeAnnotation(node) {
return node.parent.type === node || (node.parent.types && node.parent.types.indexOf(node) > -1);
}

/**
* Fixes the exports of the given TSNode
* @param {TSNode} node the TSNode
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let x: null;
let y: undefined;
203 changes: 203 additions & 0 deletions tests/lib/__snapshots__/typescript.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15659,6 +15659,209 @@ Object {
}
`;

exports[`typescript fixtures/basics/null-and-undefined-type-annotations.src 1`] = `
Object {
"body": Array [
Object {
"declarations": Array [
Object {
"id": Object {
"loc": Object {
"end": Object {
"column": 5,
"line": 1,
},
"start": Object {
"column": 4,
"line": 1,
},
},
"name": "x",
"range": Array [
4,
5,
],
"type": "Identifier",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 11,
"line": 1,
},
"start": Object {
"column": 7,
"line": 1,
},
},
"range": Array [
7,
11,
],
"type": "TypeAnnotation",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 11,
"line": 1,
},
"start": Object {
"column": 7,
"line": 1,
},
},
"range": Array [
7,
11,
],
"type": "TSNullKeyword",
},
},
},
"init": null,
"loc": Object {
"end": Object {
"column": 11,
"line": 1,
},
"start": Object {
"column": 4,
"line": 1,
},
},
"range": Array [
4,
11,
],
"type": "VariableDeclarator",
},
],
"kind": "let",
"loc": Object {
"end": Object {
"column": 12,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
12,
],
"type": "VariableDeclaration",
},
Object {
"declarations": Array [
Object {
"id": Object {
"loc": Object {
"end": Object {
"column": 5,
"line": 2,
},
"start": Object {
"column": 4,
"line": 2,
},
},
"name": "y",
"range": Array [
17,
18,
],
"type": "Identifier",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 16,
"line": 2,
},
"start": Object {
"column": 7,
"line": 2,
},
},
"range": Array [
20,
29,
],
"type": "TypeAnnotation",
"typeAnnotation": Object {
"loc": Object {
"end": Object {
"column": 16,
"line": 2,
},
"start": Object {
"column": 7,
"line": 2,
},
},
"range": Array [
20,
29,
],
"type": "TSUndefinedKeyword",
},
},
},
"init": null,
"loc": Object {
"end": Object {
"column": 16,
"line": 2,
},
"start": Object {
"column": 4,
"line": 2,
},
},
"range": Array [
17,
29,
],
"type": "VariableDeclarator",
},
],
"kind": "let",
"loc": Object {
"end": Object {
"column": 17,
"line": 2,
},
"start": Object {
"column": 0,
"line": 2,
},
},
"range": Array [
13,
30,
],
"type": "VariableDeclaration",
},
],
"loc": Object {
"end": Object {
"column": 17,
"line": 2,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
30,
],
"sourceType": "script",
"type": "Program",
}
`;

exports[`typescript fixtures/basics/type-alias-declaration.src 1`] = `
Object {
"body": Array [
Expand Down