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

Commit cbf1258

Browse files
committed
Fix: Location data for typeAnnotations
1 parent 62088b1 commit cbf1258

File tree

4 files changed

+696
-373
lines changed

4 files changed

+696
-373
lines changed

lib/convert.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,12 @@ module.exports = function convert(config) {
8585
*/
8686
function convertTypeAnnotation(child) {
8787
const annotation = convertChild(child);
88+
const annotationStartCol = child.getFullStart() - 1;
89+
const loc = nodeUtils.getLocFor(annotationStartCol, child.end, ast);
8890
return {
8991
type: AST_NODE_TYPES.TypeAnnotation,
90-
loc: annotation.loc,
91-
range: annotation.range,
92+
loc,
93+
range: [annotationStartCol, child.end],
9294
typeAnnotation: annotation
9395
};
9496
}
@@ -160,7 +162,7 @@ module.exports = function convert(config) {
160162

161163
const constraint = typeParameter.constraint
162164
? convert({ node: typeParameter.constraint, parent: typeParameter, ast, additionalOptions })
163-
: null;
165+
: undefined;
164166

165167
const defaultParameter = typeParameter.default
166168
? convert({ node: typeParameter.default, parent: typeParameter, ast, additionalOptions })
@@ -1273,7 +1275,9 @@ module.exports = function convert(config) {
12731275

12741276
if (node.type) {
12751277
parameter.typeAnnotation = convertTypeAnnotation(node.type);
1276-
parameter.range[1] = node.type.getEnd();
1278+
const end = node.type.getEnd();
1279+
parameter.range[1] = end;
1280+
parameter.loc.end.column = end;
12771281
}
12781282

12791283
if (node.questionToken) {

tests/ast-alignment/spec.js

Lines changed: 98 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ const fixturePatternsToTest = [
426426
"typescript/basics/class-with-readonly-property.src.ts",
427427
"typescript/expressions/call-expression-type-arguments.src.ts",
428428
"typescript/expressions/new-expression-type-arguments.src.ts",
429+
"typescript/basics/function-with-types.src.ts",
430+
"typescript/basics/non-null-assertion-operator.src.ts",
431+
"typescript/namespaces-and-modules/ambient-module-declaration-with-import.src.ts",
429432

430433
{
431434
pattern: "typescript/basics/export-named-enum.src.ts",
@@ -468,62 +471,95 @@ const fixturePatternsToTest = [
468471
*/
469472
// "typescript/basics/arrow-function-with-type-parameters.src.ts" // typescript-eslint-parser parse errors
470473

474+
/* ================================================== */
475+
471476
/**
472477
* TypeScript AST differences which need to be resolved
473478
*/
474-
// "typescript/babylon-convergence/type-parameters.src.ts",
479+
480+
/**
481+
* Identified major AST differences
482+
*/
483+
484+
/**
485+
* Babylon: ClassDeclaration + abstract: true
486+
* tsep: TSAbstractClassDeclaration
487+
*/
475488
// "typescript/basics/abstract-class-with-abstract-properties.src.ts",
489+
490+
/**
491+
* Babylon: ClassProperty + abstract: true
492+
* tsep: TSAbstractClassProperty
493+
*/
476494
// "typescript/basics/abstract-class-with-abstract-readonly-property.src.ts",
495+
496+
/**
497+
* Babylon: TSExpressionWithTypeArguments
498+
* tsep: ClassImplements
499+
*/
500+
// "typescript/basics/class-with-implements-generic-multiple.src.ts",
501+
// "typescript/basics/class-with-implements-generic.src.ts",
502+
// "typescript/basics/class-with-implements.src.ts",
503+
504+
/**
505+
* Babylon: TSDeclareFunction + declare: true
506+
* tsep: DeclareFunction
507+
*/
508+
// "typescript/basics/declare-function.src.ts",
509+
510+
/**
511+
* Babylon: TSDeclareFunction
512+
* tsep: TSNamespaceFunctionDeclaration
513+
*/
514+
// "typescript/namespaces-and-modules/declare-namespace-with-exported-function.src.ts",
515+
516+
/**
517+
* Babylon: FunctionDeclaration
518+
* tsep: TSNamespaceFunctionDeclaration
519+
*/
520+
// "typescript/namespaces-and-modules/module-with-default-exports.src.ts",
521+
522+
/**
523+
* Other major AST differences (e.g. fundamentally different node types)
524+
*/
525+
// "typescript/basics/class-with-mixin.src.ts",
526+
// "typescript/basics/function-with-types-assignation.src.ts",
527+
// "typescript/basics/interface-extends-multiple.src.ts",
528+
// "typescript/basics/interface-extends.src.ts",
529+
// "typescript/basics/interface-type-parameters.src.ts",
530+
// "typescript/basics/interface-with-extends-type-parameters.src.ts",
531+
// "typescript/basics/interface-with-generic.src.ts",
532+
// "typescript/basics/interface-with-jsdoc.src.ts",
533+
// "typescript/basics/interface-with-optional-properties.src.ts",
534+
// "typescript/basics/interface-without-type-annotation.src.ts",
535+
// "typescript/basics/type-alias-declaration-with-constrained-type-parameter.src.ts",
536+
// "typescript/basics/type-alias-declaration.src.ts",
537+
// "typescript/basics/type-alias-object-without-annotation.src.ts",
538+
// "typescript/basics/typed-this.src.ts",
539+
// "typescript/errorRecovery/interface-empty-extends.src.ts",
540+
541+
/**
542+
* Minor AST differences (e.g. location data)
543+
*/
477544
// "typescript/basics/class-with-accessibility-modifiers.src.ts",
478545
// "typescript/basics/class-with-extends-generic-multiple.src.ts",
479546
// "typescript/basics/class-with-extends-generic.src.ts",
480547
// "typescript/basics/class-with-generic-method-default.src.ts",
481548
// "typescript/basics/class-with-generic-method.src.ts",
482-
// "typescript/basics/class-with-implements-generic-multiple.src.ts",
483-
// "typescript/basics/class-with-implements-generic.src.ts",
484-
// "typescript/basics/class-with-implements.src.ts",
485-
// "typescript/basics/class-with-mixin.src.ts",
486549
// "typescript/basics/class-with-optional-computed-property.src.ts",
487550
// "typescript/basics/class-with-optional-properties.src.ts",
488551
// "typescript/basics/class-with-optional-property-undefined.src.ts",
489552
// "typescript/basics/class-with-private-parameter-properties.src.ts",
490553
// "typescript/basics/class-with-protected-parameter-properties.src.ts",
491554
// "typescript/basics/class-with-public-parameter-properties.src.ts",
492555
// "typescript/basics/class-with-readonly-parameter-properties.src.ts",
493-
// "typescript/basics/class-with-type-parameter-default.src.ts",
494-
// "typescript/basics/class-with-type-parameter-underscore.src.ts",
495-
// "typescript/basics/class-with-type-parameter.src.ts",
496-
// "typescript/basics/declare-function.src.ts",
497556
// "typescript/basics/destructuring-assignment.src.ts",
498-
// "typescript/basics/export-default-class-with-generic.src.ts",
499-
// "typescript/basics/export-default-class-with-multiple-generics.src.ts",
500-
// "typescript/basics/export-named-class-with-generic.src.ts",
501-
// "typescript/basics/export-named-class-with-multiple-generics.src.ts",
502557
// "typescript/basics/function-with-object-type-with-optional-properties.src.ts",
503558
// "typescript/basics/function-with-object-type-without-annotation.src.ts",
504-
// "typescript/basics/function-with-type-parameters-that-have-comments.src.ts",
505-
// "typescript/basics/function-with-type-parameters-with-constraint.src.ts",
506-
// "typescript/basics/function-with-type-parameters.src.ts",
507-
// "typescript/basics/function-with-types-assignation.src.ts",
508-
// "typescript/basics/function-with-types.src.ts",
509-
// "typescript/basics/interface-extends-multiple.src.ts",
510-
// "typescript/basics/interface-extends.src.ts",
511-
// "typescript/basics/interface-type-parameters.src.ts",
512-
// "typescript/basics/interface-with-extends-type-parameters.src.ts",
513-
// "typescript/basics/interface-with-generic.src.ts",
514-
// "typescript/basics/interface-with-jsdoc.src.ts",
515-
// "typescript/basics/interface-with-optional-properties.src.ts",
516-
// "typescript/basics/interface-without-type-annotation.src.ts",
517559
// "typescript/basics/nested-type-arguments.src.ts",
518-
// "typescript/basics/non-null-assertion-operator.src.ts",
519560
// "typescript/basics/null-and-undefined-type-annotations.src.ts",
520561
// "typescript/basics/object-with-escaped-properties.src.ts",
521-
// "typescript/basics/type-alias-declaration-with-constrained-type-parameter.src.ts",
522-
// "typescript/basics/type-alias-declaration.src.ts",
523-
// "typescript/basics/type-alias-object-without-annotation.src.ts",
524562
// "typescript/basics/type-guard.src.ts",
525-
// "typescript/basics/type-parameters-comments.src.ts",
526-
// "typescript/basics/typed-this.src.ts",
527563
// "typescript/basics/var-with-dotted-type.src.ts",
528564
// "typescript/basics/var-with-type.src.ts",
529565
// "typescript/basics/variable-declaration-type-annotation-spacing.src.ts",
@@ -546,13 +582,38 @@ const fixturePatternsToTest = [
546582
// "typescript/decorators/property-decorators/property-decorator-factory-static-member.src.ts",
547583
// "typescript/decorators/property-decorators/property-decorator-instance-member.src.ts",
548584
// "typescript/decorators/property-decorators/property-decorator-static-member.src.ts",
549-
// "typescript/errorRecovery/interface-empty-extends.src.ts",
550-
// "typescript/namespaces-and-modules/ambient-module-declaration-with-import.src.ts",
551-
// "typescript/namespaces-and-modules/declare-namespace-with-exported-function.src.ts",
552-
// "typescript/namespaces-and-modules/module-with-default-exports.src.ts",
553585

554586
/**
555-
* Requires fix in https://github.com/babel/babylon/pull/684
587+
* Requires a solution to https://github.com/babel/babylon/issues/691
588+
*/
589+
// "typescript/babylon-convergence/type-parameters.src.ts",
590+
// "typescript/babylon-convergence/type-parameter-whitespace-loc.src.ts",
591+
// "typescript/basics/class-with-type-parameter-default.src.ts",
592+
// "typescript/basics/class-with-type-parameter-underscore.src.ts",
593+
// "typescript/basics/class-with-type-parameter.src.ts",
594+
// {
595+
// pattern: "typescript/basics/export-default-class-with-generic.src.ts",
596+
// config: { babylonParserOptions: { sourceType: "module" } }
597+
// },
598+
// {
599+
// pattern: "typescript/basics/export-default-class-with-multiple-generics.src.ts",
600+
// config: { babylonParserOptions: { sourceType: "module" } }
601+
// },
602+
// {
603+
// pattern: "typescript/basics/export-named-class-with-generic.src.ts",
604+
// config: { babylonParserOptions: { sourceType: "module" } }
605+
// },
606+
// {
607+
// pattern: "typescript/basics/export-named-class-with-multiple-generics.src.ts",
608+
// config: { babylonParserOptions: { sourceType: "module" } }
609+
// },
610+
// "typescript/basics/function-with-type-parameters-that-have-comments.src.ts",
611+
// "typescript/basics/function-with-type-parameters-with-constraint.src.ts",
612+
// "typescript/basics/function-with-type-parameters.src.ts",
613+
// "typescript/basics/type-parameters-comments.src.ts",
614+
615+
/**
616+
* Requires the fix in https://github.com/babel/babylon/pull/684 (MERGED not yet released)
556617
*/
557618
// "typescript/namespaces-and-modules/shorthand-ambient-module-declaration.src.ts"
558619

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function f< T >() {}

0 commit comments

Comments
 (0)