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

Breaking: Convert Signature types to be more ESTree like (fixes #262) #264

Merged
merged 4 commits into from
May 28, 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 @@ -106,6 +106,7 @@ module.exports = {
TSConstructorType: "TSConstructorType",
TSConstructSignature: "TSConstructSignature",
TSDeclareKeyword: "TSDeclareKeyword",
TSIndexSignature: "TSIndexSignature",
TSInterfaceBody: "TSInterfaceBody",
TSInterfaceDeclaration: "TSInterfaceDeclaration",
TSInterfaceHeritage: "TSInterfaceHeritage",
Expand Down
87 changes: 71 additions & 16 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ module.exports = function convert(config) {
type: AST_NODE_TYPES.Property,
key: convertChild(node.name),
value: convertChild(node.initializer),
computed: (node.name.kind === SyntaxKind.ComputedPropertyName),
computed: nodeUtils.isComputedProperty(node.name),
method: false,
shorthand: false,
kind: "init"
Expand Down Expand Up @@ -660,7 +660,7 @@ module.exports = function convert(config) {
type: (isAbstract) ? AST_NODE_TYPES.TSAbstractClassProperty : AST_NODE_TYPES.ClassProperty,
key: convertChild(node.name),
value: convertChild(node.initializer),
computed: (node.name.kind === SyntaxKind.ComputedPropertyName),
computed: nodeUtils.isComputedProperty(node.name),
static: nodeUtils.hasStaticModifierFlag(node),
accessibility: nodeUtils.getTSNodeAccessibility(node),
decorators: convertDecorators(node.decorators),
Expand Down Expand Up @@ -713,7 +713,7 @@ module.exports = function convert(config) {
type: AST_NODE_TYPES.Property,
key: convertChild(node.name),
value: method,
computed: (node.name.kind === SyntaxKind.ComputedPropertyName),
computed: nodeUtils.isComputedProperty(node.name),
method: nodeIsMethod,
shorthand: false,
kind: "init"
Expand All @@ -730,8 +730,6 @@ module.exports = function convert(config) {
return convertedParam;
});

const isMethodNameComputed = (node.name.kind === SyntaxKind.ComputedPropertyName);

/**
* TypeScript class methods can be defined as "abstract"
*/
Expand All @@ -743,7 +741,7 @@ module.exports = function convert(config) {
type: methodDefinitionType,
key: convertChild(node.name),
value: method,
computed: isMethodNameComputed,
computed: nodeUtils.isComputedProperty(node.name),
static: nodeUtils.hasStaticModifierFlag(node),
kind: "method",
accessibility: nodeUtils.getTSNodeAccessibility(node),
Expand Down Expand Up @@ -805,7 +803,7 @@ module.exports = function convert(config) {
};

const constructorIdentifierLoc = ast.getLineAndCharacterOfPosition(firstConstructorToken.getStart()),
constructorIsComputed = !!node.name && (node.name.kind === SyntaxKind.ComputedPropertyName);
constructorIsComputed = !!node.name && nodeUtils.isComputedProperty(node.name);

let constructorKey;

Expand Down Expand Up @@ -1723,6 +1721,72 @@ module.exports = function convert(config) {

}

case SyntaxKind.MethodSignature: {
Object.assign(result, {
type: AST_NODE_TYPES.TSMethodSignature,
optional: nodeUtils.isOptional(node),
computed: nodeUtils.isComputedProperty(node.name),
key: convertChild(node.name),
params: node.parameters.map(parameter => convertChild(parameter)),
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
accessibility: nodeUtils.getTSNodeAccessibility(node),
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
});

if (node.typeParameters) {
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
}

break;
}

case SyntaxKind.PropertySignature: {
Object.assign(result, {
type: AST_NODE_TYPES.TSPropertySignature,
optional: nodeUtils.isOptional(node),
computed: nodeUtils.isComputedProperty(node.name),
key: convertChild(node.name),
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
initializer: convertChild(node.initializer),
accessibility: nodeUtils.getTSNodeAccessibility(node),
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
});

break;
}

case SyntaxKind.IndexSignature: {
Object.assign(result, {
type: AST_NODE_TYPES.TSIndexSignature,
index: convertChild(node.parameters[0]),
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
accessibility: nodeUtils.getTSNodeAccessibility(node),
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
});

break;
}

case SyntaxKind.ConstructSignature: {
Object.assign(result, {
type: AST_NODE_TYPES.TSConstructSignature,
params: node.parameters.map(parameter => convertChild(parameter)),
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null
});

if (node.typeParameters) {
result.typeParameters = convertTSTypeParametersToTypeParametersDeclaration(node.typeParameters);
}

break;
}

case SyntaxKind.InterfaceDeclaration: {
const interfaceHeritageClauses = node.heritageClauses || [];

Expand Down Expand Up @@ -1771,15 +1835,6 @@ module.exports = function convert(config) {
});
break;

case SyntaxKind.PropertySignature:
case SyntaxKind.MethodSignature:
deeplyCopy();

if (node.questionToken) {
result.name.optional = true;
}
break;

default:
deeplyCopy();
}
Expand Down
21 changes: 21 additions & 0 deletions lib/node-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ module.exports = {
hasJSXAncestor,
unescapeIdentifier,
unescapeStringLiteralText,
isComputedProperty,
isOptional,
fixExports,
getTokenType,
convertToken,
Expand Down Expand Up @@ -446,6 +448,25 @@ function unescapeStringLiteralText(text) {
return unescape(text);
}

/**
* Returns true if a given TSNode is a computed property
* @param {TSNode} node TSNode to be checked
* @returns {boolean} is Computed Property
*/
function isComputedProperty(node) {
return node.kind === SyntaxKind.ComputedPropertyName;
}

/**
* Returns true if a given TSNode is optional (has QuestionToken)
* @param {TSNode} node TSNode to be checked
* @returns {boolean} is Optional
*/
function isOptional(node) {
return (node.questionToken)
? (node.questionToken.kind === SyntaxKind.QuestionToken) : false;
}

/**
* Fixes the exports of the given TSNode
* @param {TSNode} node the TSNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,67 +107,74 @@ module.exports = {
48
],
"loc": {
"end": {
"line": 2,
"column": 17
},
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 17
}
},
"name": {
"optional": false,
"computed": false,
"key": {
"type": "Identifier",
"range": [
35,
40
],
"loc": {
"end": {
"line": 2,
"column": 9
},
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 9
}
},
"name": "count"
},
"typeAnnotation": {
"type": "TypeAnnotation",
"range": [
42,
48
],
"loc": {
"end": {
"line": 2,
"column": 17
},
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 17
}
},
"range": [
42,
48
],
"typeAnnotation": {
"type": "TSNumberKeyword",
"range": [
42,
48
],
"loc": {
"end": {
"line": 2,
"column": 17
},
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 17
}
}
}
}
},
"initializer": null,
"accessibility": null,
"readonly": false,
"static": false,
"export": false
}
]
}
Expand Down
Loading