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

Fix: Location data for typeAnnotations #378

Merged
merged 1 commit into from
Sep 5, 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
79 changes: 48 additions & 31 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ module.exports = function convert(config) {
*/
function convertTypeAnnotation(child) {
const annotation = convertChild(child);
const annotationStartCol = child.getFullStart() - 1;
const loc = nodeUtils.getLocFor(annotationStartCol, child.end, ast);
return {
type: AST_NODE_TYPES.TypeAnnotation,
loc: annotation.loc,
range: annotation.range,
loc,
range: [annotationStartCol, child.end],
typeAnnotation: annotation
};
}
Expand Down Expand Up @@ -160,7 +162,7 @@ module.exports = function convert(config) {

const constraint = typeParameter.constraint
? convert({ node: typeParameter.constraint, parent: typeParameter, ast, additionalOptions })
: null;
: undefined;

const defaultParameter = typeParameter.default
? convert({ node: typeParameter.default, parent: typeParameter, ast, additionalOptions })
Expand Down Expand Up @@ -279,7 +281,7 @@ module.exports = function convert(config) {
result.type = customType;
Object
.keys(node)
.filter(key => !(/^(?:kind|parent|pos|end|flags|modifierFlagsCache|jsDoc)$/.test(key)))
.filter(key => !(/^(?:_children|kind|parent|pos|end|flags|modifierFlagsCache|jsDoc)$/.test(key)))
.forEach(key => {
if (key === "type") {
result.typeAnnotation = (node.type) ? convertTypeAnnotation(node.type) : null;
Expand Down Expand Up @@ -392,6 +394,19 @@ module.exports = function convert(config) {
result.modifiers = remainingModifiers.map(convertChild);
}

/**
* Uses the current TSNode's end location for its `type` to adjust the location data of the given
* ESTreeNode, which should be the parent of the final typeAnnotation node
* @param {ESTreeNode} typeAnnotationParent The node that will have its location data mutated
* @returns {void}
*/
function fixTypeAnnotationParentLocation(typeAnnotationParent) {
const end = node.type.getEnd();
typeAnnotationParent.range[1] = end;
const loc = nodeUtils.getLocFor(typeAnnotationParent.range[0], typeAnnotationParent.range[1], ast);
typeAnnotationParent.loc = loc;
}

/**
* The core of the conversion logic:
* Identify and convert each relevant TypeScript SyntaxKind
Expand Down Expand Up @@ -619,15 +634,7 @@ module.exports = function convert(config) {

if (node.type) {
result.id.typeAnnotation = convertTypeAnnotation(node.type);
result.id.range[1] = node.type.getEnd();

const identifierEnd = node.name.getEnd();
const numCharsBetweenTypeAndIdentifier = node.type.getStart() - (node.type.getFullStart() - identifierEnd - ":".length) - identifierEnd;

result.id.typeAnnotation.range = [
result.id.typeAnnotation.range[0] - numCharsBetweenTypeAndIdentifier,
result.id.typeAnnotation.range[1]
];
fixTypeAnnotationParentLocation(result.id);
}
break;
}
Expand Down Expand Up @@ -704,7 +711,7 @@ module.exports = function convert(config) {
node,
parentNode =>
(parentNode.kind === SyntaxKind.BinaryExpression || parentNode.kind === SyntaxKind.ArrowFunction)
);
);
const objectAssignNode = (
ancestorNode &&
ancestorNode.kind === SyntaxKind.BinaryExpression &&
Expand Down Expand Up @@ -806,7 +813,7 @@ module.exports = function convert(config) {
value: convertChild(node.initializer),
computed: nodeUtils.isComputedProperty(node.name),
static: nodeUtils.hasStaticModifierFlag(node),
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node)
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined
});

if (node.type) {
Expand Down Expand Up @@ -836,7 +843,12 @@ module.exports = function convert(config) {
case SyntaxKind.SetAccessor:
case SyntaxKind.MethodDeclaration: {

const openingParen = nodeUtils.findNextToken(node.name, ast);
const openingParen = nodeUtils.findFirstMatchingToken(node.name, ast, token => {
if (!token || !token.kind) {
return false;
}
return nodeUtils.getTextForTokenKind(token.kind) === "(";
});

const methodLoc = ast.getLineAndCharacterOfPosition(openingParen.getStart()),
nodeIsMethod = (node.kind === SyntaxKind.MethodDeclaration),
Expand Down Expand Up @@ -1273,7 +1285,7 @@ module.exports = function convert(config) {

if (node.type) {
parameter.typeAnnotation = convertTypeAnnotation(node.type);
parameter.range[1] = node.type.getEnd();
fixTypeAnnotationParentLocation(parameter);
}

if (node.questionToken) {
Expand All @@ -1285,10 +1297,10 @@ module.exports = function convert(config) {
type: AST_NODE_TYPES.TSParameterProperty,
range: [node.getStart(), node.end],
loc: nodeUtils.getLoc(node, ast),
accessibility: nodeUtils.getTSNodeAccessibility(node),
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node),
accessibility: nodeUtils.getTSNodeAccessibility(node) || undefined,
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node) || undefined,
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node) || undefined,
parameter: result
};
}
Expand Down Expand Up @@ -1941,9 +1953,9 @@ module.exports = function convert(config) {
key: convertChild(node.name),
params: convertParameters(node.parameters),
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node) || undefined
});

const accessibility = nodeUtils.getTSNodeAccessibility(node);
Expand All @@ -1961,14 +1973,14 @@ module.exports = function convert(config) {
case SyntaxKind.PropertySignature: {
Object.assign(result, {
type: AST_NODE_TYPES.TSPropertySignature,
optional: nodeUtils.isOptional(node),
optional: nodeUtils.isOptional(node) || undefined,
computed: nodeUtils.isComputedProperty(node.name),
key: convertChild(node.name),
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
initializer: convertChild(node.initializer),
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : undefined,
initializer: convertChild(node.initializer) || undefined,
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node) || undefined,
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node) || undefined
});

const accessibility = nodeUtils.getTSNodeAccessibility(node);
Expand All @@ -1984,9 +1996,9 @@ module.exports = function convert(config) {
type: AST_NODE_TYPES.TSIndexSignature,
index: convertChild(node.parameters[0]),
typeAnnotation: (node.type) ? convertTypeAnnotation(node.type) : null,
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node),
readonly: nodeUtils.hasModifier(SyntaxKind.ReadonlyKeyword, node) || undefined,
static: nodeUtils.hasModifier(SyntaxKind.StaticKeyword, node),
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node)
export: nodeUtils.hasModifier(SyntaxKind.ExportKeyword, node) || undefined
});

const accessibility = nodeUtils.getTSNodeAccessibility(node);
Expand Down Expand Up @@ -2057,6 +2069,11 @@ module.exports = function convert(config) {
parameterName: convertChild(node.parameterName),
typeAnnotation: convertTypeAnnotation(node.type)
});
/**
* Specific fix for type-guard location data
*/
result.typeAnnotation.loc = result.typeAnnotation.typeAnnotation.loc;
result.typeAnnotation.range = result.typeAnnotation.typeAnnotation.range;
break;

case SyntaxKind.EnumDeclaration: {
Expand Down
18 changes: 18 additions & 0 deletions lib/node-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ module.exports = {
getTSNodeAccessibility,
hasStaticModifierFlag,
findNextToken,
findFirstMatchingToken,
findChildOfKind,
findFirstMatchingAncestor,
findAncestorOfKind,
Expand Down Expand Up @@ -413,6 +414,23 @@ function findNextToken(previousToken, parent) {
return ts.findNextToken(previousToken, parent);
}

/**
* Find the first matching token based on the given predicate function.
* @param {TSToken} previousToken The previous TSToken
* @param {TSNode} parent The parent TSNode
* @param {Function} predicate The predicate function to apply to each checked token
* @returns {TSToken|undefined} a matching TSToken
*/
function findFirstMatchingToken(previousToken, parent, predicate) {
while (previousToken) {
if (predicate(previousToken)) {
return previousToken;
}
previousToken = findNextToken(previousToken, parent);
}
return undefined;
}

/**
* Finds the first child TSNode which matches the given kind
* @param {TSNode} node The parent TSNode
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
},
"license": "BSD-2-Clause",
"devDependencies": {
"babel-code-frame": "^6.22.0",
"babylon": "^7.0.0-beta.20",
"eslint": "3.19.0",
"babel-code-frame": "^6.26.0",
"babylon": "^7.0.0-beta.22",
"eslint": "4.6.1",
"eslint-config-eslint": "4.0.0",
"eslint-plugin-node": "4.2.2",
"eslint-plugin-node": "5.1.1",
"eslint-release": "0.10.3",
"glob": "^7.1.2",
"jest": "20.0.4",
"jest": "21.0.1",
"lodash.isplainobject": "^4.0.6",
"npm-license": "0.3.3",
"shelljs": "0.7.7",
"shelljs": "0.7.8",
"shelljs-nodecli": "0.1.1",
"typescript": "~2.4.0"
},
Expand All @@ -54,7 +54,7 @@
},
"dependencies": {
"lodash.unescape": "4.0.1",
"semver": "5.3.0"
"semver": "5.4.1"
},
"peerDependencies": {
"typescript": "*"
Expand Down
5 changes: 0 additions & 5 deletions tests/ast-alignment/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@ function parseWithBabylonPluginTypescript(text, parserOptions) { // eslint-disab
plugins: [
"jsx",
"typescript",
"doExpressions",
"objectRestSpread",
"decorators",
"classProperties",
"exportExtensions",
"asyncGenerators",
"functionBind",
"functionSent",
"dynamicImport",
"numericSeparator",
"estree"
]
}, parserOptions));
Expand Down
Loading