From aa2566d22547becf3b82efd3d14ae912cbd9160a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 12 May 2017 12:38:37 -0700 Subject: [PATCH 01/55] WIP: TypeScript parser --- src/index.js | 15 +- src/parser/expression.js | 193 ++- src/parser/lval.js | 27 +- src/parser/statement.js | 321 ++-- src/parser/util.js | 22 +- src/plugins/estree.js | 16 +- src/plugins/flow.js | 16 +- src/plugins/typescript.js | 1472 +++++++++++++++++ src/tokenizer/context.js | 2 +- src/tokenizer/index.js | 12 +- src/tokenizer/state.js | 8 +- src/tokenizer/types.js | 3 +- src/types.js | 408 ++++- .../arrow-function/annotated/actual.js | 1 + .../arrow-function/annotated/expected.json | 134 ++ .../arrow-function/async-generic/actual.js | 1 + .../async-generic/expected.json | 202 +++ .../typescript/arrow-function/async/actual.js | 1 + .../arrow-function/async/expected.json | 135 ++ .../default-parameter-values/actual.js | 1 + .../default-parameter-values/expected.json | 157 ++ .../arrow-function/destructuring/actual.js | 1 + .../destructuring/expected.json | 197 +++ .../arrow-function/generic-tsx/actual.js | 2 + .../arrow-function/generic-tsx/expected.json | 240 +++ .../arrow-function/generic-tsx/options.json | 3 + .../arrow-function/generic/actual.js | 1 + .../arrow-function/generic/expected.json | 202 +++ .../optional-parameter/actual.js | 1 + .../optional-parameter/expected.json | 135 ++ .../arrow-function/predicate-types/actual.js | 1 + .../predicate-types/expected.json | 165 ++ test/fixtures/typescript/cast/as/actual.js | 3 + .../fixtures/typescript/cast/as/expected.json | 396 +++++ .../typescript/cast/false-positive/actual.js | 1 + .../cast/false-positive/expected.json | 156 ++ .../cast/need-parentheses/actual.js | 3 + .../cast/need-parentheses/expected.json | 346 ++++ .../actual.js | 1 + .../expected.json | 114 ++ .../typescript/cast/null-assertion/actual.js | 1 + .../cast/null-assertion/expected.json | 81 + .../type-assertion-after-operator/actual.js | 1 + .../expected.json | 135 ++ .../type-assertion-before-operator/actual.js | 1 + .../expected.json | 135 ++ .../typescript/cast/type-assertion/actual.js | 1 + .../cast/type-assertion/expected.json | 99 ++ .../typescript/class/abstract/actual.js | 6 + .../typescript/class/abstract/expected.json | 305 ++++ .../typescript/class/constructor/actual.js | 5 + .../class/constructor/expected.json | 416 +++++ .../typescript/class/declare/actual.js | 7 + .../typescript/class/declare/expected.json | 329 ++++ .../expression-extends-implements/actual.js | 2 + .../expected.json | 486 ++++++ .../class/expression-extends/actual.js | 2 + .../class/expression-extends/expected.json | 286 ++++ .../class/expression-generic/actual.js | 2 + .../class/expression-generic/expected.json | 222 +++ .../class/expression-implements/actual.js | 2 + .../class/expression-implements/expected.json | 353 ++++ .../class/extends-implements/actual.js | 1 + .../class/extends-implements/expected.json | 249 +++ .../typescript/class/extends/actual.js | 1 + .../typescript/class/extends/expected.json | 149 ++ .../typescript/class/generic/actual.js | 1 + .../typescript/class/generic/expected.json | 197 +++ .../typescript/class/get-generic/actual.js | 3 + .../class/get-generic/expected.json | 174 ++ .../typescript/class/implements/actual.js | 1 + .../typescript/class/implements/expected.json | 183 ++ .../class/index-signature/actual.js | 4 + .../class/index-signature/expected.json | 213 +++ .../members-with-modifier-names/actual.js | 6 + .../members-with-modifier-names/expected.json | 338 ++++ .../members-with-reserved-names/actual.js | 3 + .../members-with-reserved-names/expected.json | 140 ++ .../class/method-computed/actual.js | 4 + .../class/method-computed/expected.json | 261 +++ .../typescript/class/method-generic/actual.js | 4 + .../class/method-generic/expected.json | 542 ++++++ .../typescript/class/method-no-body/actual.js | 4 + .../class/method-no-body/expected.json | 179 ++ .../class/method-optional/actual.js | 3 + .../class/method-optional/expected.json | 157 ++ .../class/method-return-type/actual.js | 3 + .../class/method-return-type/expected.json | 156 ++ .../method-with-newline-with-body/actual.js | 6 + .../expected.json | 141 ++ .../actual.js | 5 + .../expected.json | 164 ++ .../class/modifiers-accessors/actual.js | 11 + .../class/modifiers-accessors/expected.json | 673 ++++++++ .../class/modifiers-methods-async/actual.js | 11 + .../modifiers-methods-async/expected.json | 493 ++++++ .../class/modifiers-properties/actual.js | 20 + .../class/modifiers-properties/expected.json | 633 +++++++ .../class/parameter-properties/actual.js | 11 + .../class/parameter-properties/expected.json | 391 +++++ .../typescript/class/properties/actual.js | 6 + .../typescript/class/properties/expected.json | 274 +++ .../class/property-computed/actual.js | 4 + .../class/property-computed/expected.json | 251 +++ .../typescript/class/static/actual.js | 6 + .../typescript/class/static/expected.json | 247 +++ .../typescript/const/no-initializer/actual.js | 1 + .../const/no-initializer/expected.json | 99 ++ .../typescript/declare/const/actual.js | 1 + .../typescript/declare/const/expected.json | 147 ++ .../typescript/declare/destructure/actual.js | 1 + .../declare/destructure/expected.json | 308 ++++ .../typescript/declare/interface/actual.js | 1 + .../declare/interface/expected.json | 68 + .../fixtures/typescript/declare/let/actual.js | 1 + .../typescript/declare/let/expected.json | 86 + .../fixtures/typescript/declare/var/actual.js | 2 + .../typescript/declare/var/expected.json | 153 ++ test/fixtures/typescript/enum/const/actual.js | 1 + .../typescript/enum/const/expected.json | 68 + .../typescript/enum/declare-const/actual.js | 1 + .../enum/declare-const/expected.json | 69 + .../typescript/enum/declare/actual.js | 1 + .../typescript/enum/declare/expected.json | 68 + .../typescript/enum/export-const/actual.js | 1 + .../enum/export-const/expected.json | 85 + .../enum/export-declare-const/actual.js | 1 + .../enum/export-declare-const/expected.json | 86 + .../fixtures/typescript/enum/export/actual.js | 1 + .../typescript/enum/export/expected.json | 84 + .../enum/members-reserved-words/actual.js | 4 + .../enum/members-reserved-words/expected.json | 132 ++ .../typescript/enum/members-strings/actual.js | 4 + .../enum/members-strings/expected.json | 158 ++ .../actual.js | 3 + .../expected.json | 120 ++ .../enum/members-trailing-comma/actual.js | 3 + .../enum/members-trailing-comma/expected.json | 100 ++ .../typescript/enum/members/actual.js | 4 + .../typescript/enum/members/expected.json | 152 ++ .../typescript/export/as-namespace/actual.js | 1 + .../export/as-namespace/expected.json | 66 + .../typescript/export/declare/actual.js | 7 + .../typescript/export/declare/expected.json | 501 ++++++ .../typescript/export/equals/actual.js | 1 + .../typescript/export/equals/expected.json | 66 + .../typescript/function/annotated/actual.js | 1 + .../function/annotated/expected.json | 204 +++ .../typescript/function/anonymous/actual.js | 1 + .../function/anonymous/expected.json | 238 +++ .../typescript/function/declare/actual.js | 2 + .../typescript/function/declare/expected.json | 189 +++ .../typescript/function/overloads/actual.js | 2 + .../function/overloads/expected.json | 236 +++ .../function/predicate-types/actual.js | 2 + .../function/predicate-types/expected.json | 303 ++++ .../import/equals-require/actual.js | 1 + .../import/equals-require/expected.json | 102 ++ .../typescript/import/equals/actual.js | 1 + .../typescript/import/equals/expected.json | 116 ++ .../import/export-import-require/actual.js | 1 + .../export-import-require/expected.json | 102 ++ .../typescript/import/export-import/actual.js | 1 + .../import/export-import/expected.json | 116 ++ .../typescript/import/not-top-level/actual.js | 3 + .../import/not-top-level/expected.json | 156 ++ .../interface/call-signature/actual.js | 3 + .../interface/call-signature/expected.json | 132 ++ .../interface/construct-signature/actual.js | 3 + .../construct-signature/expected.json | 132 ++ .../typescript/interface/export/actual.js | 2 + .../typescript/interface/export/expected.json | 121 ++ .../typescript/interface/extends/actual.js | 1 + .../interface/extends/expected.json | 167 ++ .../typescript/interface/generic/actual.js | 1 + .../interface/generic/expected.json | 181 ++ .../interface/index-accessor/actual.js | 3 + .../interface/index-accessor/expected.json | 132 ++ .../interface/method-computed/actual.js | 4 + .../interface/method-computed/expected.json | 233 +++ .../interface/method-generic/actual.js | 3 + .../interface/method-generic/expected.json | 248 +++ .../interface/method-optional/actual.js | 3 + .../interface/method-optional/expected.json | 118 ++ .../interface/method-plain/actual.js | 4 + .../interface/method-plain/expected.json | 247 +++ .../typescript/interface/modifiers/actual.js | 3 + .../interface/modifiers/expected.json | 117 ++ .../typescript/interface/properties/actual.js | 5 + .../interface/properties/expected.json | 198 +++ .../interface/property-computed/actual.js | 4 + .../interface/property-computed/expected.json | 231 +++ .../interface/property-named-public/actual.js | 3 + .../property-named-public/expected.json | 116 ++ .../interface/reserved-method-name/actual.js | 3 + .../reserved-method-name/expected.json | 117 ++ .../typescript/interface/separators/actual.js | 6 + .../interface/separators/expected.json | 424 +++++ .../module-namespace/body-declare/actual.js | 3 + .../body-declare/expected.json | 149 ++ .../body-nested-declare/actual.js | 5 + .../body-nested-declare/expected.json | 198 +++ .../module-namespace/body-nested/actual.js | 4 + .../body-nested/expected.json | 131 ++ .../module-namespace/body/actual.js | 3 + .../module-namespace/body/expected.json | 153 ++ .../declare-shorthand/actual.js | 1 + .../declare-shorthand/expected.json | 70 + .../global-in-module/actual.js | 5 + .../global-in-module/expected.json | 203 +++ .../module-namespace/head-declare/actual.js | 4 + .../head-declare/expected.json | 266 +++ .../module-namespace/head-export/actual.js | 2 + .../head-export/expected.json | 196 +++ .../module-namespace/head/actual.js | 4 + .../module-namespace/head/expected.json | 293 ++++ test/fixtures/typescript/options.json | 4 + .../typescript/tsx/brace-is-block/actual.js | 3 + .../tsx/brace-is-block/expected.json | 235 +++ test/fixtures/typescript/tsx/options.json | 4 + .../typescript/type-alias/declare/actual.js | 1 + .../type-alias/declare/expected.json | 82 + .../typescript/type-alias/export/actual.js | 2 + .../type-alias/export/expected.json | 135 ++ .../type-alias/generic-complex/actual.js | 1 + .../type-alias/generic-complex/expected.json | 246 +++ .../typescript/type-alias/generic/actual.js | 1 + .../type-alias/generic/expected.json | 132 ++ .../typescript/type-alias/plain/actual.js | 1 + .../typescript/type-alias/plain/expected.json | 81 + .../typescript/type-arguments/call/actual.js | 2 + .../type-arguments/call/expected.json | 230 +++ .../typescript/type-arguments/new/actual.js | 2 + .../type-arguments/new/expected.json | 230 +++ .../fixtures/typescript/types/array/actual.js | 1 + .../typescript/types/array/expected.json | 130 ++ .../types/function-generic/actual.js | 1 + .../types/function-generic/expected.json | 217 +++ .../types/function-in-generic/actual.js | 1 + .../types/function-in-generic/expected.json | 150 ++ .../types/function-with-this/actual.js | 1 + .../types/function-with-this/expected.json | 149 ++ .../typescript/types/function/actual.js | 1 + .../typescript/types/function/expected.json | 244 +++ .../typescript/types/indexed/actual.js | 1 + .../typescript/types/indexed/expected.json | 164 ++ .../typescript/types/keywords/actual.js | 10 + .../typescript/types/keywords/expected.json | 694 ++++++++ .../types/literal-boolean/actual.js | 2 + .../types/literal-boolean/expected.json | 198 +++ .../types/literal-number-negative/actual.js | 1 + .../literal-number-negative/expected.json | 120 ++ .../typescript/types/literal-number/actual.js | 1 + .../types/literal-number/expected.json | 120 ++ .../typescript/types/literal-string/actual.js | 1 + .../types/literal-string/expected.json | 120 ++ .../typescript/types/mapped/actual.js | 2 + .../typescript/types/mapped/expected.json | 292 ++++ .../typescript/types/parenthesized/actual.js | 1 + .../types/parenthesized/expected.json | 97 ++ .../types/reference-generic-nested/actual.js | 1 + .../reference-generic-nested/expected.json | 168 ++ .../types/reference-generic/actual.js | 1 + .../types/reference-generic/expected.json | 134 ++ .../typescript/types/reference/actual.js | 1 + .../typescript/types/reference/expected.json | 117 ++ .../typescript/types/type-literal/actual.js | 2 + .../types/type-literal/expected.json | 186 +++ .../typescript/types/type-operator/actual.js | 1 + .../types/type-operator/expected.json | 133 ++ .../typescript/types/typeof/actual.js | 1 + .../typescript/types/typeof/expected.json | 149 ++ .../types/union-intersection/actual.js | 4 + .../types/union-intersection/expected.json | 505 ++++++ 274 files changed, 28717 insertions(+), 200 deletions(-) create mode 100644 src/plugins/typescript.js create mode 100644 test/fixtures/typescript/arrow-function/annotated/actual.js create mode 100644 test/fixtures/typescript/arrow-function/annotated/expected.json create mode 100644 test/fixtures/typescript/arrow-function/async-generic/actual.js create mode 100644 test/fixtures/typescript/arrow-function/async-generic/expected.json create mode 100644 test/fixtures/typescript/arrow-function/async/actual.js create mode 100644 test/fixtures/typescript/arrow-function/async/expected.json create mode 100644 test/fixtures/typescript/arrow-function/default-parameter-values/actual.js create mode 100644 test/fixtures/typescript/arrow-function/default-parameter-values/expected.json create mode 100644 test/fixtures/typescript/arrow-function/destructuring/actual.js create mode 100644 test/fixtures/typescript/arrow-function/destructuring/expected.json create mode 100644 test/fixtures/typescript/arrow-function/generic-tsx/actual.js create mode 100644 test/fixtures/typescript/arrow-function/generic-tsx/expected.json create mode 100644 test/fixtures/typescript/arrow-function/generic-tsx/options.json create mode 100644 test/fixtures/typescript/arrow-function/generic/actual.js create mode 100644 test/fixtures/typescript/arrow-function/generic/expected.json create mode 100644 test/fixtures/typescript/arrow-function/optional-parameter/actual.js create mode 100644 test/fixtures/typescript/arrow-function/optional-parameter/expected.json create mode 100644 test/fixtures/typescript/arrow-function/predicate-types/actual.js create mode 100644 test/fixtures/typescript/arrow-function/predicate-types/expected.json create mode 100644 test/fixtures/typescript/cast/as/actual.js create mode 100644 test/fixtures/typescript/cast/as/expected.json create mode 100644 test/fixtures/typescript/cast/false-positive/actual.js create mode 100644 test/fixtures/typescript/cast/false-positive/expected.json create mode 100644 test/fixtures/typescript/cast/need-parentheses/actual.js create mode 100644 test/fixtures/typescript/cast/need-parentheses/expected.json create mode 100644 test/fixtures/typescript/cast/null-assertion-then-property-access/actual.js create mode 100644 test/fixtures/typescript/cast/null-assertion-then-property-access/expected.json create mode 100644 test/fixtures/typescript/cast/null-assertion/actual.js create mode 100644 test/fixtures/typescript/cast/null-assertion/expected.json create mode 100644 test/fixtures/typescript/cast/type-assertion-after-operator/actual.js create mode 100644 test/fixtures/typescript/cast/type-assertion-after-operator/expected.json create mode 100644 test/fixtures/typescript/cast/type-assertion-before-operator/actual.js create mode 100644 test/fixtures/typescript/cast/type-assertion-before-operator/expected.json create mode 100644 test/fixtures/typescript/cast/type-assertion/actual.js create mode 100644 test/fixtures/typescript/cast/type-assertion/expected.json create mode 100644 test/fixtures/typescript/class/abstract/actual.js create mode 100644 test/fixtures/typescript/class/abstract/expected.json create mode 100644 test/fixtures/typescript/class/constructor/actual.js create mode 100644 test/fixtures/typescript/class/constructor/expected.json create mode 100644 test/fixtures/typescript/class/declare/actual.js create mode 100644 test/fixtures/typescript/class/declare/expected.json create mode 100644 test/fixtures/typescript/class/expression-extends-implements/actual.js create mode 100644 test/fixtures/typescript/class/expression-extends-implements/expected.json create mode 100644 test/fixtures/typescript/class/expression-extends/actual.js create mode 100644 test/fixtures/typescript/class/expression-extends/expected.json create mode 100644 test/fixtures/typescript/class/expression-generic/actual.js create mode 100644 test/fixtures/typescript/class/expression-generic/expected.json create mode 100644 test/fixtures/typescript/class/expression-implements/actual.js create mode 100644 test/fixtures/typescript/class/expression-implements/expected.json create mode 100644 test/fixtures/typescript/class/extends-implements/actual.js create mode 100644 test/fixtures/typescript/class/extends-implements/expected.json create mode 100644 test/fixtures/typescript/class/extends/actual.js create mode 100644 test/fixtures/typescript/class/extends/expected.json create mode 100644 test/fixtures/typescript/class/generic/actual.js create mode 100644 test/fixtures/typescript/class/generic/expected.json create mode 100644 test/fixtures/typescript/class/get-generic/actual.js create mode 100644 test/fixtures/typescript/class/get-generic/expected.json create mode 100644 test/fixtures/typescript/class/implements/actual.js create mode 100644 test/fixtures/typescript/class/implements/expected.json create mode 100644 test/fixtures/typescript/class/index-signature/actual.js create mode 100644 test/fixtures/typescript/class/index-signature/expected.json create mode 100644 test/fixtures/typescript/class/members-with-modifier-names/actual.js create mode 100644 test/fixtures/typescript/class/members-with-modifier-names/expected.json create mode 100644 test/fixtures/typescript/class/members-with-reserved-names/actual.js create mode 100644 test/fixtures/typescript/class/members-with-reserved-names/expected.json create mode 100644 test/fixtures/typescript/class/method-computed/actual.js create mode 100644 test/fixtures/typescript/class/method-computed/expected.json create mode 100644 test/fixtures/typescript/class/method-generic/actual.js create mode 100644 test/fixtures/typescript/class/method-generic/expected.json create mode 100644 test/fixtures/typescript/class/method-no-body/actual.js create mode 100644 test/fixtures/typescript/class/method-no-body/expected.json create mode 100644 test/fixtures/typescript/class/method-optional/actual.js create mode 100644 test/fixtures/typescript/class/method-optional/expected.json create mode 100644 test/fixtures/typescript/class/method-return-type/actual.js create mode 100644 test/fixtures/typescript/class/method-return-type/expected.json create mode 100644 test/fixtures/typescript/class/method-with-newline-with-body/actual.js create mode 100644 test/fixtures/typescript/class/method-with-newline-with-body/expected.json create mode 100644 test/fixtures/typescript/class/method-with-newline-without-body/actual.js create mode 100644 test/fixtures/typescript/class/method-with-newline-without-body/expected.json create mode 100644 test/fixtures/typescript/class/modifiers-accessors/actual.js create mode 100644 test/fixtures/typescript/class/modifiers-accessors/expected.json create mode 100644 test/fixtures/typescript/class/modifiers-methods-async/actual.js create mode 100644 test/fixtures/typescript/class/modifiers-methods-async/expected.json create mode 100644 test/fixtures/typescript/class/modifiers-properties/actual.js create mode 100644 test/fixtures/typescript/class/modifiers-properties/expected.json create mode 100644 test/fixtures/typescript/class/parameter-properties/actual.js create mode 100644 test/fixtures/typescript/class/parameter-properties/expected.json create mode 100644 test/fixtures/typescript/class/properties/actual.js create mode 100644 test/fixtures/typescript/class/properties/expected.json create mode 100644 test/fixtures/typescript/class/property-computed/actual.js create mode 100644 test/fixtures/typescript/class/property-computed/expected.json create mode 100644 test/fixtures/typescript/class/static/actual.js create mode 100644 test/fixtures/typescript/class/static/expected.json create mode 100644 test/fixtures/typescript/const/no-initializer/actual.js create mode 100644 test/fixtures/typescript/const/no-initializer/expected.json create mode 100644 test/fixtures/typescript/declare/const/actual.js create mode 100644 test/fixtures/typescript/declare/const/expected.json create mode 100644 test/fixtures/typescript/declare/destructure/actual.js create mode 100644 test/fixtures/typescript/declare/destructure/expected.json create mode 100644 test/fixtures/typescript/declare/interface/actual.js create mode 100644 test/fixtures/typescript/declare/interface/expected.json create mode 100644 test/fixtures/typescript/declare/let/actual.js create mode 100644 test/fixtures/typescript/declare/let/expected.json create mode 100644 test/fixtures/typescript/declare/var/actual.js create mode 100644 test/fixtures/typescript/declare/var/expected.json create mode 100644 test/fixtures/typescript/enum/const/actual.js create mode 100644 test/fixtures/typescript/enum/const/expected.json create mode 100644 test/fixtures/typescript/enum/declare-const/actual.js create mode 100644 test/fixtures/typescript/enum/declare-const/expected.json create mode 100644 test/fixtures/typescript/enum/declare/actual.js create mode 100644 test/fixtures/typescript/enum/declare/expected.json create mode 100644 test/fixtures/typescript/enum/export-const/actual.js create mode 100644 test/fixtures/typescript/enum/export-const/expected.json create mode 100644 test/fixtures/typescript/enum/export-declare-const/actual.js create mode 100644 test/fixtures/typescript/enum/export-declare-const/expected.json create mode 100644 test/fixtures/typescript/enum/export/actual.js create mode 100644 test/fixtures/typescript/enum/export/expected.json create mode 100644 test/fixtures/typescript/enum/members-reserved-words/actual.js create mode 100644 test/fixtures/typescript/enum/members-reserved-words/expected.json create mode 100644 test/fixtures/typescript/enum/members-strings/actual.js create mode 100644 test/fixtures/typescript/enum/members-strings/expected.json create mode 100644 test/fixtures/typescript/enum/members-trailing-comma-with-initializer/actual.js create mode 100644 test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json create mode 100644 test/fixtures/typescript/enum/members-trailing-comma/actual.js create mode 100644 test/fixtures/typescript/enum/members-trailing-comma/expected.json create mode 100644 test/fixtures/typescript/enum/members/actual.js create mode 100644 test/fixtures/typescript/enum/members/expected.json create mode 100644 test/fixtures/typescript/export/as-namespace/actual.js create mode 100644 test/fixtures/typescript/export/as-namespace/expected.json create mode 100644 test/fixtures/typescript/export/declare/actual.js create mode 100644 test/fixtures/typescript/export/declare/expected.json create mode 100644 test/fixtures/typescript/export/equals/actual.js create mode 100644 test/fixtures/typescript/export/equals/expected.json create mode 100644 test/fixtures/typescript/function/annotated/actual.js create mode 100644 test/fixtures/typescript/function/annotated/expected.json create mode 100644 test/fixtures/typescript/function/anonymous/actual.js create mode 100644 test/fixtures/typescript/function/anonymous/expected.json create mode 100644 test/fixtures/typescript/function/declare/actual.js create mode 100644 test/fixtures/typescript/function/declare/expected.json create mode 100644 test/fixtures/typescript/function/overloads/actual.js create mode 100644 test/fixtures/typescript/function/overloads/expected.json create mode 100644 test/fixtures/typescript/function/predicate-types/actual.js create mode 100644 test/fixtures/typescript/function/predicate-types/expected.json create mode 100644 test/fixtures/typescript/import/equals-require/actual.js create mode 100644 test/fixtures/typescript/import/equals-require/expected.json create mode 100644 test/fixtures/typescript/import/equals/actual.js create mode 100644 test/fixtures/typescript/import/equals/expected.json create mode 100644 test/fixtures/typescript/import/export-import-require/actual.js create mode 100644 test/fixtures/typescript/import/export-import-require/expected.json create mode 100644 test/fixtures/typescript/import/export-import/actual.js create mode 100644 test/fixtures/typescript/import/export-import/expected.json create mode 100644 test/fixtures/typescript/import/not-top-level/actual.js create mode 100644 test/fixtures/typescript/import/not-top-level/expected.json create mode 100644 test/fixtures/typescript/interface/call-signature/actual.js create mode 100644 test/fixtures/typescript/interface/call-signature/expected.json create mode 100644 test/fixtures/typescript/interface/construct-signature/actual.js create mode 100644 test/fixtures/typescript/interface/construct-signature/expected.json create mode 100644 test/fixtures/typescript/interface/export/actual.js create mode 100644 test/fixtures/typescript/interface/export/expected.json create mode 100644 test/fixtures/typescript/interface/extends/actual.js create mode 100644 test/fixtures/typescript/interface/extends/expected.json create mode 100644 test/fixtures/typescript/interface/generic/actual.js create mode 100644 test/fixtures/typescript/interface/generic/expected.json create mode 100644 test/fixtures/typescript/interface/index-accessor/actual.js create mode 100644 test/fixtures/typescript/interface/index-accessor/expected.json create mode 100644 test/fixtures/typescript/interface/method-computed/actual.js create mode 100644 test/fixtures/typescript/interface/method-computed/expected.json create mode 100644 test/fixtures/typescript/interface/method-generic/actual.js create mode 100644 test/fixtures/typescript/interface/method-generic/expected.json create mode 100644 test/fixtures/typescript/interface/method-optional/actual.js create mode 100644 test/fixtures/typescript/interface/method-optional/expected.json create mode 100644 test/fixtures/typescript/interface/method-plain/actual.js create mode 100644 test/fixtures/typescript/interface/method-plain/expected.json create mode 100644 test/fixtures/typescript/interface/modifiers/actual.js create mode 100644 test/fixtures/typescript/interface/modifiers/expected.json create mode 100644 test/fixtures/typescript/interface/properties/actual.js create mode 100644 test/fixtures/typescript/interface/properties/expected.json create mode 100644 test/fixtures/typescript/interface/property-computed/actual.js create mode 100644 test/fixtures/typescript/interface/property-computed/expected.json create mode 100644 test/fixtures/typescript/interface/property-named-public/actual.js create mode 100644 test/fixtures/typescript/interface/property-named-public/expected.json create mode 100644 test/fixtures/typescript/interface/reserved-method-name/actual.js create mode 100644 test/fixtures/typescript/interface/reserved-method-name/expected.json create mode 100644 test/fixtures/typescript/interface/separators/actual.js create mode 100644 test/fixtures/typescript/interface/separators/expected.json create mode 100644 test/fixtures/typescript/module-namespace/body-declare/actual.js create mode 100644 test/fixtures/typescript/module-namespace/body-declare/expected.json create mode 100644 test/fixtures/typescript/module-namespace/body-nested-declare/actual.js create mode 100644 test/fixtures/typescript/module-namespace/body-nested-declare/expected.json create mode 100644 test/fixtures/typescript/module-namespace/body-nested/actual.js create mode 100644 test/fixtures/typescript/module-namespace/body-nested/expected.json create mode 100644 test/fixtures/typescript/module-namespace/body/actual.js create mode 100644 test/fixtures/typescript/module-namespace/body/expected.json create mode 100644 test/fixtures/typescript/module-namespace/declare-shorthand/actual.js create mode 100644 test/fixtures/typescript/module-namespace/declare-shorthand/expected.json create mode 100644 test/fixtures/typescript/module-namespace/global-in-module/actual.js create mode 100644 test/fixtures/typescript/module-namespace/global-in-module/expected.json create mode 100644 test/fixtures/typescript/module-namespace/head-declare/actual.js create mode 100644 test/fixtures/typescript/module-namespace/head-declare/expected.json create mode 100644 test/fixtures/typescript/module-namespace/head-export/actual.js create mode 100644 test/fixtures/typescript/module-namespace/head-export/expected.json create mode 100644 test/fixtures/typescript/module-namespace/head/actual.js create mode 100644 test/fixtures/typescript/module-namespace/head/expected.json create mode 100644 test/fixtures/typescript/options.json create mode 100644 test/fixtures/typescript/tsx/brace-is-block/actual.js create mode 100644 test/fixtures/typescript/tsx/brace-is-block/expected.json create mode 100644 test/fixtures/typescript/tsx/options.json create mode 100644 test/fixtures/typescript/type-alias/declare/actual.js create mode 100644 test/fixtures/typescript/type-alias/declare/expected.json create mode 100644 test/fixtures/typescript/type-alias/export/actual.js create mode 100644 test/fixtures/typescript/type-alias/export/expected.json create mode 100644 test/fixtures/typescript/type-alias/generic-complex/actual.js create mode 100644 test/fixtures/typescript/type-alias/generic-complex/expected.json create mode 100644 test/fixtures/typescript/type-alias/generic/actual.js create mode 100644 test/fixtures/typescript/type-alias/generic/expected.json create mode 100644 test/fixtures/typescript/type-alias/plain/actual.js create mode 100644 test/fixtures/typescript/type-alias/plain/expected.json create mode 100644 test/fixtures/typescript/type-arguments/call/actual.js create mode 100644 test/fixtures/typescript/type-arguments/call/expected.json create mode 100644 test/fixtures/typescript/type-arguments/new/actual.js create mode 100644 test/fixtures/typescript/type-arguments/new/expected.json create mode 100644 test/fixtures/typescript/types/array/actual.js create mode 100644 test/fixtures/typescript/types/array/expected.json create mode 100644 test/fixtures/typescript/types/function-generic/actual.js create mode 100644 test/fixtures/typescript/types/function-generic/expected.json create mode 100644 test/fixtures/typescript/types/function-in-generic/actual.js create mode 100644 test/fixtures/typescript/types/function-in-generic/expected.json create mode 100644 test/fixtures/typescript/types/function-with-this/actual.js create mode 100644 test/fixtures/typescript/types/function-with-this/expected.json create mode 100644 test/fixtures/typescript/types/function/actual.js create mode 100644 test/fixtures/typescript/types/function/expected.json create mode 100644 test/fixtures/typescript/types/indexed/actual.js create mode 100644 test/fixtures/typescript/types/indexed/expected.json create mode 100644 test/fixtures/typescript/types/keywords/actual.js create mode 100644 test/fixtures/typescript/types/keywords/expected.json create mode 100644 test/fixtures/typescript/types/literal-boolean/actual.js create mode 100644 test/fixtures/typescript/types/literal-boolean/expected.json create mode 100644 test/fixtures/typescript/types/literal-number-negative/actual.js create mode 100644 test/fixtures/typescript/types/literal-number-negative/expected.json create mode 100644 test/fixtures/typescript/types/literal-number/actual.js create mode 100644 test/fixtures/typescript/types/literal-number/expected.json create mode 100644 test/fixtures/typescript/types/literal-string/actual.js create mode 100644 test/fixtures/typescript/types/literal-string/expected.json create mode 100644 test/fixtures/typescript/types/mapped/actual.js create mode 100644 test/fixtures/typescript/types/mapped/expected.json create mode 100644 test/fixtures/typescript/types/parenthesized/actual.js create mode 100644 test/fixtures/typescript/types/parenthesized/expected.json create mode 100644 test/fixtures/typescript/types/reference-generic-nested/actual.js create mode 100644 test/fixtures/typescript/types/reference-generic-nested/expected.json create mode 100644 test/fixtures/typescript/types/reference-generic/actual.js create mode 100644 test/fixtures/typescript/types/reference-generic/expected.json create mode 100644 test/fixtures/typescript/types/reference/actual.js create mode 100644 test/fixtures/typescript/types/reference/expected.json create mode 100644 test/fixtures/typescript/types/type-literal/actual.js create mode 100644 test/fixtures/typescript/types/type-literal/expected.json create mode 100644 test/fixtures/typescript/types/type-operator/actual.js create mode 100644 test/fixtures/typescript/types/type-operator/expected.json create mode 100644 test/fixtures/typescript/types/typeof/actual.js create mode 100644 test/fixtures/typescript/types/typeof/expected.json create mode 100644 test/fixtures/typescript/types/union-intersection/actual.js create mode 100644 test/fixtures/typescript/types/union-intersection/expected.json diff --git a/src/index.js b/src/index.js index 09a5c40cbe..7d8e99bf3e 100755 --- a/src/index.js +++ b/src/index.js @@ -19,9 +19,11 @@ import type { Expression, File } from "./types"; import estreePlugin from "./plugins/estree"; import flowPlugin from "./plugins/flow"; import jsxPlugin from "./plugins/jsx"; +import typescriptPlugin from "./plugins/typescript"; plugins.estree = estreePlugin; plugins.flow = flowPlugin; plugins.jsx = jsxPlugin; +plugins.typescript = typescriptPlugin; export function parse(input: string, options?: Options): File { return getParser(options, input).parse(); @@ -48,7 +50,8 @@ const parserClassCache: { [key: string]: Class } = {}; /** Get a Parser class with plugins applied. */ function getParserClass(pluginsFromOptions: $ReadOnlyArray): Class { // Filter out just the plugins that have an actual mixin associated with them. - let pluginList = pluginsFromOptions.filter((p) => p === "estree" || p === "flow" || p === "jsx"); + let pluginList = pluginsFromOptions.filter((p) => + p === "estree" || p === "flow" || p === "jsx" || p === "typescript"); if (pluginList.indexOf("flow") >= 0) { // ensure flow plugin loads last @@ -56,6 +59,16 @@ function getParserClass(pluginsFromOptions: $ReadOnlyArray): Class= 0 && pluginList.indexOf("typescript") >= 0) { + throw new Error("Cannot combine flow and typescript plugins."); + } + + if (pluginList.indexOf("typescript") >= 0) { + // ensure typescript plugin loads last + pluginList = pluginList.filter((plugin) => plugin !== "typescript"); + pluginList.push("typescript"); + } + if (pluginList.indexOf("estree") >= 0) { // ensure estree plugin loads first pluginList = pluginList.filter((plugin) => plugin !== "estree"); diff --git a/src/parser/expression.js b/src/parser/expression.js index f11180ac42..74d0ac44d9 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -34,6 +34,17 @@ export default class ExpressionParser extends LValParser { +parseFunction: (node: T, isStatement: boolean, allowExpressionBody?: boolean, isAsync?: boolean, optionalId?: boolean) => T; +takeDecorators: (node: N.HasDecorators) => void; + // Forward-declarations: defined in typescript.js + +tsParseType: () => N.TsType; + +tsTryParseCallExpressionWithTypeArguments: ( + base: N.Expression, + startPos: number, + startLoc: Position) => { newBase: N.Expression, shouldContinue: boolean }; + +tsTypeArgumentsFromAmbiguous: (types: $ReadOnlyArray) => $ReadOnlyArray; + +tsTypeParametersFromAmbiguous: ( + types: $ReadOnlyArray) => $ReadOnlyArray; + +tsParseTypeArguments: () => $ReadOnlyArray; + // Check if property name clashes with already added. // Object/class getters and setters are not allowed to clash — // either with each other or with an init property — and in @@ -193,15 +204,20 @@ export default class ExpressionParser extends LValParser { // operator that has a lower precedence than the set it is parsing. parseExprOp(left: N.Expression, leftStartPos: number, leftStartLoc: Position, minPrec: number, noIn: ?boolean): N.Expression { - const prec = this.state.type.binop; + let prec = this.state.type.binop; + let isAs = false; + if (this.hasPlugin("typescript") && prec == null && this.isContextual("as") && !this.hasPrecedingLineBreak()) { + prec = tt._in.binop; + isAs = true; + } + if (prec != null && (!noIn || !this.match(tt._in))) { if (prec > minPrec) { const node = this.startNodeAt(leftStartPos, leftStartLoc); - node.left = left; - node.operator = this.state.value; + const operator = this.state.value; if ( - node.operator === "**" && + operator === "**" && left.type === "UnaryExpression" && left.extra && !left.extra.parenthesizedArgument && @@ -215,9 +231,17 @@ export default class ExpressionParser extends LValParser { const startPos = this.state.start; const startLoc = this.state.startLoc; - node.right = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn); + if (isAs) { + node.expression = left; + node.typeAnnotation = this.tsParseType(); + this.finishNode(node, "TSAsExpression"); + } else { + node.left = left; + node.operator = operator; + node.right = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn); + this.finishNode(node, (op === tt.logicalOR || op === tt.logicalAND) ? "LogicalExpression" : "BinaryExpression"); + } - this.finishNode(node, (op === tt.logicalOR || op === tt.logicalAND) ? "LogicalExpression" : "BinaryExpression"); return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn); } } @@ -294,6 +318,10 @@ export default class ExpressionParser extends LValParser { node.object = base; node.callee = this.parseNoCallExpr(); return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls); + } else if (this.hasPlugin("typescript") && this.eat(tt.bang)) { + const nonNullExpression: N.TsNonNullExpression = this.startNodeAt(startPos, startLoc); + nonNullExpression.expression = base; + base = this.finishNode(nonNullExpression, "TSNonNullExpression"); } else if (this.eat(tt.dot)) { const node = this.startNodeAt(startPos, startLoc); node.object = base; @@ -307,22 +335,20 @@ export default class ExpressionParser extends LValParser { node.computed = true; this.expect(tt.bracketR); base = this.finishNode(node, "MemberExpression"); - } else if (!noCalls && this.match(tt.parenL)) { - const possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); - this.next(); - - const node = this.startNodeAt(startPos, startLoc); - node.callee = base; - node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); - if (node.callee.type === "Import" && node.arguments.length !== 1) { - this.raise(node.start, "import() requires exactly one argument"); + } else if (!noCalls && this.hasPlugin("typescript") && this.isRelational("<")) { + // May be a call `f()`. But may be a less-than expression. + const { newBase, shouldContinue } = this.tsTryParseCallExpressionWithTypeArguments(base, startPos, startLoc); + if (shouldContinue) { + base = newBase; + } else { + return newBase; } - base = this.finishNode(node, "CallExpression"); - - if (possibleAsync && this.shouldParseAsyncArrow()) { - return this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node); + } else if (!noCalls && this.match(tt.parenL)) { + const { newBase, shouldContinue } = this.parseCallExpression(base, startPos, startLoc); + if (shouldContinue) { + base = newBase; } else { - this.toReferencedList(node.arguments); + return newBase; } } else if (this.match(tt.backQuote)) { const node = this.startNodeAt(startPos, startLoc); @@ -337,6 +363,42 @@ export default class ExpressionParser extends LValParser { throw new Error("Unreachable"); } + // In TypeScript, when we see `f(`, it is usually a call expression. + // But in the case of `async(`, both `async();` (call expression) and + // `async () => { await foo() };` (generic async arrow function) are valid syntax. + // So, types must be ambiguous, as they could be either parameters or arguments. + parseCallExpression( + base: N.Expression, + startPos: number, + startLoc: Position, + typeAmbiguous?: ?$ReadOnlyArray): { newBase: N.Expression, shouldContinue: boolean } { + + const possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); + this.next(); + + const node = this.startNodeAt(startPos, startLoc); + node.callee = base; + node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); + if (node.callee.type === "Import" && node.arguments.length !== 1) { + this.raise(node.start, "import() requires exactly one argument"); + } + base = this.finishNode(node, "CallExpression"); + + if (possibleAsync && this.shouldParseAsyncArrow()) { + const arrow = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node, typeAmbiguous); + return { newBase: arrow, shouldContinue: false }; + } else { + if (typeAmbiguous) { + if (!this.hasPlugin("typescript")) { + throw new Error("typeAmbiguous should only be passed in typescript mode."); + } + node.typeArguments = this.tsTypeArgumentsFromAmbiguous(typeAmbiguous); + } + this.toReferencedList(node.arguments); + return { newBase: base, shouldContinue: true }; + } + } + parseCallExpressionArguments(close: TokenType, possibleAsyncArrow: boolean): $ReadOnlyArray { const elts = []; let innerParenStart; @@ -370,8 +432,11 @@ export default class ExpressionParser extends LValParser { return this.match(tt.arrow); } - parseAsyncArrowFromCallExpression(node: N.ArrowFunctionExpression, call: N.CallExpression): N.ArrowFunctionExpression { + parseAsyncArrowFromCallExpression(node: N.ArrowFunctionExpression, call: N.CallExpression, types: ?$ReadOnlyArray): N.ArrowFunctionExpression { this.expect(tt.arrow); + if (types) { + node.typeParameters = this.tsTypeParametersFromAmbiguous(types); + } return this.parseArrowExpression(node, call.arguments, true); } @@ -489,10 +554,7 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "NullLiteral"); case tt._true: case tt._false: - node = this.startNode(); - node.value = this.match(tt._true); - this.next(); - return this.finishNode(node, "BooleanLiteral"); + return this.parseBooleanLiteral(); case tt.parenL: return this.parseParenAndDistinguishExpression(null, null, canBeArrow); @@ -540,6 +602,13 @@ export default class ExpressionParser extends LValParser { } } + parseBooleanLiteral(): N.BooleanLiteral { + const node = this.startNode(); + node.value = this.match(tt._true); + this.next(); + return this.finishNode(node, "BooleanLiteral"); + } + parseFunctionExpression(): N.FunctionExpression | N.MetaProperty { const node = this.startNode(); const meta = this.parseIdentifier(true); @@ -665,7 +734,8 @@ export default class ExpressionParser extends LValParser { } } - parseParenItem(node: N.Expression): N.Expression { + // eslint-disable-next-line no-unused-vars + parseParenItem(node: N.Expression, startPos: number, startLoc: Position): N.Expression { return node; } @@ -689,6 +759,10 @@ export default class ExpressionParser extends LValParser { node.callee = this.parseNoCallExpr(); + if (this.hasPlugin("typescript") && this.isRelational("<")) { + node.typeArguments = this.tsParseTypeArguments(); + } + if (this.eat(tt.parenL)) { node.arguments = this.parseExprList(tt.parenR); this.toReferencedList(node.arguments); @@ -705,6 +779,7 @@ export default class ExpressionParser extends LValParser { const elem = this.startNode(); if (this.state.value === null) { if (!isTagged) { + // $FlowFixMe this.raise(this.state.invalidTemplateEscapePosition, "Invalid escape sequence in template"); } else { this.state.invalidTemplateEscapePosition = null; @@ -846,13 +921,13 @@ export default class ExpressionParser extends LValParser { this.match(tt.num) || // get 1() {} this.match(tt.bracketL) || // get ["string"]() {} this.match(tt.name) || // get foo() {} - this.state.type.keyword // get debugger() {} + !!this.state.type.keyword // get debugger() {} ); } // get methods aren't allowed to have any parameters // set methods must have exactly 1 parameter - checkGetterSetterParamCount(method: N.ObjectMethod): void { + checkGetterSetterParamCount(method: N.ObjectMethod | N.ClassMethod): void { const paramCount = method.kind === "get" ? 0 : 1; if (method.params.length !== paramCount) { const start = method.start; @@ -869,7 +944,7 @@ export default class ExpressionParser extends LValParser { if (isPattern) this.unexpected(); prop.kind = "method"; prop.method = true; - this.parseMethod(prop, isGenerator, isAsync); + this.parseMethod(prop, isGenerator, isAsync, /* isConstructor */ false); return this.finishNode(prop, "ObjectMethod"); } @@ -878,7 +953,7 @@ export default class ExpressionParser extends LValParser { if (isGenerator || isAsync) this.unexpected(); prop.kind = prop.key.name; this.parsePropertyName(prop); - this.parseMethod(prop); + this.parseMethod(prop, /* isGenerator */false, /* isAsync */ false, /* isConstructor */ false); this.checkGetterSetterParamCount(prop); return this.finishNode(prop, "ObjectMethod"); @@ -919,21 +994,30 @@ export default class ExpressionParser extends LValParser { this.parseObjectProperty(prop, startPos, startLoc, isPattern, refShorthandDefaultPos); if (!node) this.unexpected(); + + // $FlowFixMe + return node; + } + + parsePropertyName(prop: N.ObjectOrClassMember): N.Expression { + const { computed, key } = this.parsePropertyNameInfo(); + prop.computed = computed; + prop.key = key; + return key; } - parsePropertyName(prop: N.ObjectMember): N.Identifier { + parsePropertyNameInfo(): { computed: boolean; key: N.Expression } { if (this.eat(tt.bracketL)) { - prop.computed = true; - prop.key = this.parseMaybeAssign(); + const key = this.parseMaybeAssign(); this.expect(tt.bracketR); + return { computed: true, key }; } else { - prop.computed = false; const oldInPropertyName = this.state.inPropertyName; this.state.inPropertyName = true; - prop.key = (this.match(tt.num) || this.match(tt.string)) ? this.parseExprAtom() : this.parseIdentifier(true); + const key = (this.match(tt.num) || this.match(tt.string)) ? this.parseExprAtom() : this.parseIdentifier(true); this.state.inPropertyName = oldInPropertyName; + return { computed: false, key }; } - return prop.key; } // Initialize empty function node. @@ -947,14 +1031,15 @@ export default class ExpressionParser extends LValParser { // Parse object or class method. - parseMethod(node: N.MethodLike, isGenerator?: boolean, isAsync?: boolean): N.MethodLike { + parseMethod(node: N.MethodLike, isGenerator: boolean, isAsync: boolean, isConstructor: boolean): N.MethodLike { const oldInMethod = this.state.inMethod; this.state.inMethod = node.kind || true; this.initFunction(node, isAsync); this.expect(tt.parenL); - node.params = this.parseBindingList(tt.parenR); + const allowModifiers = isConstructor; // For TypeScript parameter properties + node.params = this.parseBindingList(tt.parenR, /* allowEmpty */ false, allowModifiers); node.generator = !!isGenerator; - this.parseFunctionBody(node); + this.maybeParseFunctionBody(node, /* allowExpression */ false); this.state.inMethod = oldInMethod; return node; } @@ -968,7 +1053,7 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "ArrowFunctionExpression"); } - isStrictBody(node: { body: N.BlockStatement }, isExpression?: boolean): boolean { + isStrictBody(node: { body: N.BlockStatement }, isExpression: ?boolean): boolean { if (!isExpression && node.body.directives.length) { for (const directive of node.body.directives) { if (directive.value.value === "use strict") { @@ -980,8 +1065,25 @@ export default class ExpressionParser extends LValParser { return false; } + maybeParseFunctionBody(node: N.Function, allowExpression?: boolean): void { + if (!allowExpression) { + // For arrow functions, `parseArrow` handles the return type itself. + this.parseFunctionReturnType(node); + } + + // In TypeScript, a body may be missing on a declaration. + // See `parseFunctionBlockOrSemicolon` in TS source. + if (!(this.hasPlugin("typescript") && !this.match(tt.braceL) && this.isLineTerminator())) { + this.parseFunctionBody(node, allowExpression); + } + } + + // TypeScript / Flow plugins will override this + // eslint-disable-next-line no-unused-vars + parseFunctionReturnType(node: N.Function): void {} + // Parse function body and check parameters. - parseFunctionBody(node: N.Function, allowExpression?: boolean): void { + parseFunctionBody(node: N.Function, allowExpression: ?boolean): void { const isExpression = allowExpression && !this.match(tt.braceL); const oldInAsync = this.state.inAsync; @@ -1036,7 +1138,7 @@ export default class ExpressionParser extends LValParser { // nothing in between them to be parsed as `null` (which is needed // for array literals). - parseExprList(close: TokenType, allowEmpty?: boolean, refShorthandDefaultPos?: Pos): $ReadOnlyArray { + parseExprList(close: TokenType, allowEmpty?: boolean, refShorthandDefaultPos?: ?Pos): $ReadOnlyArray { const elts = []; let first = true; @@ -1094,6 +1196,13 @@ export default class ExpressionParser extends LValParser { } checkReservedWord(word: string, startLoc: number, checkKeywords: boolean, isBinding: boolean): void { + if (this.hasPlugin("typescript")) { + // Don't bother checking for TypeScript code. + // Strict mode words may be allowed as in `declare namespace N { const static: number; }`. + // And we have a type checker anyway, so don't bother having the parser do it. + return; + } + if (this.isReservedWord(word) || (checkKeywords && this.isKeyword(word))) { this.raise(startLoc, word + " is a reserved word"); } diff --git a/src/parser/lval.js b/src/parser/lval.js index 8576198b61..e896296e43 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -1,8 +1,8 @@ // @flow import { types as tt, type TokenType } from "../tokenizer/types"; -import type { Decorator, Expression, Identifier, Node, ObjectExpression, ObjectPattern, Pattern, RestElement, - SpreadElement } from "../types"; +import type { Accessibility, Decorator, Expression, Identifier, Node, ObjectExpression, ObjectPattern, + Pattern, RestElement, SpreadElement } from "../types"; import type { Pos, Position } from "../util/location"; import { NodeUtils } from "./node"; @@ -161,12 +161,22 @@ export default class LValParser extends NodeUtils { case tt.braceL: return this.parseObj(true); + case tt._this: + if (this.hasPlugin("typescript")) { + // "this" may be the name of a parameter, so allow it. + return this.parseIdentifier(/* liberal */ true); + } + default: throw this.unexpected(); } } - parseBindingList(close: TokenType, allowEmpty?: boolean): $ReadOnlyArray { + parseBindingList( + close: TokenType, + allowEmpty?: boolean, + allowModifiers?: boolean + ): $ReadOnlyArray { const elts = []; let first = true; while (!this.eat(close)) { @@ -189,17 +199,26 @@ export default class LValParser extends NodeUtils { while (this.match(tt.at)) { decorators.push(this.parseDecorator()); } + const { access, readonly } = allowModifiers ? this.parseParameterModifiers() : {}; const left = this.parseMaybeDefault(); if (decorators.length) { left.decorators = decorators; } this.parseAssignableListItemTypes(left); - elts.push(this.parseMaybeDefault(left.start, left.loc.start, left)); + const elt = this.parseMaybeDefault(left.start, left.loc.start, left); + if (access) elt.access = access; + if (readonly) elt.readonly = readonly; + elts.push(elt); } } return elts; } + // Overridden in typescript.js + parseParameterModifiers(): { access: ?Accessibility, readonly: boolean } { + return { access: undefined, readonly: false }; + } + parseAssignableListItemTypes(param: Pattern): Pattern { return param; } diff --git a/src/parser/statement.js b/src/parser/statement.js index 153b614ce0..98539a4fe7 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -3,10 +3,10 @@ // @flow import * as N from "../types"; -import { types as tt, TokenType } from "../tokenizer/types"; -import ExpressionParser from "./expression"; +import { types as tt, type TokenType } from "../tokenizer/types"; import type { Position } from "../util/location"; import { lineBreak } from "../util/whitespace"; +import ExpressionParser from "./expression"; // Reused empty array added for node fields that are always empty. @@ -15,6 +15,12 @@ const empty = []; const loopLabel = { kind: "loop" }, switchLabel = { kind: "switch" }; export default class StatementParser extends ExpressionParser { + // Forward-declarations: Code defined in typescript.js + +tsParseEnumDeclaration: (node: N.TsEnumDeclaration, isConst: boolean) => N.TsEnumDeclaration; + +tsTryParseIndexSignature: (node: N.TsIndexSignatureDeclaration) => ?N.TsIndexSignatureDeclaration + +tsParseImportEqualsDeclaration: ( + node: N.TsImportEqualsDeclaration, + isExport?: boolean) => N.TsImportEqualsDeclaration // ### Statement parsing @@ -74,6 +80,7 @@ export default class StatementParser extends ExpressionParser { // complexity. switch (starttype) { + // $FlowFixMe case tt._break: case tt._continue: return this.parseBreakContinueStatement(node, starttype.keyword); case tt._debugger: return this.parseDebuggerStatement(node); case tt._do: return this.parseDoStatement(node); @@ -92,8 +99,19 @@ export default class StatementParser extends ExpressionParser { case tt._throw: return this.parseThrowStatement(node); case tt._try: return this.parseTryStatement(node); - case tt._let: case tt._const: + // Handle `const enum` + if (this.hasPlugin("typescript")) { + const ahead = this.lookahead(); + if (ahead.type === tt.name && ahead.value === "enum") { + this.expect(tt._const); + this.expectContextual("enum"); + return this.tsParseEnumDeclaration(node, /* isConst */ true); + } + } + // fall through + + case tt._let: if (!declaration) this.unexpected(); // NOTE: falls through to _var case tt._var: @@ -249,7 +267,7 @@ export default class StatementParser extends ExpressionParser { return this.parseFor(node, null); } - if (this.match(tt._var) || this.match(tt._let) || this.match(tt._const)) { + if (this.matchVarLetOrConst()) { const init = this.startNode(); const varKind = this.state.type; this.next(); @@ -474,9 +492,13 @@ export default class StatementParser extends ExpressionParser { } parseBlockBody(node: N.BlockStatementLike, allowDirectives: ?boolean, topLevel: boolean, end: TokenType): void { - const body = node.body = []; - const directives = node.directives = []; + node.body = []; + node.directives = []; + this.parseBlockOrModuleBlockBody(node.body, allowDirectives ? node.directives : undefined, topLevel, end); + } + // Undefined directives means that directives are not allowed. + parseBlockOrModuleBlockBody(body: N.Statement[], directives: ?N.Directive[], topLevel: boolean, end: TokenType): void { let parsedNonDirective = false; let oldStrict; let octalPosition; @@ -488,7 +510,7 @@ export default class StatementParser extends ExpressionParser { const stmt = this.parseStatement(true, topLevel); - if (allowDirectives && !parsedNonDirective && this.isValidDirective(stmt)) { + if (directives && !parsedNonDirective && this.isValidDirective(stmt)) { const directive = this.stmtToDirective(stmt); directives.push(directive); @@ -554,6 +576,7 @@ export default class StatementParser extends ExpressionParser { parseVar(node: N.VariableDeclaration, isFor: boolean, kind: TokenType): N.VariableDeclaration { const declarations = node.declarations = []; + // $FlowFixMe node.kind = kind.keyword; for (;;) { const decl = this.startNode(); @@ -561,7 +584,10 @@ export default class StatementParser extends ExpressionParser { if (this.eat(tt.eq)) { decl.init = this.parseMaybeAssign(isFor); } else if (kind === tt._const && !(this.match(tt._in) || this.isContextual("of"))) { - this.unexpected(); + // `const` with no initializer is allowed in TypeScript. It could be a declaration `const x: number;`. + if (!this.hasPlugin("typescript")) { + this.unexpected(); + } } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(tt._in) || this.isContextual("of")))) { this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value"); } else { @@ -605,7 +631,7 @@ export default class StatementParser extends ExpressionParser { } this.parseFunctionParams(node); - this.parseFunctionBody(node, allowExpressionBody); + this.maybeParseFunctionBody(node, allowExpressionBody); this.state.inMethod = oldInMethod; @@ -620,7 +646,7 @@ export default class StatementParser extends ExpressionParser { // Parse a class declaration or literal (depending on the // `isStatement` parameter). - parseClass(node: N.Class, isStatement: boolean, optionalId?: boolean): N.Class { + parseClass(node: T, isStatement: /* T === ClassDeclaration */boolean, optionalId?: boolean): T { this.next(); this.takeDecorators(node); this.parseClassId(node, isStatement, optionalId); @@ -649,9 +675,9 @@ export default class StatementParser extends ExpressionParser { const oldStrict = this.state.strict; this.state.strict = true; - let hadConstructor = false; - let decorators = []; - const classBody = this.startNode(); + const hadConstructor = [false]; + let decorators: N.Decorator[] = []; + const classBody: N.ClassBody = this.startNode(); classBody.body = []; @@ -678,114 +704,170 @@ export default class StatementParser extends ExpressionParser { decorators = []; } - method.static = false; - if (this.match(tt.name) && this.state.value === "static") { - const key = this.parseIdentifier(true); // eats 'static' - if (this.isClassMethod()) { - // a method named 'static' - method.kind = "method"; - method.computed = false; - method.key = key; - this.parseClassMethod(classBody, method, false, false); - continue; - } else if (this.isClassProperty()) { - // a property named 'static' - method.computed = false; - method.key = key; - classBody.body.push(this.parseClassProperty(method)); - continue; - } - // otherwise something static - method.static = true; - } + this.parseClassMember(classBody, method, hadConstructor); + } + + if (decorators.length) { + this.raise(this.state.start, "You have trailing decorators with no method"); + } - if (this.eat(tt.star)) { - // a generator + node.body = this.finishNode(classBody, "ClassBody"); + + this.state.strict = oldStrict; + } + + parseClassMember(classBody: N.ClassBody, member: N.ClassMember, hadConstructor: [boolean]): void { + // Get as much type-checking as we can... + const memberAny: any = member; + const methodOrProp: N.ClassMethod | N.ClassProperty = memberAny; + const method: N.ClassMethod = memberAny; + const prop: N.ClassProperty = memberAny; + + const access = this.parseAccessModifier(); + if (access) methodOrProp.access = access; + + let isStatic = false; + if (this.match(tt.name) && this.state.value === "static") { + const key = this.parseIdentifier(true); // eats 'static' + if (this.isClassMethod()) { + // a method named 'static' + method.static = false; method.kind = "method"; - this.parsePropertyName(method); - if (this.isNonstaticConstructor(method)) { - this.raise(method.key.start, "Constructor can't be a generator"); - } - if (!method.computed && method.static && (method.key.name === "prototype" || method.key.value === "prototype")) { - this.raise(method.key.start, "Classes may not have static property named prototype"); - } - this.parseClassMethod(classBody, method, true, false); + method.computed = false; + method.key = key; + this.parseClassMethod(classBody, method, false, false, /* isConstructor */ false); + return; + } else if (this.isClassProperty()) { + // a property named 'static' + prop.static = false; + prop.computed = false; + prop.key = key; + classBody.body.push(this.parseClassProperty(prop)); + return; + } + // otherwise something static + isStatic = true; + } + + const { abstract, readonly } = this.parseClassMemberModifiers(); + if (readonly) prop.readonly = true; + if (abstract) methodOrProp.abstract = true; + + if (this.hasPlugin("typescript") && !abstract && !isStatic && !access) { + const idx = this.tsTryParseIndexSignature(memberAny); + if (idx) { + classBody.body.push(idx); + return; + } + } + + methodOrProp.static = isStatic; + + if (!readonly && this.eat(tt.star)) { + // a generator + method.kind = "method"; + this.parsePropertyName(method); + if (this.isNonstaticConstructor(method)) { + this.raise(method.key.start, "Constructor can't be a generator"); + } + if (!method.computed && method.static && (method.key.name === "prototype" || method.key.value === "prototype")) { + this.raise(method.key.start, "Classes may not have static property named prototype"); + } + this.parseClassMethod(classBody, method, true, false, /* isConstructor */ false); + return; + } + + const isSimple = this.match(tt.name); + const key = this.parsePropertyName(method); + if (!method.computed && method.static && (method.key.name === "prototype" || method.key.value === "prototype")) { + this.raise(method.key.start, "Classes may not have static property named prototype"); + } + + if (this.hasPlugin("typescript")) { + const optional = this.eat(tt.question); + if (optional) method.optional = true; + } + + if (!readonly && this.isClassMethod()) { + // a normal method + const isConstructor = this.isNonstaticConstructor(method); + if (isConstructor) { + method.kind = "constructor"; } else { - const isSimple = this.match(tt.name); - const key = this.parsePropertyName(method); - if (!method.computed && method.static && (method.key.name === "prototype" || method.key.value === "prototype")) { - this.raise(method.key.start, "Classes may not have static property named prototype"); + method.kind = "method"; + } + + if (isConstructor && !this.hasPlugin("typescript")) { + if (method.decorators) { + this.raise(method.start, "You can't attach decorators to a class constructor"); } - if (this.isClassMethod()) { - // a normal method - if (this.isNonstaticConstructor(method)) { - if (hadConstructor) { - this.raise(key.start, "Duplicate constructor in the same class"); - } else if (method.decorators) { - this.raise(method.start, "You can't attach decorators to a class constructor"); - } - hadConstructor = true; - method.kind = "constructor"; - } else { - method.kind = "method"; - } - this.parseClassMethod(classBody, method, false, false); - } else if (this.isClassProperty()) { - // a normal property - if (this.isNonstaticConstructor(method)) { - this.raise(method.key.start, "Classes may not have a non-static field named 'constructor'"); - } - classBody.body.push(this.parseClassProperty(method)); - } else if (isSimple && key.name === "async" && !this.isLineTerminator()) { - // an async method - const isGenerator = this.hasPlugin("asyncGenerators") && this.eat(tt.star); - method.kind = "method"; - this.parsePropertyName(method); - if (this.isNonstaticConstructor(method)) { - this.raise(method.key.start, "Constructor can't be an async function"); - } - this.parseClassMethod(classBody, method, isGenerator, true); - } else if (isSimple && (key.name === "get" || key.name === "set") && !(this.isLineTerminator() && this.match(tt.star))) { // `get\n*` is an uninitialized property named 'get' followed by a generator. - // a getter or setter - method.kind = key.name; - this.parsePropertyName(method); - if (this.isNonstaticConstructor(method)) { - this.raise(method.key.start, "Constructor can't have get/set modifier"); - } - this.parseClassMethod(classBody, method, false, false); - this.checkGetterSetterParamCount(method); - } else if (this.isLineTerminator()) { - // an uninitialized class property (due to ASI, since we don't otherwise recognize the next token) - if (this.isNonstaticConstructor(method)) { - this.raise(method.key.start, "Classes may not have a non-static field named 'constructor'"); + + // TypeScript allows multiple overloaded constructor declarations. + if (!this.hasPlugin("typescript")) { + if (hadConstructor[0]) { + this.raise(key.start, "Duplicate constructor in the same class"); } - classBody.body.push(this.parseClassProperty(method)); - } else { - this.unexpected(); + hadConstructor[0] = true; } } - } - if (decorators.length) { - this.raise(this.state.start, "You have trailing decorators with no method"); + this.parseClassMethod(classBody, method, false, false, isConstructor); + } else if (readonly || this.isClassProperty()) { + // a normal property + if (this.isNonstaticConstructor(method)) { + this.raise(method.key.start, "Classes may not have a non-static field named 'constructor'"); + } + classBody.body.push(this.parseClassProperty(prop)); + } else if (isSimple && key.name === "async" && !this.isLineTerminator()) { + // an async method + const isGenerator = this.hasPlugin("asyncGenerators") && this.eat(tt.star); + method.kind = "method"; + this.parsePropertyName(method); + if (this.isNonstaticConstructor(method)) { + this.raise(method.key.start, "Constructor can't be an async function"); + } + this.parseClassMethod(classBody, method, isGenerator, true, /* isConstructor */ false); + } else if (isSimple && (key.name === "get" || key.name === "set") && !(this.isLineTerminator() && this.match(tt.star))) { // `get\n*` is an uninitialized property named 'get' followed by a generator. + // a getter or setter + method.kind = key.name; + this.parsePropertyName(method); + if (this.isNonstaticConstructor(method)) { + this.raise(method.key.start, "Constructor can't have get/set modifier"); + } + this.parseClassMethod(classBody, method, false, false, /* isConstructor */ false); + this.checkGetterSetterParamCount(method); + } else if (this.isLineTerminator()) { + // an uninitialized class property (due to ASI, since we don't otherwise recognize the next token) + if (this.isNonstaticConstructor(method)) { + this.raise(method.key.start, "Classes may not have a non-static field named 'constructor'"); + } + classBody.body.push(this.parseClassProperty(prop)); + } else { + this.unexpected(); } + } - node.body = this.finishNode(classBody, "ClassBody"); + // Overridden in typescript.js + parseAccessModifier(): ?N.Accessibility { + return undefined; + } - this.state.strict = oldStrict; + // Overridden in typescript.js + parseClassMemberModifiers(): { abstract: boolean, readonly: boolean } { + return { abstract: false, readonly: false }; } parseClassProperty(node: N.ClassProperty): N.ClassProperty { - const hasPlugin = this.hasPlugin("classProperties"); + const allowProperties = this.hasPlugin("classProperties") || this.hasPlugin("typescript"); const noPluginMsg = "You can only use Class Properties when the 'classProperties' plugin is enabled."; - if (!node.typeAnnotation && !hasPlugin) { + if (!node.typeAnnotation && !allowProperties) { this.raise(node.start, noPluginMsg); } this.state.inClassProperty = true; if (this.match(tt.eq)) { - if (!hasPlugin) this.raise(this.state.start, noPluginMsg); + if (!allowProperties) this.raise(this.state.start, noPluginMsg); this.next(); node.value = this.parseMaybeAssign(); } else { @@ -796,8 +878,8 @@ export default class StatementParser extends ExpressionParser { return this.finishNode(node, "ClassProperty"); } - parseClassMethod(classBody: N.ClassBody, method: N.ClassMethod, isGenerator: boolean, isAsync: boolean): void { - this.parseMethod(method, isGenerator, isAsync); + parseClassMethod(classBody: N.ClassBody, method: N.ClassMethod, isGenerator: boolean, isAsync: boolean, isConstructor: boolean): void { + this.parseMethod(method, isGenerator, isAsync, isConstructor); classBody.body.push(this.finishNode(method, "ClassMethod")); } @@ -819,7 +901,8 @@ export default class StatementParser extends ExpressionParser { // Parses module export declaration. - parseExport(node: N.ExportNamedDeclaration): N.ExportNamedDeclaration { + // TODO: better type. Node is an N.AnyExport. + parseExport(node: N.Node): N.Node { this.eat(tt._export); // export * from '...' @@ -869,11 +952,25 @@ export default class StatementParser extends ExpressionParser { needsSemi = true; expr = this.parseMaybeAssign(); } - // $FlowFixMe node.declaration = expr; if (needsSemi) this.semicolon(); this.checkExport(node, true, true); return this.finishNode(node, "ExportDefaultDeclaration"); + } else if (this.hasPlugin("typescript") && this.match(tt._import)) { // `export import A = B;` + this.expect(tt._import); + return this.tsParseImportEqualsDeclaration(node, /* isExport */ true); + } else if (this.hasPlugin("typescript") && this.eat(tt.eq)) { // `export = x;` + const assign: N.TsExportAssignment = node; + assign.expression = this.parseExpression(); + this.semicolon(); + return this.finishNode(assign, "TSExportAssignment"); + } else if (this.hasPlugin("typescript") && this.eatContextual("as")) { // `export as namespace A;` + const decl: N.TsNamespaceExportDeclaration = node; + // See `parseNamespaceExportDeclaration` in TypeScript's own parser + this.expectContextual("namespace"); + decl.name = this.parseIdentifier(); + this.semicolon(); + return this.finishNode(decl, "TSNamespaceExportDeclaration"); } else if (this.shouldParseExportDeclaration()) { node.specifiers = []; node.source = null; @@ -887,7 +984,8 @@ export default class StatementParser extends ExpressionParser { return this.finishNode(node, "ExportNamedDeclaration"); } - parseExportDeclaration(): N.Declaration { + // eslint-disable-next-line no-unused-vars + parseExportDeclaration(node: N.ExportNamedDeclaration): ?N.Declaration { return this.parseStatement(true); } @@ -936,7 +1034,12 @@ export default class StatementParser extends ExpressionParser { || this.isContextual("async"); } - checkExport(node: N.ExportNamedDeclaration, checkNames: ?boolean, isDefault: ?boolean): void { + checkExport(node: N.ExportNamedDeclaration, checkNames: ?boolean, isDefault?: boolean): void { + if (this.hasPlugin("typescript")) { + // TS will check exports. + return; + } + if (checkNames) { // Check for duplicate exports if (isDefault) { @@ -1040,13 +1143,15 @@ export default class StatementParser extends ExpressionParser { // Parses import declaration. - parseImport(node: N.ImportDeclaration): N.ImportDeclaration { - this.eat(tt._import); + parseImport(node: N.Node): N.ImportDeclaration | N.TsImportEqualsDeclaration { + this.expect(tt._import); // import '...' if (this.match(tt.string)) { node.specifiers = []; node.source = this.parseExprAtom(); + } else if (this.hasPlugin("typescript") && this.match(tt.name) && this.lookahead().type === tt.eq) { + return this.tsParseImportEqualsDeclaration(node); } else { node.specifiers = []; this.parseImportSpecifiers(node); diff --git a/src/parser/util.js b/src/parser/util.js index 89ec41154e..c4147b7912 100644 --- a/src/parser/util.js +++ b/src/parser/util.js @@ -17,6 +17,12 @@ export default class UtilParser extends Tokenizer { extra[key] = val; } + // Tests whether we are at the beginning of a variable declaration: at "var", "let", or "const". + + matchVarLetOrConst(): boolean { + return this.match(tt._var) || this.match(tt._let) || this.match(tt._const); + } + // TODO isRelational(op: "<" | ">"): boolean { @@ -33,6 +39,16 @@ export default class UtilParser extends Tokenizer { } } + // eat() for relational operators. + + eatRelational(op: "<" | ">"): boolean { + if (this.isRelational(op)) { + this.next(); + return true; + } + return false; + } + // Tests whether parsed token is a contextual keyword. isContextual(name: string): boolean { @@ -56,7 +72,11 @@ export default class UtilParser extends Tokenizer { canInsertSemicolon(): boolean { return this.match(tt.eof) || this.match(tt.braceR) || - lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start)); + this.hasPrecedingLineBreak(); + } + + hasPrecedingLineBreak(): boolean { + return lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start)); } // TODO diff --git a/src/plugins/estree.js b/src/plugins/estree.js index e4b7041774..5e1eadc3ef 100644 --- a/src/plugins/estree.js +++ b/src/plugins/estree.js @@ -59,8 +59,9 @@ export default (superClass: Class): Class => class extends super } } - checkGetterSetterParamCount(prop: N.ObjectMethod): void { + checkGetterSetterParamCount(prop: N.ObjectMethod | N.ClassMethod): void { const paramCount = prop.kind === "get" ? 0 : 1; + // $FlowFixMe if (prop.value.params.length !== paramCount) { const start = prop.start; if (prop.kind === "get") { @@ -102,7 +103,7 @@ export default (superClass: Class): Class => class extends super } } - isStrictBody(node: { body: N.BlockStatement }, isExpression?: boolean): boolean { + isStrictBody(node: { body: N.BlockStatement }, isExpression: ?boolean): boolean { if (!isExpression && node.body.body.length > 0) { for (const directive of node.body.body) { if (directive.type === "ExpressionStatement" && directive.expression.type === "Literal") { @@ -132,10 +133,11 @@ export default (superClass: Class): Class => class extends super delete node.directives; } - parseClassMethod(classBody, ...args) { + parseClassMethod(classBody: N.ClassBody, ...args): void { super.parseClassMethod(classBody, ...args); const body = classBody.body; + // $FlowIgnore body[body.length - 1].type = "MethodDefinition"; } @@ -171,8 +173,8 @@ export default (superClass: Class): Class => class extends super } parseMethod(node: N.MethodLike, ...args): N.MethodLike { - let funcNode = this.startNode(); - funcNode.kind = node.kind; // provide kind, so super method correctly sets state + let funcNode: any = this.startNode(); + funcNode.kind = node.kind; // provide kind, so inner method correctly sets state funcNode = super.parseMethod(funcNode, ...args); delete funcNode.kind; // $FlowIgnore @@ -185,7 +187,9 @@ export default (superClass: Class): Class => class extends super const node = super.parseObjectMethod(...args); if (node) { + // $FlowIgnore if (node.kind === "method") node.kind = "init"; + // $FlowIgnore node.type = "Property"; } @@ -196,7 +200,9 @@ export default (superClass: Class): Class => class extends super const node = super.parseObjectProperty(...args); if (node) { + // $FlowIgnore node.kind = "init"; + // $FlowIgnore node.type = "Property"; } diff --git a/src/plugins/flow.js b/src/plugins/flow.js index f3a1b739a7..ed3c018312 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -1006,21 +1006,15 @@ export default (superClass: Class): Class => class extends super // Overrides // ================================== - // plain function return types: function name(): string {} - parseFunctionBody(node: N.Function, allowExpression?: boolean): void { - if (this.match(tt.colon) && !allowExpression) { - // if allowExpression is true then we're parsing an arrow function and if - // there's a return type then it's been handled elsewhere + parseFunctionReturnType(node: N.Function): void { + if (this.match(tt.colon)) { const typeNode = this.startNode(); - // $FlowFixMe (destructuring not yet supported) + // $FlowFixMe [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser(); - node.returnType = typeNode.typeAnnotation ? this.finishNode(typeNode, "TypeAnnotation") : null; } - - return super.parseFunctionBody(node, allowExpression); } // interfaces @@ -1256,7 +1250,7 @@ export default (superClass: Class): Class => class extends super } if (this.isContextual("implements")) { this.next(); - const implemented = node.implements = []; + const implemented: N.FlowClassImplements[] = node.implements = []; do { const node = this.startNode(); node.id = this.parseIdentifier(); @@ -1270,7 +1264,7 @@ export default (superClass: Class): Class => class extends super } } - parsePropertyName(node: N.ObjectMember): N.Identifier { + parsePropertyName(node: N.ObjectOrClassMember): N.Identifier { const variance = this.flowParseVariance(); const key = super.parsePropertyName(node); node.variance = variance; diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js new file mode 100644 index 0000000000..46faa01498 --- /dev/null +++ b/src/plugins/typescript.js @@ -0,0 +1,1472 @@ +// @flow + +import type { TokenType } from "../tokenizer/types"; +import { types as tt } from "../tokenizer/types"; +import { types as ct } from "../tokenizer/context"; +import * as N from "../types"; +import type { Pos, Position } from "../util/location"; +import Parser from "../parser"; + +type TsModifier = "readonly" | "abstract" | "static" | "public" | "private" | "protected"; + +function nonNull(x: ?T): T { + if (x == null) { + // $FlowIgnore + throw new Error(`Unexpected ${x} value.`); + } + return x; +} + +function assert(x: boolean): void { + if (!x) { + throw new Error("Assert fail"); + } +} + +type ParsingContext = + | "EnumMembers" + | "HeritageClauseElement" + | "TupleElementTypes" + | "TypeMembers" + | "TypeParametersOrArguments"; + +// Doesn't handle "void" or "null" because those are keywords, not identifiers. +function keywordTypeFromName(value: string): N.TsKeywordTypeType | typeof undefined { + switch (value) { + case "any": + return "TSAnyKeyword"; + case "boolean": + return "TSBooleanKeyword"; + case "never": + return "TSNeverKeyword"; + case "number": + return "TSNumberKeyword"; + case "object": + return "TSObjectKeyword"; + case "string": + return "TSStringKeyword"; + case "symbol": + return "TSSymbolKeyword"; + case "undefined": + return "TSUndefinedKeyword"; + default: + return undefined; + } +} + +export default (superClass: Class): Class => class extends superClass { + + tsIsIdentifier(): boolean { + // TODO: actually a bit more complex in TypeScript, but shouldn't matter. + // See https://github.com/Microsoft/TypeScript/issues/15008 + return this.match(tt.name); + } + + tsNextTokenCanFollowModifier() { + // Note: TypeScript's implementation is much more complicated because + // more things are considered modifiers there. + // This implementation only handles modifiers not handled by babylon itself. And "static". + // TODO: Would be nice to avoid lookahead. Want a hasLineBreakUpNext() method... + this.next(); + return !this.hasPrecedingLineBreak() + && !this.match(tt.parenL) + && !this.match(tt.colon) + && !this.match(tt.eq) + && !this.match(tt.question); + } + + /** Parses a modifier matching one the given modifier names. */ + tsParseModifier(allowedModifiers: T[]): ?T { + if (!this.match(tt.name)) { + return undefined; + } + + const modifier = this.state.value; + if (allowedModifiers.includes(modifier) + && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) { + return modifier; + } + return undefined; + } + + tsIsListTerminator(kind: ParsingContext): boolean { + switch (kind) { + case "EnumMembers": + case "TypeMembers": + return this.match(tt.braceR); + case "HeritageClauseElement": + return this.match(tt.braceL); + case "TupleElementTypes": + return this.match(tt.bracketR); + case "TypeParametersOrArguments": + return this.isRelational(">"); + } + + throw new Error("Unreachable"); + } + + tsParseList(kind: ParsingContext, parseElement: () => T): T[] { + const result: T[] = []; + while (!this.tsIsListTerminator(kind)) { + // Skipping "parseListElement" from the TS source since that's just for error handling. + result.push(parseElement(kind, parseElement)); + } + return result; + } + + tsParseDelimitedList(kind: ParsingContext, parseElement: () => T): T[] { + return nonNull(this.tsParseDelimitedListWorker(kind, parseElement, /* expectSuccess */ true)); + } + + tsTryParseDelimitedList(kind: ParsingContext, parseElement: () => ?T): ?T[] { + return this.tsParseDelimitedListWorker(kind, parseElement, /* expectSuccess */ false); + } + + /** + * If !expectSuccess, returns undefined instead of failing to parse. + * If expectSuccess, parseElement should always return a defined value. + */ + tsParseDelimitedListWorker( + kind: ParsingContext, + parseElement: () => ?T, + expectSuccess: boolean): ?T[] { + + const result = []; + + while (true) { + if (this.tsIsListTerminator(kind)) { + break; + } + + const element = parseElement(kind, parseElement); + if (element == null) { + return undefined; + } + result.push(element); + + if (this.eat(tt.comma)) { + continue; + } + + if (this.tsIsListTerminator(kind)) { + break; + } + + if (expectSuccess) { + // This will fail with an error about a missing comma + this.expect(tt.comma); + } + return undefined; + } + + return result; + } + + tsParseBracketedList( + kind: ParsingContext, + parseElement: () => T, + bracket: boolean, + skipFirstToken: boolean): T[] { + + if (!skipFirstToken) { + if (bracket) { + this.expect(tt.bracketL); + } else { + this.expectRelational("<"); + } + } + + const result = this.tsParseDelimitedList(kind, parseElement); + + if (bracket) { + this.expect(tt.bracketR); + } else { + this.expectRelational(">"); + } + + return result; + } + + tsParseEntityName(allowReservedWords: boolean): N.TsEntityName { + let entity: N.TsEntityName = this.parseIdentifier(); + while (this.eat(tt.dot)) { + const node: N.TsQualifiedName = this.startNodeAtNode(entity); + node.left = entity; + node.right = this.parseIdentifier(allowReservedWords); + entity = this.finishNode(node, "TSQualifiedName"); + } + return entity; + } + + tsParseTypeReference(): N.TsTypeReference { + const node: N.TsTypeReference = this.startNode(); + node.typeName = this.tsParseEntityName(/* allowReservedWords */ false); + if (!this.hasPrecedingLineBreak() && this.eatRelational("<")) { + node.typeArguments = this.tsParseBracketedList( + "TypeParametersOrArguments", + this.tsParseType.bind(this), + /* bracket */ false, + /* skipFirstToken */ true); + } + return this.finishNode(node, "TSTypeReference"); + } + + tsParseThisTypePredicate(lhs: N.TsThisType): N.TsTypePredicate { + this.next(); + const node: N.TsTypePredicate = this.startNode(); + node.parameterName = lhs; + node.typeAnnotation = this.tsParseType(); + return this.finishNode(node, "TSTypePredicate"); + } + + tsParseThisTypeNode(): N.TsThisType { + const node: N.TsThisType = this.startNode(); + this.next(); + return this.finishNode(node, "TSThisType"); + } + + tsParseTypeQuery(): N.TsTypeQuery { + const node: N.TsTypeQuery = this.startNode(); + this.expect(tt._typeof); + node.exprName = this.tsParseEntityName(/* allowReservedWords */ true); + return this.finishNode(node, "TSTypeQuery"); + } + + tsParseTypeParameter(): N.TsTypeParameterDeclaration { + const node: N.TsTypeParameterDeclaration = this.startNode(); + node.name = this.parseIdentifier(); + if (this.eat(tt._extends)) { + node.constraint = this.tsParseType(); + } + + if (this.eat(tt.eq)) { + node.default = this.tsParseType(); + } + + return this.finishNode(node, "TSTypeParameterDeclaration"); + } + + // Parse something that may be a type parameter or a type argument. + tsParseTypeAmbiguous(): ?N.TsTypeAmbiguous { + const node: N.TsTypeAmbiguous = this.startNode(); + try { + node.typeAnnotation = this.tsParseType(); + if (this.eat(tt._extends)) { + node.constraint = this.tsParseType(); + } + + if (this.eat(tt.eq)) { + node.default = this.tsParseType(); + } + } catch (err) { + if (err instanceof SyntaxError) { + return undefined; + } + throw err; + } + + return this.finishNode(node, "TSTypeAmbiguous"); + } + + tsTryParseTypeParameters(): ?$ReadOnlyArray { + if (this.eatRelational("<")) { + return this.tsParseTypeParameters(); + } + } + + tsParseTypeParameters(): $ReadOnlyArray { + return this.tsParseBracketedList( + "TypeParametersOrArguments", + this.tsParseTypeParameter.bind(this), + /* bracket */ false, + /* skipFirstToken */ true); + } + + // Note: In TypeScript implementation we must provide `yieldContext` and `awaitContext`, + // but here it's always false, because this is only used for types. + tsFillSignature(returnToken: TokenType, signature: N.TsSignatureDeclaration): void { + // Arrow fns *must* have return token (`=>`). Normal functions can omit it. + const returnTokenRequired = returnToken === tt.arrow; + signature.typeParameters = this.tsTryParseTypeParameters(); + this.expect(tt.parenL); + signature.parameters = this.tsParseBindingListForSignature(); + if (returnTokenRequired) { + this.expect(returnToken); + signature.typeAnnotation = this.tsParseTypeOrTypePredicate(); + } else if (this.eat(returnToken)) { + signature.typeAnnotation = this.tsParseTypeOrTypePredicate(); + } + } + + tsParseBindingListForSignature(): $ReadOnlyArray { + return this.parseBindingList(tt.parenR).map((pattern) => { + if (pattern.type !== "Identifier" && pattern.type !== "RestElement") { + throw this.unexpected(pattern.start, "Name in a signature must be an Identifier."); + } + return pattern; + }); + } + + tsParseTypeMemberSemicolon(): void { + if (!this.eat(tt.comma)) { + this.semicolon(); + } + } + + tsParseSignatureMember(kind: "TSCallSignatureDeclaration" | "TSConstructSignatureDeclaration") { + const node: N.TsCallSignatureDeclaration | N.TsConstructSignatureDeclaration = this.startNode(); + if (kind === "TSConstructSignatureDeclaration") { + this.expect(tt._new); + } + this.tsFillSignature(tt.colon, node); + this.tsParseTypeMemberSemicolon(); + return this.finishNode(node, kind); + } + + tsIsUnambiguouslyIndexSignature() { + this.next(); // Skip '{' + return this.eat(tt.name) && this.match(tt.colon); + } + + tsTryParseIndexSignature(node: N.TsIndexSignatureDeclaration): ?N.TsIndexSignatureDeclaration { + if (!(this.match(tt.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) { + return undefined; + } + + this.expect(tt.bracketL); + const id = this.parseIdentifier(); + this.expect(tt.colon); + id.typeAnnotation = this.tsParseType(); + this.expect(tt.bracketR); + node.parameters = [id]; + + const type = this.tsTryParseTypeAnnotation(); + if (type) node.typeAnnotation = type; + this.tsParseTypeMemberSemicolon(); + return this.finishNode(node, "TSIndexSignatureDeclaration"); + } + + tsParsePropertyOrMethodSignature( + node: N.TsPropertySignature | N.TsMethodSignature, + readonly: boolean): N.TsPropertySignature | N.TsMethodSignature { + + const { key, computed } = this.parsePropertyNameInfo(); + node.computed = computed; + node.name = key; + if (this.eat(tt.question)) node.optional = true; + const nodeAny: any = node; + + if (!readonly && (this.match(tt.parenL) || this.isRelational("<"))) { + const method: N.TsMethodSignature = nodeAny; + this.tsFillSignature(tt.colon, method); + this.tsParseTypeMemberSemicolon(); + return this.finishNode(method, "TSMethodSignature"); + } else { + const property: N.TsPropertySignature = nodeAny; + if (readonly) property.readonly = true; + const type = this.tsTryParseTypeAnnotation(); + if (type) property.typeAnnotation = type; + this.tsParseTypeMemberSemicolon(); + return this.finishNode(property, "TSPropertySignature"); + } + } + + tsParseTypeMember(): N.TsTypeElement { + if (this.match(tt.parenL) || this.isRelational("<")) { + return this.tsParseSignatureMember("TSCallSignatureDeclaration"); + } + if (this.match(tt._new) && this.tsLookAhead(this.tsIsStartOfConstructSignature.bind(this))) { + return this.tsParseSignatureMember("TSConstructSignatureDeclaration"); + } + // Instead of fullStart, we create a node here. + const node: any = this.startNode(); + const readonly = !!this.tsParseModifier(["readonly"]); + + const idx = this.tsTryParseIndexSignature(node); + if (idx) { + if (readonly) node.readonly = true; + return idx; + } + return this.tsParsePropertyOrMethodSignature(node, readonly); + } + + tsIsStartOfConstructSignature() { + this.next(); + return this.match(tt.parenL) || this.isRelational("<"); + } + + tsParseTypeLiteral(): N.TsTypeLiteral { + const node: N.TsTypeLiteral = this.startNode(); + node.members = this.tsParseObjectTypeMembers(); + return this.finishNode(node, "TSTypeLiteral"); + } + + tsParseObjectTypeMembers(): $ReadOnlyArray { + this.expect(tt.braceL); + const members = this.tsParseList("TypeMembers", this.tsParseTypeMember.bind(this)); + this.expect(tt.braceR); + return members; + } + + tsIsStartOfMappedType(): boolean { + this.next(); + if (this.isContextual("readonly")) { + this.next(); + } + if (!this.match(tt.bracketL)) { + return false; + } + this.next(); + if (!this.tsIsIdentifier()) { + return false; + } + this.next(); + return this.match(tt._in); + } + + tsParseMappedTypeParameter(): N.TsTypeParameterDeclaration { + const node: N.TsTypeParameterDeclaration = this.startNode(); + node.name = this.parseIdentifier(); + this.expect(tt._in); + node.constraint = this.tsParseType(); + return this.finishNode(node, "TSTypeParameterDeclaration"); + } + + tsParseMappedType(): N.TsMappedType { + const node: N.TsMappedType = this.startNode(); + + this.expect(tt.braceL); + if (this.eatContextual("readonly")) { + node.readonly = true; + } + this.expect(tt.bracketL); + node.typeParameter = this.tsParseMappedTypeParameter(); + this.expect(tt.bracketR); + if (this.eat(tt.question)) { + node.optional = true; + } + node.typeAnnotation = this.tsTryParseTypeAnnotation(); + this.semicolon(); + this.expect(tt.braceR); + + return this.finishNode(node, "TSMappedType"); + } + + tsParseTupleType(): N.TsTupleType { + const node: N.TsTupleType = this.startNode(); + node.elementTypes = this.tsParseBracketedList( + "TupleElementTypes", + this.tsParseType.bind(this), + /* bracket */true, + /* skipFirstToken */false); + return this.finishNode(node, "TSTupleType"); + } + + tsParseParenthesizedType(): N.TsParenthesizedType { + const node = this.startNode(); + this.expect(tt.parenL); + node.typeAnnotation = this.tsParseType(); + this.expect(tt.parenR); + return this.finishNode(node, "TSParenthesizedType"); + } + + tsParseFunctionOrConstructorType( + type: "TSFunctionType" | "TSConstructorType"): N.TsFunctionOrConstructorType { + + const node: N.TsFunctionOrConstructorType = this.startNode(); + if (type === "TSConstructorType") { + this.expect(tt._new); + } + this.tsFillSignature(tt.arrow, node); + return this.finishNode(node, type); + } + + tsParseLiteralTypeNode(): N.TsLiteralType { + const node: N.TsLiteralType = this.startNode(); + node.literal = (() => { + switch (this.state.type) { + case tt.num: + return this.parseLiteral(this.state.value, "NumericLiteral"); + case tt.string: + return this.parseLiteral(this.state.value, "StringLiteral"); + case tt._true: + case tt._false: + return this.parseBooleanLiteral(); + default: + throw this.unexpected(); + } + })(); + return this.finishNode(node, "TSLiteralType"); + } + + tsParseNonArrayType(): N.TsType { + if (this.match(tt.name) || this.match(tt._void) || this.match(tt._null)) { + const type = this.match(tt._void) + ? "TSVoidKeyword" + : this.match(tt._null) + ? "TSNullKeyword" + : keywordTypeFromName(this.state.value); + if (type !== undefined && this.lookahead().type !== tt.dot) { + const node: N.TsKeywordType = this.startNode(); + this.next(); + return this.finishNode(node, type); + } + return this.tsParseTypeReference(); + } else if (this.match(tt.string) || this.match(tt.num) || this.match(tt._true) || this.match(tt._false)) { + return this.tsParseLiteralTypeNode(); + } else if (this.match(tt.plusMin) && this.state.value === "-") { + const node: N.TsLiteralType = this.startNode(); + this.next(); + if (!this.match(tt.num)) { + throw this.unexpected(); + } + node.literal = this.parseLiteral(-this.state.value, "NumericLiteral", node.start, node.loc.start); + return this.finishNode(node, "TSLiteralType"); + } else if (this.match(tt._this)) { + const thisKeyword = this.tsParseThisTypeNode(); + if (this.isContextual("is") && !this.hasPrecedingLineBreak()) { + return this.tsParseThisTypePredicate(thisKeyword); + } else { + return thisKeyword; + } + } else if (this.match(tt._typeof)) { + return this.tsParseTypeQuery(); + } else if (this.match(tt.braceL)) { + return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) + ? this.tsParseMappedType() + : this.tsParseTypeLiteral(); + } else if (this.match(tt.bracketL)) { + return this.tsParseTupleType(); + } else if (this.match(tt.parenL)) { + return this.tsParseParenthesizedType(); + } else { + return this.tsParseTypeReference(); + } + } + + tsParseArrayTypeOrHigher(): N.TsType { + let type = this.tsParseNonArrayType(); + while (!this.hasPrecedingLineBreak() && this.eat(tt.bracketL)) { + if (this.match(tt.bracketR)) { + const node: N.TsArrayType = this.startNodeAtNode(type); + node.elementType = type; + this.expect(tt.bracketR); + type = this.finishNode(node, "TSArrayType"); + } else { + const node: N.TsIndexedAccessType = this.startNodeAtNode(type); + node.objectType = type; + node.indexType = this.tsParseType(); + this.expect(tt.bracketR); + type = this.finishNode(node, "TSIndexedAccessType"); + } + } + return type; + } + + tsParseTypeOperator(operator: "keyof"): N.TsTypeOperator { + const node = this.startNode(); + this.expectContextual(operator); + node.operator = operator; + node.typeAnnotation = this.tsParseTypeOperatorOrHigher(); + return this.finishNode(node, "TSTypeOperator"); + } + + tsParseTypeOperatorOrHigher(): N.TsType { + if (this.isContextual("keyof")) { + return this.tsParseTypeOperator("keyof"); + } + return this.tsParseArrayTypeOrHigher(); + } + + tsParseUnionOrIntersectionType( + kind: "TSUnionType" | "TSIntersectionType", + parseConstituentType: () => N.TsType, + operator: TokenType): N.TsType { + + this.eat(operator); + let type = parseConstituentType(); + if (this.match(operator)) { + const types = [type]; + while (this.eat(operator)) { + types.push(parseConstituentType()); + } + const node: N.TsUnionType | N.TsIntersectionType = this.startNodeAtNode(type); + node.types = types; + type = this.finishNode(node, kind); + } + return type; + } + + tsParseIntersectionTypeOrHigher(): N.TsType { + return this.tsParseUnionOrIntersectionType( + "TSIntersectionType", + this.tsParseTypeOperatorOrHigher.bind(this), + tt.bitwiseAND); + } + + tsParseUnionTypeOrHigher() { + return this.tsParseUnionOrIntersectionType( + "TSUnionType", + this.tsParseIntersectionTypeOrHigher.bind(this), + tt.bitwiseOR); + } + + tsIsStartOfFunctionType() { + if (this.isRelational("<")) { + return true; + } + return this.match(tt.parenL) && this.tsLookAhead(this.tsIsUnambiguouslyStartOfFunctionType.bind(this)); + } + + tsSkipParameterStart(): boolean { + if (this.match(tt.name) || this.match(tt._this)) { + this.next(); + return true; + } + return false; + } + + tsIsUnambiguouslyStartOfFunctionType(): boolean { + this.next(); + if (this.match(tt.parenR) || this.match(tt.ellipsis)) { + // ( ) + // ( ... + return true; + } + if (this.tsSkipParameterStart()) { + if (this.match(tt.colon) || this.match(tt.comma) || this.match(tt.question) || this.match(tt.eq)) { + // ( xxx : + // ( xxx , + // ( xxx ? + // ( xxx = + return true; + } + if (this.match(tt.parenR)) { + this.next(); + if (this.match(tt.arrow)) { + // ( xxx ) => + return true; + } + } + } + return false; + } + + tsParseTypeOrTypePredicate(): N.TsType { + const typePredicateVariable = this.tsIsIdentifier() + && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this)); + const type = this.tsParseType(); + if (typePredicateVariable) { + const node: N.TsTypePredicate = this.startNodeAtNode(typePredicateVariable); + node.parameterName = typePredicateVariable; + node.typeAnnotation = type; + return this.finishNode(node, "TSTypePredicate"); + } else { + return type; + } + } + + tsTryParseTypeAnnotation(): ?N.TsType { + return this.eat(tt.colon) ? this.tsParseType() : undefined; + } + + tsParseTypePredicatePrefix(): ?N.Identifier { + const id = this.parseIdentifier(); + if (this.isContextual("is") && !this.hasPrecedingLineBreak()) { + this.next(); + return id; + } + } + + tsParseType(): N.TsType { + // Need to set `state.inType` so that we don't parse JSX in a type context. + const oldInType = this.state.inType; + this.state.inType = true; + try { + if (this.tsIsStartOfFunctionType()) { + return this.tsParseFunctionOrConstructorType("TSFunctionType"); + } + if (this.match(tt._new)) { // As in `new () => Date` + return this.tsParseFunctionOrConstructorType("TSConstructorType"); + } + return this.tsParseUnionTypeOrHigher(); + } finally { + this.state.inType = oldInType; + } + } + + tsParseTypeAssertion(): N.TsTypeAssertion { + const node: N.TsTypeAssertion = this.startNode(); + node.typeAnnotation = this.tsParseType(); + this.expectRelational(">"); + node.expression = this.parseMaybeUnary(); + return this.finishNode(node, "TSTypeAssertion"); + } + + // NOTE: this is only called within a `tryParse`. So it shouldn't fail, it should just return `undefined`. + tsParseTypeArgumentsInExpression(): ?$ReadOnlyArray { + this.expectRelational("<"); + const typeArguments = this.tsTryParseDelimitedList( + "TypeParametersOrArguments", + this.tsParseTypeAmbiguous.bind(this)); + return typeArguments && this.eatRelational(">") && this.match(tt.parenL) ? typeArguments : undefined; + } + + tsParseHeritageClause(): $ReadOnlyArray { + return this.tsParseDelimitedList( + "HeritageClauseElement", + this.tsParseExpressionWithTypeArguments.bind(this)); + } + + tsParseExpressionWithTypeArguments(): N.TsExpressionWithTypeArguments { + const node: N.TsExpressionWithTypeArguments = this.startNode(); + // Note: TS uses parseLeftHandSideExpressionOrHigher, + // then has grammar errors later if it's not an EntityName. + node.expression = this.tsParseEntityName(/* allowReservedWords */ false); + if (this.isRelational("<")) { + node.typeArguments = this.tsParseTypeArguments(); + } + + return this.finishNode(node, "TSExpressionWithTypeArguments"); + } + + tsParseInterfaceDeclaration(node: N.TsInterfaceDeclaration): N.TsInterfaceDeclaration { + node.name = this.parseIdentifier(); + node.typeParameters = this.tsTryParseTypeParameters(); + if (this.eat(tt._extends)) { + node.extends = this.tsParseHeritageClause(); + } + node.members = this.tsParseObjectTypeMembers(); + return this.finishNode(node, "TSInterfaceDeclaration"); + } + + tsParseTypeAliasDeclaration(node: N.TsTypeAliasDeclaration): N.TsTypeAliasDeclaration { + node.name = this.parseIdentifier(); + node.typeParameters = this.tsTryParseTypeParameters(); + this.expect(tt.eq); + node.typeAnnotation = this.tsParseType(); + this.semicolon(); + return this.finishNode(node, "TSTypeAliasDeclaration"); + } + + tsParseEnumMember(): N.TsEnumMember { + const node: N.TsEnumMember = this.startNode(); + // Computed property names are grammar errors in an enum, so accept just string literal or identifier. + node.name = this.match(tt.string) + ? this.parseLiteral(this.state.value, "StringLiteral") + : this.parseIdentifier(/* liberal */ true); + if (this.eat(tt.eq)) { + node.initializer = this.parseMaybeAssign(); + } + return this.finishNode(node, "TSEnumMember"); + } + + tsParseEnumDeclaration(node: N.TsEnumDeclaration, isConst: boolean): N.TsEnumDeclaration { + if (isConst) node.const = true; + node.name = this.parseIdentifier(); + this.expect(tt.braceL); + node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this)); + this.expect(tt.braceR); + return this.finishNode(node, "TSEnumDeclaration"); + } + + tsParseModuleBlock(): N.TsModuleBlock { + const node: N.TsModuleBlock = this.startNode(); + this.expect(tt.braceL); + // Inside of a module block is considered "top-level", meaning it can have imports and exports. + this.parseBlockOrModuleBlockBody( + node.statements = [], /* directives */ undefined, /* topLevel */ true, /* end */ tt.braceR); + return this.finishNode(node, "TSModuleBlock"); + } + + tsParseModuleOrNamespaceDeclaration(node: N.TsModuleDeclaration): N.TsModuleDeclaration { + node.name = this.parseIdentifier(); + if (this.eat(tt.dot)) { + const inner = this.startNode(); + this.tsParseModuleOrNamespaceDeclaration(inner); + node.body = inner; + } else { + node.body = this.tsParseModuleBlock(); + } + return this.finishNode(node, "TSModuleDeclaration"); + } + + tsParseAmbientExternalModuleDeclaration(node: N.TsModuleDeclaration): N.TsModuleDeclaration { + if (this.isContextual("global")) { + node.global = true; + node.name = this.parseIdentifier(); + } else if (this.match(tt.string)) { + node.name = this.parseLiteral(this.state.value, "StringLiteral"); + } else { + this.unexpected(); + } + + if (this.match(tt.braceL)) { + node.body = this.tsParseModuleBlock(); + } else { + this.semicolon(); + } + + return this.finishNode(node, "TSModuleDeclaration"); + } + + tsParseImportEqualsDeclaration( + node: N.TsImportEqualsDeclaration, + isExport?: boolean): N.TsImportEqualsDeclaration { + + node.isExport = isExport || false; + node.name = this.parseIdentifier(); + this.expect(tt.eq); + node.moduleReference = this.tsParseModuleReference(); + this.semicolon(); + return this.finishNode(node, "TSImportEqualsDeclaration"); + } + + tsIsExternalModuleReference(): boolean { + return this.isContextual("require") && this.lookahead().type === tt.parenL; + } + + tsParseModuleReference(): N.TsModuleReference { + return this.tsIsExternalModuleReference() + ? this.tsParseExternalModuleReference() + : this.tsParseEntityName(/* allowReservedWords */ false); + } + + tsParseExternalModuleReference(): N.TsExternalModuleReference { + const node: N.TsExternalModuleReference = this.startNode(); + this.expectContextual("require"); + this.expect(tt.parenL); + if (!this.match(tt.string)) { + throw this.unexpected(); + } + node.expression = this.parseLiteral(this.state.value, "StringLiteral"); + this.expect(tt.parenR); + return this.finishNode(node, "TSExternalModuleReference"); + } + + // Utilities + + /** Start a new node with a previous node's location. */ + startNodeAtNode(type: N.Node): T { + return this.startNodeAt(type.start, type.loc.start); + } + + tsLookAhead(f: () => T): T { + const state = this.state.clone(); + const res = f(); + this.state = state; + return res; + } + + tsTryParse(f: () => ?T): ?T { + const state = this.state.clone(); + const result = f(); + if (result !== undefined && result !== false) { + return result; + } else { + this.state = state; + return undefined; + } + } + + // Not in TypeScript's own parser, but similar to its `parseCallExpressionRest`. + tsTryParseCallExpressionWithTypeArguments( + base: N.Expression, + startPos: number, + startLoc: Position): { newBase: N.Expression, shouldContinue: boolean } { + + const typeArguments = this.tsTryParse(this.tsParseTypeArgumentsInExpression.bind(this)); + return typeArguments + ? this.parseCallExpression(base, startPos, startLoc, typeArguments) + : { newBase: base, shouldContinue: false }; + } + + // Not in TypeScript's own parser. Called after seeing `new C<` in `parseNew`. + tsParseTypeArguments(): $ReadOnlyArray { + this.expectRelational("<"); + const typeArguments = this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this)); + this.expectRelational(">"); + return typeArguments; + } + + tsTypeArgumentsFromAmbiguous(types: $ReadOnlyArray): $ReadOnlyArray { + return types.map(({ constraint, default: defaultType, typeAnnotation: type, start, end, loc }) => { + if (constraint || defaultType) { + throw this.raise(start, "Type argument may not have a constraint or default."); + } + + // Set location + type.start = start; + type.end = end; + type.loc.start = loc.start; + type.loc.end = loc.end; + + return type; + }); + } + + tsTypeParametersFromAmbiguous( + types: $ReadOnlyArray): $ReadOnlyArray { + + return types.map((typeAmbiguous) => { + const typeParam = this.nodeWithSamePosition(typeAmbiguous, "TSTypeParameterDeclaration"); + const { typeAnnotation: type, constraint, default: defaultType } = typeAmbiguous; + if (type.type === "TSTypeReference" && !type.typeArguments && type.typeName.type === "Identifier") { + typeParam.name = type.typeName; + } else { + throw this.raise(type.start, "Type parameter must be a plain identifier."); + } + + if (constraint) typeParam.constriant = constraint; + if (defaultType) typeParam.default = defaultType; + return typeParam; + }); + } + + nodeWithSamePosition(original: N.Node, type: string): T { + const node = this.startNodeAtNode(original); + node.type = type; + node.end = original.end; + node.loc.end = original.loc.end; + + if (original.leadingComments) node.leadingComments = original.leadingComments; + if (original.trailingComments) node.trailingComments = original.trailingComments; + if (original.innerComments) node.innerComments = original.innerComments; + + return node; + } + + tsTryParseDeclare(nany: any): ?N.Declaration { + if (this.eat(tt._function)) { + return this.parseFunction(nany, /* isStatement */ true); + } else if (this.match(tt._class)) { + return this.parseClass(nany, /* isStatement */ true, /* optionalId */ false); + } else if (this.matchVarLetOrConst()) { + if (this.match(tt._const) && this.lookaheadIsContextual("enum")) { + // `const enum = 0;` not allowed because "enum" is a strict mode reserved word. + this.expect(tt._const); + this.expectContextual("enum"); + return this.tsParseEnumDeclaration(nany, /* isConst */ true); + } else { + return this.parseVarStatement(nany, this.state.type); + } + } else if (this.match(tt.name)) { + const value = this.state.value; + if (value === "global") { + return this.tsParseAmbientExternalModuleDeclaration(nany); + } else { + return this.tsParseDeclaration(nany, value, /* next */ true); + } + } + } + + lookaheadIsContextual(name: string): boolean { + const l = this.lookahead(); + return l.type === tt.name && l.value === name; + } + + // Note: this won't be called unless the keyword is allowed in `shouldParseExportDeclaration`. + tsTryParseExportDeclaration(): ?N.Declaration { + return this.tsParseDeclaration(this.startNode(), this.state.value, /* next */ true); + } + + tsParseExpressionStatement(node: any, expr: N.Identifier): ?N.Declaration { + switch (expr.name) { + case "declare": + const declaration = this.tsTryParseDeclare(node); + if (declaration) { + declaration.declare = true; + return declaration; + } + break; + + case "global": + // `global { }` (with no `declare`) may appear inside an ambient module declaration. + // Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global". + if (this.match(tt.braceL)) { + const mod: N.TsModuleDeclaration = node; + mod.global = true; + mod.name = expr; + mod.body = this.tsParseModuleBlock(); + return this.finishNode(mod, "TSModuleDeclaration"); + } + break; + + default: + return this.tsParseDeclaration(node, expr.name, /* next */ false); + } + } + + // Common to tsTryParseDeclare, tsTryParseExportDeclaration, and tsParseExpressionStatement. + tsParseDeclaration(node: any, value: string, next: boolean): ?N.Declaration { + switch (value) { + case "abstract": + if (next || this.match(tt._class)) { + const cls: N.ClassDeclaration = node; + cls.abstract = true; + if (next) this.next(); + return this.parseClass(cls, /* isStatement */ true, /* optionalId */ false); + } + break; + + case "enum": + if (next || this.match(tt.name)) { + if (next) this.next(); + return this.tsParseEnumDeclaration(node, /* isConst */ false); + } + break; + + case "interface": + if (next || this.match(tt.name)) { + if (next) this.next(); + return this.tsParseInterfaceDeclaration(node); + } + break; + + case "module": + if (next) this.next(); + if (this.match(tt.string)) { + return this.tsParseAmbientExternalModuleDeclaration(node); + } else if (next || this.match(tt.name)) { + return this.tsParseModuleOrNamespaceDeclaration(node); + } + break; + + case "namespace": + if (next || this.match(tt.name)) { + if (next) this.next(); + return this.tsParseModuleOrNamespaceDeclaration(node); + } + break; + + case "type": + if (next || this.match(tt.name)) { + if (next) this.next(); + return this.tsParseTypeAliasDeclaration(node); + } + break; + } + } + + // ====================================================== + // OVERRIDES + // ====================================================== + + parseAccessModifier(): ?N.Accessibility { + return this.tsParseModifier(["public", "protected", "private"]); + } + + parseParameterModifiers(): { access: ?N.Accessibility, readonly: boolean } { + const access = this.parseAccessModifier(); + const readonly = !!this.tsParseModifier(["readonly"]); + return { access, readonly }; + } + + parseClassMemberModifiers(): { abstract: boolean, readonly: boolean } { + let abstract = false, readonly = false; + + const mod = this.tsParseModifier(["abstract", "readonly"]); + switch (mod) { + case "readonly": + readonly = true; + abstract = !!this.tsParseModifier(["abstract"]); + break; + case "abstract": + abstract = true; + readonly = !!this.tsParseModifier(["readonly"]); + break; + } + + return { abstract, readonly }; + } + + parseFunctionReturnType(node: N.Function): void { + if (this.eat(tt.colon)) { + node.returnType = this.tsParseTypeOrTypePredicate(); + } + } + + // Note: The reason we do this in `parseExpressionStatement` and not `parseStatement` + // is that e.g. `type()` is valid JS, so we must try parsing that first. + // If it's really a type, we will parse `type` as the statement, and can correct it here + // by parsing the rest. + parseExpressionStatement(node: N.ExpressionStatement, expr: N.Expression): N.Statement { + const decl = expr.type === "Identifier" ? this.tsParseExpressionStatement(node, expr) : undefined; + return decl || super.parseExpressionStatement(node, expr); + } + + // export type + // Should be true for anything parsed by `tsTryParseExportDeclaration`. + shouldParseExportDeclaration(): boolean { + if (this.match(tt.name)) { + switch (this.state.value) { + case "abstract": + case "declare": + case "enum": + case "interface": + case "module": + case "namespace": + case "type": + return true; + } + } + return super.shouldParseExportDeclaration(); + } + + // An apparent conditional expression could actually be an optional parameter in an arrow function. + parseConditional( + expr: N.Expression, + noIn: ?boolean, + startPos: number, + startLoc: Position, + refNeedsArrowPos?: Pos): N.Expression { + + // only do the expensive clone if there is a question mark + // and if we come from inside parens + if (!refNeedsArrowPos || !this.match(tt.question)) { + return super.parseConditional(expr, noIn, startPos, startLoc, refNeedsArrowPos); + } + + const state = this.state.clone(); + try { + return super.parseConditional(expr, noIn, startPos, startLoc); + } catch (err) { + if (!(err instanceof SyntaxError)) { + // istanbul ignore next: no such error is expected + throw err; + } + + this.state = state; + refNeedsArrowPos.start = err.pos || this.state.start; + return expr; + } + } + + // Note: These "type casts" are *not* valid TS expressions. + // But we parse them here and change them when completing the arrow function. + parseParenItem(node: N.Expression, startPos: number, startLoc: Position): N.Expression { + node = super.parseParenItem(node, startPos, startLoc); + if (this.eat(tt.question)) { + node.optional = true; + } + + if (this.eat(tt.colon)) { + const typeCastNode = this.startNodeAt(startPos, startLoc); + typeCastNode.expression = node; + typeCastNode.typeAnnotation = this.tsParseType(); + + return this.finishNode(typeCastNode, "TypeCastExpression"); + } + + return node; + } + + parseExportDeclaration(node: N.ExportNamedDeclaration): ?N.Declaration { + // "export declare" is equivalent to just "export". + const isDeclare = this.eatContextual("declare"); + + let declaration: ?N.Declaration; + if (this.match(tt.name)) { + declaration = this.tsTryParseExportDeclaration(); + } + if (!declaration) { + declaration = super.parseExportDeclaration(node); + } + + if (declaration && isDeclare) { + declaration.declare = true; + } + + return declaration; + } + + parseClassId(node: N.Class, isStatement: boolean, optionalId: ?boolean): void { + if ((!isStatement || optionalId) && this.isContextual("implements")) { + return; + } + + super.parseClassId(...arguments); + const typeParameters = this.tsTryParseTypeParameters(); + if (typeParameters) node.typeParameters = typeParameters; + } + + parseClassProperty(node: N.ClassProperty): N.ClassProperty { + const type = this.tsTryParseTypeAnnotation(); + if (type) node.typeAnnotation = type; + return super.parseClassProperty(node); + } + + parseClassMethod( + classBody: N.ClassBody, + method: N.ClassMethod, + isGenerator: boolean, + isAsync: boolean, + isConstructor: boolean): void { + + const typeParameters = this.tsTryParseTypeParameters(); + if (typeParameters) method.typeParameters = typeParameters; + super.parseClassMethod(classBody, method, isGenerator, isAsync, isConstructor); + } + + parseClassSuper(node: N.Class, isStatement?: boolean): void { + super.parseClassSuper(node, isStatement); + if (node.superClass && this.isRelational("<")) { + node.superTypeParameters = this.tsParseTypeArguments(); + } + if (this.eatContextual("implements")) { + node.implements = this.tsParseHeritageClause(); + } + } + + parseObjPropValue(prop: N.ObjectMember, ...args): void { + if (this.isRelational("<")) { + throw new Error("TODO"); + } + + super.parseObjPropValue(prop, ...args); + } + + parseFunctionParams(node: N.NormalFunction): void { + const typeParameters = this.tsTryParseTypeParameters(); + if (typeParameters) node.typeParameters = typeParameters; + super.parseFunctionParams(node); + } + + // `let x: number;` + parseVarHead(decl: N.VariableDeclarator): void { + super.parseVarHead(decl); + const type = this.tsTryParseTypeAnnotation(); + if (type) { + decl.id.typeAnnotation = type; + this.finishNode(decl.id, decl.id.type); // set end position to end of type + } + } + + // parse the return type of an async arrow function - let foo = (async (): number => {}); + parseAsyncArrowFromCallExpression( + node: N.ArrowFunctionExpression, + call: N.CallExpression, + types: ?$ReadOnlyArray): N.ArrowFunctionExpression { + + if (this.eat(tt.colon)) { + node.returnType = this.tsParseType(); + } + return super.parseAsyncArrowFromCallExpression(node, call, types); + } + + parseMaybeAssign(...args): N.Expression { + // Note: When the JSX plugin is on, type assertions (` x`) aren't valid syntax. + + let jsxError: ?SyntaxError; + + if (this.match(tt.jsxTagStart)) { + const context = this.curContext(); + assert(context === ct.j_oTag); + // Only time j_oTag is pushed is right after j_expr. + assert(this.state.context[this.state.context.length - 2] === ct.j_expr); + + // Prefer to parse JSX if possible. But may be an arrow fn. + const state = this.state.clone(); + try { + return super.parseMaybeAssign(...args); + } catch (err) { + if (!(err instanceof SyntaxError)) { + // istanbul ignore next: no such error is expected + throw err; + } + + this.state = state; + // Pop the context added by the jsxTagStart. + assert(this.curContext() === ct.j_oTag); + this.state.context.pop(); + assert(this.curContext() === ct.j_expr); + this.state.context.pop(); + jsxError = err; + } + } + + if (jsxError === undefined && !this.isRelational("<")) { + return super.parseMaybeAssign(...args); + } + + // Either way, we're looking at a '<': tt.jsxTagStart or relational. + + let arrowExpression; + let typeParameters; + const state = this.state.clone(); + this.next(); // skip the jsx start + try { + // This is similar to TypeScript's `tryParseParenthesizedArrowFunctionExpression`. + typeParameters = this.tsParseTypeParameters(); + arrowExpression = super.parseMaybeAssign(...args); + if (arrowExpression.type !== "ArrowFunctionExpression") { + this.unexpected(); // Go to the catch block (needs a SyntaxError). + } + } catch (err) { + if (!(err instanceof SyntaxError)) { + // istanbul ignore next: no such error is expected + throw err; + } + + if (jsxError) { + throw jsxError; + } + + // Try parsing a type cast instead of an arrow function. + // This will never happen outside of JSX. + // (Because in JSX the '<' should be a jsxTagStart and not a relational. + assert(!this.hasPlugin("jsx")); + // Parsing an arrow function failed, so try a type cast. + this.state = state; + // This will start with a type assertion (via parseMaybeUnary). + // But don't directly call `this.tsParseTypeAssertion` because we want to handle any binary after it. + return super.parseMaybeAssign(...args); + } + + // Correct TypeScript code should have at least 1 type parameter, but don't crash on bad code. + if (typeParameters.length) this.resetStartLocationFromNode(arrowExpression, typeParameters[0]); + arrowExpression.typeParameters = typeParameters; + return arrowExpression; + } + + // Handle type assertions + parseMaybeUnary(refShorthandDefaultPos?: ?Pos): N.Expression { + if (!this.hasPlugin("jsx") && this.eatRelational("<")) { + return this.tsParseTypeAssertion(); + } else { + return super.parseMaybeUnary(refShorthandDefaultPos); + } + } + + parseArrow(node: N.ArrowFunctionExpression): ?N.ArrowFunctionExpression { + if (this.match(tt.colon)) { + // This is different from how the TS parser does it. + // TS uses lookahead. Babylon parses it as a parenthesized expression and converts. + const state = this.state.clone(); + this.next(); + try { + const returnType = this.tsParseTypeOrTypePredicate(); + if (this.canInsertSemicolon()) this.unexpected(); + if (!this.match(tt.arrow)) this.unexpected(); + node.returnType = returnType; + } catch (err) { + if (err instanceof SyntaxError) { + this.state = state; + } else { + // istanbul ignore next: no such error is expected + throw err; + } + } + } + + return super.parseArrow(node); + } + + // Allow type annotations inside of a parameter list. + parseAssignableListItemTypes(param: N.Pattern) { + if (this.eat(tt.question)) { + param.optional = true; + } + const type = this.tsTryParseTypeAnnotation(); + if (type) param.typeAnnotation = type; + return this.finishNode(param, param.type); + } + + // === === === === === === === === === === === === === === === === + // Note: All below methods are duplicates of something in flow.js. + // Not sure what the best way to combine these is. + // === === === === === === === === === === === === === === === === + + isClassMethod(): boolean { + return this.isRelational("<") || super.isClassMethod(); + } + + isClassProperty(): boolean { + return this.match(tt.colon) || super.isClassProperty(); + } + + parseMaybeDefault(...args): N.Pattern { + const node = super.parseMaybeDefault(...args); + + if (node.type === "AssignmentPattern" + && node.typeAnnotation + && node.right.start < node.typeAnnotation.start) { + this.raise( + node.typeAnnotation.start, + "Type annotations must come before default assignments, " + + "e.g. instead of `age = 25: number` use `age: number = 25`"); + } + + return node; + } + + // ensure that inside types, we bypass the jsx parser plugin + readToken(code: number): void { + if (this.state.inType && (code === 62 || code === 60)) { + return this.finishOp(tt.relational, 1); + } else { + return super.readToken(code); + } + } + + toAssignable(node: N.Node, isBinding: ?boolean, contextDescription: string): N.Node { + if (node.type === "TypeCastExpression") { + return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription); + } else { + return super.toAssignable(node, isBinding, contextDescription); + } + } + + toAssignableList( + exprList: N.Expression[], + isBinding: ?boolean, + contextDescription: string): $ReadOnlyArray { + + for (let i = 0; i < exprList.length; i++) { + const expr = exprList[i]; + if (expr && expr.type === "TypeCastExpression") { + exprList[i] = this.typeCastToParameter(expr); + } + } + return super.toAssignableList(exprList, isBinding, contextDescription); + } + + typeCastToParameter(node: N.Node): N.Node { + node.expression.typeAnnotation = node.typeAnnotation; + + return this.finishNodeAt( + node.expression, + node.expression.type, + node.typeAnnotation.end, + node.typeAnnotation.loc.end + ); + } + + toReferencedList(exprList: $ReadOnlyArray): $ReadOnlyArray { + for (let i = 0; i < exprList.length; i++) { + const expr = exprList[i]; + if (expr && expr._exprListItem && expr.type === "TypeCastExpression") { + this.raise(expr.start, "Did not expect a type annotation here."); + } + } + + return exprList; + } + + checkLVal(node: N.Expression): void { + // Allow "typecasts" to appear on the left of assignment expressions, + // because it may be in an arrow function. + // e.g. `const f = (foo: number = 0) => foo;` + if (node.type !== "TypeCastExpression") { + return super.checkLVal(...arguments); + } + } + + shouldParseArrow() { + return this.match(tt.colon) || super.shouldParseArrow(); + } + + shouldParseAsyncArrow(): boolean { + return this.match(tt.colon) || super.shouldParseAsyncArrow(); + } +}; diff --git a/src/tokenizer/context.js b/src/tokenizer/context.js index 7b7b97b6d7..67a62c9657 100644 --- a/src/tokenizer/context.js +++ b/src/tokenizer/context.js @@ -12,7 +12,7 @@ export class TokContext { token: string, isExpr?: boolean, preserveSpace?: boolean, - override?: Function, + override?: Function, // Takes a Tokenizer as a this-parameter, and returns void. ) { this.token = token; this.isExpr = !!isExpr; diff --git a/src/tokenizer/index.js b/src/tokenizer/index.js index 42273d664c..90bc2b2aa1 100644 --- a/src/tokenizer/index.js +++ b/src/tokenizer/index.js @@ -2,11 +2,10 @@ // @flow -import type { TokenType } from "./types"; import type { Options } from "../options"; import type { Position } from "../util/location"; import { isIdentifierStart, isIdentifierChar, isKeyword } from "../util/identifier"; -import { types as tt, keywords as keywordTypes } from "./types"; +import { types as tt, keywords as keywordTypes, type TokenType } from "./types"; import { type TokContext, types as ct } from "./context"; import LocationParser from "../parser/location"; import { SourceLocation } from "../util/location"; @@ -400,7 +399,7 @@ export default class Tokenizer extends LocationParser { this.state.pos += 2; return this.finishToken(tt.arrow); } - return this.finishOp(code === 61 ? tt.eq : tt.prefix, 1); + return this.finishOp(code === 61 ? tt.eq : tt.bang, 1); } getTokenFromCode(code: number): void { @@ -485,7 +484,7 @@ export default class Tokenizer extends LocationParser { return this.readToken_eq_excl(code); case 126: // '~' - return this.finishOp(tt.prefix, 1); + return this.finishOp(tt.tilde, 1); } this.raise(this.state.pos, `Unexpected character '${codePointToString(code)}'`); @@ -858,6 +857,11 @@ export default class Tokenizer extends LocationParser { return this.curContext() === ct.braceStatement; } + if (prevType === tt.relational) { + // `class C { ... }` + return true; + } + return !this.state.exprAllowed; } diff --git a/src/tokenizer/state.js b/src/tokenizer/state.js index 8fe08ac001..31925df982 100644 --- a/src/tokenizer/state.js +++ b/src/tokenizer/state.js @@ -2,13 +2,11 @@ import type { Options } from "../options"; import * as N from "../types"; +import { Position } from "../util/location"; -import type { TokContext } from "./context"; +import { types as ct, type TokContext } from "./context"; import type { Token } from "./index"; -import type { TokenType } from "./types"; -import { Position } from "../util/location"; -import { types as ct } from "./context"; -import { types as tt } from "./types"; +import { types as tt, type TokenType } from "./types"; export default class State { init(options: Options, input: string): void { diff --git a/src/tokenizer/types.js b/src/tokenizer/types.js index e6552249e6..1d02fde5b9 100644 --- a/src/tokenizer/types.js +++ b/src/tokenizer/types.js @@ -126,7 +126,8 @@ export const types: { [name: string]: TokenType } = { eq: new TokenType("=", { beforeExpr, isAssign }), assign: new TokenType("_=", { beforeExpr, isAssign }), incDec: new TokenType("++/--", { prefix, postfix, startsExpr }), - prefix: new TokenType("prefix", { beforeExpr, prefix, startsExpr }), + bang: new TokenType("!", { beforeExpr, prefix, startsExpr }), + tilde: new TokenType("~", { beforeExpr, prefix, startsExpr }), logicalOR: new BinopTokenType("||", 1), logicalAND: new BinopTokenType("&&", 2), bitwiseOR: new BinopTokenType("|", 3), diff --git a/src/types.js b/src/types.js index 50e8bd95d3..26fa109cea 100644 --- a/src/types.js +++ b/src/types.js @@ -47,8 +47,15 @@ export type Pattern = export type Declaration = | VariableDeclaration | ClassDeclaration - | FunctionDeclaration; -export type DeclarationBase = NodeBase; + | FunctionDeclaration + | TsInterfaceDeclaration + | TsTypeAliasDeclaration + | TsEnumDeclaration + | TsModuleDeclaration; +export type DeclarationBase = NodeBase & { + // TypeScript allows declarations to be prefixed by `declare`. + declare?: true; +} // TODO: Not in spec export type HasDecorators = NodeBase & { @@ -124,9 +131,10 @@ export type FunctionBase = HasDecorators & { generator: boolean; async: boolean; - expression: boolean; // TODO: Not in spec - typeParameters?: ?FlowTypeParameterDeclaration; // TODO: Not in spec - returnType?: ?FlowTypeAnnotation; // TODO: Not in spec + // TODO: All not in spec + expression: boolean; + typeParameters?: ?$ReadOnlyArray | FlowTypeParameterDeclaration; + returnType?: ?(TsType | FlowTypeAnnotation); }; // Statements @@ -450,6 +458,7 @@ export type ConditionalExpression = NodeBase & { export type CallOrNewBase = NodeBase & { callee: Expression | Super | Import; arguments: Array; // TODO: $ReadOnlyArray + typeArguments?: ?$ReadOnlyArray; // TODO: Not in spec }; export type CallExpression = CallOrNewBase & { @@ -490,10 +499,17 @@ export type TemplateElement = NodeBase & { // Patterns +// TypeScript access modifiers +export type Accessibility = "public" | "protected" | "private"; + export type PatternBase = HasDecorators & { - // Flow only: - optional?: true; - typeAnnotation?: ?FlowTypeAnnotation; + // TODO: All not in spec + // Flow/TypeScript only: + typeAnnotation?: ?(TsType | FlowTypeAnnotation); + // TypeScript only: + optional?: ?true; + access?: ?Accessibility; // For parameter properties + readonly?: ?true; }; export type AssignmentProperty = ObjectProperty & { @@ -531,9 +547,10 @@ export type ClassBase = HasDecorators & { body: ClassBody; decorators: $ReadOnlyArray; - typeParameters?: ?FlowTypeParameterDeclaration; // TODO: Not in spec - superTypeParameters?: ?FlowTypeParameterInstantiation; // TODO: Not in spec - implements?: $ReadOnlyArray; + // TODO: All not in spec + typeParameters?: ?$ReadOnlyArray | FlowTypeParameterDeclaration; + superTypeParameters?: ?$ReadOnlyArray | FlowTypeParameterInstantiation; + implements?: ?$ReadOnlyArray | $ReadOnlyArray; }; export type ClassBody = NodeBase & { @@ -550,9 +567,7 @@ export type ClassMemberBase = NodeBase & HasDecorators & { optional?: ?true; } -export type Accessibility = "public" | "protected" | "private"; - -export type ClassMember = ClassMethod | ClassProperty; +export type ClassMember = ClassMethod | ClassProperty | TsIndexSignatureDeclaration; export type MethodLike = ObjectMethod | FunctionExpression | ClassMethod; @@ -577,7 +592,7 @@ export type ClassProperty = ClassMemberBase & { key: Identifier; value: ?Expression; // TODO: Not in spec that this is nullable. - typeAnnotation?: ?FlowTypeAnnotation; // TODO: Not in spec + typeAnnotation?: ?(TsType | FlowTypeAnnotation); // TODO: Not in spec variance?: ?FlowVariance; // TODO: Not in spec // TypeScript only: (TODO: Not in spec) @@ -606,12 +621,13 @@ export type MetaProperty = NodeBase & { export type ModuleDeclaration = AnyImport | AnyExport; -export type AnyImport = ImportDeclaration; +export type AnyImport = ImportDeclaration | TsImportEqualsDeclaration; export type AnyExport = | ExportNamedDeclaration | ExportDefaultDeclaration - | ExportAllDeclaration; + | ExportAllDeclaration + | TsExportAssignment; export type ModuleSpecifier = NodeBase & { local: Identifier; @@ -713,3 +729,361 @@ export type FlowFunctionTypeParam = Node; export type FlowTypeAnnotation = Node; export type FlowVariance = Node; export type FlowClassImplements = Node; + +// === === === === +// TypeScript +// === === === === + +// Note: A type named `TsFoo` is based on TypeScript's `FooNode` type, +// defined in https://github.com/Microsoft/TypeScript/blob/master/src/compiler/types.ts +// Differences: +// * Change `NodeArray` to just `$ReadOnlyArray`. +// * Don't give nodes a "modifiers" list; use boolean flags instead, +// and only allow modifiers that are not considered errors. +// * A property named `type` must be renamed to `typeAnnotation` to avoid conflict with the node's type. +// * Sometimes TypeScript allows to parse something which will be a grammar error later; +// in babylon these cause exceptions, so the AST format is stricter. + +// ================ +// Misc +// ================ + +export type TsQualifiedName = NodeBase & { + type: "TSQualifiedName"; + left: TsEntityName; + right: Identifier; +} + +export type TsEntityName = Identifier | TsQualifiedName; + +/** + * For internal use only. + * This is for something which may be a type parameter or type argument, but which is not yet known. + * These shouldn't appear in the output AST. + */ +export type TsTypeAmbiguous = NodeBase & { + type: "TSTypeAmbiguous"; + typeAnnotation: TsType; + constraint?: TsType; + default?: TsType; +} + +export type TsTypeParameterDeclaration = NodeBase & { + type: "TSTypeParameter"; + name: Identifier; + constraint?: TsType; + default?: TsType; +} + +export type TsSignatureDeclaration = + | TsCallSignatureDeclaration + | TsConstructSignatureDeclaration + | TsMethodSignature + | TsFunctionType + | TsConstructorType; + +export type TsSignatureDeclarationOrIndexSignatureBase = { + // Not using TypeScript's "ParameterDeclaration" here, since it's inconsistent with regular functions. + parameters: $ReadOnlyArray; + typeAnnotation: ?TsType; +} + +export type TsSignatureDeclarationBase = TsSignatureDeclarationOrIndexSignatureBase & { + typeParameters: ?$ReadOnlyArray; +} + +// ================ +// TypeScript type members (for type literal / interface / class) +// ================ + +export type TsTypeElement = + | TsCallSignatureDeclaration + | TsConstructSignatureDeclaration + | TsPropertySignature + | TsMethodSignature + | TsIndexSignatureDeclaration; + +export type TsTypeElementBase = NodeBase & { + // TypeScript uses a `PropertyName` here, + // but babylon just uses an `Expression` for object keys, so use that. + name?: Expression; + optional?: true; +} + +export type TsCallSignatureDeclaration = TsSignatureDeclarationBase & TsTypeElementBase & { + type: "TSCallSignatureDeclaration"; +} + +export type TsConstructSignatureDeclaration = TsSignatureDeclarationBase & TsTypeElementBase & { + type: "TSConstructSignature"; +} + +export type TsNamedTypeElementBase = TsTypeElementBase & { + // Not using TypeScript's `PropertyName` here since we don't have a `ComputedPropertyName` node type. + // This is usually an Identifier but may be e.g. `Symbol.iterator` if `computed` is true. + name: Expression; + computed: boolean; + optional?: true; +} + +export type TsPropertySignature = TsNamedTypeElementBase & { + type: "TSPropertySignature"; + readonly?: true; + typeAnnotation?: TsType; + initializer?: Expression; +}; + +export type TsMethodSignature = TsSignatureDeclarationBase & TsNamedTypeElementBase & { + type: "TSMethodSignature"; +} + +// *Not* a ClassMemberBase: Can't have accessibility, can't be abstract, can't be optional. +export type TsIndexSignatureDeclaration = TsSignatureDeclarationOrIndexSignatureBase & TsTypeElementBase & { + readonly?: true; + type: "TSIndexSignatureDeclaration"; + // Note: parameters.length must be 1. +}; + +// ================ +// TypeScript types +// ================ + +export type TsType = + | TsKeywordType + | TsThisType + | TsFunctionOrConstructorType + | TsTypeReference + | TsTypeQuery + | TsTypeLiteral + | TsArrayType + | TsTupleType + | TsUnionOrIntersectionType + | TsParenthesizedType + | TsTypeOperator + | TsIndexedAccessType + | TsMappedType + | TsLiteralType + // TODO: This probably shouldn't be included here. + | TsTypePredicate; + +export type TsTypeBase = NodeBase; + +export type TsKeywordTypeType = + | "TSAnyKeyword" + | "TSNumberKeyword" + | "TSObjectKeyword" + | "TSBooleanKeyword" + | "TSStringKeyword" + | "TSSymbolKeyword" + | "TSVoidKeyword" + | "TSUndefinedKeyword" + | "TSNullKeyword" + | "TSNeverKeyword"; +export type TsKeywordType = TsTypeBase & { + type: TsKeywordTypeType; +} + +export type TsThisType = TsTypeBase & { + type: "TSThisType" +}; + +export type TsFunctionOrConstructorType = TsFunctionType | TsConstructorType; + +export type TsFunctionType = TsTypeBase & TsSignatureDeclarationBase & { + type: "TSFunctionType"; + typeAnnotation: TsType; // not optional +}; + +export type TsConstructorType = TsTypeBase & TsSignatureDeclarationBase & { + type: "TSConstructorType"; + typeAnnotation: TsType; +}; + +export type TsTypeReference = TsTypeBase & { + type: "TSTypeReference"; + typeName: TsEntityName; + typeArguments?: $ReadOnlyArray; // Should be non-empty if present. +}; + +export type TsTypePredicate = TsTypeBase & { + type: "TSTypePredicate"; + parameterName: Identifier | TsThisType; + typeAnnotation: TsType; +}; + +// `typeof` operator +export type TsTypeQuery = TsTypeBase & { + type: "TSTypeQuery"; + exprName: TsEntityName; +}; + +export type TsTypeLiteral = TsTypeBase & { + type: "TSTypeLiteral"; + members: $ReadOnlyArray; +}; + +export type TsArrayType = TsTypeBase & { + type: "TSArrayType"; + elementType: TsType; +}; + +export type TsTupleType = TsTypeBase & { + type: "TSTupleType"; + elementTypes: $ReadOnlyArray; +}; + +export type TsUnionOrIntersectionType = TsUnionType | TsIntersectionType; + +export type TsUnionOrIntersectionTypeBase = TsTypeBase & { + types: $ReadOnlyArray; +}; + +export type TsUnionType = TsUnionOrIntersectionTypeBase & { + type: "TSUnionType"; +}; + +export type TsIntersectionType = TsUnionOrIntersectionTypeBase & { + type: "TSIntersectionType"; +}; + +export type TsParenthesizedType = TsTypeBase & { + type: "TSParenthesizedType"; + typeAnnotation: TsType; +}; + +export type TsTypeOperator = TsTypeBase & { + type: "TSTypeOperator"; + operator: "keyof"; + typeAnnotation: TsType; +}; + +export type TsIndexedAccessType = TsTypeBase & { + type: "TSIndexedAccessType"; + objectType: TsType; + indexType: TsType; +}; + +export type TsMappedType = TsTypeBase & { + type: "TSMappedType"; + readonly?: true; + typeParameter: TsTypeParameterDeclaration; + optional?: true; + typeAnnotation: ?TsType; +}; + +export type TsLiteralType = TsTypeBase & { + type: "TSLiteralType"; + literal: NumericLiteral | StringLiteral | BooleanLiteral; +}; + +// ================ +// TypeScript declarations +// ================ + +export type TsInterfaceDeclaration = DeclarationBase & { + type: "TSInterfaceDeclaration"; + name: Identifier; + typeParameters: ?$ReadOnlyArray; + // TS uses "heritageClauses", but want this to resemble ClassBase. + extends?: $ReadOnlyArray; + members: $ReadOnlyArray; +}; + +export type TsExpressionWithTypeArguments = TsTypeBase & { + type: "TSExpressionWithTypeArguments"; + expression: TsEntityName; + typeArguments?: $ReadOnlyArray; // Must not be empty if present. +}; + +export type TsTypeAliasDeclaration = DeclarationBase & { + type: "TSTypeAliasDeclaration"; + name: Identifier; + typeParameters: ?$ReadOnlyArray; + typeAnnotation: TsType; +}; + +export type TsEnumDeclaration = DeclarationBase & { + type: "TSEnumDeclaration"; + const?: true; + name: Identifier; + members: $ReadOnlyArray; +}; + +export type TsEnumMember = NodeBase & { + type: "TSEnumMemodulmber"; + name: Identifier | StringLiteral; + initializer?: Expression; +}; + +export type TsModuleDeclaration = DeclarationBase & { + type: "TSModuleDeclaration"; + global?: true; // In TypeScript, this is only available through `node.flags`. + name: TsModuleName; + body: TsNamespaceBody; +}; + +// `namespace A.B { }` is a namespace named `A` with another TsNamespaceDeclaration as its body. +export type TsNamespaceBody = TsModuleBlock | TsNamespaceDeclaration; + +export type TsModuleBlock = NodeBase & { + type: "TSModuleBlock"; + statements: $ReadOnlyArray; +}; + +export type TsNamespaceDeclaration = TsModuleDeclaration & { + name: Identifier; + body: TsNamespaceBody; +}; + +export type TsModuleName = Identifier | StringLiteral; + +export type TsImportEqualsDeclaration = NodeBase & { + type: "TSImportEqualsDeclaration"; + isExport: boolean; + name: Identifier; + moduleReference: TsModuleReference; +}; + +export type TsModuleReference = TsEntityName | TsExternalModuleReference; + +export type TsExternalModuleReference = NodeBase & { + type: "TSExternalModuleReference"; + expression: StringLiteral; +}; + +// TypeScript's own parser uses ExportAssignment for both `export default` and `export =`. +// But for babylon, `export default` is an ExportDefaultDeclaration, +// so a TsExportAssignment is always `export =`. +export type TsExportAssignment = NodeBase & { + type: "TSExportAssignment"; + expression: Expression; +}; + +export type TsNamespaceExportDeclaration = NodeBase & { + type: "TSNamespaceExportDeclaration"; + name: Identifier; +}; + +// ================ +// TypeScript expressions +// ================ + +export type TsTypeAssertionLikeBase = NodeBase & { + expression: Expression; + typeAnnotation: TsType; +}; + +export type TsAsExpression = TsTypeAssertionLikeBase & { + type: "TSAsExpression"; +}; + +export type TsTypeAssertion = TsTypeAssertionLikeBase & { + type: "TSTypeAssertion"; + typeAnnotation: TsType; + expression: Expression; +}; + +export type TsNonNullExpression = NodeBase & { + type: "TSNonNullExpression"; + expression: Expression; +}; diff --git a/test/fixtures/typescript/arrow-function/annotated/actual.js b/test/fixtures/typescript/arrow-function/annotated/actual.js new file mode 100644 index 0000000000..ae22bd8d69 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/annotated/actual.js @@ -0,0 +1 @@ +(x: number): number => x; diff --git a/test/fixtures/typescript/arrow-function/annotated/expected.json b/test/fixtures/typescript/arrow-function/annotated/expected.json new file mode 100644 index 0000000000..4bc1b9b5bb --- /dev/null +++ b/test/fixtures/typescript/arrow-function/annotated/expected.json @@ -0,0 +1,134 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "returnType": { + "type": "TSNumberKeyword", + "start": 13, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 19 + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + } + } + } + } + ], + "body": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "x" + }, + "name": "x" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/async-generic/actual.js b/test/fixtures/typescript/arrow-function/async-generic/actual.js new file mode 100644 index 0000000000..6295801168 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async-generic/actual.js @@ -0,0 +1 @@ +async (a: T): T => a; diff --git a/test/fixtures/typescript/arrow-function/async-generic/expected.json b/test/fixtures/typescript/arrow-function/async-generic/expected.json new file mode 100644 index 0000000000..9cfd9d9e65 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async-generic/expected.json @@ -0,0 +1,202 @@ +{ + "type": "File", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "returnType": { + "type": "TSTypeReference", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "typeName": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "name": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "id": null, + "generator": false, + "expression": true, + "async": true, + "params": [ + { + "type": "Identifier", + "start": 10, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "typeName": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + ], + "body": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "a" + }, + "name": "a" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/async/actual.js b/test/fixtures/typescript/arrow-function/async/actual.js new file mode 100644 index 0000000000..5f4fa39a1c --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async/actual.js @@ -0,0 +1 @@ +async (x?: number): any => x; diff --git a/test/fixtures/typescript/arrow-function/async/expected.json b/test/fixtures/typescript/arrow-function/async/expected.json new file mode 100644 index 0000000000..073aacd594 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "returnType": { + "type": "TSAnyKeyword", + "start": 20, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": true, + "params": [ + { + "type": "Identifier", + "start": 7, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + } + } + } + } + ], + "body": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 28 + }, + "identifierName": "x" + }, + "name": "x" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/default-parameter-values/actual.js b/test/fixtures/typescript/arrow-function/default-parameter-values/actual.js new file mode 100644 index 0000000000..99e3245659 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/default-parameter-values/actual.js @@ -0,0 +1 @@ +(x: number = 0) => 0; diff --git a/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json b/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json new file mode 100644 index 0000000000..f822bebee4 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json @@ -0,0 +1,157 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "AssignmentPattern", + "start": 1, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "left": { + "type": "Identifier", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + } + } + } + }, + "right": { + "type": "NumericLiteral", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "body": { + "type": "NumericLiteral", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/destructuring/actual.js b/test/fixtures/typescript/arrow-function/destructuring/actual.js new file mode 100644 index 0000000000..4bb8dcbff4 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/destructuring/actual.js @@ -0,0 +1 @@ +({ a = 0 }) => 0; diff --git a/test/fixtures/typescript/arrow-function/destructuring/expected.json b/test/fixtures/typescript/arrow-function/destructuring/expected.json new file mode 100644 index 0000000000..c48ece9678 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/destructuring/expected.json @@ -0,0 +1,197 @@ +{ + "type": "File", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "ObjectPattern", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 3, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "method": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 3, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 4 + }, + "identifierName": "a" + }, + "name": "a" + }, + "shorthand": true, + "value": { + "type": "AssignmentPattern", + "start": 3, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "left": { + "type": "Identifier", + "start": 3, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 4 + }, + "identifierName": "a" + }, + "name": "a" + }, + "right": { + "type": "NumericLiteral", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + "extra": { + "shorthand": true + } + } + ] + } + ], + "body": { + "type": "NumericLiteral", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/actual.js b/test/fixtures/typescript/arrow-function/generic-tsx/actual.js new file mode 100644 index 0000000000..b9eea2b05a --- /dev/null +++ b/test/fixtures/typescript/arrow-function/generic-tsx/actual.js @@ -0,0 +1,2 @@ +// Same as `generic`. Verify that JSX doesn't change things. +(a: T): T => a; diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json new file mode 100644 index 0000000000..ce020bdcb4 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json @@ -0,0 +1,240 @@ +{ + "type": "File", + "start": 0, + "end": 79, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 79, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 61, + "end": 79, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 62, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "returnType": { + "type": "TSTypeReference", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "typeName": { + "type": "Identifier", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 65, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 68, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "typeName": { + "type": "Identifier", + "start": 68, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + ], + "body": { + "type": "Identifier", + "start": 77, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "a" + }, + "name": "a" + }, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 2 + } + }, + "name": { + "type": "Identifier", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 2 + }, + "identifierName": "T" + }, + "name": "T", + "leadingComments": null + }, + "leadingComments": null + } + ] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Same as `generic`. Verify that JSX doesn't change things.", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 60 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Same as `generic`. Verify that JSX doesn't change things.", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 60 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/options.json b/test/fixtures/typescript/arrow-function/generic-tsx/options.json new file mode 100644 index 0000000000..6e5bbd52f8 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/generic-tsx/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["jsx", "typescript"] +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/generic/actual.js b/test/fixtures/typescript/arrow-function/generic/actual.js new file mode 100644 index 0000000000..110f583654 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/generic/actual.js @@ -0,0 +1 @@ +(a: T): T => a; diff --git a/test/fixtures/typescript/arrow-function/generic/expected.json b/test/fixtures/typescript/arrow-function/generic/expected.json new file mode 100644 index 0000000000..3571308acb --- /dev/null +++ b/test/fixtures/typescript/arrow-function/generic/expected.json @@ -0,0 +1,202 @@ +{ + "type": "File", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 1, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "returnType": { + "type": "TSTypeReference", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "typeName": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 4, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + ], + "body": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "a" + }, + "name": "a" + }, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 1, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 2 + } + }, + "name": { + "type": "Identifier", + "start": 1, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 2 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/optional-parameter/actual.js b/test/fixtures/typescript/arrow-function/optional-parameter/actual.js new file mode 100644 index 0000000000..4b8fea779c --- /dev/null +++ b/test/fixtures/typescript/arrow-function/optional-parameter/actual.js @@ -0,0 +1 @@ +(x?: number): any => x; \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/optional-parameter/expected.json b/test/fixtures/typescript/arrow-function/optional-parameter/expected.json new file mode 100644 index 0000000000..afe06c383e --- /dev/null +++ b/test/fixtures/typescript/arrow-function/optional-parameter/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "returnType": { + "type": "TSAnyKeyword", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 1, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 5, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 11 + } + } + } + } + ], + "body": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + }, + "identifierName": "x" + }, + "name": "x" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/predicate-types/actual.js b/test/fixtures/typescript/arrow-function/predicate-types/actual.js new file mode 100644 index 0000000000..5c26fdbc26 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/predicate-types/actual.js @@ -0,0 +1 @@ +(x: any): x is string => true; diff --git a/test/fixtures/typescript/arrow-function/predicate-types/expected.json b/test/fixtures/typescript/arrow-function/predicate-types/expected.json new file mode 100644 index 0000000000..c46fc887e8 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/predicate-types/expected.json @@ -0,0 +1,165 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "returnType": { + "type": "TSTypePredicate", + "start": 10, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "parameterName": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 21 + } + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 1, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 4, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + } + } + } + ], + "body": { + "type": "BooleanLiteral", + "start": 25, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "value": true + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/as/actual.js b/test/fixtures/typescript/cast/as/actual.js new file mode 100644 index 0000000000..10c643e062 --- /dev/null +++ b/test/fixtures/typescript/cast/as/actual.js @@ -0,0 +1,3 @@ +x as T; +x < y as boolean; // (x < y) as boolean; +x === 1 as number; // x === (1 as number); diff --git a/test/fixtures/typescript/cast/as/expected.json b/test/fixtures/typescript/cast/as/expected.json new file mode 100644 index 0000000000..7b1fd54278 --- /dev/null +++ b/test/fixtures/typescript/cast/as/expected.json @@ -0,0 +1,396 @@ +{ + "type": "File", + "start": 0, + "end": 91, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 42 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 91, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 42 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "expression": { + "type": "TSAsExpression", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "expression": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "typeName": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + }, + { + "type": "ExpressionStatement", + "start": 8, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "expression": { + "type": "TSAsExpression", + "start": 8, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 8, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "left": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + }, + "operator": "<", + "right": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "y" + }, + "name": "y" + } + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 17, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 16 + } + } + } + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " (x < y) as boolean;", + "start": 26, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 40 + } + } + } + ] + }, + { + "type": "ExpressionStatement", + "start": 49, + "end": 67, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 49, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "left": { + "type": "Identifier", + "start": 49, + "end": 50, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x", + "leadingComments": null + }, + "operator": "===", + "right": { + "type": "TSAsExpression", + "start": 55, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 17 + } + }, + "expression": { + "type": "NumericLiteral", + "start": 55, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 7 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 60, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 17 + } + } + } + }, + "leadingComments": null + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " (x < y) as boolean;", + "start": 26, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 40 + } + } + } + ], + "trailingComments": [ + { + "type": "CommentLine", + "value": " x === (1 as number);", + "start": 68, + "end": 91, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 42 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " (x < y) as boolean;", + "start": 26, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 40 + } + } + }, + { + "type": "CommentLine", + "value": " x === (1 as number);", + "start": 68, + "end": 91, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 42 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/false-positive/actual.js b/test/fixtures/typescript/cast/false-positive/actual.js new file mode 100644 index 0000000000..51f9677f39 --- /dev/null +++ b/test/fixtures/typescript/cast/false-positive/actual.js @@ -0,0 +1 @@ +f(x < 0, /a/); diff --git a/test/fixtures/typescript/cast/false-positive/expected.json b/test/fixtures/typescript/cast/false-positive/expected.json new file mode 100644 index 0000000000..d4088d6475 --- /dev/null +++ b/test/fixtures/typescript/cast/false-positive/expected.json @@ -0,0 +1,156 @@ +{ + "type": "File", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "callee": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [ + { + "type": "BinaryExpression", + "start": 2, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "left": { + "type": "Identifier", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + }, + "identifierName": "x" + }, + "name": "x" + }, + "operator": "<", + "right": { + "type": "NumericLiteral", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + { + "type": "RegExpLiteral", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "extra": { + "raw": "/a/" + }, + "pattern": "a", + "flags": "" + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/need-parentheses/actual.js b/test/fixtures/typescript/cast/need-parentheses/actual.js new file mode 100644 index 0000000000..4c924ac9b0 --- /dev/null +++ b/test/fixtures/typescript/cast/need-parentheses/actual.js @@ -0,0 +1,3 @@ +( x).y; +(x as T).y; +x!.y; diff --git a/test/fixtures/typescript/cast/need-parentheses/expected.json b/test/fixtures/typescript/cast/need-parentheses/expected.json new file mode 100644 index 0000000000..92adbf3de4 --- /dev/null +++ b/test/fixtures/typescript/cast/need-parentheses/expected.json @@ -0,0 +1,346 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "object": { + "type": "TSTypeAssertion", + "start": 2, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "typeName": { + "type": "Identifier", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "expression": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "x" + }, + "name": "x" + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + }, + "property": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "y" + }, + "name": "y" + }, + "computed": false + } + }, + { + "type": "ExpressionStatement", + "start": 11, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "expression": { + "type": "MemberExpression", + "start": 11, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "object": { + "type": "TSAsExpression", + "start": 12, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "expression": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 2 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "typeName": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "extra": { + "parenthesized": true, + "parenStart": 11 + } + }, + "property": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "y" + }, + "name": "y" + }, + "computed": false + } + }, + { + "type": "ExpressionStatement", + "start": 23, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "expression": { + "type": "MemberExpression", + "start": 23, + "end": 27, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "object": { + "type": "TSNonNullExpression", + "start": 23, + "end": 25, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "expression": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "property": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 4 + }, + "identifierName": "y" + }, + "name": "y" + }, + "computed": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/null-assertion-then-property-access/actual.js b/test/fixtures/typescript/cast/null-assertion-then-property-access/actual.js new file mode 100644 index 0000000000..b89be6ad16 --- /dev/null +++ b/test/fixtures/typescript/cast/null-assertion-then-property-access/actual.js @@ -0,0 +1 @@ +x!.y; diff --git a/test/fixtures/typescript/cast/null-assertion-then-property-access/expected.json b/test/fixtures/typescript/cast/null-assertion-then-property-access/expected.json new file mode 100644 index 0000000000..6ba14d5e7e --- /dev/null +++ b/test/fixtures/typescript/cast/null-assertion-then-property-access/expected.json @@ -0,0 +1,114 @@ +{ + "type": "File", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "expression": { + "type": "MemberExpression", + "start": 0, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 4 + } + }, + "object": { + "type": "TSNonNullExpression", + "start": 0, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 2 + } + }, + "expression": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + "property": { + "type": "Identifier", + "start": 3, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 4 + }, + "identifierName": "y" + }, + "name": "y" + }, + "computed": false + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/null-assertion/actual.js b/test/fixtures/typescript/cast/null-assertion/actual.js new file mode 100644 index 0000000000..703cb104ad --- /dev/null +++ b/test/fixtures/typescript/cast/null-assertion/actual.js @@ -0,0 +1 @@ +x!; diff --git a/test/fixtures/typescript/cast/null-assertion/expected.json b/test/fixtures/typescript/cast/null-assertion/expected.json new file mode 100644 index 0000000000..5cf3ef60f4 --- /dev/null +++ b/test/fixtures/typescript/cast/null-assertion/expected.json @@ -0,0 +1,81 @@ +{ + "type": "File", + "start": 0, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "expression": { + "type": "TSNonNullExpression", + "start": 0, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 2 + } + }, + "expression": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/type-assertion-after-operator/actual.js b/test/fixtures/typescript/cast/type-assertion-after-operator/actual.js new file mode 100644 index 0000000000..7286cf709b --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion-after-operator/actual.js @@ -0,0 +1 @@ +1 + 1; diff --git a/test/fixtures/typescript/cast/type-assertion-after-operator/expected.json b/test/fixtures/typescript/cast/type-assertion-after-operator/expected.json new file mode 100644 index 0000000000..4ab0e02125 --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion-after-operator/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "left": { + "type": "NumericLiteral", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "operator": "+", + "right": { + "type": "TSTypeAssertion", + "start": 5, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 5, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + "expression": { + "type": "NumericLiteral", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/type-assertion-before-operator/actual.js b/test/fixtures/typescript/cast/type-assertion-before-operator/actual.js new file mode 100644 index 0000000000..03f7f08051 --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion-before-operator/actual.js @@ -0,0 +1 @@ + 1 + 1; diff --git a/test/fixtures/typescript/cast/type-assertion-before-operator/expected.json b/test/fixtures/typescript/cast/type-assertion-before-operator/expected.json new file mode 100644 index 0000000000..9fa8bc989b --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion-before-operator/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "left": { + "type": "TSTypeAssertion", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 1, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + "expression": { + "type": "NumericLiteral", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + }, + "operator": "+", + "right": { + "type": "NumericLiteral", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/cast/type-assertion/actual.js b/test/fixtures/typescript/cast/type-assertion/actual.js new file mode 100644 index 0000000000..742b94ba61 --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion/actual.js @@ -0,0 +1 @@ + 1; diff --git a/test/fixtures/typescript/cast/type-assertion/expected.json b/test/fixtures/typescript/cast/type-assertion/expected.json new file mode 100644 index 0000000000..fa15bfa001 --- /dev/null +++ b/test/fixtures/typescript/cast/type-assertion/expected.json @@ -0,0 +1,99 @@ +{ + "type": "File", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "expression": { + "type": "TSTypeAssertion", + "start": 1, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 1, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + "expression": { + "type": "NumericLiteral", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/abstract/actual.js b/test/fixtures/typescript/class/abstract/actual.js new file mode 100644 index 0000000000..97bbd960bc --- /dev/null +++ b/test/fixtures/typescript/class/abstract/actual.js @@ -0,0 +1,6 @@ +abstract class C {} +declare abstract class C {} +export abstract class C {} +// `export abstract class { }` is not valid. +// `export default abstract class C { }` is not valid. +// `abstract class` is not valid as an expression. diff --git a/test/fixtures/typescript/class/abstract/expected.json b/test/fixtures/typescript/class/abstract/expected.json new file mode 100644 index 0000000000..0c084659fb --- /dev/null +++ b/test/fixtures/typescript/class/abstract/expected.json @@ -0,0 +1,305 @@ +{ + "type": "File", + "start": 0, + "end": 225, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 50 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 225, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 50 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 17, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "body": [] + } + }, + { + "type": "ClassDeclaration", + "start": 20, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 43, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 24 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 45, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "body": [] + }, + "declare": true + }, + { + "type": "ExportNamedDeclaration", + "start": 48, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start": 55, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 70, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 23 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 72, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "body": [], + "leadingComments": null, + "trailingComments": null + }, + "trailingComments": null + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " `export abstract class { }` is not valid.", + "start": 75, + "end": 119, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 44 + } + } + }, + { + "type": "CommentLine", + "value": " `export default abstract class C { }` is not valid.", + "start": 120, + "end": 174, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 54 + } + } + }, + { + "type": "CommentLine", + "value": " `abstract class` is not valid as an expression.", + "start": 175, + "end": 225, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 50 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " `export abstract class { }` is not valid.", + "start": 75, + "end": 119, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 44 + } + } + }, + { + "type": "CommentLine", + "value": " `export default abstract class C { }` is not valid.", + "start": 120, + "end": 174, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 54 + } + } + }, + { + "type": "CommentLine", + "value": " `abstract class` is not valid as an expression.", + "start": 175, + "end": 225, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 50 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/constructor/actual.js b/test/fixtures/typescript/class/constructor/actual.js new file mode 100644 index 0000000000..e1621c489f --- /dev/null +++ b/test/fixtures/typescript/class/constructor/actual.js @@ -0,0 +1,5 @@ +class C { + constructor(x: number, y: number); + constructor(x: string, y: string); + constructor(x: any, y: any) {} +} diff --git a/test/fixtures/typescript/class/constructor/expected.json b/test/fixtures/typescript/class/constructor/expected.json new file mode 100644 index 0000000000..c289bb055f --- /dev/null +++ b/test/fixtures/typescript/class/constructor/expected.json @@ -0,0 +1,416 @@ +{ + "type": "File", + "start": 0, + "end": 124, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 124, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 124, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 124, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 26, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 25 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 29, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 25 + } + } + } + }, + { + "type": "Identifier", + "start": 37, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 36 + }, + "identifierName": "y" + }, + "name": "y", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 40, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } + } + } + } + ] + }, + { + "type": "ClassMethod", + "start": 53, + "end": 87, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 38 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 53, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 65, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 25 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 68, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 25 + } + } + } + }, + { + "type": "Identifier", + "start": 76, + "end": 85, + "loc": { + "start": { + "line": 3, + "column": 27 + }, + "end": { + "line": 3, + "column": 36 + }, + "identifierName": "y" + }, + "name": "y", + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 79, + "end": 85, + "loc": { + "start": { + "line": 3, + "column": 30 + }, + "end": { + "line": 3, + "column": 36 + } + } + } + } + ] + }, + { + "type": "ClassMethod", + "start": 92, + "end": 122, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 34 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 92, + "end": 103, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 104, + "end": 110, + "loc": { + "start": { + "line": 4, + "column": 16 + }, + "end": { + "line": 4, + "column": 22 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 107, + "end": 110, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 22 + } + } + } + }, + { + "type": "Identifier", + "start": 112, + "end": 118, + "loc": { + "start": { + "line": 4, + "column": 24 + }, + "end": { + "line": 4, + "column": 30 + }, + "identifierName": "y" + }, + "name": "y", + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 115, + "end": 118, + "loc": { + "start": { + "line": 4, + "column": 27 + }, + "end": { + "line": 4, + "column": 30 + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 120, + "end": 122, + "loc": { + "start": { + "line": 4, + "column": 32 + }, + "end": { + "line": 4, + "column": 34 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/declare/actual.js b/test/fixtures/typescript/class/declare/actual.js new file mode 100644 index 0000000000..4fe9d2b0cb --- /dev/null +++ b/test/fixtures/typescript/class/declare/actual.js @@ -0,0 +1,7 @@ +declare class C { + [x: string]: any; + x; + x: number; + f(); + f(): void; +} diff --git a/test/fixtures/typescript/class/declare/expected.json b/test/fixtures/typescript/class/declare/expected.json new file mode 100644 index 0000000000..e1e3aae403 --- /dev/null +++ b/test/fixtures/typescript/class/declare/expected.json @@ -0,0 +1,329 @@ +{ + "type": "File", + "start": 0, + "end": 87, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 87, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 87, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 16, + "end": 87, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "body": [ + { + "type": "TSIndexSignatureDeclaration", + "start": 22, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + ], + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 35, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + } + } + }, + { + "type": "ClassProperty", + "start": 44, + "end": 46, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 6 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 51, + "end": 61, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 51, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 54, + "end": 60, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + } + }, + "value": null + }, + { + "type": "ClassMethod", + "start": 66, + "end": 70, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 8 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 66, + "end": 67, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start": 75, + "end": 85, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 14 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 75, + "end": 76, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 80, + "end": 84, + "loc": { + "start": { + "line": 6, + "column": 9 + }, + "end": { + "line": 6, + "column": 13 + } + } + } + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/expression-extends-implements/actual.js b/test/fixtures/typescript/class/expression-extends-implements/actual.js new file mode 100644 index 0000000000..77b5d3bb18 --- /dev/null +++ b/test/fixtures/typescript/class/expression-extends-implements/actual.js @@ -0,0 +1,2 @@ +(class extends f() implements X.Y {}); +(class C extends f() implements X.Y {}); diff --git a/test/fixtures/typescript/class/expression-extends-implements/expected.json b/test/fixtures/typescript/class/expression-extends-implements/expected.json new file mode 100644 index 0000000000..a823c6c09f --- /dev/null +++ b/test/fixtures/typescript/class/expression-extends-implements/expected.json @@ -0,0 +1,486 @@ +{ + "type": "File", + "start": 0, + "end": 91, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 91, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "expression": { + "type": "ClassExpression", + "start": 1, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "id": null, + "superClass": { + "type": "CallExpression", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "callee": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": [ + { + "type": "TSTypeReference", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "typeName": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 33, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "left": { + "type": "Identifier", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 35, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "typeName": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 38 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + ], + "body": { + "type": "ClassBody", + "start": 40, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 40 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + }, + { + "type": "ExpressionStatement", + "start": 45, + "end": 91, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "expression": { + "type": "ClassExpression", + "start": 46, + "end": 89, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 44 + } + }, + "id": { + "type": "Identifier", + "start": 52, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": { + "type": "CallExpression", + "start": 62, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "callee": { + "type": "Identifier", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": [ + { + "type": "TSTypeReference", + "start": 66, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "typeName": { + "type": "Identifier", + "start": 66, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 80, + "end": 86, + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 41 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 80, + "end": 83, + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "left": { + "type": "Identifier", + "start": 80, + "end": 81, + "loc": { + "start": { + "line": 2, + "column": 35 + }, + "end": { + "line": 2, + "column": 36 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 82, + "end": 83, + "loc": { + "start": { + "line": 2, + "column": 37 + }, + "end": { + "line": 2, + "column": 38 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 84, + "end": 85, + "loc": { + "start": { + "line": 2, + "column": 39 + }, + "end": { + "line": 2, + "column": 40 + } + }, + "typeName": { + "type": "Identifier", + "start": 84, + "end": 85, + "loc": { + "start": { + "line": 2, + "column": 39 + }, + "end": { + "line": 2, + "column": 40 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + ], + "body": { + "type": "ClassBody", + "start": 87, + "end": 89, + "loc": { + "start": { + "line": 2, + "column": 42 + }, + "end": { + "line": 2, + "column": 44 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 45 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/expression-extends/actual.js b/test/fixtures/typescript/class/expression-extends/actual.js new file mode 100644 index 0000000000..b5054b0c75 --- /dev/null +++ b/test/fixtures/typescript/class/expression-extends/actual.js @@ -0,0 +1,2 @@ +(class extends f() {}); +(class C extends f() {}); diff --git a/test/fixtures/typescript/class/expression-extends/expected.json b/test/fixtures/typescript/class/expression-extends/expected.json new file mode 100644 index 0000000000..a3f3d7a682 --- /dev/null +++ b/test/fixtures/typescript/class/expression-extends/expected.json @@ -0,0 +1,286 @@ +{ + "type": "File", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "expression": { + "type": "ClassExpression", + "start": 1, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "id": null, + "superClass": { + "type": "CallExpression", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "callee": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": [ + { + "type": "TSTypeReference", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "typeName": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "body": { + "type": "ClassBody", + "start": 22, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + }, + { + "type": "ExpressionStatement", + "start": 27, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "expression": { + "type": "ClassExpression", + "start": 28, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "id": { + "type": "Identifier", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": { + "type": "CallExpression", + "start": 44, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "callee": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": [ + { + "type": "TSTypeReference", + "start": 48, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "typeName": { + "type": "Identifier", + "start": 48, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "body": { + "type": "ClassBody", + "start": 51, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 27 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/expression-generic/actual.js b/test/fixtures/typescript/class/expression-generic/actual.js new file mode 100644 index 0000000000..94973f7a29 --- /dev/null +++ b/test/fixtures/typescript/class/expression-generic/actual.js @@ -0,0 +1,2 @@ +(class {}); +(class C {}); diff --git a/test/fixtures/typescript/class/expression-generic/expected.json b/test/fixtures/typescript/class/expression-generic/expected.json new file mode 100644 index 0000000000..42899a920c --- /dev/null +++ b/test/fixtures/typescript/class/expression-generic/expected.json @@ -0,0 +1,222 @@ +{ + "type": "File", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "expression": { + "type": "ClassExpression", + "start": 1, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "id": null, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "name": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "superClass": null, + "body": { + "type": "ClassBody", + "start": 10, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + }, + { + "type": "ExpressionStatement", + "start": 15, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "expression": { + "type": "ClassExpression", + "start": 16, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "C" + }, + "name": "C" + }, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "name": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "superClass": null, + "body": { + "type": "ClassBody", + "start": 27, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 15 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/expression-implements/actual.js b/test/fixtures/typescript/class/expression-implements/actual.js new file mode 100644 index 0000000000..7b5e192fdf --- /dev/null +++ b/test/fixtures/typescript/class/expression-implements/actual.js @@ -0,0 +1,2 @@ +(class implements X.Y {}); +(class C implements X.Y {}); diff --git a/test/fixtures/typescript/class/expression-implements/expected.json b/test/fixtures/typescript/class/expression-implements/expected.json new file mode 100644 index 0000000000..0091779db2 --- /dev/null +++ b/test/fixtures/typescript/class/expression-implements/expected.json @@ -0,0 +1,353 @@ +{ + "type": "File", + "start": 0, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "expression": { + "type": "ClassExpression", + "start": 1, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "superClass": null, + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 18, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "left": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeName": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + ], + "body": { + "type": "ClassBody", + "start": 25, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + }, + { + "type": "ExpressionStatement", + "start": 30, + "end": 61, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "expression": { + "type": "ClassExpression", + "start": 31, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 50, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 50, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "left": { + "type": "Identifier", + "start": 50, + "end": 51, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 21 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 52, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 22 + }, + "end": { + "line": 2, + "column": 23 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "typeName": { + "type": "Identifier", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + ], + "body": { + "type": "ClassBody", + "start": 57, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "body": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 30 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/extends-implements/actual.js b/test/fixtures/typescript/class/extends-implements/actual.js new file mode 100644 index 0000000000..960c58c5e6 --- /dev/null +++ b/test/fixtures/typescript/class/extends-implements/actual.js @@ -0,0 +1 @@ +class C extends f() implements X.Y {} diff --git a/test/fixtures/typescript/class/extends-implements/expected.json b/test/fixtures/typescript/class/extends-implements/expected.json new file mode 100644 index 0000000000..4580b1bf71 --- /dev/null +++ b/test/fixtures/typescript/class/extends-implements/expected.json @@ -0,0 +1,249 @@ +{ + "type": "File", + "start": 0, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": { + "type": "CallExpression", + "start": 16, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "callee": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": [ + { + "type": "TSTypeReference", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 34, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 34, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "left": { + "type": "Identifier", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 35 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 36, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 37 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "typeName": { + "type": "Identifier", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 39 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + ], + "body": { + "type": "ClassBody", + "start": 41, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 41 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/extends/actual.js b/test/fixtures/typescript/class/extends/actual.js new file mode 100644 index 0000000000..c527424e5c --- /dev/null +++ b/test/fixtures/typescript/class/extends/actual.js @@ -0,0 +1 @@ +class C extends f() {} diff --git a/test/fixtures/typescript/class/extends/expected.json b/test/fixtures/typescript/class/extends/expected.json new file mode 100644 index 0000000000..79dc2619b5 --- /dev/null +++ b/test/fixtures/typescript/class/extends/expected.json @@ -0,0 +1,149 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": { + "type": "CallExpression", + "start": 16, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "callee": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [] + }, + "superTypeParameters": [ + { + "type": "TSTypeReference", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "body": { + "type": "ClassBody", + "start": 23, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/generic/actual.js b/test/fixtures/typescript/class/generic/actual.js new file mode 100644 index 0000000000..dbaeabda8c --- /dev/null +++ b/test/fixtures/typescript/class/generic/actual.js @@ -0,0 +1 @@ +class C {} diff --git a/test/fixtures/typescript/class/generic/expected.json b/test/fixtures/typescript/class/generic/expected.json new file mode 100644 index 0000000000..131db796fc --- /dev/null +++ b/test/fixtures/typescript/class/generic/expected.json @@ -0,0 +1,197 @@ +{ + "type": "File", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 8, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "name": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "T" + }, + "name": "T" + }, + "constraint": { + "type": "TSObjectKeyword", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + "default": { + "type": "TSTypeLiteral", + "start": 27, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 29, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 30 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 38 + } + } + } + } + ] + } + } + ], + "superClass": null, + "body": { + "type": "ClassBody", + "start": 42, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 42 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/get-generic/actual.js b/test/fixtures/typescript/class/get-generic/actual.js new file mode 100644 index 0000000000..85739b3c2f --- /dev/null +++ b/test/fixtures/typescript/class/get-generic/actual.js @@ -0,0 +1,3 @@ +declare class C { + get(): void; +} diff --git a/test/fixtures/typescript/class/get-generic/expected.json b/test/fixtures/typescript/class/get-generic/expected.json new file mode 100644 index 0000000000..f093b0c0a3 --- /dev/null +++ b/test/fixtures/typescript/class/get-generic/expected.json @@ -0,0 +1,174 @@ +{ + "type": "File", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 16, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 22, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 22, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "get" + }, + "name": "get" + }, + "kind": "method", + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "name": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 18 + } + } + } + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/implements/actual.js b/test/fixtures/typescript/class/implements/actual.js new file mode 100644 index 0000000000..725bd441be --- /dev/null +++ b/test/fixtures/typescript/class/implements/actual.js @@ -0,0 +1 @@ +class C implements X.Y {} diff --git a/test/fixtures/typescript/class/implements/expected.json b/test/fixtures/typescript/class/implements/expected.json new file mode 100644 index 0000000000..e3fd8b4f71 --- /dev/null +++ b/test/fixtures/typescript/class/implements/expected.json @@ -0,0 +1,183 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "implements": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "left": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "typeName": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + ], + "body": { + "type": "ClassBody", + "start": 26, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/index-signature/actual.js b/test/fixtures/typescript/class/index-signature/actual.js new file mode 100644 index 0000000000..a05c783338 --- /dev/null +++ b/test/fixtures/typescript/class/index-signature/actual.js @@ -0,0 +1,4 @@ +class C { + [x: string]: any; + readonly [x: string]: any; +} diff --git a/test/fixtures/typescript/class/index-signature/expected.json b/test/fixtures/typescript/class/index-signature/expected.json new file mode 100644 index 0000000000..0b104e6f81 --- /dev/null +++ b/test/fixtures/typescript/class/index-signature/expected.json @@ -0,0 +1,213 @@ +{ + "type": "File", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSIndexSignatureDeclaration", + "start": 14, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + ], + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + } + } + }, + { + "type": "TSIndexSignatureDeclaration", + "start": 36, + "end": 62, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 30 + } + }, + "readonly": true, + "parameters": [ + { + "type": "Identifier", + "start": 46, + "end": 47, + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 15 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 49, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 23 + } + } + } + } + ], + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 58, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 29 + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/members-with-modifier-names/actual.js b/test/fixtures/typescript/class/members-with-modifier-names/actual.js new file mode 100644 index 0000000000..cbf422a383 --- /dev/null +++ b/test/fixtures/typescript/class/members-with-modifier-names/actual.js @@ -0,0 +1,6 @@ +class C { + public(): void; + public static(): void; + readonly = 0; + async(): void; +} diff --git a/test/fixtures/typescript/class/members-with-modifier-names/expected.json b/test/fixtures/typescript/class/members-with-modifier-names/expected.json new file mode 100644 index 0000000000..bd88206e00 --- /dev/null +++ b/test/fixtures/typescript/class/members-with-modifier-names/expected.json @@ -0,0 +1,338 @@ +{ + "type": "File", + "start": 0, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "public" + }, + "name": "public" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 24, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 18 + } + } + } + }, + { + "type": "ClassMethod", + "start": 34, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 26 + } + }, + "access": "public", + "static": false, + "kind": "method", + "computed": false, + "key": { + "type": "Identifier", + "start": 41, + "end": 47, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 17 + }, + "identifierName": "static" + }, + "name": "static" + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 51, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 25 + } + } + } + }, + { + "type": "ClassProperty", + "start": 61, + "end": 74, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 61, + "end": 69, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 12 + }, + "identifierName": "readonly" + }, + "name": "readonly" + }, + "value": { + "type": "NumericLiteral", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 16 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + }, + { + "type": "ClassMethod", + "start": 79, + "end": 96, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 21 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 79, + "end": 84, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 9 + }, + "identifierName": "async" + }, + "name": "async" + }, + "kind": "method", + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 85, + "end": 86, + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 11 + } + }, + "name": { + "type": "Identifier", + "start": 85, + "end": 86, + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 11 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 91, + "end": 95, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 20 + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/members-with-reserved-names/actual.js b/test/fixtures/typescript/class/members-with-reserved-names/actual.js new file mode 100644 index 0000000000..579d8c8998 --- /dev/null +++ b/test/fixtures/typescript/class/members-with-reserved-names/actual.js @@ -0,0 +1,3 @@ +class C { + public delete(): void; +} diff --git a/test/fixtures/typescript/class/members-with-reserved-names/expected.json b/test/fixtures/typescript/class/members-with-reserved-names/expected.json new file mode 100644 index 0000000000..8779c86d25 --- /dev/null +++ b/test/fixtures/typescript/class/members-with-reserved-names/expected.json @@ -0,0 +1,140 @@ +{ + "type": "File", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "access": "public", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "delete" + }, + "name": "delete" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 31, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 25 + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-computed/actual.js b/test/fixtures/typescript/class/method-computed/actual.js new file mode 100644 index 0000000000..ce02ff3566 --- /dev/null +++ b/test/fixtures/typescript/class/method-computed/actual.js @@ -0,0 +1,4 @@ +class C { + [Symbol.iterator](): void; + [Symbol.iterator]?(): void; +} diff --git a/test/fixtures/typescript/class/method-computed/expected.json b/test/fixtures/typescript/class/method-computed/expected.json new file mode 100644 index 0000000000..eb2b2cfb70 --- /dev/null +++ b/test/fixtures/typescript/class/method-computed/expected.json @@ -0,0 +1,261 @@ +{ + "type": "File", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 15, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 22, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 35, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + } + } + } + }, + { + "type": "ClassMethod", + "start": 45, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 31 + } + }, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 46, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 46, + "end": 52, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 53, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "optional": true, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 67, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 30 + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-generic/actual.js b/test/fixtures/typescript/class/method-generic/actual.js new file mode 100644 index 0000000000..ceefb4d0ce --- /dev/null +++ b/test/fixtures/typescript/class/method-generic/actual.js @@ -0,0 +1,4 @@ +class C { + f(a: T, b?: T, ...c: T[]): T {} + [Symbol.iterator](): T {} +} diff --git a/test/fixtures/typescript/class/method-generic/expected.json b/test/fixtures/typescript/class/method-generic/expected.json new file mode 100644 index 0000000000..a317590030 --- /dev/null +++ b/test/fixtures/typescript/class/method-generic/expected.json @@ -0,0 +1,542 @@ +{ + "type": "File", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "name": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "typeName": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + { + "type": "Identifier", + "start": 25, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "b" + }, + "name": "b", + "optional": true, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "typeName": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + { + "type": "RestElement", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 22 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "argument": { + "type": "Identifier", + "start": 35, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 26 + }, + "identifierName": "c" + }, + "name": "c" + }, + "typeAnnotation": { + "type": "TSArrayType", + "start": 38, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "elementType": { + "type": "TSTypeReference", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "typeName": { + "type": "Identifier", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 29 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ], + "returnType": { + "type": "TSTypeReference", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "typeName": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 35 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "body": { + "type": "BlockStatement", + "start": 46, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 36 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 53, + "end": 81, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 32 + } + }, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 54, + "end": 69, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 54, + "end": 60, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 61, + "end": 69, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "kind": "method", + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 71, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "name": { + "type": "Identifier", + "start": 71, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 23 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSTypeReference", + "start": 77, + "end": 78, + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 29 + } + }, + "typeName": { + "type": "Identifier", + "start": 77, + "end": 78, + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 29 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "body": { + "type": "BlockStatement", + "start": 79, + "end": 81, + "loc": { + "start": { + "line": 3, + "column": 30 + }, + "end": { + "line": 3, + "column": 32 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-no-body/actual.js b/test/fixtures/typescript/class/method-no-body/actual.js new file mode 100644 index 0000000000..8641a976d7 --- /dev/null +++ b/test/fixtures/typescript/class/method-no-body/actual.js @@ -0,0 +1,4 @@ +class C { + f(); + f(): void; +} diff --git a/test/fixtures/typescript/class/method-no-body/expected.json b/test/fixtures/typescript/class/method-no-body/expected.json new file mode 100644 index 0000000000..4bbb26e551 --- /dev/null +++ b/test/fixtures/typescript/class/method-no-body/expected.json @@ -0,0 +1,179 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start": 23, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 14 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 28, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 13 + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-optional/actual.js b/test/fixtures/typescript/class/method-optional/actual.js new file mode 100644 index 0000000000..6c8694960d --- /dev/null +++ b/test/fixtures/typescript/class/method-optional/actual.js @@ -0,0 +1,3 @@ +class C { + m?(): void {} +} diff --git a/test/fixtures/typescript/class/method-optional/expected.json b/test/fixtures/typescript/class/method-optional/expected.json new file mode 100644 index 0000000000..c5ec66f42c --- /dev/null +++ b/test/fixtures/typescript/class/method-optional/expected.json @@ -0,0 +1,157 @@ +{ + "type": "File", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "optional": true, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 20, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 14 + } + } + }, + "body": { + "type": "BlockStatement", + "start": 25, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-return-type/actual.js b/test/fixtures/typescript/class/method-return-type/actual.js new file mode 100644 index 0000000000..6c537e03f6 --- /dev/null +++ b/test/fixtures/typescript/class/method-return-type/actual.js @@ -0,0 +1,3 @@ +class C { + f(): void {} +} diff --git a/test/fixtures/typescript/class/method-return-type/expected.json b/test/fixtures/typescript/class/method-return-type/expected.json new file mode 100644 index 0000000000..e11905c45b --- /dev/null +++ b/test/fixtures/typescript/class/method-return-type/expected.json @@ -0,0 +1,156 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 13 + } + } + }, + "body": { + "type": "BlockStatement", + "start": 24, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-with-newline-with-body/actual.js b/test/fixtures/typescript/class/method-with-newline-with-body/actual.js new file mode 100644 index 0000000000..65bd228eed --- /dev/null +++ b/test/fixtures/typescript/class/method-with-newline-with-body/actual.js @@ -0,0 +1,6 @@ +class C +{ + m() + { + } +} diff --git a/test/fixtures/typescript/class/method-with-newline-with-body/expected.json b/test/fixtures/typescript/class/method-with-newline-with-body/expected.json new file mode 100644 index 0000000000..585a5b2920 --- /dev/null +++ b/test/fixtures/typescript/class/method-with-newline-with-body/expected.json @@ -0,0 +1,141 @@ +{ + "type": "File", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 29, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 22, + "end": 29, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/method-with-newline-without-body/actual.js b/test/fixtures/typescript/class/method-with-newline-without-body/actual.js new file mode 100644 index 0000000000..0a2f3719eb --- /dev/null +++ b/test/fixtures/typescript/class/method-with-newline-without-body/actual.js @@ -0,0 +1,5 @@ +class C +{ + m() + n() +} diff --git a/test/fixtures/typescript/class/method-with-newline-without-body/expected.json b/test/fixtures/typescript/class/method-with-newline-without-body/expected.json new file mode 100644 index 0000000000..9d0bd11572 --- /dev/null +++ b/test/fixtures/typescript/class/method-with-newline-without-body/expected.json @@ -0,0 +1,164 @@ +{ + "type": "File", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 7 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start": 22, + "end": 25, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 7 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "n" + }, + "name": "n" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/modifiers-accessors/actual.js b/test/fixtures/typescript/class/modifiers-accessors/actual.js new file mode 100644 index 0000000000..07b7d79c0c --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-accessors/actual.js @@ -0,0 +1,11 @@ +// Copy of modifiers-methods with 'get' +abstract class C { + abstract get a(); + static get s() { return 0; } + public abstract get pua(); + public static get pus() { return 0; } + + public get pu() { return 0; } + protected get po() { return 0; } + private get pi() { return 0; } +} diff --git a/test/fixtures/typescript/class/modifiers-accessors/expected.json b/test/fixtures/typescript/class/modifiers-accessors/expected.json new file mode 100644 index 0000000000..d0d0d150c7 --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-accessors/expected.json @@ -0,0 +1,673 @@ +{ + "type": "File", + "start": 0, + "end": 295, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 295, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 40, + "end": 295, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 55, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 16 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 57, + "end": 295, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 63, + "end": 80, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 76, + "end": 77, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 18 + }, + "identifierName": "a" + }, + "name": "a" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start": 85, + "end": 113, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 32 + } + }, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 96, + "end": 97, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 16 + }, + "identifierName": "s" + }, + "name": "s" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 100, + "end": 113, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 32 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 102, + "end": 111, + "loc": { + "start": { + "line": 4, + "column": 21 + }, + "end": { + "line": 4, + "column": 30 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 109, + "end": 110, + "loc": { + "start": { + "line": 4, + "column": 28 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 118, + "end": 144, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 30 + } + }, + "access": "public", + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 138, + "end": 141, + "loc": { + "start": { + "line": 5, + "column": 24 + }, + "end": { + "line": 5, + "column": 27 + }, + "identifierName": "pua" + }, + "name": "pua" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start": 149, + "end": 186, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 41 + } + }, + "access": "public", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 167, + "end": 170, + "loc": { + "start": { + "line": 6, + "column": 22 + }, + "end": { + "line": 6, + "column": 25 + }, + "identifierName": "pus" + }, + "name": "pus" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 173, + "end": 186, + "loc": { + "start": { + "line": 6, + "column": 28 + }, + "end": { + "line": 6, + "column": 41 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 175, + "end": 184, + "loc": { + "start": { + "line": 6, + "column": 30 + }, + "end": { + "line": 6, + "column": 39 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 182, + "end": 183, + "loc": { + "start": { + "line": 6, + "column": 37 + }, + "end": { + "line": 6, + "column": 38 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 192, + "end": 221, + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 33 + } + }, + "access": "public", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 203, + "end": 205, + "loc": { + "start": { + "line": 8, + "column": 15 + }, + "end": { + "line": 8, + "column": 17 + }, + "identifierName": "pu" + }, + "name": "pu" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 208, + "end": 221, + "loc": { + "start": { + "line": 8, + "column": 20 + }, + "end": { + "line": 8, + "column": 33 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 210, + "end": 219, + "loc": { + "start": { + "line": 8, + "column": 22 + }, + "end": { + "line": 8, + "column": 31 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 217, + "end": 218, + "loc": { + "start": { + "line": 8, + "column": 29 + }, + "end": { + "line": 8, + "column": 30 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 226, + "end": 258, + "loc": { + "start": { + "line": 9, + "column": 4 + }, + "end": { + "line": 9, + "column": 36 + } + }, + "access": "protected", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 240, + "end": 242, + "loc": { + "start": { + "line": 9, + "column": 18 + }, + "end": { + "line": 9, + "column": 20 + }, + "identifierName": "po" + }, + "name": "po" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 245, + "end": 258, + "loc": { + "start": { + "line": 9, + "column": 23 + }, + "end": { + "line": 9, + "column": 36 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 247, + "end": 256, + "loc": { + "start": { + "line": 9, + "column": 25 + }, + "end": { + "line": 9, + "column": 34 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 254, + "end": 255, + "loc": { + "start": { + "line": 9, + "column": 32 + }, + "end": { + "line": 9, + "column": 33 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 263, + "end": 293, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 34 + } + }, + "access": "private", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 275, + "end": 277, + "loc": { + "start": { + "line": 10, + "column": 16 + }, + "end": { + "line": 10, + "column": 18 + }, + "identifierName": "pi" + }, + "name": "pi" + }, + "kind": "get", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 280, + "end": 293, + "loc": { + "start": { + "line": 10, + "column": 21 + }, + "end": { + "line": 10, + "column": 34 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 282, + "end": 291, + "loc": { + "start": { + "line": 10, + "column": 23 + }, + "end": { + "line": 10, + "column": 32 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 289, + "end": 290, + "loc": { + "start": { + "line": 10, + "column": 30 + }, + "end": { + "line": 10, + "column": 31 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "directives": [] + } + } + ] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Copy of modifiers-methods with 'get'", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 39 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Copy of modifiers-methods with 'get'", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 39 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/modifiers-methods-async/actual.js b/test/fixtures/typescript/class/modifiers-methods-async/actual.js new file mode 100644 index 0000000000..9632a64212 --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-methods-async/actual.js @@ -0,0 +1,11 @@ +// Copy of modifiers-methods with 'async' +abstract class C { + abstract async a(); + static async s() {} + public abstract async pua(); + public static async pus() {} + + public async pu() {} + protected async po() {} + private async pi() {} +} diff --git a/test/fixtures/typescript/class/modifiers-methods-async/expected.json b/test/fixtures/typescript/class/modifiers-methods-async/expected.json new file mode 100644 index 0000000000..9a1aa19de1 --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-methods-async/expected.json @@ -0,0 +1,493 @@ +{ + "type": "File", + "start": 0, + "end": 256, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 256, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 42, + "end": 256, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 16 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 59, + "end": 256, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 65, + "end": 84, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 80, + "end": 81, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "a" + }, + "name": "a" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [] + }, + { + "type": "ClassMethod", + "start": 89, + "end": 108, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 23 + } + }, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 102, + "end": 103, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 18 + }, + "identifierName": "s" + }, + "name": "s" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 106, + "end": 108, + "loc": { + "start": { + "line": 4, + "column": 21 + }, + "end": { + "line": 4, + "column": 23 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 113, + "end": 141, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 32 + } + }, + "access": "public", + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 135, + "end": 138, + "loc": { + "start": { + "line": 5, + "column": 26 + }, + "end": { + "line": 5, + "column": 29 + }, + "identifierName": "pua" + }, + "name": "pua" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [] + }, + { + "type": "ClassMethod", + "start": 146, + "end": 174, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 32 + } + }, + "access": "public", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 166, + "end": 169, + "loc": { + "start": { + "line": 6, + "column": 24 + }, + "end": { + "line": 6, + "column": 27 + }, + "identifierName": "pus" + }, + "name": "pus" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 172, + "end": 174, + "loc": { + "start": { + "line": 6, + "column": 30 + }, + "end": { + "line": 6, + "column": 32 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 180, + "end": 200, + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 24 + } + }, + "access": "public", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 193, + "end": 195, + "loc": { + "start": { + "line": 8, + "column": 17 + }, + "end": { + "line": 8, + "column": 19 + }, + "identifierName": "pu" + }, + "name": "pu" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 198, + "end": 200, + "loc": { + "start": { + "line": 8, + "column": 22 + }, + "end": { + "line": 8, + "column": 24 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 205, + "end": 228, + "loc": { + "start": { + "line": 9, + "column": 4 + }, + "end": { + "line": 9, + "column": 27 + } + }, + "access": "protected", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 221, + "end": 223, + "loc": { + "start": { + "line": 9, + "column": 20 + }, + "end": { + "line": 9, + "column": 22 + }, + "identifierName": "po" + }, + "name": "po" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 226, + "end": 228, + "loc": { + "start": { + "line": 9, + "column": 25 + }, + "end": { + "line": 9, + "column": 27 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ClassMethod", + "start": 233, + "end": 254, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 25 + } + }, + "access": "private", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 247, + "end": 249, + "loc": { + "start": { + "line": 10, + "column": 18 + }, + "end": { + "line": 10, + "column": 20 + }, + "identifierName": "pi" + }, + "name": "pi" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 252, + "end": 254, + "loc": { + "start": { + "line": 10, + "column": 23 + }, + "end": { + "line": 10, + "column": 25 + } + }, + "body": [], + "directives": [] + } + } + ] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Copy of modifiers-methods with 'async'", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 41 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Copy of modifiers-methods with 'async'", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 41 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/modifiers-properties/actual.js b/test/fixtures/typescript/class/modifiers-properties/actual.js new file mode 100644 index 0000000000..874a23ca80 --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-properties/actual.js @@ -0,0 +1,20 @@ +abstract class C { + readonly r; + abstract a; + static s; + + public pu; + protected po; + private pi; + + readonly abstract ra; + abstract readonly ar; + static readonly sr; + + public readonly pur; + public abstract pua; + public static pus; + public readonly abstract pura; + public abstract readonly puar; + public static readonly pusr; +} diff --git a/test/fixtures/typescript/class/modifiers-properties/expected.json b/test/fixtures/typescript/class/modifiers-properties/expected.json new file mode 100644 index 0000000000..f4ca369a74 --- /dev/null +++ b/test/fixtures/typescript/class/modifiers-properties/expected.json @@ -0,0 +1,633 @@ +{ + "type": "File", + "start": 0, + "end": 370, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 20, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 370, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 20, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 370, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 20, + "column": 1 + } + }, + "abstract": true, + "id": { + "type": "Identifier", + "start": 15, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 16 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 17, + "end": 370, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 20, + "column": 1 + } + }, + "body": [ + { + "type": "ClassProperty", + "start": 23, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "readonly": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 32, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "r" + }, + "name": "r" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 39, + "end": 50, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 48, + "end": 49, + "loc": { + "start": { + "line": 3, + "column": 13 + }, + "end": { + "line": 3, + "column": 14 + }, + "identifierName": "a" + }, + "name": "a" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 55, + "end": 64, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 12 + }, + "identifierName": "s" + }, + "name": "s" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 70, + "end": 80, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 14 + } + }, + "access": "public", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 77, + "end": 79, + "loc": { + "start": { + "line": 6, + "column": 11 + }, + "end": { + "line": 6, + "column": 13 + }, + "identifierName": "pu" + }, + "name": "pu" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 85, + "end": 98, + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 17 + } + }, + "access": "protected", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 95, + "end": 97, + "loc": { + "start": { + "line": 7, + "column": 14 + }, + "end": { + "line": 7, + "column": 16 + }, + "identifierName": "po" + }, + "name": "po" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 103, + "end": 114, + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 15 + } + }, + "access": "private", + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 111, + "end": 113, + "loc": { + "start": { + "line": 8, + "column": 12 + }, + "end": { + "line": 8, + "column": 14 + }, + "identifierName": "pi" + }, + "name": "pi" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 120, + "end": 141, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 25 + } + }, + "readonly": true, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 138, + "end": 140, + "loc": { + "start": { + "line": 10, + "column": 22 + }, + "end": { + "line": 10, + "column": 24 + }, + "identifierName": "ra" + }, + "name": "ra" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 146, + "end": 167, + "loc": { + "start": { + "line": 11, + "column": 4 + }, + "end": { + "line": 11, + "column": 25 + } + }, + "readonly": true, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 164, + "end": 166, + "loc": { + "start": { + "line": 11, + "column": 22 + }, + "end": { + "line": 11, + "column": 24 + }, + "identifierName": "ar" + }, + "name": "ar" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 172, + "end": 191, + "loc": { + "start": { + "line": 12, + "column": 4 + }, + "end": { + "line": 12, + "column": 23 + } + }, + "readonly": true, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 188, + "end": 190, + "loc": { + "start": { + "line": 12, + "column": 20 + }, + "end": { + "line": 12, + "column": 22 + }, + "identifierName": "sr" + }, + "name": "sr" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 197, + "end": 217, + "loc": { + "start": { + "line": 14, + "column": 4 + }, + "end": { + "line": 14, + "column": 24 + } + }, + "access": "public", + "readonly": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 213, + "end": 216, + "loc": { + "start": { + "line": 14, + "column": 20 + }, + "end": { + "line": 14, + "column": 23 + }, + "identifierName": "pur" + }, + "name": "pur" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 222, + "end": 242, + "loc": { + "start": { + "line": 15, + "column": 4 + }, + "end": { + "line": 15, + "column": 24 + } + }, + "access": "public", + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 238, + "end": 241, + "loc": { + "start": { + "line": 15, + "column": 20 + }, + "end": { + "line": 15, + "column": 23 + }, + "identifierName": "pua" + }, + "name": "pua" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 247, + "end": 265, + "loc": { + "start": { + "line": 16, + "column": 4 + }, + "end": { + "line": 16, + "column": 22 + } + }, + "access": "public", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 261, + "end": 264, + "loc": { + "start": { + "line": 16, + "column": 18 + }, + "end": { + "line": 16, + "column": 21 + }, + "identifierName": "pus" + }, + "name": "pus" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 270, + "end": 300, + "loc": { + "start": { + "line": 17, + "column": 4 + }, + "end": { + "line": 17, + "column": 34 + } + }, + "access": "public", + "readonly": true, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 295, + "end": 299, + "loc": { + "start": { + "line": 17, + "column": 29 + }, + "end": { + "line": 17, + "column": 33 + }, + "identifierName": "pura" + }, + "name": "pura" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 305, + "end": 335, + "loc": { + "start": { + "line": 18, + "column": 4 + }, + "end": { + "line": 18, + "column": 34 + } + }, + "access": "public", + "readonly": true, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 330, + "end": 334, + "loc": { + "start": { + "line": 18, + "column": 29 + }, + "end": { + "line": 18, + "column": 33 + }, + "identifierName": "puar" + }, + "name": "puar" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 340, + "end": 368, + "loc": { + "start": { + "line": 19, + "column": 4 + }, + "end": { + "line": 19, + "column": 32 + } + }, + "access": "public", + "readonly": true, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 363, + "end": 367, + "loc": { + "start": { + "line": 19, + "column": 27 + }, + "end": { + "line": 19, + "column": 31 + }, + "identifierName": "pusr" + }, + "name": "pusr" + }, + "value": null + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/parameter-properties/actual.js b/test/fixtures/typescript/class/parameter-properties/actual.js new file mode 100644 index 0000000000..846c2f1c6a --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties/actual.js @@ -0,0 +1,11 @@ +class C { + constructor( + readonly r, + public pu, + protected po, + private pi, + public readonly pur, + // Also works on AssignmentPattern + readonly x = 0, + public y: number = 0) {} +} diff --git a/test/fixtures/typescript/class/parameter-properties/expected.json b/test/fixtures/typescript/class/parameter-properties/expected.json new file mode 100644 index 0000000000..4b4c549ea7 --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties/expected.json @@ -0,0 +1,391 @@ +{ + "type": "File", + "start": 0, + "end": 238, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 238, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 238, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 238, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 11, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 236, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 10, + "column": 32 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 18 + }, + "identifierName": "r" + }, + "name": "r", + "readonly": true + }, + { + "type": "Identifier", + "start": 62, + "end": 64, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 17 + }, + "identifierName": "pu" + }, + "name": "pu", + "access": "public" + }, + { + "type": "Identifier", + "start": 84, + "end": 86, + "loc": { + "start": { + "line": 5, + "column": 18 + }, + "end": { + "line": 5, + "column": 20 + }, + "identifierName": "po" + }, + "name": "po", + "access": "protected" + }, + { + "type": "Identifier", + "start": 104, + "end": 106, + "loc": { + "start": { + "line": 6, + "column": 16 + }, + "end": { + "line": 6, + "column": 18 + }, + "identifierName": "pi" + }, + "name": "pi", + "access": "private" + }, + { + "type": "Identifier", + "start": 132, + "end": 135, + "loc": { + "start": { + "line": 7, + "column": 24 + }, + "end": { + "line": 7, + "column": 27 + }, + "identifierName": "pur" + }, + "name": "pur", + "access": "public", + "readonly": true + }, + { + "type": "AssignmentPattern", + "start": 197, + "end": 202, + "loc": { + "start": { + "line": 9, + "column": 17 + }, + "end": { + "line": 9, + "column": 22 + } + }, + "left": { + "type": "Identifier", + "start": 197, + "end": 198, + "loc": { + "start": { + "line": 9, + "column": 17 + }, + "end": { + "line": 9, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x", + "leadingComments": null + }, + "right": { + "type": "NumericLiteral", + "start": 201, + "end": 202, + "loc": { + "start": { + "line": 9, + "column": 21 + }, + "end": { + "line": 9, + "column": 22 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Also works on AssignmentPattern", + "start": 145, + "end": 179, + "loc": { + "start": { + "line": 8, + "column": 8 + }, + "end": { + "line": 8, + "column": 42 + } + } + } + ], + "readonly": true + }, + { + "type": "AssignmentPattern", + "start": 219, + "end": 232, + "loc": { + "start": { + "line": 10, + "column": 15 + }, + "end": { + "line": 10, + "column": 28 + } + }, + "left": { + "type": "Identifier", + "start": 219, + "end": 228, + "loc": { + "start": { + "line": 10, + "column": 15 + }, + "end": { + "line": 10, + "column": 24 + }, + "identifierName": "y" + }, + "name": "y", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 222, + "end": 228, + "loc": { + "start": { + "line": 10, + "column": 18 + }, + "end": { + "line": 10, + "column": 24 + } + } + } + }, + "right": { + "type": "NumericLiteral", + "start": 231, + "end": 232, + "loc": { + "start": { + "line": 10, + "column": 27 + }, + "end": { + "line": 10, + "column": 28 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + }, + "access": "public" + } + ], + "body": { + "type": "BlockStatement", + "start": 234, + "end": 236, + "loc": { + "start": { + "line": 10, + "column": 30 + }, + "end": { + "line": 10, + "column": 32 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Also works on AssignmentPattern", + "start": 145, + "end": 179, + "loc": { + "start": { + "line": 8, + "column": 8 + }, + "end": { + "line": 8, + "column": 42 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/properties/actual.js b/test/fixtures/typescript/class/properties/actual.js new file mode 100644 index 0000000000..8c0ad68965 --- /dev/null +++ b/test/fixtures/typescript/class/properties/actual.js @@ -0,0 +1,6 @@ +class C { + x; + x?; + x: number; + x: number = 1; +} diff --git a/test/fixtures/typescript/class/properties/expected.json b/test/fixtures/typescript/class/properties/expected.json new file mode 100644 index 0000000000..cd4b94d078 --- /dev/null +++ b/test/fixtures/typescript/class/properties/expected.json @@ -0,0 +1,274 @@ +{ + "type": "File", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassProperty", + "start": 14, + "end": 16, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 7 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "optional": true, + "value": null + }, + { + "type": "ClassProperty", + "start": 29, + "end": 39, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + } + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 44, + "end": 58, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 18 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 47, + "end": 53, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 13 + } + } + }, + "value": { + "type": "NumericLiteral", + "start": 56, + "end": 57, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 17 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/property-computed/actual.js b/test/fixtures/typescript/class/property-computed/actual.js new file mode 100644 index 0000000000..10ee099fa9 --- /dev/null +++ b/test/fixtures/typescript/class/property-computed/actual.js @@ -0,0 +1,4 @@ +class C { + [Symbol.iterator]: number; + [Symbol.iterator]?: number; +} diff --git a/test/fixtures/typescript/class/property-computed/expected.json b/test/fixtures/typescript/class/property-computed/expected.json new file mode 100644 index 0000000000..ff1b4c35f7 --- /dev/null +++ b/test/fixtures/typescript/class/property-computed/expected.json @@ -0,0 +1,251 @@ +{ + "type": "File", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ClassProperty", + "start": 14, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 15, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 22, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 29 + } + } + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 45, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 31 + } + }, + "static": false, + "computed": true, + "key": { + "type": "MemberExpression", + "start": 46, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 46, + "end": 52, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 53, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "optional": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 65, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } + } + }, + "value": null + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/static/actual.js b/test/fixtures/typescript/class/static/actual.js new file mode 100644 index 0000000000..aca8d85ba7 --- /dev/null +++ b/test/fixtures/typescript/class/static/actual.js @@ -0,0 +1,6 @@ +class C { + static f(); + public static f(); + protected static f(); + private static f(); +} diff --git a/test/fixtures/typescript/class/static/expected.json b/test/fixtures/typescript/class/static/expected.json new file mode 100644 index 0000000000..8dfe7aa998 --- /dev/null +++ b/test/fixtures/typescript/class/static/expected.json @@ -0,0 +1,247 @@ +{ + "type": "File", + "start": 0, + "end": 100, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 100, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 100, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 100, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start": 30, + "end": 48, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "access": "public", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 18 + }, + "end": { + "line": 3, + "column": 19 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start": 53, + "end": 74, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 25 + } + }, + "access": "protected", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 70, + "end": 71, + "loc": { + "start": { + "line": 4, + "column": 21 + }, + "end": { + "line": 4, + "column": 22 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + }, + { + "type": "ClassMethod", + "start": 79, + "end": 98, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 23 + } + }, + "access": "private", + "static": true, + "computed": false, + "key": { + "type": "Identifier", + "start": 94, + "end": 95, + "loc": { + "start": { + "line": 5, + "column": 19 + }, + "end": { + "line": 5, + "column": 20 + }, + "identifierName": "f" + }, + "name": "f" + }, + "kind": "method", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [] + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/const/no-initializer/actual.js b/test/fixtures/typescript/const/no-initializer/actual.js new file mode 100644 index 0000000000..8caae258b9 --- /dev/null +++ b/test/fixtures/typescript/const/no-initializer/actual.js @@ -0,0 +1 @@ +const x: number; diff --git a/test/fixtures/typescript/const/no-initializer/expected.json b/test/fixtures/typescript/const/no-initializer/expected.json new file mode 100644 index 0000000000..8f6d1953eb --- /dev/null +++ b/test/fixtures/typescript/const/no-initializer/expected.json @@ -0,0 +1,99 @@ +{ + "type": "File", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/declare/const/actual.js b/test/fixtures/typescript/declare/const/actual.js new file mode 100644 index 0000000000..f394dbd226 --- /dev/null +++ b/test/fixtures/typescript/declare/const/actual.js @@ -0,0 +1 @@ +declare const x: number, y: string; diff --git a/test/fixtures/typescript/declare/const/expected.json b/test/fixtures/typescript/declare/const/expected.json new file mode 100644 index 0000000000..a6a9fa3e83 --- /dev/null +++ b/test/fixtures/typescript/declare/const/expected.json @@ -0,0 +1,147 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 14, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "id": { + "type": "Identifier", + "start": 14, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + } + } + }, + { + "type": "VariableDeclarator", + "start": 25, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "id": { + "type": "Identifier", + "start": 25, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 34 + }, + "identifierName": "y" + }, + "name": "y", + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 34 + } + } + } + } + } + ], + "kind": "const", + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/declare/destructure/actual.js b/test/fixtures/typescript/declare/destructure/actual.js new file mode 100644 index 0000000000..7f46b962e9 --- /dev/null +++ b/test/fixtures/typescript/declare/destructure/actual.js @@ -0,0 +1 @@ +declare const { x, y }: { x: number, y: number }; diff --git a/test/fixtures/typescript/declare/destructure/expected.json b/test/fixtures/typescript/declare/destructure/expected.json new file mode 100644 index 0000000000..8d3b0abcd5 --- /dev/null +++ b/test/fixtures/typescript/declare/destructure/expected.json @@ -0,0 +1,308 @@ +{ + "type": "File", + "start": 0, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 49 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 49 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 49 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 14, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "id": { + "type": "ObjectPattern", + "start": 14, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "method": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x" + }, + "shorthand": true, + "value": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x" + }, + "extra": { + "shorthand": true + } + }, + { + "type": "ObjectProperty", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "method": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "y" + }, + "name": "y" + }, + "shorthand": true, + "value": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "y" + }, + "name": "y" + }, + "extra": { + "shorthand": true + } + } + ], + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 24, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 26, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 29, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 35 + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 37, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 38 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 40, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 40 + }, + "end": { + "line": 1, + "column": 46 + } + } + } + } + ] + } + } + } + ], + "kind": "const", + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/declare/interface/actual.js b/test/fixtures/typescript/declare/interface/actual.js new file mode 100644 index 0000000000..85d7623f14 --- /dev/null +++ b/test/fixtures/typescript/declare/interface/actual.js @@ -0,0 +1 @@ +declare interface I {} diff --git a/test/fixtures/typescript/declare/interface/expected.json b/test/fixtures/typescript/declare/interface/expected.json new file mode 100644 index 0000000000..f1860bb53e --- /dev/null +++ b/test/fixtures/typescript/declare/interface/expected.json @@ -0,0 +1,68 @@ +{ + "type": "File", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [], + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/declare/let/actual.js b/test/fixtures/typescript/declare/let/actual.js new file mode 100644 index 0000000000..3dc63502e9 --- /dev/null +++ b/test/fixtures/typescript/declare/let/actual.js @@ -0,0 +1 @@ +declare let x; diff --git a/test/fixtures/typescript/declare/let/expected.json b/test/fixtures/typescript/declare/let/expected.json new file mode 100644 index 0000000000..9644d592b1 --- /dev/null +++ b/test/fixtures/typescript/declare/let/expected.json @@ -0,0 +1,86 @@ +{ + "type": "File", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": null + } + ], + "kind": "let", + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/declare/var/actual.js b/test/fixtures/typescript/declare/var/actual.js new file mode 100644 index 0000000000..c1a3d52a76 --- /dev/null +++ b/test/fixtures/typescript/declare/var/actual.js @@ -0,0 +1,2 @@ +declare var x; +declare var x: any; diff --git a/test/fixtures/typescript/declare/var/expected.json b/test/fixtures/typescript/declare/var/expected.json new file mode 100644 index 0000000000..cab8ef29b5 --- /dev/null +++ b/test/fixtures/typescript/declare/var/expected.json @@ -0,0 +1,153 @@ +{ + "type": "File", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": null + } + ], + "kind": "var", + "declare": true + }, + { + "type": "VariableDeclaration", + "start": 15, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 27, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 27, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 30, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 18 + } + } + } + }, + "init": null + } + ], + "kind": "var", + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/const/actual.js b/test/fixtures/typescript/enum/const/actual.js new file mode 100644 index 0000000000..6eb417807c --- /dev/null +++ b/test/fixtures/typescript/enum/const/actual.js @@ -0,0 +1 @@ +const enum E {} diff --git a/test/fixtures/typescript/enum/const/expected.json b/test/fixtures/typescript/enum/const/expected.json new file mode 100644 index 0000000000..f387d86cc5 --- /dev/null +++ b/test/fixtures/typescript/enum/const/expected.json @@ -0,0 +1,68 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "const": true, + "name": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/declare-const/actual.js b/test/fixtures/typescript/enum/declare-const/actual.js new file mode 100644 index 0000000000..4a3fc75be0 --- /dev/null +++ b/test/fixtures/typescript/enum/declare-const/actual.js @@ -0,0 +1 @@ +declare const enum E {} diff --git a/test/fixtures/typescript/enum/declare-const/expected.json b/test/fixtures/typescript/enum/declare-const/expected.json new file mode 100644 index 0000000000..92ea1e4a07 --- /dev/null +++ b/test/fixtures/typescript/enum/declare-const/expected.json @@ -0,0 +1,69 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "const": true, + "name": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [], + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/declare/actual.js b/test/fixtures/typescript/enum/declare/actual.js new file mode 100644 index 0000000000..5218d402d3 --- /dev/null +++ b/test/fixtures/typescript/enum/declare/actual.js @@ -0,0 +1 @@ +declare enum E {} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/declare/expected.json b/test/fixtures/typescript/enum/declare/expected.json new file mode 100644 index 0000000000..507a2f8f94 --- /dev/null +++ b/test/fixtures/typescript/enum/declare/expected.json @@ -0,0 +1,68 @@ +{ + "type": "File", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "name": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [], + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/export-const/actual.js b/test/fixtures/typescript/enum/export-const/actual.js new file mode 100644 index 0000000000..6b1e42773f --- /dev/null +++ b/test/fixtures/typescript/enum/export-const/actual.js @@ -0,0 +1 @@ +export const enum E {} diff --git a/test/fixtures/typescript/enum/export-const/expected.json b/test/fixtures/typescript/enum/export-const/expected.json new file mode 100644 index 0000000000..1866fda344 --- /dev/null +++ b/test/fixtures/typescript/enum/export-const/expected.json @@ -0,0 +1,85 @@ +{ + "type": "File", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSEnumDeclaration", + "start": 7, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "const": true, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/export-declare-const/actual.js b/test/fixtures/typescript/enum/export-declare-const/actual.js new file mode 100644 index 0000000000..e6d28ba285 --- /dev/null +++ b/test/fixtures/typescript/enum/export-declare-const/actual.js @@ -0,0 +1 @@ +export declare const enum E {} diff --git a/test/fixtures/typescript/enum/export-declare-const/expected.json b/test/fixtures/typescript/enum/export-declare-const/expected.json new file mode 100644 index 0000000000..25339bc233 --- /dev/null +++ b/test/fixtures/typescript/enum/export-declare-const/expected.json @@ -0,0 +1,86 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSEnumDeclaration", + "start": 15, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "const": true, + "name": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [], + "declare": true + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/export/actual.js b/test/fixtures/typescript/enum/export/actual.js new file mode 100644 index 0000000000..5bda4c84bc --- /dev/null +++ b/test/fixtures/typescript/enum/export/actual.js @@ -0,0 +1 @@ +export enum E {} diff --git a/test/fixtures/typescript/enum/export/expected.json b/test/fixtures/typescript/enum/export/expected.json new file mode 100644 index 0000000000..d9527678fe --- /dev/null +++ b/test/fixtures/typescript/enum/export/expected.json @@ -0,0 +1,84 @@ +{ + "type": "File", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSEnumDeclaration", + "start": 7, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "name": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/members-reserved-words/actual.js b/test/fixtures/typescript/enum/members-reserved-words/actual.js new file mode 100644 index 0000000000..06c4ab796c --- /dev/null +++ b/test/fixtures/typescript/enum/members-reserved-words/actual.js @@ -0,0 +1,4 @@ +enum E { + const, + default +} diff --git a/test/fixtures/typescript/enum/members-reserved-words/expected.json b/test/fixtures/typescript/enum/members-reserved-words/expected.json new file mode 100644 index 0000000000..58f79e6d16 --- /dev/null +++ b/test/fixtures/typescript/enum/members-reserved-words/expected.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [ + { + "type": "TSEnumMember", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "name": { + "type": "Identifier", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "const" + }, + "name": "const" + } + }, + { + "type": "TSEnumMember", + "start": 24, + "end": 31, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 11 + } + }, + "name": { + "type": "Identifier", + "start": 24, + "end": 31, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "default" + }, + "name": "default" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/members-strings/actual.js b/test/fixtures/typescript/enum/members-strings/actual.js new file mode 100644 index 0000000000..fe909aa5c4 --- /dev/null +++ b/test/fixtures/typescript/enum/members-strings/actual.js @@ -0,0 +1,4 @@ +enum E { + "foo", + "bar" = 1 +} diff --git a/test/fixtures/typescript/enum/members-strings/expected.json b/test/fixtures/typescript/enum/members-strings/expected.json new file mode 100644 index 0000000000..91b0af7255 --- /dev/null +++ b/test/fixtures/typescript/enum/members-strings/expected.json @@ -0,0 +1,158 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [ + { + "type": "TSEnumMember", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "name": { + "type": "StringLiteral", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + }, + { + "type": "TSEnumMember", + "start": 24, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "name": { + "type": "StringLiteral", + "start": 24, + "end": 29, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "extra": { + "rawValue": "bar", + "raw": "\"bar\"" + }, + "value": "bar" + }, + "initializer": { + "type": "NumericLiteral", + "start": 32, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/actual.js b/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/actual.js new file mode 100644 index 0000000000..0c1f115f61 --- /dev/null +++ b/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/actual.js @@ -0,0 +1,3 @@ +enum E { + A = 0, +} diff --git a/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json b/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json new file mode 100644 index 0000000000..2cb16a8d72 --- /dev/null +++ b/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json @@ -0,0 +1,120 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [ + { + "type": "TSEnumMember", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "name": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + }, + "initializer": { + "type": "NumericLiteral", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/members-trailing-comma/actual.js b/test/fixtures/typescript/enum/members-trailing-comma/actual.js new file mode 100644 index 0000000000..7db24478a7 --- /dev/null +++ b/test/fixtures/typescript/enum/members-trailing-comma/actual.js @@ -0,0 +1,3 @@ +enum E { + A, +} diff --git a/test/fixtures/typescript/enum/members-trailing-comma/expected.json b/test/fixtures/typescript/enum/members-trailing-comma/expected.json new file mode 100644 index 0000000000..61fec59557 --- /dev/null +++ b/test/fixtures/typescript/enum/members-trailing-comma/expected.json @@ -0,0 +1,100 @@ +{ + "type": "File", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [ + { + "type": "TSEnumMember", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "name": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/enum/members/actual.js b/test/fixtures/typescript/enum/members/actual.js new file mode 100644 index 0000000000..c0d9e76d28 --- /dev/null +++ b/test/fixtures/typescript/enum/members/actual.js @@ -0,0 +1,4 @@ +enum E { + A, + B = 0 +} diff --git a/test/fixtures/typescript/enum/members/expected.json b/test/fixtures/typescript/enum/members/expected.json new file mode 100644 index 0000000000..0128196715 --- /dev/null +++ b/test/fixtures/typescript/enum/members/expected.json @@ -0,0 +1,152 @@ +{ + "type": "File", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "E" + }, + "name": "E" + }, + "members": [ + { + "type": "TSEnumMember", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "name": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + } + }, + { + "type": "TSEnumMember", + "start": 20, + "end": 25, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "name": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "B" + }, + "name": "B" + }, + "initializer": { + "type": "NumericLiteral", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/export/as-namespace/actual.js b/test/fixtures/typescript/export/as-namespace/actual.js new file mode 100644 index 0000000000..b971448035 --- /dev/null +++ b/test/fixtures/typescript/export/as-namespace/actual.js @@ -0,0 +1 @@ +export as namespace A; diff --git a/test/fixtures/typescript/export/as-namespace/expected.json b/test/fixtures/typescript/export/as-namespace/expected.json new file mode 100644 index 0000000000..abf438a2b3 --- /dev/null +++ b/test/fixtures/typescript/export/as-namespace/expected.json @@ -0,0 +1,66 @@ +{ + "type": "File", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSNamespaceExportDeclaration", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "name": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "A" + }, + "name": "A" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/export/declare/actual.js b/test/fixtures/typescript/export/declare/actual.js new file mode 100644 index 0000000000..58d3f7937e --- /dev/null +++ b/test/fixtures/typescript/export/declare/actual.js @@ -0,0 +1,7 @@ +export declare const x: number; +export declare function f(): void; +export declare class C {} +export declare interface I {} +export declare type T = number; +export declare module M {} +export declare namespace N {} diff --git a/test/fixtures/typescript/export/declare/expected.json b/test/fixtures/typescript/export/declare/expected.json new file mode 100644 index 0000000000..be07e8efc2 --- /dev/null +++ b/test/fixtures/typescript/export/declare/expected.json @@ -0,0 +1,501 @@ +{ + "type": "File", + "start": 0, + "end": 211, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 29 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 211, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 29 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start": 15, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 21, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 30 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 24, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 30 + } + } + } + } + } + ], + "kind": "const", + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 32, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "FunctionDeclaration", + "start": 47, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "id": { + "type": "Identifier", + "start": 56, + "end": 57, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 61, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 29 + }, + "end": { + "line": 2, + "column": 33 + } + } + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 67, + "end": 92, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start": 82, + "end": 92, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "id": { + "type": "Identifier", + "start": 88, + "end": 89, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 22 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 90, + "end": 92, + "loc": { + "start": { + "line": 3, + "column": 23 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "body": [] + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 93, + "end": 122, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSInterfaceDeclaration", + "start": 108, + "end": 122, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "name": { + "type": "Identifier", + "start": 118, + "end": 119, + "loc": { + "start": { + "line": 4, + "column": 25 + }, + "end": { + "line": 4, + "column": 26 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [], + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 123, + "end": 154, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 31 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSTypeAliasDeclaration", + "start": 138, + "end": 154, + "loc": { + "start": { + "line": 5, + "column": 15 + }, + "end": { + "line": 5, + "column": 31 + } + }, + "name": { + "type": "Identifier", + "start": 143, + "end": 144, + "loc": { + "start": { + "line": 5, + "column": 20 + }, + "end": { + "line": 5, + "column": 21 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 147, + "end": 153, + "loc": { + "start": { + "line": 5, + "column": 24 + }, + "end": { + "line": 5, + "column": 30 + } + } + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 155, + "end": 181, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 26 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 170, + "end": 181, + "loc": { + "start": { + "line": 6, + "column": 15 + }, + "end": { + "line": 6, + "column": 26 + } + }, + "name": { + "type": "Identifier", + "start": 177, + "end": 178, + "loc": { + "start": { + "line": 6, + "column": 22 + }, + "end": { + "line": 6, + "column": 23 + }, + "identifierName": "M" + }, + "name": "M" + }, + "body": { + "type": "TSModuleBlock", + "start": 179, + "end": 181, + "loc": { + "start": { + "line": 6, + "column": 24 + }, + "end": { + "line": 6, + "column": 26 + } + }, + "statements": [] + }, + "declare": true + } + }, + { + "type": "ExportNamedDeclaration", + "start": 182, + "end": 211, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 29 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 197, + "end": 211, + "loc": { + "start": { + "line": 7, + "column": 15 + }, + "end": { + "line": 7, + "column": 29 + } + }, + "name": { + "type": "Identifier", + "start": 207, + "end": 208, + "loc": { + "start": { + "line": 7, + "column": 25 + }, + "end": { + "line": 7, + "column": 26 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 209, + "end": 211, + "loc": { + "start": { + "line": 7, + "column": 27 + }, + "end": { + "line": 7, + "column": 29 + } + }, + "statements": [] + }, + "declare": true + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/export/equals/actual.js b/test/fixtures/typescript/export/equals/actual.js new file mode 100644 index 0000000000..af251c676f --- /dev/null +++ b/test/fixtures/typescript/export/equals/actual.js @@ -0,0 +1 @@ +export = f; diff --git a/test/fixtures/typescript/export/equals/expected.json b/test/fixtures/typescript/export/equals/expected.json new file mode 100644 index 0000000000..9e0da1e567 --- /dev/null +++ b/test/fixtures/typescript/export/equals/expected.json @@ -0,0 +1,66 @@ +{ + "type": "File", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSExportAssignment", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "expression": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "f" + }, + "name": "f" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/annotated/actual.js b/test/fixtures/typescript/function/annotated/actual.js new file mode 100644 index 0000000000..139772fad0 --- /dev/null +++ b/test/fixtures/typescript/function/annotated/actual.js @@ -0,0 +1 @@ +function f(x?: T): T {} diff --git a/test/fixtures/typescript/function/annotated/expected.json b/test/fixtures/typescript/function/annotated/expected.json new file mode 100644 index 0000000000..474acd6269 --- /dev/null +++ b/test/fixtures/typescript/function/annotated/expected.json @@ -0,0 +1,204 @@ +{ + "type": "File", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "name": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "params": [ + { + "type": "Identifier", + "start": 14, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "typeName": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + ], + "returnType": { + "type": "TSTypeReference", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeName": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "body": { + "type": "BlockStatement", + "start": 24, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/anonymous/actual.js b/test/fixtures/typescript/function/anonymous/actual.js new file mode 100644 index 0000000000..395ce03c04 --- /dev/null +++ b/test/fixtures/typescript/function/anonymous/actual.js @@ -0,0 +1 @@ +const f = function(x?: T): T {}; diff --git a/test/fixtures/typescript/function/anonymous/expected.json b/test/fixtures/typescript/function/anonymous/expected.json new file mode 100644 index 0000000000..9d5aacb912 --- /dev/null +++ b/test/fixtures/typescript/function/anonymous/expected.json @@ -0,0 +1,238 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "f" + }, + "name": "f" + }, + "init": { + "type": "FunctionExpression", + "start": 10, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "name": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "params": [ + { + "type": "Identifier", + "start": 22, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "typeName": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + ], + "returnType": { + "type": "TSTypeReference", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "typeName": { + "type": "Identifier", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 31 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "body": { + "type": "BlockStatement", + "start": 32, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "body": [], + "directives": [] + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/declare/actual.js b/test/fixtures/typescript/function/declare/actual.js new file mode 100644 index 0000000000..f3a0c553cf --- /dev/null +++ b/test/fixtures/typescript/function/declare/actual.js @@ -0,0 +1,2 @@ +declare function f(): void; +declare function f(): T; diff --git a/test/fixtures/typescript/function/declare/expected.json b/test/fixtures/typescript/function/declare/expected.json new file mode 100644 index 0000000000..6b6eeb935e --- /dev/null +++ b/test/fixtures/typescript/function/declare/expected.json @@ -0,0 +1,189 @@ +{ + "type": "File", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "params": [], + "returnType": { + "type": "TSVoidKeyword", + "start": 22, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + "declare": true + }, + { + "type": "FunctionDeclaration", + "start": 28, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "id": { + "type": "Identifier", + "start": 45, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 47, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "name": { + "type": "Identifier", + "start": 47, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "params": [], + "returnType": { + "type": "TSTypeReference", + "start": 53, + "end": 54, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "typeName": { + "type": "Identifier", + "start": 53, + "end": 54, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 26 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/overloads/actual.js b/test/fixtures/typescript/function/overloads/actual.js new file mode 100644 index 0000000000..2fb36ce1be --- /dev/null +++ b/test/fixtures/typescript/function/overloads/actual.js @@ -0,0 +1,2 @@ +export function f(x: number): number; +export function f(x: string): string; diff --git a/test/fixtures/typescript/function/overloads/expected.json b/test/fixtures/typescript/function/overloads/expected.json new file mode 100644 index 0000000000..1ffe055e7a --- /dev/null +++ b/test/fixtures/typescript/function/overloads/expected.json @@ -0,0 +1,236 @@ +{ + "type": "File", + "start": 0, + "end": 75, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 75, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "FunctionDeclaration", + "start": 7, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "id": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 18, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 27 + } + } + } + } + ], + "returnType": { + "type": "TSNumberKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 36 + } + } + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 38, + "end": 75, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "FunctionDeclaration", + "start": 45, + "end": 75, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "id": { + "type": "Identifier", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 56, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 27 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 59, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 27 + } + } + } + } + ], + "returnType": { + "type": "TSStringKeyword", + "start": 68, + "end": 74, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/function/predicate-types/actual.js b/test/fixtures/typescript/function/predicate-types/actual.js new file mode 100644 index 0000000000..374c71766c --- /dev/null +++ b/test/fixtures/typescript/function/predicate-types/actual.js @@ -0,0 +1,2 @@ +function f(x: any): x is boolean {} +(function(x: any): x is boolean {}) diff --git a/test/fixtures/typescript/function/predicate-types/expected.json b/test/fixtures/typescript/function/predicate-types/expected.json new file mode 100644 index 0000000000..45cd0b6073 --- /dev/null +++ b/test/fixtures/typescript/function/predicate-types/expected.json @@ -0,0 +1,303 @@ +{ + "type": "File", + "start": 0, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "sourceType": "module", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "f" + }, + "name": "f" + }, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + } + } + } + ], + "returnType": { + "type": "TSTypePredicate", + "start": 20, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 32 + } + }, + "parameterName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 25, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 32 + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 33, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ExpressionStatement", + "start": 36, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 35 + } + }, + "expression": { + "type": "FunctionExpression", + "start": 37, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 46, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 16 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 49, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 16 + } + } + } + } + ], + "returnType": { + "type": "TSTypePredicate", + "start": 55, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "parameterName": { + "type": "Identifier", + "start": 55, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 60, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 31 + } + } + } + }, + "body": { + "type": "BlockStatement", + "start": 68, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 32 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "body": [], + "directives": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 36 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/import/equals-require/actual.js b/test/fixtures/typescript/import/equals-require/actual.js new file mode 100644 index 0000000000..2c82fffdd9 --- /dev/null +++ b/test/fixtures/typescript/import/equals-require/actual.js @@ -0,0 +1 @@ +import a = require("a"); diff --git a/test/fixtures/typescript/import/equals-require/expected.json b/test/fixtures/typescript/import/equals-require/expected.json new file mode 100644 index 0000000000..438e3f1a14 --- /dev/null +++ b/test/fixtures/typescript/import/equals-require/expected.json @@ -0,0 +1,102 @@ +{ + "type": "File", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSImportEqualsDeclaration", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "isExport": false, + "name": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "a" + }, + "name": "a" + }, + "moduleReference": { + "type": "TSExternalModuleReference", + "start": 11, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "expression": { + "type": "StringLiteral", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "extra": { + "rawValue": "a", + "raw": "\"a\"" + }, + "value": "a" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/import/equals/actual.js b/test/fixtures/typescript/import/equals/actual.js new file mode 100644 index 0000000000..4e414cfa82 --- /dev/null +++ b/test/fixtures/typescript/import/equals/actual.js @@ -0,0 +1 @@ +import A = B.C; diff --git a/test/fixtures/typescript/import/equals/expected.json b/test/fixtures/typescript/import/equals/expected.json new file mode 100644 index 0000000000..437770f234 --- /dev/null +++ b/test/fixtures/typescript/import/equals/expected.json @@ -0,0 +1,116 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSImportEqualsDeclaration", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "isExport": false, + "name": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "A" + }, + "name": "A" + }, + "moduleReference": { + "type": "TSQualifiedName", + "start": 11, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "left": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "B" + }, + "name": "B" + }, + "right": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "C" + }, + "name": "C" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/import/export-import-require/actual.js b/test/fixtures/typescript/import/export-import-require/actual.js new file mode 100644 index 0000000000..8b6df4184e --- /dev/null +++ b/test/fixtures/typescript/import/export-import-require/actual.js @@ -0,0 +1 @@ +export import a = require("a"); diff --git a/test/fixtures/typescript/import/export-import-require/expected.json b/test/fixtures/typescript/import/export-import-require/expected.json new file mode 100644 index 0000000000..290b305bbd --- /dev/null +++ b/test/fixtures/typescript/import/export-import-require/expected.json @@ -0,0 +1,102 @@ +{ + "type": "File", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSImportEqualsDeclaration", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "isExport": true, + "name": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "a" + }, + "name": "a" + }, + "moduleReference": { + "type": "TSExternalModuleReference", + "start": 18, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "expression": { + "type": "StringLiteral", + "start": 26, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "extra": { + "rawValue": "a", + "raw": "\"a\"" + }, + "value": "a" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/import/export-import/actual.js b/test/fixtures/typescript/import/export-import/actual.js new file mode 100644 index 0000000000..fb89f3bc50 --- /dev/null +++ b/test/fixtures/typescript/import/export-import/actual.js @@ -0,0 +1 @@ +export import A = B.C; diff --git a/test/fixtures/typescript/import/export-import/expected.json b/test/fixtures/typescript/import/export-import/expected.json new file mode 100644 index 0000000000..e88bee97e1 --- /dev/null +++ b/test/fixtures/typescript/import/export-import/expected.json @@ -0,0 +1,116 @@ +{ + "type": "File", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSImportEqualsDeclaration", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "isExport": true, + "name": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "A" + }, + "name": "A" + }, + "moduleReference": { + "type": "TSQualifiedName", + "start": 18, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "left": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "B" + }, + "name": "B" + }, + "right": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "C" + }, + "name": "C" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/import/not-top-level/actual.js b/test/fixtures/typescript/import/not-top-level/actual.js new file mode 100644 index 0000000000..b6278d2b78 --- /dev/null +++ b/test/fixtures/typescript/import/not-top-level/actual.js @@ -0,0 +1,3 @@ +declare module "m" { + import * as a from "a"; +} diff --git a/test/fixtures/typescript/import/not-top-level/expected.json b/test/fixtures/typescript/import/not-top-level/expected.json new file mode 100644 index 0000000000..33747240d2 --- /dev/null +++ b/test/fixtures/typescript/import/not-top-level/expected.json @@ -0,0 +1,156 @@ +{ + "type": "File", + "start": 0, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "StringLiteral", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "extra": { + "rawValue": "m", + "raw": "\"m\"" + }, + "value": "m" + }, + "body": { + "type": "TSModuleBlock", + "start": 19, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "statements": [ + { + "type": "ImportDeclaration", + "start": 25, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "local": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "a" + }, + "name": "a" + } + } + ], + "source": { + "type": "StringLiteral", + "start": 44, + "end": 47, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "extra": { + "rawValue": "a", + "raw": "\"a\"" + }, + "value": "a" + } + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/call-signature/actual.js b/test/fixtures/typescript/interface/call-signature/actual.js new file mode 100644 index 0000000000..5d6b541719 --- /dev/null +++ b/test/fixtures/typescript/interface/call-signature/actual.js @@ -0,0 +1,3 @@ +interface I { + (x: number): void; +} diff --git a/test/fixtures/typescript/interface/call-signature/expected.json b/test/fixtures/typescript/interface/call-signature/expected.json new file mode 100644 index 0000000000..9a566496e1 --- /dev/null +++ b/test/fixtures/typescript/interface/call-signature/expected.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSCallSignatureDeclaration", + "start": 18, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 19, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + ], + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 31, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 21 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/construct-signature/actual.js b/test/fixtures/typescript/interface/construct-signature/actual.js new file mode 100644 index 0000000000..d726e4c4ed --- /dev/null +++ b/test/fixtures/typescript/interface/construct-signature/actual.js @@ -0,0 +1,3 @@ +interface I { + new (x: number): void; +} diff --git a/test/fixtures/typescript/interface/construct-signature/expected.json b/test/fixtures/typescript/interface/construct-signature/expected.json new file mode 100644 index 0000000000..6000d12875 --- /dev/null +++ b/test/fixtures/typescript/interface/construct-signature/expected.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSConstructSignatureDeclaration", + "start": 18, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 26 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 23, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + } + } + } + ], + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 35, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 25 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/export/actual.js b/test/fixtures/typescript/interface/export/actual.js new file mode 100644 index 0000000000..509789ea6f --- /dev/null +++ b/test/fixtures/typescript/interface/export/actual.js @@ -0,0 +1,2 @@ +export interface I {} +// `export default` does not work with interfaces diff --git a/test/fixtures/typescript/interface/export/expected.json b/test/fixtures/typescript/interface/export/expected.json new file mode 100644 index 0000000000..41c3acafc8 --- /dev/null +++ b/test/fixtures/typescript/interface/export/expected.json @@ -0,0 +1,121 @@ +{ + "type": "File", + "start": 0, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 49 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 71, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 49 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSInterfaceDeclaration", + "start": 7, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "name": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [], + "trailingComments": null + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " `export default` does not work with interfaces", + "start": 22, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 49 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " `export default` does not work with interfaces", + "start": 22, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 49 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/extends/actual.js b/test/fixtures/typescript/interface/extends/actual.js new file mode 100644 index 0000000000..8cf474f227 --- /dev/null +++ b/test/fixtures/typescript/interface/extends/actual.js @@ -0,0 +1 @@ +interface I extends X.Y {} diff --git a/test/fixtures/typescript/interface/extends/expected.json b/test/fixtures/typescript/interface/extends/expected.json new file mode 100644 index 0000000000..0e854c0bb7 --- /dev/null +++ b/test/fixtures/typescript/interface/extends/expected.json @@ -0,0 +1,167 @@ +{ + "type": "File", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "extends": [ + { + "type": "TSExpressionWithTypeArguments", + "start": 20, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "expression": { + "type": "TSQualifiedName", + "start": 20, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "left": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "X" + }, + "name": "X" + }, + "right": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "Y" + }, + "name": "Y" + } + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "typeName": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + }, + "identifierName": "Z" + }, + "name": "Z" + } + } + ] + } + ], + "members": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/generic/actual.js b/test/fixtures/typescript/interface/generic/actual.js new file mode 100644 index 0000000000..62f2ede348 --- /dev/null +++ b/test/fixtures/typescript/interface/generic/actual.js @@ -0,0 +1 @@ +interface I {} diff --git a/test/fixtures/typescript/interface/generic/expected.json b/test/fixtures/typescript/interface/generic/expected.json new file mode 100644 index 0000000000..163634139c --- /dev/null +++ b/test/fixtures/typescript/interface/generic/expected.json @@ -0,0 +1,181 @@ +{ + "type": "File", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 12, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "name": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "T" + }, + "name": "T" + }, + "constraint": { + "type": "TSObjectKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 28 + } + } + }, + "default": { + "type": "TSTypeLiteral", + "start": 31, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 33, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 36, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 42 + } + } + } + } + ] + } + } + ], + "members": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/index-accessor/actual.js b/test/fixtures/typescript/interface/index-accessor/actual.js new file mode 100644 index 0000000000..2c676f629f --- /dev/null +++ b/test/fixtures/typescript/interface/index-accessor/actual.js @@ -0,0 +1,3 @@ +interface I { + [s: string]: number; +} diff --git a/test/fixtures/typescript/interface/index-accessor/expected.json b/test/fixtures/typescript/interface/index-accessor/expected.json new file mode 100644 index 0000000000..7e46b8d956 --- /dev/null +++ b/test/fixtures/typescript/interface/index-accessor/expected.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSIndexSignatureDeclaration", + "start": 18, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "s" + }, + "name": "s", + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + ], + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/method-computed/actual.js b/test/fixtures/typescript/interface/method-computed/actual.js new file mode 100644 index 0000000000..688dc322af --- /dev/null +++ b/test/fixtures/typescript/interface/method-computed/actual.js @@ -0,0 +1,4 @@ +interface I { + [Symbol.iterator](): void; + [Symbol.iterator]?(): number; +} diff --git a/test/fixtures/typescript/interface/method-computed/expected.json b/test/fixtures/typescript/interface/method-computed/expected.json new file mode 100644 index 0000000000..8baebd2959 --- /dev/null +++ b/test/fixtures/typescript/interface/method-computed/expected.json @@ -0,0 +1,233 @@ +{ + "type": "File", + "start": 0, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "computed": true, + "name": { + "type": "MemberExpression", + "start": 19, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 26, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "parameters": [], + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 39, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + } + } + } + }, + { + "type": "TSMethodSignature", + "start": 49, + "end": 78, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 33 + } + }, + "computed": true, + "name": { + "type": "MemberExpression", + "start": 50, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 50, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 57, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "optional": true, + "parameters": [], + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 71, + "end": 77, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 32 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/method-generic/actual.js b/test/fixtures/typescript/interface/method-generic/actual.js new file mode 100644 index 0000000000..27f962006e --- /dev/null +++ b/test/fixtures/typescript/interface/method-generic/actual.js @@ -0,0 +1,3 @@ +interface I { + m(): T; +} diff --git a/test/fixtures/typescript/interface/method-generic/expected.json b/test/fixtures/typescript/interface/method-generic/expected.json new file mode 100644 index 0000000000..991ab91a96 --- /dev/null +++ b/test/fixtures/typescript/interface/method-generic/expected.json @@ -0,0 +1,248 @@ +{ + "type": "File", + "start": 0, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 45 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 20, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "name": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + }, + "constraint": { + "type": "TSObjectKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 22 + } + } + }, + "default": { + "type": "TSTypeLiteral", + "start": 39, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 41, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 36 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 41, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 28 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 44, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } + } + } + } + ] + } + } + ], + "parameters": [], + "typeAnnotation": { + "type": "TSTypeReference", + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 43 + }, + "end": { + "line": 2, + "column": 44 + } + }, + "typeName": { + "type": "Identifier", + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 43 + }, + "end": { + "line": 2, + "column": 44 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/method-optional/actual.js b/test/fixtures/typescript/interface/method-optional/actual.js new file mode 100644 index 0000000000..9ceae32a53 --- /dev/null +++ b/test/fixtures/typescript/interface/method-optional/actual.js @@ -0,0 +1,3 @@ +interface I { + m?(): void; +} diff --git a/test/fixtures/typescript/interface/method-optional/expected.json b/test/fixtures/typescript/interface/method-optional/expected.json new file mode 100644 index 0000000000..d6b75e8883 --- /dev/null +++ b/test/fixtures/typescript/interface/method-optional/expected.json @@ -0,0 +1,118 @@ +{ + "type": "File", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "optional": true, + "parameters": [], + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 24, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/method-plain/actual.js b/test/fixtures/typescript/interface/method-plain/actual.js new file mode 100644 index 0000000000..8d4d83dcce --- /dev/null +++ b/test/fixtures/typescript/interface/method-plain/actual.js @@ -0,0 +1,4 @@ +interface I { + m(); + m(x?: number, ...y: number[]): void; +} diff --git a/test/fixtures/typescript/interface/method-plain/expected.json b/test/fixtures/typescript/interface/method-plain/expected.json new file mode 100644 index 0000000000..1a75413e26 --- /dev/null +++ b/test/fixtures/typescript/interface/method-plain/expected.json @@ -0,0 +1,247 @@ +{ + "type": "File", + "start": 0, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "parameters": [] + }, + { + "type": "TSMethodSignature", + "start": 27, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 40 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" + }, + "parameters": [ + { + "type": "Identifier", + "start": 29, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 16 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 16 + } + } + } + }, + { + "type": "RestElement", + "start": 41, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 18 + }, + "end": { + "line": 3, + "column": 32 + } + }, + "argument": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 22 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TSArrayType", + "start": 47, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 32 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 47, + "end": 53, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 58, + "end": 62, + "loc": { + "start": { + "line": 3, + "column": 35 + }, + "end": { + "line": 3, + "column": 39 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/modifiers/actual.js b/test/fixtures/typescript/interface/modifiers/actual.js new file mode 100644 index 0000000000..e5836883ef --- /dev/null +++ b/test/fixtures/typescript/interface/modifiers/actual.js @@ -0,0 +1,3 @@ +interface I { + readonly x: number; +} diff --git a/test/fixtures/typescript/interface/modifiers/expected.json b/test/fixtures/typescript/interface/modifiers/expected.json new file mode 100644 index 0000000000..be81ef67b2 --- /dev/null +++ b/test/fixtures/typescript/interface/modifiers/expected.json @@ -0,0 +1,117 @@ +{ + "type": "File", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 18, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "x" + }, + "name": "x" + }, + "readonly": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 22 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/properties/actual.js b/test/fixtures/typescript/interface/properties/actual.js new file mode 100644 index 0000000000..94c10ef4f5 --- /dev/null +++ b/test/fixtures/typescript/interface/properties/actual.js @@ -0,0 +1,5 @@ +interface I { + x; + y: number; + z?: number; +} diff --git a/test/fixtures/typescript/interface/properties/expected.json b/test/fixtures/typescript/interface/properties/expected.json new file mode 100644 index 0000000000..f72369911f --- /dev/null +++ b/test/fixtures/typescript/interface/properties/expected.json @@ -0,0 +1,198 @@ +{ + "type": "File", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 18, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + } + }, + { + "type": "TSPropertySignature", + "start": 25, + "end": 35, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 14 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 13 + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 40, + "end": 51, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 15 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 40, + "end": 41, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "z" + }, + "name": "z" + }, + "optional": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 44, + "end": 50, + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 14 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/property-computed/actual.js b/test/fixtures/typescript/interface/property-computed/actual.js new file mode 100644 index 0000000000..78f3ac4ab0 --- /dev/null +++ b/test/fixtures/typescript/interface/property-computed/actual.js @@ -0,0 +1,4 @@ +interface I { + [Symbol.iterator]: number; + [Symbol.iterator]?: number; +} diff --git a/test/fixtures/typescript/interface/property-computed/expected.json b/test/fixtures/typescript/interface/property-computed/expected.json new file mode 100644 index 0000000000..e066168546 --- /dev/null +++ b/test/fixtures/typescript/interface/property-computed/expected.json @@ -0,0 +1,231 @@ +{ + "type": "File", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 18, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 30 + } + }, + "computed": true, + "name": { + "type": "MemberExpression", + "start": 19, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 26, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 37, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 29 + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 49, + "end": 76, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 31 + } + }, + "computed": true, + "name": { + "type": "MemberExpression", + "start": 50, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 50, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" + }, + "name": "Symbol" + }, + "property": { + "type": "Identifier", + "start": 57, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" + }, + "computed": false + }, + "optional": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 69, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/property-named-public/actual.js b/test/fixtures/typescript/interface/property-named-public/actual.js new file mode 100644 index 0000000000..ef6e1f3bf1 --- /dev/null +++ b/test/fixtures/typescript/interface/property-named-public/actual.js @@ -0,0 +1,3 @@ +interface I { + public: number; +} diff --git a/test/fixtures/typescript/interface/property-named-public/expected.json b/test/fixtures/typescript/interface/property-named-public/expected.json new file mode 100644 index 0000000000..8eaedc0094 --- /dev/null +++ b/test/fixtures/typescript/interface/property-named-public/expected.json @@ -0,0 +1,116 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 15, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "public" + }, + "name": "public" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 23, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 15 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/reserved-method-name/actual.js b/test/fixtures/typescript/interface/reserved-method-name/actual.js new file mode 100644 index 0000000000..76dff7a531 --- /dev/null +++ b/test/fixtures/typescript/interface/reserved-method-name/actual.js @@ -0,0 +1,3 @@ +interface I { + catch(): void; +} diff --git a/test/fixtures/typescript/interface/reserved-method-name/expected.json b/test/fixtures/typescript/interface/reserved-method-name/expected.json new file mode 100644 index 0000000000..4b745a912f --- /dev/null +++ b/test/fixtures/typescript/interface/reserved-method-name/expected.json @@ -0,0 +1,117 @@ +{ + "type": "File", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "I" + }, + "name": "I" + }, + "members": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "catch" + }, + "name": "catch" + }, + "parameters": [], + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 27, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 17 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/interface/separators/actual.js b/test/fixtures/typescript/interface/separators/actual.js new file mode 100644 index 0000000000..f8e941cd41 --- /dev/null +++ b/test/fixtures/typescript/interface/separators/actual.js @@ -0,0 +1,6 @@ +interface Comma { x: number, y: number } +interface Semi { x: number; y: number } +interface Newline { + x: number + y: number +} diff --git a/test/fixtures/typescript/interface/separators/expected.json b/test/fixtures/typescript/interface/separators/expected.json new file mode 100644 index 0000000000..bae854af32 --- /dev/null +++ b/test/fixtures/typescript/interface/separators/expected.json @@ -0,0 +1,424 @@ +{ + "type": "File", + "start": 0, + "end": 130, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 130, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "Comma" + }, + "name": "Comma" + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 18, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 27 + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 29, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 38 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 30 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 38 + } + } + } + } + ] + }, + { + "type": "TSInterfaceDeclaration", + "start": 41, + "end": 80, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 39 + } + }, + "name": { + "type": "Identifier", + "start": 51, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "Semi" + }, + "name": "Semi" + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 58, + "end": 68, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 61, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 26 + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 69, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 37 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 69, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 29 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 72, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 37 + } + } + } + } + ] + }, + { + "type": "TSInterfaceDeclaration", + "start": 81, + "end": 130, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 91, + "end": 98, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 17 + }, + "identifierName": "Newline" + }, + "name": "Newline" + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 105, + "end": 114, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 105, + "end": 106, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 108, + "end": 114, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + } + } + }, + { + "type": "TSPropertySignature", + "start": 119, + "end": 128, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 13 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 119, + "end": 120, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "y" + }, + "name": "y" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 122, + "end": 128, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 13 + } + } + } + } + ] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/body-declare/actual.js b/test/fixtures/typescript/module-namespace/body-declare/actual.js new file mode 100644 index 0000000000..2723dc49e2 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-declare/actual.js @@ -0,0 +1,3 @@ +declare namespace N { + const x: number; +} diff --git a/test/fixtures/typescript/module-namespace/body-declare/expected.json b/test/fixtures/typescript/module-namespace/body-declare/expected.json new file mode 100644 index 0000000000..7146d59db1 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-declare/expected.json @@ -0,0 +1,149 @@ +{ + "type": "File", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 20, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "statements": [ + { + "type": "VariableDeclaration", + "start": 26, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 19 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 35, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 19 + } + } + } + } + } + ], + "kind": "const" + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/body-nested-declare/actual.js b/test/fixtures/typescript/module-namespace/body-nested-declare/actual.js new file mode 100644 index 0000000000..8ed35530c9 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-nested-declare/actual.js @@ -0,0 +1,5 @@ +declare namespace A { + namespace B { + const x: number; + } +} diff --git a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json new file mode 100644 index 0000000000..422dabe179 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json @@ -0,0 +1,198 @@ +{ + "type": "File", + "start": 0, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSModuleBlock", + "start": 20, + "end": 72, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "statements": [ + { + "type": "TSModuleDeclaration", + "start": 26, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "name": { + "type": "Identifier", + "start": 36, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "B" + }, + "name": "B" + }, + "body": { + "type": "TSModuleBlock", + "start": 38, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "statements": [ + { + "type": "VariableDeclaration", + "start": 48, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 24 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 54, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "id": { + "type": "Identifier", + "start": 54, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 14 + }, + "end": { + "line": 3, + "column": 23 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 57, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 23 + } + } + } + } + } + ], + "kind": "const" + } + ] + } + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/body-nested/actual.js b/test/fixtures/typescript/module-namespace/body-nested/actual.js new file mode 100644 index 0000000000..b03744fb9d --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-nested/actual.js @@ -0,0 +1,4 @@ +namespace A { + namespace B { + } +} diff --git a/test/fixtures/typescript/module-namespace/body-nested/expected.json b/test/fixtures/typescript/module-namespace/body-nested/expected.json new file mode 100644 index 0000000000..44f550cf28 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body-nested/expected.json @@ -0,0 +1,131 @@ +{ + "type": "File", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSModuleBlock", + "start": 12, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "statements": [ + { + "type": "TSModuleDeclaration", + "start": 18, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "name": { + "type": "Identifier", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "B" + }, + "name": "B" + }, + "body": { + "type": "TSModuleBlock", + "start": 30, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 3, + "column": 5 + } + }, + "statements": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/body/actual.js b/test/fixtures/typescript/module-namespace/body/actual.js new file mode 100644 index 0000000000..fbfadc1859 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body/actual.js @@ -0,0 +1,3 @@ +namespace N { + const x = 0; +} diff --git a/test/fixtures/typescript/module-namespace/body/expected.json b/test/fixtures/typescript/module-namespace/body/expected.json new file mode 100644 index 0000000000..4fdfe3070e --- /dev/null +++ b/test/fixtures/typescript/module-namespace/body/expected.json @@ -0,0 +1,153 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 12, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "statements": [ + { + "type": "VariableDeclaration", + "start": 18, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 24, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "NumericLiteral", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "kind": "const" + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/declare-shorthand/actual.js b/test/fixtures/typescript/module-namespace/declare-shorthand/actual.js new file mode 100644 index 0000000000..b6cc3a564c --- /dev/null +++ b/test/fixtures/typescript/module-namespace/declare-shorthand/actual.js @@ -0,0 +1 @@ +declare module "m"; diff --git a/test/fixtures/typescript/module-namespace/declare-shorthand/expected.json b/test/fixtures/typescript/module-namespace/declare-shorthand/expected.json new file mode 100644 index 0000000000..ddde3e40cb --- /dev/null +++ b/test/fixtures/typescript/module-namespace/declare-shorthand/expected.json @@ -0,0 +1,70 @@ +{ + "type": "File", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "name": { + "type": "StringLiteral", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "extra": { + "rawValue": "m", + "raw": "\"m\"" + }, + "value": "m" + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/global-in-module/actual.js b/test/fixtures/typescript/module-namespace/global-in-module/actual.js new file mode 100644 index 0000000000..8da25cacf8 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/global-in-module/actual.js @@ -0,0 +1,5 @@ +declare module "m" { + global { + var x: number; + } +} diff --git a/test/fixtures/typescript/module-namespace/global-in-module/expected.json b/test/fixtures/typescript/module-namespace/global-in-module/expected.json new file mode 100644 index 0000000000..b1323002d9 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/global-in-module/expected.json @@ -0,0 +1,203 @@ +{ + "type": "File", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "name": { + "type": "StringLiteral", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "extra": { + "rawValue": "m", + "raw": "\"m\"" + }, + "value": "m" + }, + "body": { + "type": "TSModuleBlock", + "start": 19, + "end": 64, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "statements": [ + { + "type": "TSModuleDeclaration", + "start": 25, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "global": true, + "name": { + "type": "Identifier", + "start": 25, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "global" + }, + "name": "global" + }, + "body": { + "type": "TSModuleBlock", + "start": 32, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "statements": [ + { + "type": "VariableDeclaration", + "start": 42, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 46, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 46, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 21 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 49, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 21 + } + } + } + }, + "init": null + } + ], + "kind": "var" + } + ] + } + } + ] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/head-declare/actual.js b/test/fixtures/typescript/module-namespace/head-declare/actual.js new file mode 100644 index 0000000000..d70669ebc7 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head-declare/actual.js @@ -0,0 +1,4 @@ +declare namespace N.M {} +declare module M {} +declare module "m" {} +declare global {} diff --git a/test/fixtures/typescript/module-namespace/head-declare/expected.json b/test/fixtures/typescript/module-namespace/head-declare/expected.json new file mode 100644 index 0000000000..840c9d1bd3 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head-declare/expected.json @@ -0,0 +1,266 @@ +{ + "type": "File", + "start": 0, + "end": 84, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 84, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleDeclaration", + "start": 20, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "name": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "M" + }, + "name": "M" + }, + "body": { + "type": "TSModuleBlock", + "start": 22, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "statements": [] + } + }, + "declare": true + }, + { + "type": "TSModuleDeclaration", + "start": 25, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "name": { + "type": "Identifier", + "start": 40, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 16 + }, + "identifierName": "M" + }, + "name": "M" + }, + "body": { + "type": "TSModuleBlock", + "start": 42, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "statements": [] + }, + "declare": true + }, + { + "type": "TSModuleDeclaration", + "start": 45, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "name": { + "type": "StringLiteral", + "start": 60, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 18 + } + }, + "extra": { + "rawValue": "m", + "raw": "\"m\"" + }, + "value": "m" + }, + "body": { + "type": "TSModuleBlock", + "start": 64, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "statements": [] + }, + "declare": true + }, + { + "type": "TSModuleDeclaration", + "start": 67, + "end": 84, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "global": true, + "name": { + "type": "Identifier", + "start": 75, + "end": 81, + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 14 + }, + "identifierName": "global" + }, + "name": "global" + }, + "body": { + "type": "TSModuleBlock", + "start": 82, + "end": 84, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 17 + } + }, + "statements": [] + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/head-export/actual.js b/test/fixtures/typescript/module-namespace/head-export/actual.js new file mode 100644 index 0000000000..a3a6c205fa --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head-export/actual.js @@ -0,0 +1,2 @@ +export namespace X.Y {} +export module X {} diff --git a/test/fixtures/typescript/module-namespace/head-export/expected.json b/test/fixtures/typescript/module-namespace/head-export/expected.json new file mode 100644 index 0000000000..8443609e8f --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head-export/expected.json @@ -0,0 +1,196 @@ +{ + "type": "File", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 7, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "name": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "X" + }, + "name": "X" + }, + "body": { + "type": "TSModuleDeclaration", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "name": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "Y" + }, + "name": "Y" + }, + "body": { + "type": "TSModuleBlock", + "start": 21, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "statements": [] + } + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 24, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 31, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "name": { + "type": "Identifier", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "X" + }, + "name": "X" + }, + "body": { + "type": "TSModuleBlock", + "start": 40, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "statements": [] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/module-namespace/head/actual.js b/test/fixtures/typescript/module-namespace/head/actual.js new file mode 100644 index 0000000000..f764e1bd26 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head/actual.js @@ -0,0 +1,4 @@ +namespace N {} +namespace M.N.O {} +module M {} +module "m" {} diff --git a/test/fixtures/typescript/module-namespace/head/expected.json b/test/fixtures/typescript/module-namespace/head/expected.json new file mode 100644 index 0000000000..fe0ead1f44 --- /dev/null +++ b/test/fixtures/typescript/module-namespace/head/expected.json @@ -0,0 +1,293 @@ +{ + "type": "File", + "start": 0, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "name": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "statements": [] + } + }, + { + "type": "TSModuleDeclaration", + "start": 15, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "name": { + "type": "Identifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "M" + }, + "name": "M" + }, + "body": { + "type": "TSModuleDeclaration", + "start": 27, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "name": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleDeclaration", + "start": 29, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "name": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "O" + }, + "name": "O" + }, + "body": { + "type": "TSModuleBlock", + "start": 31, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "statements": [] + } + } + } + }, + { + "type": "TSModuleDeclaration", + "start": 34, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 11 + } + }, + "name": { + "type": "Identifier", + "start": 41, + "end": 42, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 8 + }, + "identifierName": "M" + }, + "name": "M" + }, + "body": { + "type": "TSModuleBlock", + "start": 43, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 11 + } + }, + "statements": [] + } + }, + { + "type": "TSModuleDeclaration", + "start": 46, + "end": 59, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "name": { + "type": "StringLiteral", + "start": 53, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 10 + } + }, + "extra": { + "rawValue": "m", + "raw": "\"m\"" + }, + "value": "m" + }, + "body": { + "type": "TSModuleBlock", + "start": 57, + "end": 59, + "loc": { + "start": { + "line": 4, + "column": 11 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "statements": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/options.json b/test/fixtures/typescript/options.json new file mode 100644 index 0000000000..fe9bffaa5e --- /dev/null +++ b/test/fixtures/typescript/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "plugins": ["typescript"] +} diff --git a/test/fixtures/typescript/tsx/brace-is-block/actual.js b/test/fixtures/typescript/tsx/brace-is-block/actual.js new file mode 100644 index 0000000000..dbe88dfe03 --- /dev/null +++ b/test/fixtures/typescript/tsx/brace-is-block/actual.js @@ -0,0 +1,3 @@ +// Regression test for tokenizer bug where the `{` after `` was considered a JSX interpolation. +class C extends D {} + diff --git a/test/fixtures/typescript/tsx/brace-is-block/expected.json b/test/fixtures/typescript/tsx/brace-is-block/expected.json new file mode 100644 index 0000000000..a6ecf227da --- /dev/null +++ b/test/fixtures/typescript/tsx/brace-is-block/expected.json @@ -0,0 +1,235 @@ +{ + "type": "File", + "start": 0, + "end": 127, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 127, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 99, + "end": 122, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "id": { + "type": "Identifier", + "start": 105, + "end": 106, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C", + "leadingComments": null + }, + "superClass": { + "type": "Identifier", + "start": 115, + "end": 116, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "D" + }, + "name": "D" + }, + "superTypeParameters": [ + { + "type": "TSTypeReference", + "start": 117, + "end": 118, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 19 + } + }, + "typeName": { + "type": "Identifier", + "start": 117, + "end": 118, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 19 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "body": { + "type": "ClassBody", + "start": 120, + "end": 122, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 23 + } + }, + "body": [] + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Regression test for tokenizer bug where the `{` after `` was considered a JSX interpolation.", + "start": 0, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 98 + } + } + } + ] + }, + { + "type": "ExpressionStatement", + "start": 123, + "end": 127, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "expression": { + "type": "JSXElement", + "start": 123, + "end": 127, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "openingElement": { + "type": "JSXOpeningElement", + "start": 123, + "end": 127, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 4 + } + }, + "attributes": [], + "name": { + "type": "JSXIdentifier", + "start": 124, + "end": 125, + "loc": { + "start": { + "line": 3, + "column": 1 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "name": "C" + }, + "selfClosing": true + }, + "closingElement": null, + "children": [] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Regression test for tokenizer bug where the `{` after `` was considered a JSX interpolation.", + "start": 0, + "end": 98, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 98 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/tsx/options.json b/test/fixtures/typescript/tsx/options.json new file mode 100644 index 0000000000..3a319216ff --- /dev/null +++ b/test/fixtures/typescript/tsx/options.json @@ -0,0 +1,4 @@ +{ + "plugins": ["jsx", "typescript"], + "sourceType": "module" +} diff --git a/test/fixtures/typescript/type-alias/declare/actual.js b/test/fixtures/typescript/type-alias/declare/actual.js new file mode 100644 index 0000000000..301931812c --- /dev/null +++ b/test/fixtures/typescript/type-alias/declare/actual.js @@ -0,0 +1 @@ +declare type T = number; diff --git a/test/fixtures/typescript/type-alias/declare/expected.json b/test/fixtures/typescript/type-alias/declare/expected.json new file mode 100644 index 0000000000..05186b6446 --- /dev/null +++ b/test/fixtures/typescript/type-alias/declare/expected.json @@ -0,0 +1,82 @@ +{ + "type": "File", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "name": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-alias/export/actual.js b/test/fixtures/typescript/type-alias/export/actual.js new file mode 100644 index 0000000000..0ffb0fcdd6 --- /dev/null +++ b/test/fixtures/typescript/type-alias/export/actual.js @@ -0,0 +1,2 @@ +export type T = number; +// `export default type` is not valid. diff --git a/test/fixtures/typescript/type-alias/export/expected.json b/test/fixtures/typescript/type-alias/export/expected.json new file mode 100644 index 0000000000..969254f6a9 --- /dev/null +++ b/test/fixtures/typescript/type-alias/export/expected.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 62, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 62, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 38 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSTypeAliasDeclaration", + "start": 7, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "name": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 16, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 22 + } + } + }, + "trailingComments": null + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " `export default type` is not valid.", + "start": 24, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 38 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " `export default type` is not valid.", + "start": 24, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 38 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-alias/generic-complex/actual.js b/test/fixtures/typescript/type-alias/generic-complex/actual.js new file mode 100644 index 0000000000..9fb8928d4f --- /dev/null +++ b/test/fixtures/typescript/type-alias/generic-complex/actual.js @@ -0,0 +1 @@ +type T = Array; diff --git a/test/fixtures/typescript/type-alias/generic-complex/expected.json b/test/fixtures/typescript/type-alias/generic-complex/expected.json new file mode 100644 index 0000000000..45547371c0 --- /dev/null +++ b/test/fixtures/typescript/type-alias/generic-complex/expected.json @@ -0,0 +1,246 @@ +{ + "type": "File", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 52 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 52 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 52 + } + }, + "name": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 7, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "name": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "U" + }, + "name": "U" + }, + "constraint": { + "type": "TSObjectKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + "default": { + "type": "TSTypeLiteral", + "start": 26, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 39 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 28, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 29 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 37 + } + } + } + } + ] + } + } + ], + "typeAnnotation": { + "type": "TSTypeReference", + "start": 43, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 43 + }, + "end": { + "line": 1, + "column": 51 + } + }, + "typeName": { + "type": "Identifier", + "start": 43, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 43 + }, + "end": { + "line": 1, + "column": 48 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 49, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 50 + } + }, + "typeName": { + "type": "Identifier", + "start": 49, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 50 + }, + "identifierName": "U" + }, + "name": "U" + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-alias/generic/actual.js b/test/fixtures/typescript/type-alias/generic/actual.js new file mode 100644 index 0000000000..0e910abfc6 --- /dev/null +++ b/test/fixtures/typescript/type-alias/generic/actual.js @@ -0,0 +1 @@ +type T = U; diff --git a/test/fixtures/typescript/type-alias/generic/expected.json b/test/fixtures/typescript/type-alias/generic/expected.json new file mode 100644 index 0000000000..df6951f90e --- /dev/null +++ b/test/fixtures/typescript/type-alias/generic/expected.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "name": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "name": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "U" + }, + "name": "U" + } + } + ], + "typeAnnotation": { + "type": "TSTypeReference", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "typeName": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "U" + }, + "name": "U" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-alias/plain/actual.js b/test/fixtures/typescript/type-alias/plain/actual.js new file mode 100644 index 0000000000..42126b96d0 --- /dev/null +++ b/test/fixtures/typescript/type-alias/plain/actual.js @@ -0,0 +1 @@ +type T = number; diff --git a/test/fixtures/typescript/type-alias/plain/expected.json b/test/fixtures/typescript/type-alias/plain/expected.json new file mode 100644 index 0000000000..dee2026e1e --- /dev/null +++ b/test/fixtures/typescript/type-alias/plain/expected.json @@ -0,0 +1,81 @@ +{ + "type": "File", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "name": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-arguments/call/actual.js b/test/fixtures/typescript/type-arguments/call/actual.js new file mode 100644 index 0000000000..3a4d08a538 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/call/actual.js @@ -0,0 +1,2 @@ +f(); +f(); diff --git a/test/fixtures/typescript/type-arguments/call/expected.json b/test/fixtures/typescript/type-arguments/call/expected.json new file mode 100644 index 0000000000..1ea668f381 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/call/expected.json @@ -0,0 +1,230 @@ +{ + "type": "File", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "callee": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [], + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "typeName": { + "type": "Identifier", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + }, + { + "type": "ExpressionStatement", + "start": 8, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "expression": { + "type": "CallExpression", + "start": 8, + "end": 17, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 9 + } + }, + "callee": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + }, + "identifierName": "f" + }, + "name": "f" + }, + "arguments": [], + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 3 + } + }, + "typeName": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 3 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + { + "type": "TSTypeReference", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "typeName": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "U" + }, + "name": "U" + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/type-arguments/new/actual.js b/test/fixtures/typescript/type-arguments/new/actual.js new file mode 100644 index 0000000000..d474bb65c3 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new/actual.js @@ -0,0 +1,2 @@ +new C(); +new C(); diff --git a/test/fixtures/typescript/type-arguments/new/expected.json b/test/fixtures/typescript/type-arguments/new/expected.json new file mode 100644 index 0000000000..33908a10b6 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new/expected.json @@ -0,0 +1,230 @@ +{ + "type": "File", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "expression": { + "type": "NewExpression", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "callee": { + "type": "Identifier", + "start": 4, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + }, + "identifierName": "C" + }, + "name": "C" + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "typeName": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "arguments": [] + } + }, + { + "type": "ExpressionStatement", + "start": 12, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "expression": { + "type": "NewExpression", + "start": 12, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "callee": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "C" + }, + "name": "C" + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "typeName": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + { + "type": "TSTypeReference", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "typeName": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "U" + }, + "name": "U" + } + } + ], + "arguments": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/array/actual.js b/test/fixtures/typescript/types/array/actual.js new file mode 100644 index 0000000000..aff94656eb --- /dev/null +++ b/test/fixtures/typescript/types/array/actual.js @@ -0,0 +1 @@ +let arr: number[][]; diff --git a/test/fixtures/typescript/types/array/expected.json b/test/fixtures/typescript/types/array/expected.json new file mode 100644 index 0000000000..17ac9f8707 --- /dev/null +++ b/test/fixtures/typescript/types/array/expected.json @@ -0,0 +1,130 @@ +{ + "type": "File", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "arr" + }, + "name": "arr", + "typeAnnotation": { + "type": "TSArrayType", + "start": 9, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "elementType": { + "type": "TSArrayType", + "start": 9, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/function-generic/actual.js b/test/fixtures/typescript/types/function-generic/actual.js new file mode 100644 index 0000000000..61d8ece714 --- /dev/null +++ b/test/fixtures/typescript/types/function-generic/actual.js @@ -0,0 +1 @@ +let f: (a: T) => T; diff --git a/test/fixtures/typescript/types/function-generic/expected.json b/test/fixtures/typescript/types/function-generic/expected.json new file mode 100644 index 0000000000..8b8abdfa41 --- /dev/null +++ b/test/fixtures/typescript/types/function-generic/expected.json @@ -0,0 +1,217 @@ +{ + "type": "File", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "f" + }, + "name": "f", + "typeAnnotation": { + "type": "TSFunctionType", + "start": 7, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeParameters": [ + { + "type": "TSTypeParameterDeclaration", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ], + "parameters": [ + { + "type": "Identifier", + "start": 11, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "typeName": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + ], + "typeAnnotation": { + "type": "TSTypeReference", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "typeName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/function-in-generic/actual.js b/test/fixtures/typescript/types/function-in-generic/actual.js new file mode 100644 index 0000000000..6b9a571a7c --- /dev/null +++ b/test/fixtures/typescript/types/function-in-generic/actual.js @@ -0,0 +1 @@ +let x: Array<() => void>; diff --git a/test/fixtures/typescript/types/function-in-generic/expected.json b/test/fixtures/typescript/types/function-in-generic/expected.json new file mode 100644 index 0000000000..fede131143 --- /dev/null +++ b/test/fixtures/typescript/types/function-in-generic/expected.json @@ -0,0 +1,150 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 7, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeArguments": [ + { + "type": "TSFunctionType", + "start": 13, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "parameters": [], + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 23 + } + } + } + } + ] + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/function-with-this/actual.js b/test/fixtures/typescript/types/function-with-this/actual.js new file mode 100644 index 0000000000..cd13ed098d --- /dev/null +++ b/test/fixtures/typescript/types/function-with-this/actual.js @@ -0,0 +1 @@ +let f: (this: number) => void; diff --git a/test/fixtures/typescript/types/function-with-this/expected.json b/test/fixtures/typescript/types/function-with-this/expected.json new file mode 100644 index 0000000000..6a617488b9 --- /dev/null +++ b/test/fixtures/typescript/types/function-with-this/expected.json @@ -0,0 +1,149 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 29 + }, + "identifierName": "f" + }, + "name": "f", + "typeAnnotation": { + "type": "TSFunctionType", + "start": 7, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 8, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "this" + }, + "name": "this", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 20 + } + } + } + } + ], + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 25, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 29 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/function/actual.js b/test/fixtures/typescript/types/function/actual.js new file mode 100644 index 0000000000..15173a1b65 --- /dev/null +++ b/test/fixtures/typescript/types/function/actual.js @@ -0,0 +1 @@ +let f: (a: number, b?: number, ...c: number[]) => void; diff --git a/test/fixtures/typescript/types/function/expected.json b/test/fixtures/typescript/types/function/expected.json new file mode 100644 index 0000000000..a18456b555 --- /dev/null +++ b/test/fixtures/typescript/types/function/expected.json @@ -0,0 +1,244 @@ +{ + "type": "File", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 55 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 55 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 55 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 54 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 54 + }, + "identifierName": "f" + }, + "name": "f", + "typeAnnotation": { + "type": "TSFunctionType", + "start": 7, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 54 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 8, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + } + } + } + }, + { + "type": "Identifier", + "start": 19, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 29 + }, + "identifierName": "b" + }, + "name": "b", + "optional": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 23, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 29 + } + } + } + }, + { + "type": "RestElement", + "start": 31, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "argument": { + "type": "Identifier", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 35 + }, + "identifierName": "c" + }, + "name": "c" + }, + "typeAnnotation": { + "type": "TSArrayType", + "start": 37, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 37, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 43 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 50, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 50 + }, + "end": { + "line": 1, + "column": 54 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/indexed/actual.js b/test/fixtures/typescript/types/indexed/actual.js new file mode 100644 index 0000000000..4a5809fdd5 --- /dev/null +++ b/test/fixtures/typescript/types/indexed/actual.js @@ -0,0 +1 @@ +let x: T[K]; diff --git a/test/fixtures/typescript/types/indexed/expected.json b/test/fixtures/typescript/types/indexed/expected.json new file mode 100644 index 0000000000..a04ff81e27 --- /dev/null +++ b/test/fixtures/typescript/types/indexed/expected.json @@ -0,0 +1,164 @@ +{ + "type": "File", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSIndexedAccessType", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "objectType": { + "type": "TSTypeReference", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "indexType": { + "type": "TSTypeReference", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "typeName": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "K" + }, + "name": "K" + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/keywords/actual.js b/test/fixtures/typescript/types/keywords/actual.js new file mode 100644 index 0000000000..d2ddfb94bb --- /dev/null +++ b/test/fixtures/typescript/types/keywords/actual.js @@ -0,0 +1,10 @@ +let a: any; +let b: boolean; +let ne: never; +let nul: null; +let num: number; +let o: object; +let st: string; +let sy: symbol; +let u: undefined; +let v: void; diff --git a/test/fixtures/typescript/types/keywords/expected.json b/test/fixtures/typescript/types/keywords/expected.json new file mode 100644 index 0000000000..e11add3b2a --- /dev/null +++ b/test/fixtures/typescript/types/keywords/expected.json @@ -0,0 +1,694 @@ +{ + "type": "File", + "start": 0, + "end": 152, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 10, + "column": 12 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 152, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 10, + "column": 12 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "a" + }, + "name": "a", + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 12, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "b" + }, + "name": "b", + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 19, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 14 + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 28, + "end": 42, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 32, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 13 + }, + "identifierName": "ne" + }, + "name": "ne", + "typeAnnotation": { + "type": "TSNeverKeyword", + "start": 36, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 13 + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 43, + "end": 57, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 47, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 47, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 13 + }, + "identifierName": "nul" + }, + "name": "nul", + "typeAnnotation": { + "type": "TSNullKeyword", + "start": 52, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 13 + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 58, + "end": 74, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 16 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 62, + "end": 73, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 62, + "end": 73, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 15 + }, + "identifierName": "num" + }, + "name": "num", + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 67, + "end": 73, + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 15 + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 75, + "end": 89, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 79, + "end": 88, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "start": 79, + "end": 88, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 13 + }, + "identifierName": "o" + }, + "name": "o", + "typeAnnotation": { + "type": "TSObjectKeyword", + "start": 82, + "end": 88, + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 13 + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 90, + "end": 105, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 15 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 94, + "end": 104, + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 94, + "end": 104, + "loc": { + "start": { + "line": 7, + "column": 4 + }, + "end": { + "line": 7, + "column": 14 + }, + "identifierName": "st" + }, + "name": "st", + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 98, + "end": 104, + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 14 + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 106, + "end": 121, + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 8, + "column": 15 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 110, + "end": 120, + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 110, + "end": 120, + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 14 + }, + "identifierName": "sy" + }, + "name": "sy", + "typeAnnotation": { + "type": "TSSymbolKeyword", + "start": 114, + "end": 120, + "loc": { + "start": { + "line": 8, + "column": 8 + }, + "end": { + "line": 8, + "column": 14 + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 122, + "end": 139, + "loc": { + "start": { + "line": 9, + "column": 0 + }, + "end": { + "line": 9, + "column": 17 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 126, + "end": 138, + "loc": { + "start": { + "line": 9, + "column": 4 + }, + "end": { + "line": 9, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 126, + "end": 138, + "loc": { + "start": { + "line": 9, + "column": 4 + }, + "end": { + "line": 9, + "column": 16 + }, + "identifierName": "u" + }, + "name": "u", + "typeAnnotation": { + "type": "TSUndefinedKeyword", + "start": 129, + "end": 138, + "loc": { + "start": { + "line": 9, + "column": 7 + }, + "end": { + "line": 9, + "column": 16 + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 140, + "end": 152, + "loc": { + "start": { + "line": 10, + "column": 0 + }, + "end": { + "line": 10, + "column": 12 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 144, + "end": 151, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 144, + "end": 151, + "loc": { + "start": { + "line": 10, + "column": 4 + }, + "end": { + "line": 10, + "column": 11 + }, + "identifierName": "v" + }, + "name": "v", + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 147, + "end": 151, + "loc": { + "start": { + "line": 10, + "column": 7 + }, + "end": { + "line": 10, + "column": 11 + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/literal-boolean/actual.js b/test/fixtures/typescript/types/literal-boolean/actual.js new file mode 100644 index 0000000000..d2cea8ee69 --- /dev/null +++ b/test/fixtures/typescript/types/literal-boolean/actual.js @@ -0,0 +1,2 @@ +let x: true; +let x: false; diff --git a/test/fixtures/typescript/types/literal-boolean/expected.json b/test/fixtures/typescript/types/literal-boolean/expected.json new file mode 100644 index 0000000000..fe855a59a7 --- /dev/null +++ b/test/fixtures/typescript/types/literal-boolean/expected.json @@ -0,0 +1,198 @@ +{ + "type": "File", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSLiteralType", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "literal": { + "type": "BooleanLiteral", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "value": true + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 13, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 17, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSLiteralType", + "start": 20, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "literal": { + "type": "BooleanLiteral", + "start": 20, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "value": false + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/literal-number-negative/actual.js b/test/fixtures/typescript/types/literal-number-negative/actual.js new file mode 100644 index 0000000000..48e57ca110 --- /dev/null +++ b/test/fixtures/typescript/types/literal-number-negative/actual.js @@ -0,0 +1 @@ +let x: -1; diff --git a/test/fixtures/typescript/types/literal-number-negative/expected.json b/test/fixtures/typescript/types/literal-number-negative/expected.json new file mode 100644 index 0000000000..4c43274c4d --- /dev/null +++ b/test/fixtures/typescript/types/literal-number-negative/expected.json @@ -0,0 +1,120 @@ +{ + "type": "File", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSLiteralType", + "start": 7, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "literal": { + "type": "NumericLiteral", + "start": 7, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "extra": { + "rawValue": -1, + "raw": "-1" + }, + "value": -1 + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/literal-number/actual.js b/test/fixtures/typescript/types/literal-number/actual.js new file mode 100644 index 0000000000..1e234e36b8 --- /dev/null +++ b/test/fixtures/typescript/types/literal-number/actual.js @@ -0,0 +1 @@ +let x: 0; diff --git a/test/fixtures/typescript/types/literal-number/expected.json b/test/fixtures/typescript/types/literal-number/expected.json new file mode 100644 index 0000000000..579c851ad2 --- /dev/null +++ b/test/fixtures/typescript/types/literal-number/expected.json @@ -0,0 +1,120 @@ +{ + "type": "File", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSLiteralType", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "literal": { + "type": "NumericLiteral", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/literal-string/actual.js b/test/fixtures/typescript/types/literal-string/actual.js new file mode 100644 index 0000000000..926a42ae7e --- /dev/null +++ b/test/fixtures/typescript/types/literal-string/actual.js @@ -0,0 +1 @@ +let x: "foo"; diff --git a/test/fixtures/typescript/types/literal-string/expected.json b/test/fixtures/typescript/types/literal-string/expected.json new file mode 100644 index 0000000000..82cd450f65 --- /dev/null +++ b/test/fixtures/typescript/types/literal-string/expected.json @@ -0,0 +1,120 @@ +{ + "type": "File", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSLiteralType", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "literal": { + "type": "StringLiteral", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/mapped/actual.js b/test/fixtures/typescript/types/mapped/actual.js new file mode 100644 index 0000000000..a3a1c9a3b4 --- /dev/null +++ b/test/fixtures/typescript/types/mapped/actual.js @@ -0,0 +1,2 @@ +let map: { [P in string]: number; }; +let map: { readonly [P in string]?: number; }; diff --git a/test/fixtures/typescript/types/mapped/expected.json b/test/fixtures/typescript/types/mapped/expected.json new file mode 100644 index 0000000000..f5bdb52a1c --- /dev/null +++ b/test/fixtures/typescript/types/mapped/expected.json @@ -0,0 +1,292 @@ +{ + "type": "File", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 83, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 35 + }, + "identifierName": "map" + }, + "name": "map", + "typeAnnotation": { + "type": "TSMappedType", + "start": 9, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 35 + } + }, + "typeParameter": { + "type": "TSTypeParameterDeclaration", + "start": 12, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "name": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "P" + }, + "name": "P" + }, + "constraint": { + "type": "TSStringKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 32 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 37, + "end": 83, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 46 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 41, + "end": 82, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 45 + } + }, + "id": { + "type": "Identifier", + "start": 41, + "end": 82, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 45 + }, + "identifierName": "map" + }, + "name": "map", + "typeAnnotation": { + "type": "TSMappedType", + "start": 46, + "end": 82, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 45 + } + }, + "readonly": true, + "typeParameter": { + "type": "TSTypeParameterDeclaration", + "start": 58, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 32 + } + }, + "name": { + "type": "Identifier", + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + }, + "identifierName": "P" + }, + "name": "P" + }, + "constraint": { + "type": "TSStringKeyword", + "start": 63, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 26 + }, + "end": { + "line": 2, + "column": 32 + } + } + } + }, + "optional": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 73, + "end": 79, + "loc": { + "start": { + "line": 2, + "column": 36 + }, + "end": { + "line": 2, + "column": 42 + } + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/parenthesized/actual.js b/test/fixtures/typescript/types/parenthesized/actual.js new file mode 100644 index 0000000000..a8a9d2d955 --- /dev/null +++ b/test/fixtures/typescript/types/parenthesized/actual.js @@ -0,0 +1 @@ +type T = ({}); diff --git a/test/fixtures/typescript/types/parenthesized/expected.json b/test/fixtures/typescript/types/parenthesized/expected.json new file mode 100644 index 0000000000..dd4b167a5e --- /dev/null +++ b/test/fixtures/typescript/types/parenthesized/expected.json @@ -0,0 +1,97 @@ +{ + "type": "File", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "sourceType": "module", + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "name": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "T" + }, + "name": "T" + }, + "typeAnnotation": { + "type": "TSParenthesizedType", + "start": 9, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 10, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "members": [] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/reference-generic-nested/actual.js b/test/fixtures/typescript/types/reference-generic-nested/actual.js new file mode 100644 index 0000000000..e38dfca9d5 --- /dev/null +++ b/test/fixtures/typescript/types/reference-generic-nested/actual.js @@ -0,0 +1 @@ +let x: Array>; diff --git a/test/fixtures/typescript/types/reference-generic-nested/expected.json b/test/fixtures/typescript/types/reference-generic-nested/expected.json new file mode 100644 index 0000000000..d7663b125d --- /dev/null +++ b/test/fixtures/typescript/types/reference-generic-nested/expected.json @@ -0,0 +1,168 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 7, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeArguments": [ + { + "type": "TSTypeReference", + "start": 13, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "typeName": { + "type": "Identifier", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeArguments": [ + { + "type": "TSNumberKeyword", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 25 + } + } + } + ] + } + ] + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/reference-generic/actual.js b/test/fixtures/typescript/types/reference-generic/actual.js new file mode 100644 index 0000000000..4925bd4778 --- /dev/null +++ b/test/fixtures/typescript/types/reference-generic/actual.js @@ -0,0 +1 @@ +let x: Array; diff --git a/test/fixtures/typescript/types/reference-generic/expected.json b/test/fixtures/typescript/types/reference-generic/expected.json new file mode 100644 index 0000000000..be824ce58b --- /dev/null +++ b/test/fixtures/typescript/types/reference-generic/expected.json @@ -0,0 +1,134 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 7, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeArguments": [ + { + "type": "TSNumberKeyword", + "start": 13, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 19 + } + } + } + ] + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/reference/actual.js b/test/fixtures/typescript/types/reference/actual.js new file mode 100644 index 0000000000..330fb1d224 --- /dev/null +++ b/test/fixtures/typescript/types/reference/actual.js @@ -0,0 +1 @@ +let x: T; diff --git a/test/fixtures/typescript/types/reference/expected.json b/test/fixtures/typescript/types/reference/expected.json new file mode 100644 index 0000000000..cc0181ee3f --- /dev/null +++ b/test/fixtures/typescript/types/reference/expected.json @@ -0,0 +1,117 @@ +{ + "type": "File", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/type-literal/actual.js b/test/fixtures/typescript/types/type-literal/actual.js new file mode 100644 index 0000000000..074c0b246a --- /dev/null +++ b/test/fixtures/typescript/types/type-literal/actual.js @@ -0,0 +1,2 @@ +let obj: { x: number }; +// Type literals have the same body syntax as interfaces, so see `interface` directory for that. diff --git a/test/fixtures/typescript/types/type-literal/expected.json b/test/fixtures/typescript/types/type-literal/expected.json new file mode 100644 index 0000000000..8b1c64358e --- /dev/null +++ b/test/fixtures/typescript/types/type-literal/expected.json @@ -0,0 +1,186 @@ +{ + "type": "File", + "start": 0, + "end": 120, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 96 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 120, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 96 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 22 + }, + "identifierName": "obj" + }, + "name": "obj", + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 9, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "members": [ + { + "type": "TSPropertySignature", + "start": 11, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "x" + }, + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 20 + } + } + } + } + ] + } + }, + "init": null + } + ], + "kind": "let", + "trailingComments": [ + { + "type": "CommentLine", + "value": " Type literals have the same body syntax as interfaces, so see `interface` directory for that.", + "start": 24, + "end": 120, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 96 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " Type literals have the same body syntax as interfaces, so see `interface` directory for that.", + "start": 24, + "end": 120, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 96 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/type-operator/actual.js b/test/fixtures/typescript/types/type-operator/actual.js new file mode 100644 index 0000000000..2027543c4e --- /dev/null +++ b/test/fixtures/typescript/types/type-operator/actual.js @@ -0,0 +1 @@ +let x: keyof T; diff --git a/test/fixtures/typescript/types/type-operator/expected.json b/test/fixtures/typescript/types/type-operator/expected.json new file mode 100644 index 0000000000..9a906d4875 --- /dev/null +++ b/test/fixtures/typescript/types/type-operator/expected.json @@ -0,0 +1,133 @@ +{ + "type": "File", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSTypeOperator", + "start": 7, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "operator": "keyof", + "typeAnnotation": { + "type": "TSTypeReference", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "typeName": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/typeof/actual.js b/test/fixtures/typescript/types/typeof/actual.js new file mode 100644 index 0000000000..eebcd19b13 --- /dev/null +++ b/test/fixtures/typescript/types/typeof/actual.js @@ -0,0 +1 @@ +let x: typeof y.z; diff --git a/test/fixtures/typescript/types/typeof/expected.json b/test/fixtures/typescript/types/typeof/expected.json new file mode 100644 index 0000000000..88f8848d31 --- /dev/null +++ b/test/fixtures/typescript/types/typeof/expected.json @@ -0,0 +1,149 @@ +{ + "type": "File", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TSTypeQuery", + "start": 7, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "exprName": { + "type": "TSQualifiedName", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "left": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "y" + }, + "name": "y" + }, + "right": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "z" + }, + "name": "z" + } + } + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/types/union-intersection/actual.js b/test/fixtures/typescript/types/union-intersection/actual.js new file mode 100644 index 0000000000..93f391fdc4 --- /dev/null +++ b/test/fixtures/typescript/types/union-intersection/actual.js @@ -0,0 +1,4 @@ +let union: number | null | undefined; +let intersection: number & string; +let precedence1: number | string & boolean; +let precedence2: number & string | boolean; diff --git a/test/fixtures/typescript/types/union-intersection/expected.json b/test/fixtures/typescript/types/union-intersection/expected.json new file mode 100644 index 0000000000..6b93c6d468 --- /dev/null +++ b/test/fixtures/typescript/types/union-intersection/expected.json @@ -0,0 +1,505 @@ +{ + "type": "File", + "start": 0, + "end": 160, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 43 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 160, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 43 + } + }, + "sourceType": "module", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 36 + }, + "identifierName": "union" + }, + "name": "union", + "typeAnnotation": { + "type": "TSUnionType", + "start": 11, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "types": [ + { + "type": "TSNumberKeyword", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + } + } + }, + { + "type": "TSNullKeyword", + "start": 20, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "TSUndefinedKeyword", + "start": 27, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 36 + } + } + } + ] + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 38, + "end": 72, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 34 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 42, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "id": { + "type": "Identifier", + "start": 42, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 33 + }, + "identifierName": "intersection" + }, + "name": "intersection", + "typeAnnotation": { + "type": "TSIntersectionType", + "start": 56, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 33 + } + }, + "types": [ + { + "type": "TSNumberKeyword", + "start": 56, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 24 + } + } + }, + { + "type": "TSStringKeyword", + "start": 65, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 33 + } + } + } + ] + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 73, + "end": 116, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 43 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 77, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 42 + } + }, + "id": { + "type": "Identifier", + "start": 77, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 42 + }, + "identifierName": "precedence1" + }, + "name": "precedence1", + "typeAnnotation": { + "type": "TSUnionType", + "start": 90, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 42 + } + }, + "types": [ + { + "type": "TSNumberKeyword", + "start": 90, + "end": 96, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 23 + } + } + }, + { + "type": "TSIntersectionType", + "start": 99, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 42 + } + }, + "types": [ + { + "type": "TSStringKeyword", + "start": 99, + "end": 105, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 32 + } + } + }, + { + "type": "TSBooleanKeyword", + "start": 108, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 35 + }, + "end": { + "line": 3, + "column": 42 + } + } + } + ] + } + ] + } + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 117, + "end": 160, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 43 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 121, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 42 + } + }, + "id": { + "type": "Identifier", + "start": 121, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 42 + }, + "identifierName": "precedence2" + }, + "name": "precedence2", + "typeAnnotation": { + "type": "TSUnionType", + "start": 134, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 42 + } + }, + "types": [ + { + "type": "TSIntersectionType", + "start": 134, + "end": 149, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 32 + } + }, + "types": [ + { + "type": "TSNumberKeyword", + "start": 134, + "end": 140, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 23 + } + } + }, + { + "type": "TSStringKeyword", + "start": 143, + "end": 149, + "loc": { + "start": { + "line": 4, + "column": 26 + }, + "end": { + "line": 4, + "column": 32 + } + } + } + ] + }, + { + "type": "TSBooleanKeyword", + "start": 152, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 35 + }, + "end": { + "line": 4, + "column": 42 + } + } + } + ] + } + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file From fc81c2352a5ba875ad4783635c023c9682ccf4be Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 12 May 2017 12:59:58 -0700 Subject: [PATCH 02/55] Use '.indexOf' instead of '.includes' --- src/plugins/typescript.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 46faa01498..3052f52225 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -82,7 +82,7 @@ export default (superClass: Class): Class => class extends super } const modifier = this.state.value; - if (allowedModifiers.includes(modifier) + if (allowedModifiers.indexOf(modifier) !== -1 && this.tsTryParse(this.tsNextTokenCanFollowModifier.bind(this))) { return modifier; } From 7e239a7c980c2bb12822c3cb9f6ae7928c4d4307 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 12 May 2017 14:38:22 -0700 Subject: [PATCH 03/55] "access" -> "accessibility" (Fix tsep-babylon-test#14) --- src/parser/lval.js | 8 ++++---- src/parser/statement.js | 6 +++--- src/plugins/typescript.js | 6 +++--- src/types.js | 4 ++-- .../members-with-modifier-names/expected.json | 2 +- .../members-with-reserved-names/expected.json | 2 +- .../class/modifiers-accessors/expected.json | 10 +++++----- .../modifiers-methods-async/expected.json | 10 +++++----- .../class/modifiers-properties/expected.json | 18 +++++++++--------- .../class/parameter-properties/expected.json | 10 +++++----- .../typescript/class/static/expected.json | 6 +++--- 11 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/parser/lval.js b/src/parser/lval.js index e896296e43..5b9c029690 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -199,14 +199,14 @@ export default class LValParser extends NodeUtils { while (this.match(tt.at)) { decorators.push(this.parseDecorator()); } - const { access, readonly } = allowModifiers ? this.parseParameterModifiers() : {}; + const { accessibility, readonly } = allowModifiers ? this.parseParameterModifiers() : {}; const left = this.parseMaybeDefault(); if (decorators.length) { left.decorators = decorators; } this.parseAssignableListItemTypes(left); const elt = this.parseMaybeDefault(left.start, left.loc.start, left); - if (access) elt.access = access; + if (accessibility) elt.accessibility = accessibility; if (readonly) elt.readonly = readonly; elts.push(elt); } @@ -215,8 +215,8 @@ export default class LValParser extends NodeUtils { } // Overridden in typescript.js - parseParameterModifiers(): { access: ?Accessibility, readonly: boolean } { - return { access: undefined, readonly: false }; + parseParameterModifiers(): { accessibility: ?Accessibility, readonly: boolean } { + return { accessibility: undefined, readonly: false }; } parseAssignableListItemTypes(param: Pattern): Pattern { diff --git a/src/parser/statement.js b/src/parser/statement.js index 98539a4fe7..eb76fcb58a 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -723,8 +723,8 @@ export default class StatementParser extends ExpressionParser { const method: N.ClassMethod = memberAny; const prop: N.ClassProperty = memberAny; - const access = this.parseAccessModifier(); - if (access) methodOrProp.access = access; + const accessibility = this.parseAccessModifier(); + if (accessibility) methodOrProp.accessibility = accessibility; let isStatic = false; if (this.match(tt.name) && this.state.value === "static") { @@ -753,7 +753,7 @@ export default class StatementParser extends ExpressionParser { if (readonly) prop.readonly = true; if (abstract) methodOrProp.abstract = true; - if (this.hasPlugin("typescript") && !abstract && !isStatic && !access) { + if (this.hasPlugin("typescript") && !abstract && !isStatic && !accessibility) { const idx = this.tsTryParseIndexSignature(memberAny); if (idx) { classBody.body.push(idx); diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 3052f52225..8181806347 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1056,10 +1056,10 @@ export default (superClass: Class): Class => class extends super return this.tsParseModifier(["public", "protected", "private"]); } - parseParameterModifiers(): { access: ?N.Accessibility, readonly: boolean } { - const access = this.parseAccessModifier(); + parseParameterModifiers(): { accessibility: ?N.Accessibility, readonly: boolean } { + const accessibility = this.parseAccessModifier(); const readonly = !!this.tsParseModifier(["readonly"]); - return { access, readonly }; + return { accessibility, readonly }; } parseClassMemberModifiers(): { abstract: boolean, readonly: boolean } { diff --git a/src/types.js b/src/types.js index 26fa109cea..76b89febf3 100644 --- a/src/types.js +++ b/src/types.js @@ -508,7 +508,7 @@ export type PatternBase = HasDecorators & { typeAnnotation?: ?(TsType | FlowTypeAnnotation); // TypeScript only: optional?: ?true; - access?: ?Accessibility; // For parameter properties + accessibility?: ?Accessibility; // For parameter properties readonly?: ?true; }; @@ -562,7 +562,7 @@ export type ClassMemberBase = NodeBase & HasDecorators & { static: boolean; computed: boolean; // TypeScript only: - access?: ?Accessibility; + accessibility?: ?Accessibility; abstract?: ?true; optional?: ?true; } diff --git a/test/fixtures/typescript/class/members-with-modifier-names/expected.json b/test/fixtures/typescript/class/members-with-modifier-names/expected.json index bd88206e00..66df64cbf3 100644 --- a/test/fixtures/typescript/class/members-with-modifier-names/expected.json +++ b/test/fixtures/typescript/class/members-with-modifier-names/expected.json @@ -144,7 +144,7 @@ "column": 26 } }, - "access": "public", + "accessibility": "public", "static": false, "kind": "method", "computed": false, diff --git a/test/fixtures/typescript/class/members-with-reserved-names/expected.json b/test/fixtures/typescript/class/members-with-reserved-names/expected.json index 8779c86d25..5638646f3a 100644 --- a/test/fixtures/typescript/class/members-with-reserved-names/expected.json +++ b/test/fixtures/typescript/class/members-with-reserved-names/expected.json @@ -89,7 +89,7 @@ "column": 26 } }, - "access": "public", + "accessibility": "public", "static": false, "computed": false, "key": { diff --git a/test/fixtures/typescript/class/modifiers-accessors/expected.json b/test/fixtures/typescript/class/modifiers-accessors/expected.json index d0d0d150c7..64f5ee3d96 100644 --- a/test/fixtures/typescript/class/modifiers-accessors/expected.json +++ b/test/fixtures/typescript/class/modifiers-accessors/expected.json @@ -224,7 +224,7 @@ "column": 30 } }, - "access": "public", + "accessibility": "public", "abstract": true, "static": false, "computed": false, @@ -266,7 +266,7 @@ "column": 41 } }, - "access": "public", + "accessibility": "public", "static": true, "computed": false, "key": { @@ -360,7 +360,7 @@ "column": 33 } }, - "access": "public", + "accessibility": "public", "static": false, "computed": false, "key": { @@ -454,7 +454,7 @@ "column": 36 } }, - "access": "protected", + "accessibility": "protected", "static": false, "computed": false, "key": { @@ -548,7 +548,7 @@ "column": 34 } }, - "access": "private", + "accessibility": "private", "static": false, "computed": false, "key": { diff --git a/test/fixtures/typescript/class/modifiers-methods-async/expected.json b/test/fixtures/typescript/class/modifiers-methods-async/expected.json index 9a1aa19de1..41828f1684 100644 --- a/test/fixtures/typescript/class/modifiers-methods-async/expected.json +++ b/test/fixtures/typescript/class/modifiers-methods-async/expected.json @@ -188,7 +188,7 @@ "column": 32 } }, - "access": "public", + "accessibility": "public", "abstract": true, "static": false, "computed": false, @@ -230,7 +230,7 @@ "column": 32 } }, - "access": "public", + "accessibility": "public", "static": true, "computed": false, "key": { @@ -288,7 +288,7 @@ "column": 24 } }, - "access": "public", + "accessibility": "public", "static": false, "computed": false, "key": { @@ -346,7 +346,7 @@ "column": 27 } }, - "access": "protected", + "accessibility": "protected", "static": false, "computed": false, "key": { @@ -404,7 +404,7 @@ "column": 25 } }, - "access": "private", + "accessibility": "private", "static": false, "computed": false, "key": { diff --git a/test/fixtures/typescript/class/modifiers-properties/expected.json b/test/fixtures/typescript/class/modifiers-properties/expected.json index f4ca369a74..105f235590 100644 --- a/test/fixtures/typescript/class/modifiers-properties/expected.json +++ b/test/fixtures/typescript/class/modifiers-properties/expected.json @@ -197,7 +197,7 @@ "column": 14 } }, - "access": "public", + "accessibility": "public", "static": false, "computed": false, "key": { @@ -233,7 +233,7 @@ "column": 17 } }, - "access": "protected", + "accessibility": "protected", "static": false, "computed": false, "key": { @@ -269,7 +269,7 @@ "column": 15 } }, - "access": "private", + "accessibility": "private", "static": false, "computed": false, "key": { @@ -415,7 +415,7 @@ "column": 24 } }, - "access": "public", + "accessibility": "public", "readonly": true, "static": false, "computed": false, @@ -452,7 +452,7 @@ "column": 24 } }, - "access": "public", + "accessibility": "public", "abstract": true, "static": false, "computed": false, @@ -489,7 +489,7 @@ "column": 22 } }, - "access": "public", + "accessibility": "public", "static": true, "computed": false, "key": { @@ -525,7 +525,7 @@ "column": 34 } }, - "access": "public", + "accessibility": "public", "readonly": true, "abstract": true, "static": false, @@ -563,7 +563,7 @@ "column": 34 } }, - "access": "public", + "accessibility": "public", "readonly": true, "abstract": true, "static": false, @@ -601,7 +601,7 @@ "column": 32 } }, - "access": "public", + "accessibility": "public", "readonly": true, "static": true, "computed": false, diff --git a/test/fixtures/typescript/class/parameter-properties/expected.json b/test/fixtures/typescript/class/parameter-properties/expected.json index 4b4c549ea7..311aa0688c 100644 --- a/test/fixtures/typescript/class/parameter-properties/expected.json +++ b/test/fixtures/typescript/class/parameter-properties/expected.json @@ -148,7 +148,7 @@ "identifierName": "pu" }, "name": "pu", - "access": "public" + "accessibility": "public" }, { "type": "Identifier", @@ -166,7 +166,7 @@ "identifierName": "po" }, "name": "po", - "access": "protected" + "accessibility": "protected" }, { "type": "Identifier", @@ -184,7 +184,7 @@ "identifierName": "pi" }, "name": "pi", - "access": "private" + "accessibility": "private" }, { "type": "Identifier", @@ -202,7 +202,7 @@ "identifierName": "pur" }, "name": "pur", - "access": "public", + "accessibility": "public", "readonly": true }, { @@ -343,7 +343,7 @@ }, "value": 0 }, - "access": "public" + "accessibility": "public" } ], "body": { diff --git a/test/fixtures/typescript/class/static/expected.json b/test/fixtures/typescript/class/static/expected.json index 8dfe7aa998..8c77a0575a 100644 --- a/test/fixtures/typescript/class/static/expected.json +++ b/test/fixtures/typescript/class/static/expected.json @@ -129,7 +129,7 @@ "column": 22 } }, - "access": "public", + "accessibility": "public", "static": true, "computed": false, "key": { @@ -170,7 +170,7 @@ "column": 25 } }, - "access": "protected", + "accessibility": "protected", "static": true, "computed": false, "key": { @@ -211,7 +211,7 @@ "column": 23 } }, - "access": "private", + "accessibility": "private", "static": true, "computed": false, "key": { From 4a69f1fb4067cc4ea1d4c428bf3a17cb094ea729 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 17 May 2017 10:08:24 -0700 Subject: [PATCH 04/55] Change TSIndexSignatureDeclaration to TSIndexSignature --- src/parser/statement.js | 2 +- src/plugins/typescript.js | 4 ++-- src/types.js | 8 ++++---- test/fixtures/typescript/class/declare/expected.json | 2 +- .../typescript/class/index-signature/expected.json | 4 ++-- .../{index-accessor => index-signature}/actual.js | 0 .../{index-accessor => index-signature}/expected.json | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) rename test/fixtures/typescript/interface/{index-accessor => index-signature}/actual.js (100%) rename test/fixtures/typescript/interface/{index-accessor => index-signature}/expected.json (98%) diff --git a/src/parser/statement.js b/src/parser/statement.js index eb76fcb58a..1a783470fb 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -17,7 +17,7 @@ const loopLabel = { kind: "loop" }, switchLabel = { kind: "switch" }; export default class StatementParser extends ExpressionParser { // Forward-declarations: Code defined in typescript.js +tsParseEnumDeclaration: (node: N.TsEnumDeclaration, isConst: boolean) => N.TsEnumDeclaration; - +tsTryParseIndexSignature: (node: N.TsIndexSignatureDeclaration) => ?N.TsIndexSignatureDeclaration + +tsTryParseIndexSignature: (node: N.TsIndexSignature) => ?N.TsIndexSignature +tsParseImportEqualsDeclaration: ( node: N.TsImportEqualsDeclaration, isExport?: boolean) => N.TsImportEqualsDeclaration diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 8181806347..b063f2ea0f 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -328,7 +328,7 @@ export default (superClass: Class): Class => class extends super return this.eat(tt.name) && this.match(tt.colon); } - tsTryParseIndexSignature(node: N.TsIndexSignatureDeclaration): ?N.TsIndexSignatureDeclaration { + tsTryParseIndexSignature(node: N.TsIndexSignature): ?N.TsIndexSignature { if (!(this.match(tt.bracketL) && this.tsLookAhead(this.tsIsUnambiguouslyIndexSignature.bind(this)))) { return undefined; } @@ -343,7 +343,7 @@ export default (superClass: Class): Class => class extends super const type = this.tsTryParseTypeAnnotation(); if (type) node.typeAnnotation = type; this.tsParseTypeMemberSemicolon(); - return this.finishNode(node, "TSIndexSignatureDeclaration"); + return this.finishNode(node, "TSIndexSignature"); } tsParsePropertyOrMethodSignature( diff --git a/src/types.js b/src/types.js index 76b89febf3..7fddef99c6 100644 --- a/src/types.js +++ b/src/types.js @@ -567,7 +567,7 @@ export type ClassMemberBase = NodeBase & HasDecorators & { optional?: ?true; } -export type ClassMember = ClassMethod | ClassProperty | TsIndexSignatureDeclaration; +export type ClassMember = ClassMethod | ClassProperty | TsIndexSignature export type MethodLike = ObjectMethod | FunctionExpression | ClassMethod; @@ -801,7 +801,7 @@ export type TsTypeElement = | TsConstructSignatureDeclaration | TsPropertySignature | TsMethodSignature - | TsIndexSignatureDeclaration; + | TsIndexSignature; export type TsTypeElementBase = NodeBase & { // TypeScript uses a `PropertyName` here, @@ -838,9 +838,9 @@ export type TsMethodSignature = TsSignatureDeclarationBase & TsNamedTypeElementB } // *Not* a ClassMemberBase: Can't have accessibility, can't be abstract, can't be optional. -export type TsIndexSignatureDeclaration = TsSignatureDeclarationOrIndexSignatureBase & TsTypeElementBase & { +export type TsIndexSignature = TsSignatureDeclarationOrIndexSignatureBase & TsTypeElementBase & { readonly?: true; - type: "TSIndexSignatureDeclaration"; + type: "TSIndexSignature"; // Note: parameters.length must be 1. }; diff --git a/test/fixtures/typescript/class/declare/expected.json b/test/fixtures/typescript/class/declare/expected.json index e1e3aae403..653556e3ba 100644 --- a/test/fixtures/typescript/class/declare/expected.json +++ b/test/fixtures/typescript/class/declare/expected.json @@ -76,7 +76,7 @@ }, "body": [ { - "type": "TSIndexSignatureDeclaration", + "type": "TSIndexSignature", "start": 22, "end": 39, "loc": { diff --git a/test/fixtures/typescript/class/index-signature/expected.json b/test/fixtures/typescript/class/index-signature/expected.json index 0b104e6f81..c822ecce6e 100644 --- a/test/fixtures/typescript/class/index-signature/expected.json +++ b/test/fixtures/typescript/class/index-signature/expected.json @@ -76,7 +76,7 @@ }, "body": [ { - "type": "TSIndexSignatureDeclaration", + "type": "TSIndexSignature", "start": 14, "end": 31, "loc": { @@ -140,7 +140,7 @@ } }, { - "type": "TSIndexSignatureDeclaration", + "type": "TSIndexSignature", "start": 36, "end": 62, "loc": { diff --git a/test/fixtures/typescript/interface/index-accessor/actual.js b/test/fixtures/typescript/interface/index-signature/actual.js similarity index 100% rename from test/fixtures/typescript/interface/index-accessor/actual.js rename to test/fixtures/typescript/interface/index-signature/actual.js diff --git a/test/fixtures/typescript/interface/index-accessor/expected.json b/test/fixtures/typescript/interface/index-signature/expected.json similarity index 98% rename from test/fixtures/typescript/interface/index-accessor/expected.json rename to test/fixtures/typescript/interface/index-signature/expected.json index 7e46b8d956..02c36bf936 100644 --- a/test/fixtures/typescript/interface/index-accessor/expected.json +++ b/test/fixtures/typescript/interface/index-signature/expected.json @@ -61,7 +61,7 @@ }, "members": [ { - "type": "TSIndexSignatureDeclaration", + "type": "TSIndexSignature", "start": 18, "end": 38, "loc": { From e67d239ea6056184701a62dfbb8261b42dabf5c5 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 17 May 2017 10:16:35 -0700 Subject: [PATCH 05/55] Change TSModuleBlock `.statements` to `.body` --- src/plugins/typescript.js | 2 +- src/types.js | 2 +- test/fixtures/typescript/export/declare/expected.json | 4 ++-- .../typescript/import/not-top-level/expected.json | 2 +- .../module-namespace/body-declare/expected.json | 2 +- .../module-namespace/body-nested-declare/expected.json | 4 ++-- .../typescript/module-namespace/body-nested/expected.json | 4 ++-- .../typescript/module-namespace/body/expected.json | 2 +- .../module-namespace/global-in-module/expected.json | 4 ++-- .../module-namespace/head-declare/expected.json | 8 ++++---- .../typescript/module-namespace/head-export/expected.json | 4 ++-- .../typescript/module-namespace/head/expected.json | 8 ++++---- 12 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index b063f2ea0f..2517230e1e 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -775,7 +775,7 @@ export default (superClass: Class): Class => class extends super this.expect(tt.braceL); // Inside of a module block is considered "top-level", meaning it can have imports and exports. this.parseBlockOrModuleBlockBody( - node.statements = [], /* directives */ undefined, /* topLevel */ true, /* end */ tt.braceR); + node.body = [], /* directives */ undefined, /* topLevel */ true, /* end */ tt.braceR); return this.finishNode(node, "TSModuleBlock"); } diff --git a/src/types.js b/src/types.js index 7fddef99c6..c6c3168caa 100644 --- a/src/types.js +++ b/src/types.js @@ -1027,7 +1027,7 @@ export type TsNamespaceBody = TsModuleBlock | TsNamespaceDeclaration; export type TsModuleBlock = NodeBase & { type: "TSModuleBlock"; - statements: $ReadOnlyArray; + body: $ReadOnlyArray; }; export type TsNamespaceDeclaration = TsModuleDeclaration & { diff --git a/test/fixtures/typescript/export/declare/expected.json b/test/fixtures/typescript/export/declare/expected.json index be07e8efc2..df05591583 100644 --- a/test/fixtures/typescript/export/declare/expected.json +++ b/test/fixtures/typescript/export/declare/expected.json @@ -424,7 +424,7 @@ "column": 26 } }, - "statements": [] + "body": [] }, "declare": true } @@ -490,7 +490,7 @@ "column": 29 } }, - "statements": [] + "body": [] }, "declare": true } diff --git a/test/fixtures/typescript/import/not-top-level/expected.json b/test/fixtures/typescript/import/not-top-level/expected.json index 33747240d2..4642b6eefd 100644 --- a/test/fixtures/typescript/import/not-top-level/expected.json +++ b/test/fixtures/typescript/import/not-top-level/expected.json @@ -76,7 +76,7 @@ "column": 1 } }, - "statements": [ + "body": [ { "type": "ImportDeclaration", "start": 25, diff --git a/test/fixtures/typescript/module-namespace/body-declare/expected.json b/test/fixtures/typescript/module-namespace/body-declare/expected.json index 7146d59db1..32154a93fb 100644 --- a/test/fixtures/typescript/module-namespace/body-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/body-declare/expected.json @@ -73,7 +73,7 @@ "column": 1 } }, - "statements": [ + "body": [ { "type": "VariableDeclaration", "start": 26, diff --git a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json index 422dabe179..ce3aa7aa0c 100644 --- a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json @@ -73,7 +73,7 @@ "column": 1 } }, - "statements": [ + "body": [ { "type": "TSModuleDeclaration", "start": 26, @@ -119,7 +119,7 @@ "column": 5 } }, - "statements": [ + "body": [ { "type": "VariableDeclaration", "start": 48, diff --git a/test/fixtures/typescript/module-namespace/body-nested/expected.json b/test/fixtures/typescript/module-namespace/body-nested/expected.json index 44f550cf28..80ec878cb8 100644 --- a/test/fixtures/typescript/module-namespace/body-nested/expected.json +++ b/test/fixtures/typescript/module-namespace/body-nested/expected.json @@ -73,7 +73,7 @@ "column": 1 } }, - "statements": [ + "body": [ { "type": "TSModuleDeclaration", "start": 18, @@ -119,7 +119,7 @@ "column": 5 } }, - "statements": [] + "body": [] } } ] diff --git a/test/fixtures/typescript/module-namespace/body/expected.json b/test/fixtures/typescript/module-namespace/body/expected.json index 4fdfe3070e..de38e956a7 100644 --- a/test/fixtures/typescript/module-namespace/body/expected.json +++ b/test/fixtures/typescript/module-namespace/body/expected.json @@ -73,7 +73,7 @@ "column": 1 } }, - "statements": [ + "body": [ { "type": "VariableDeclaration", "start": 18, diff --git a/test/fixtures/typescript/module-namespace/global-in-module/expected.json b/test/fixtures/typescript/module-namespace/global-in-module/expected.json index b1323002d9..acbfcdbc33 100644 --- a/test/fixtures/typescript/module-namespace/global-in-module/expected.json +++ b/test/fixtures/typescript/module-namespace/global-in-module/expected.json @@ -76,7 +76,7 @@ "column": 1 } }, - "statements": [ + "body": [ { "type": "TSModuleDeclaration", "start": 25, @@ -123,7 +123,7 @@ "column": 5 } }, - "statements": [ + "body": [ { "type": "VariableDeclaration", "start": 42, diff --git a/test/fixtures/typescript/module-namespace/head-declare/expected.json b/test/fixtures/typescript/module-namespace/head-declare/expected.json index 840c9d1bd3..c44bcdd14b 100644 --- a/test/fixtures/typescript/module-namespace/head-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/head-declare/expected.json @@ -104,7 +104,7 @@ "column": 24 } }, - "statements": [] + "body": [] } }, "declare": true @@ -154,7 +154,7 @@ "column": 19 } }, - "statements": [] + "body": [] }, "declare": true }, @@ -206,7 +206,7 @@ "column": 21 } }, - "statements": [] + "body": [] }, "declare": true }, @@ -256,7 +256,7 @@ "column": 17 } }, - "statements": [] + "body": [] }, "declare": true } diff --git a/test/fixtures/typescript/module-namespace/head-export/expected.json b/test/fixtures/typescript/module-namespace/head-export/expected.json index 8443609e8f..cec33f1c85 100644 --- a/test/fixtures/typescript/module-namespace/head-export/expected.json +++ b/test/fixtures/typescript/module-namespace/head-export/expected.json @@ -120,7 +120,7 @@ "column": 23 } }, - "statements": [] + "body": [] } } } @@ -186,7 +186,7 @@ "column": 18 } }, - "statements": [] + "body": [] } } } diff --git a/test/fixtures/typescript/module-namespace/head/expected.json b/test/fixtures/typescript/module-namespace/head/expected.json index fe0ead1f44..a60b2b9580 100644 --- a/test/fixtures/typescript/module-namespace/head/expected.json +++ b/test/fixtures/typescript/module-namespace/head/expected.json @@ -73,7 +73,7 @@ "column": 14 } }, - "statements": [] + "body": [] } }, { @@ -183,7 +183,7 @@ "column": 18 } }, - "statements": [] + "body": [] } } } @@ -233,7 +233,7 @@ "column": 11 } }, - "statements": [] + "body": [] } }, { @@ -284,7 +284,7 @@ "column": 13 } }, - "statements": [] + "body": [] } } ], From cd1941e8eb1766c4092083379ada0ddc0b71fa62 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 17 May 2017 11:31:29 -0700 Subject: [PATCH 06/55] Use TypeParameterDeclaration and TypeParameter nodes instead of a TsTypeParameterDeclaration[] --- src/parser/expression.js | 2 +- src/plugins/flow.js | 2 +- src/plugins/typescript.js | 34 ++-- src/types.js | 34 ++-- .../async-generic/expected.json | 57 ++++-- .../arrow-function/generic-tsx/expected.json | 60 +++--- .../arrow-function/generic/expected.json | 57 ++++-- .../class/expression-generic/expected.json | 114 +++++++---- .../typescript/class/generic/expected.json | 187 ++++++++++-------- .../class/get-generic/expected.json | 57 ++++-- .../members-with-modifier-names/expected.json | 57 ++++-- .../class/method-generic/expected.json | 114 +++++++---- .../function/annotated/expected.json | 57 ++++-- .../function/anonymous/expected.json | 57 ++++-- .../typescript/function/declare/expected.json | 57 ++++-- .../interface/generic/expected.json | 187 ++++++++++-------- .../interface/method-generic/expected.json | 187 ++++++++++-------- .../type-alias/generic-complex/expected.json | 187 ++++++++++-------- .../type-alias/generic/expected.json | 57 ++++-- .../types/function-generic/expected.json | 57 ++++-- .../typescript/types/mapped/expected.json | 4 +- 21 files changed, 954 insertions(+), 671 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index dc3dc00a6c..328b5b060a 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -42,7 +42,7 @@ export default class ExpressionParser extends LValParser { startLoc: Position) => { newBase: N.Expression, shouldContinue: boolean }; +tsTypeArgumentsFromAmbiguous: (types: $ReadOnlyArray) => $ReadOnlyArray; +tsTypeParametersFromAmbiguous: ( - types: $ReadOnlyArray) => $ReadOnlyArray; + types: $ReadOnlyArray) => N.TypeParameterDeclaration; +tsParseTypeArguments: () => $ReadOnlyArray; // Check if property name clashes with already added. diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 2607e373f2..be0107a216 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -390,7 +390,7 @@ export default (superClass: Class): Class => class extends super return this.finishNode(node, "TypeParameter"); } - flowParseTypeParameterDeclaration(): N.FlowTypeParameterDeclaration { + flowParseTypeParameterDeclaration(): N.TypeParameterDeclaration { const oldInType = this.state.inType; const node = this.startNode(); node.params = []; diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 2517230e1e..c1a961e7ad 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -232,8 +232,8 @@ export default (superClass: Class): Class => class extends super return this.finishNode(node, "TSTypeQuery"); } - tsParseTypeParameter(): N.TsTypeParameterDeclaration { - const node: N.TsTypeParameterDeclaration = this.startNode(); + tsParseTypeParameter(): N.TypeParameter { + const node: N.TypeParameter = this.startNode(); node.name = this.parseIdentifier(); if (this.eat(tt._extends)) { node.constraint = this.tsParseType(); @@ -243,7 +243,7 @@ export default (superClass: Class): Class => class extends super node.default = this.tsParseType(); } - return this.finishNode(node, "TSTypeParameterDeclaration"); + return this.finishNode(node, "TypeParameter"); } // Parse something that may be a type parameter or a type argument. @@ -268,18 +268,20 @@ export default (superClass: Class): Class => class extends super return this.finishNode(node, "TSTypeAmbiguous"); } - tsTryParseTypeParameters(): ?$ReadOnlyArray { + tsTryParseTypeParameters(): ?N.TypeParameterDeclaration { if (this.eatRelational("<")) { return this.tsParseTypeParameters(); } } - tsParseTypeParameters(): $ReadOnlyArray { - return this.tsParseBracketedList( + tsParseTypeParameters(): N.TypeParameterDeclaration { + const node: N.TypeParameterDeclaration = this.startNode(); + node.params = this.tsParseBracketedList( "TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), /* bracket */ false, /* skipFirstToken */ true); + return this.finishNode(node, "TypeParameterDeclaration"); } // Note: In TypeScript implementation we must provide `yieldContext` and `awaitContext`, @@ -424,12 +426,12 @@ export default (superClass: Class): Class => class extends super return this.match(tt._in); } - tsParseMappedTypeParameter(): N.TsTypeParameterDeclaration { - const node: N.TsTypeParameterDeclaration = this.startNode(); + tsParseMappedTypeParameter(): N.TypeParameter { + const node: N.TypeParameter = this.startNode(); node.name = this.parseIdentifier(); this.expect(tt._in); node.constraint = this.tsParseType(); - return this.finishNode(node, "TSTypeParameterDeclaration"); + return this.finishNode(node, "TypeParameter"); } tsParseMappedType(): N.TsMappedType { @@ -906,10 +908,11 @@ export default (superClass: Class): Class => class extends super } tsTypeParametersFromAmbiguous( - types: $ReadOnlyArray): $ReadOnlyArray { + types: $ReadOnlyArray): N.TypeParameterDeclaration { - return types.map((typeAmbiguous) => { - const typeParam = this.nodeWithSamePosition(typeAmbiguous, "TSTypeParameterDeclaration"); + const node: N.TypeParameterDeclaration = this.startNode(); + node.params = types.map((typeAmbiguous) => { + const typeParam = this.nodeWithSamePosition(typeAmbiguous, "TypeParameter"); const { typeAnnotation: type, constraint, default: defaultType } = typeAmbiguous; if (type.type === "TSTypeReference" && !type.typeArguments && type.typeName.type === "Identifier") { typeParam.name = type.typeName; @@ -921,6 +924,7 @@ export default (superClass: Class): Class => class extends super if (defaultType) typeParam.default = defaultType; return typeParam; }); + return this.finishNode(node, "TypeParameterDeclaration"); } nodeWithSamePosition(original: N.Node, type: string): T { @@ -1292,7 +1296,7 @@ export default (superClass: Class): Class => class extends super // Either way, we're looking at a '<': tt.jsxTagStart or relational. let arrowExpression; - let typeParameters; + let typeParameters: N.TypeParameterDeclaration; const state = this.state.clone(); this.next(); // skip the jsx start try { @@ -1324,7 +1328,9 @@ export default (superClass: Class): Class => class extends super } // Correct TypeScript code should have at least 1 type parameter, but don't crash on bad code. - if (typeParameters.length) this.resetStartLocationFromNode(arrowExpression, typeParameters[0]); + if (typeParameters && typeParameters.params.length !== 0) { + this.resetStartLocationFromNode(arrowExpression, typeParameters.params[0]); + } arrowExpression.typeParameters = typeParameters; return arrowExpression; } diff --git a/src/types.js b/src/types.js index c6c3168caa..cb835ce8b9 100644 --- a/src/types.js +++ b/src/types.js @@ -133,7 +133,7 @@ export type FunctionBase = HasDecorators & { // TODO: All not in spec expression: boolean; - typeParameters?: ?$ReadOnlyArray | FlowTypeParameterDeclaration; + typeParameters?: ?TypeParameterDeclaration; returnType?: ?(TsType | FlowTypeAnnotation); }; @@ -548,7 +548,7 @@ export type ClassBase = HasDecorators & { decorators: $ReadOnlyArray; // TODO: All not in spec - typeParameters?: ?$ReadOnlyArray | FlowTypeParameterDeclaration; + typeParameters?: ?TypeParameterDeclaration; superTypeParameters?: ?$ReadOnlyArray | FlowTypeParameterInstantiation; implements?: ?$ReadOnlyArray | $ReadOnlyArray; }; @@ -696,6 +696,20 @@ export type JSXOpeningElement = Node; export type JSXClosingElement = Node; export type JSXElement = Node; +// Flow/TypeScript common + +export type TypeParameterDeclaration = NodeBase & { + type: "TypeParameterDeclaration"; + params: $ReadOnlyArray; +}; + +export type TypeParameter = NodeBase & { + type: "TypeParameter"; + name: Identifier; + constraint?: TsType; + default?: TsType; +}; + // Flow (TODO: Not in spec) export type FlowType = Node; @@ -713,7 +727,6 @@ export type FlowInterface = Node; export type FlowInterfaceExtends = Node; export type FlowTypeAlias = Node; export type FlowTypeParameter = Node; -export type FlowTypeParameterDeclaration = Node; export type FlowTypeParameterInstantiation = Node; export type FlowObjectTypeIndexer = Node; export type FlowFunctionTypeAnnotation = Node; @@ -768,13 +781,6 @@ export type TsTypeAmbiguous = NodeBase & { default?: TsType; } -export type TsTypeParameterDeclaration = NodeBase & { - type: "TSTypeParameter"; - name: Identifier; - constraint?: TsType; - default?: TsType; -} - export type TsSignatureDeclaration = | TsCallSignatureDeclaration | TsConstructSignatureDeclaration @@ -789,7 +795,7 @@ export type TsSignatureDeclarationOrIndexSignatureBase = { } export type TsSignatureDeclarationBase = TsSignatureDeclarationOrIndexSignatureBase & { - typeParameters: ?$ReadOnlyArray; + typeParameters: ?TypeParameterDeclaration; } // ================ @@ -966,7 +972,7 @@ export type TsIndexedAccessType = TsTypeBase & { export type TsMappedType = TsTypeBase & { type: "TSMappedType"; readonly?: true; - typeParameter: TsTypeParameterDeclaration; + typeParameter: TypeParameter; optional?: true; typeAnnotation: ?TsType; }; @@ -983,7 +989,7 @@ export type TsLiteralType = TsTypeBase & { export type TsInterfaceDeclaration = DeclarationBase & { type: "TSInterfaceDeclaration"; name: Identifier; - typeParameters: ?$ReadOnlyArray; + typeParameters: ?TypeParameterDeclaration; // TS uses "heritageClauses", but want this to resemble ClassBase. extends?: $ReadOnlyArray; members: $ReadOnlyArray; @@ -998,7 +1004,7 @@ export type TsExpressionWithTypeArguments = TsTypeBase & { export type TsTypeAliasDeclaration = DeclarationBase & { type: "TSTypeAliasDeclaration"; name: Identifier; - typeParameters: ?$ReadOnlyArray; + typeParameters: ?TypeParameterDeclaration; typeAnnotation: TsType; }; diff --git a/test/fixtures/typescript/arrow-function/async-generic/expected.json b/test/fixtures/typescript/arrow-function/async-generic/expected.json index 9cfd9d9e65..14e40f4a1a 100644 --- a/test/fixtures/typescript/arrow-function/async-generic/expected.json +++ b/test/fixtures/typescript/arrow-function/async-generic/expected.json @@ -88,23 +88,23 @@ "name": "T" } }, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 7, - "end": 8, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 22, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 22 }, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 21 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 7, "end": 8, "loc": { @@ -115,13 +115,28 @@ "end": { "line": 1, "column": 8 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "id": null, "generator": false, "expression": true, diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json index ce020bdcb4..9d166e6ae3 100644 --- a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json +++ b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json @@ -160,23 +160,23 @@ }, "name": "a" }, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 62, - "end": 63, - "loc": { - "start": { - "line": 2, - "column": 1 - }, - "end": { - "line": 2, - "column": 2 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 62, + "end": 64, + "loc": { + "start": { + "line": 2, + "column": 1 }, - "name": { - "type": "Identifier", + "end": { + "line": 2, + "column": 3 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 62, "end": 63, "loc": { @@ -187,15 +187,31 @@ "end": { "line": 2, "column": 2 + } + }, + "name": { + "type": "Identifier", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 2 + }, + "identifierName": "T" }, - "identifierName": "T" + "name": "T", + "leadingComments": null }, - "name": "T", "leadingComments": null - }, - "leadingComments": null - } - ] + } + ], + "leadingComments": null + } }, "leadingComments": [ { diff --git a/test/fixtures/typescript/arrow-function/generic/expected.json b/test/fixtures/typescript/arrow-function/generic/expected.json index 3571308acb..10a010ee14 100644 --- a/test/fixtures/typescript/arrow-function/generic/expected.json +++ b/test/fixtures/typescript/arrow-function/generic/expected.json @@ -160,23 +160,23 @@ }, "name": "a" }, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 1, - "end": 2, - "loc": { - "start": { - "line": 1, - "column": 1 - }, - "end": { - "line": 1, - "column": 2 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 1, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 1 }, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 3 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 1, "end": 2, "loc": { @@ -187,13 +187,28 @@ "end": { "line": 1, "column": 2 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 1, + "end": 2, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 2 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ] + ] + } } } ], diff --git a/test/fixtures/typescript/class/expression-generic/expected.json b/test/fixtures/typescript/class/expression-generic/expected.json index 42899a920c..8a4ad66cbc 100644 --- a/test/fixtures/typescript/class/expression-generic/expected.json +++ b/test/fixtures/typescript/class/expression-generic/expected.json @@ -57,23 +57,23 @@ } }, "id": null, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 7, - "end": 8, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 7, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 7 }, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 9 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 7, "end": 8, "loc": { @@ -84,13 +84,28 @@ "end": { "line": 1, "column": 8 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "superClass": null, "body": { "type": "ClassBody", @@ -159,23 +174,23 @@ }, "name": "C" }, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 24, - "end": 25, - "loc": { - "start": { - "line": 2, - "column": 9 - }, - "end": { - "line": 2, - "column": 10 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 24, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 9 }, - "name": { - "type": "Identifier", + "end": { + "line": 2, + "column": 11 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 24, "end": 25, "loc": { @@ -186,13 +201,28 @@ "end": { "line": 2, "column": 10 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "superClass": null, "body": { "type": "ClassBody", diff --git a/test/fixtures/typescript/class/generic/expected.json b/test/fixtures/typescript/class/generic/expected.json index 131db796fc..60a566c59a 100644 --- a/test/fixtures/typescript/class/generic/expected.json +++ b/test/fixtures/typescript/class/generic/expected.json @@ -59,25 +59,25 @@ }, "name": "C" }, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 8, - "end": 40, - "loc": { - "start": { - "line": 1, - "column": 8 - }, - "end": { - "line": 1, - "column": 40 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 8, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 8 }, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 41 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 8, - "end": 9, + "end": 40, "loc": { "start": { "line": 1, @@ -85,61 +85,60 @@ }, "end": { "line": 1, - "column": 9 - }, - "identifierName": "T" - }, - "name": "T" - }, - "constraint": { - "type": "TSObjectKeyword", - "start": 18, - "end": 24, - "loc": { - "start": { - "line": 1, - "column": 18 - }, - "end": { - "line": 1, - "column": 24 + "column": 40 } - } - }, - "default": { - "type": "TSTypeLiteral", - "start": 27, - "end": 40, - "loc": { - "start": { - "line": 1, - "column": 27 + }, + "name": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "T" }, - "end": { - "line": 1, - "column": 40 + "name": "T" + }, + "constraint": { + "type": "TSObjectKeyword", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 24 + } } }, - "members": [ - { - "type": "TSPropertySignature", - "start": 29, - "end": 38, - "loc": { - "start": { - "line": 1, - "column": 29 - }, - "end": { - "line": 1, - "column": 38 - } + "default": { + "type": "TSTypeLiteral", + "start": 27, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 27 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 40 + } + }, + "members": [ + { + "type": "TSPropertySignature", "start": 29, - "end": 30, + "end": 38, "loc": { "start": { "line": 1, @@ -147,32 +146,48 @@ }, "end": { "line": 1, - "column": 30 - }, - "identifierName": "x" + "column": 38 + } }, - "name": "x" - }, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 32, - "end": 38, - "loc": { - "start": { - "line": 1, - "column": 32 + "computed": false, + "name": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 30 + }, + "identifierName": "x" }, - "end": { - "line": 1, - "column": 38 + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 38 + } } } } - } - ] + ] + } } - } - ], + ] + }, "superClass": null, "body": { "type": "ClassBody", diff --git a/test/fixtures/typescript/class/get-generic/expected.json b/test/fixtures/typescript/class/get-generic/expected.json index f093b0c0a3..06dc9756e9 100644 --- a/test/fixtures/typescript/class/get-generic/expected.json +++ b/test/fixtures/typescript/class/get-generic/expected.json @@ -109,23 +109,23 @@ "name": "get" }, "kind": "method", - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 26, - "end": 27, - "loc": { - "start": { - "line": 2, - "column": 8 - }, - "end": { - "line": 2, - "column": 9 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 26, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 }, - "name": { - "type": "Identifier", + "end": { + "line": 2, + "column": 10 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 26, "end": 27, "loc": { @@ -136,13 +136,28 @@ "end": { "line": 2, "column": 9 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "id": null, "generator": false, "expression": false, diff --git a/test/fixtures/typescript/class/members-with-modifier-names/expected.json b/test/fixtures/typescript/class/members-with-modifier-names/expected.json index 66df64cbf3..5403a3da26 100644 --- a/test/fixtures/typescript/class/members-with-modifier-names/expected.json +++ b/test/fixtures/typescript/class/members-with-modifier-names/expected.json @@ -274,23 +274,23 @@ "name": "async" }, "kind": "method", - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 85, - "end": 86, - "loc": { - "start": { - "line": 5, - "column": 10 - }, - "end": { - "line": 5, - "column": 11 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 85, + "end": 87, + "loc": { + "start": { + "line": 5, + "column": 10 }, - "name": { - "type": "Identifier", + "end": { + "line": 5, + "column": 12 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 85, "end": 86, "loc": { @@ -301,13 +301,28 @@ "end": { "line": 5, "column": 11 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 85, + "end": 86, + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 11 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "id": null, "generator": false, "expression": false, diff --git a/test/fixtures/typescript/class/method-generic/expected.json b/test/fixtures/typescript/class/method-generic/expected.json index a317590030..bb3bdf073a 100644 --- a/test/fixtures/typescript/class/method-generic/expected.json +++ b/test/fixtures/typescript/class/method-generic/expected.json @@ -109,23 +109,23 @@ "name": "f" }, "kind": "method", - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 16, - "end": 17, - "loc": { - "start": { - "line": 2, - "column": 6 - }, - "end": { - "line": 2, - "column": 7 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 16, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 6 }, - "name": { - "type": "Identifier", + "end": { + "line": 2, + "column": 8 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 16, "end": 17, "loc": { @@ -136,13 +136,28 @@ "end": { "line": 2, "column": 7 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "id": null, "generator": false, "expression": false, @@ -444,23 +459,23 @@ "computed": false }, "kind": "method", - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 71, - "end": 72, - "loc": { - "start": { - "line": 3, - "column": 22 - }, - "end": { - "line": 3, - "column": 23 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 71, + "end": 73, + "loc": { + "start": { + "line": 3, + "column": 22 }, - "name": { - "type": "Identifier", + "end": { + "line": 3, + "column": 24 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 71, "end": 72, "loc": { @@ -471,13 +486,28 @@ "end": { "line": 3, "column": 23 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 71, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 23 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "id": null, "generator": false, "expression": false, diff --git a/test/fixtures/typescript/function/annotated/expected.json b/test/fixtures/typescript/function/annotated/expected.json index 474acd6269..0b02abdc2a 100644 --- a/test/fixtures/typescript/function/annotated/expected.json +++ b/test/fixtures/typescript/function/annotated/expected.json @@ -62,23 +62,23 @@ "generator": false, "expression": false, "async": false, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 11, - "end": 12, - "loc": { - "start": { - "line": 1, - "column": 11 - }, - "end": { - "line": 1, - "column": 12 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 11, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 11 }, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 13 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 11, "end": 12, "loc": { @@ -89,13 +89,28 @@ "end": { "line": 1, "column": 12 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "params": [ { "type": "Identifier", diff --git a/test/fixtures/typescript/function/anonymous/expected.json b/test/fixtures/typescript/function/anonymous/expected.json index 9d5aacb912..eec2ce0fc6 100644 --- a/test/fixtures/typescript/function/anonymous/expected.json +++ b/test/fixtures/typescript/function/anonymous/expected.json @@ -92,23 +92,23 @@ "generator": false, "expression": false, "async": false, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 19, - "end": 20, - "loc": { - "start": { - "line": 1, - "column": 19 - }, - "end": { - "line": 1, - "column": 20 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 19, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 19 }, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 21 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 19, "end": 20, "loc": { @@ -119,13 +119,28 @@ "end": { "line": 1, "column": 20 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "params": [ { "type": "Identifier", diff --git a/test/fixtures/typescript/function/declare/expected.json b/test/fixtures/typescript/function/declare/expected.json index 6b6eeb935e..c31d06dc99 100644 --- a/test/fixtures/typescript/function/declare/expected.json +++ b/test/fixtures/typescript/function/declare/expected.json @@ -114,23 +114,23 @@ "generator": false, "expression": false, "async": false, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 47, - "end": 48, - "loc": { - "start": { - "line": 2, - "column": 19 - }, - "end": { - "line": 2, - "column": 20 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 47, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 19 }, - "name": { - "type": "Identifier", + "end": { + "line": 2, + "column": 21 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 47, "end": 48, "loc": { @@ -141,13 +141,28 @@ "end": { "line": 2, "column": 20 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 47, + "end": 48, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "params": [], "returnType": { "type": "TSTypeReference", diff --git a/test/fixtures/typescript/interface/generic/expected.json b/test/fixtures/typescript/interface/generic/expected.json index 163634139c..c2ae14a250 100644 --- a/test/fixtures/typescript/interface/generic/expected.json +++ b/test/fixtures/typescript/interface/generic/expected.json @@ -59,25 +59,25 @@ }, "name": "I" }, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 12, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 12 - }, - "end": { - "line": 1, - "column": 44 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 12, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 45 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 12, - "end": 13, + "end": 44, "loc": { "start": { "line": 1, @@ -85,61 +85,60 @@ }, "end": { "line": 1, - "column": 13 - }, - "identifierName": "T" - }, - "name": "T" - }, - "constraint": { - "type": "TSObjectKeyword", - "start": 22, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 22 - }, - "end": { - "line": 1, - "column": 28 + "column": 44 } - } - }, - "default": { - "type": "TSTypeLiteral", - "start": 31, - "end": 44, - "loc": { - "start": { - "line": 1, - "column": 31 + }, + "name": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "T" }, - "end": { - "line": 1, - "column": 44 + "name": "T" + }, + "constraint": { + "type": "TSObjectKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 28 + } } }, - "members": [ - { - "type": "TSPropertySignature", - "start": 33, - "end": 42, - "loc": { - "start": { - "line": 1, - "column": 33 - }, - "end": { - "line": 1, - "column": 42 - } + "default": { + "type": "TSTypeLiteral", + "start": 31, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 31 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 44 + } + }, + "members": [ + { + "type": "TSPropertySignature", "start": 33, - "end": 34, + "end": 42, "loc": { "start": { "line": 1, @@ -147,32 +146,48 @@ }, "end": { "line": 1, - "column": 34 - }, - "identifierName": "x" + "column": 42 + } }, - "name": "x" - }, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 36, - "end": 42, - "loc": { - "start": { - "line": 1, - "column": 36 + "computed": false, + "name": { + "type": "Identifier", + "start": 33, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + }, + "identifierName": "x" }, - "end": { - "line": 1, - "column": 42 + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 36, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 42 + } } } } - } - ] + ] + } } - } - ], + ] + }, "members": [] } ], diff --git a/test/fixtures/typescript/interface/method-generic/expected.json b/test/fixtures/typescript/interface/method-generic/expected.json index 991ab91a96..f841b26a67 100644 --- a/test/fixtures/typescript/interface/method-generic/expected.json +++ b/test/fixtures/typescript/interface/method-generic/expected.json @@ -92,25 +92,25 @@ }, "name": "m" }, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 20, - "end": 52, - "loc": { - "start": { - "line": 2, - "column": 6 - }, - "end": { - "line": 2, - "column": 38 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 20, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 6 }, - "name": { - "type": "Identifier", + "end": { + "line": 2, + "column": 39 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 20, - "end": 21, + "end": 52, "loc": { "start": { "line": 2, @@ -118,61 +118,60 @@ }, "end": { "line": 2, - "column": 7 - }, - "identifierName": "T" - }, - "name": "T" - }, - "constraint": { - "type": "TSObjectKeyword", - "start": 30, - "end": 36, - "loc": { - "start": { - "line": 2, - "column": 16 - }, - "end": { - "line": 2, - "column": 22 + "column": 38 } - } - }, - "default": { - "type": "TSTypeLiteral", - "start": 39, - "end": 52, - "loc": { - "start": { - "line": 2, - "column": 25 + }, + "name": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" }, - "end": { - "line": 2, - "column": 38 + "name": "T" + }, + "constraint": { + "type": "TSObjectKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 22 + } } }, - "members": [ - { - "type": "TSPropertySignature", - "start": 41, - "end": 50, - "loc": { - "start": { - "line": 2, - "column": 27 - }, - "end": { - "line": 2, - "column": 36 - } + "default": { + "type": "TSTypeLiteral", + "start": 39, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 25 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 2, + "column": 38 + } + }, + "members": [ + { + "type": "TSPropertySignature", "start": 41, - "end": 42, + "end": 50, "loc": { "start": { "line": 2, @@ -180,32 +179,48 @@ }, "end": { "line": 2, - "column": 28 - }, - "identifierName": "x" + "column": 36 + } }, - "name": "x" - }, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 44, - "end": 50, - "loc": { - "start": { - "line": 2, - "column": 30 + "computed": false, + "name": { + "type": "Identifier", + "start": 41, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 28 + }, + "identifierName": "x" }, - "end": { - "line": 2, - "column": 36 + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 44, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } } } } - } - ] + ] + } } - } - ], + ] + }, "parameters": [], "typeAnnotation": { "type": "TSTypeReference", diff --git a/test/fixtures/typescript/type-alias/generic-complex/expected.json b/test/fixtures/typescript/type-alias/generic-complex/expected.json index 45547371c0..909818094c 100644 --- a/test/fixtures/typescript/type-alias/generic-complex/expected.json +++ b/test/fixtures/typescript/type-alias/generic-complex/expected.json @@ -59,25 +59,25 @@ }, "name": "T" }, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 7, - "end": 39, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 39 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 7, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 7 }, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 40 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 7, - "end": 8, + "end": 39, "loc": { "start": { "line": 1, @@ -85,61 +85,60 @@ }, "end": { "line": 1, - "column": 8 - }, - "identifierName": "U" - }, - "name": "U" - }, - "constraint": { - "type": "TSObjectKeyword", - "start": 17, - "end": 23, - "loc": { - "start": { - "line": 1, - "column": 17 - }, - "end": { - "line": 1, - "column": 23 + "column": 39 } - } - }, - "default": { - "type": "TSTypeLiteral", - "start": 26, - "end": 39, - "loc": { - "start": { - "line": 1, - "column": 26 + }, + "name": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "U" }, - "end": { - "line": 1, - "column": 39 + "name": "U" + }, + "constraint": { + "type": "TSObjectKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } } }, - "members": [ - { - "type": "TSPropertySignature", - "start": 28, - "end": 37, - "loc": { - "start": { - "line": 1, - "column": 28 - }, - "end": { - "line": 1, - "column": 37 - } + "default": { + "type": "TSTypeLiteral", + "start": 26, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 26 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 39 + } + }, + "members": [ + { + "type": "TSPropertySignature", "start": 28, - "end": 29, + "end": 37, "loc": { "start": { "line": 1, @@ -147,32 +146,48 @@ }, "end": { "line": 1, - "column": 29 - }, - "identifierName": "x" + "column": 37 + } }, - "name": "x" - }, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 31, - "end": 37, - "loc": { - "start": { - "line": 1, - "column": 31 + "computed": false, + "name": { + "type": "Identifier", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 29 + }, + "identifierName": "x" }, - "end": { - "line": 1, - "column": 37 + "name": "x" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 37 + } } } } - } - ] + ] + } } - } - ], + ] + }, "typeAnnotation": { "type": "TSTypeReference", "start": 43, diff --git a/test/fixtures/typescript/type-alias/generic/expected.json b/test/fixtures/typescript/type-alias/generic/expected.json index df6951f90e..e81961a7be 100644 --- a/test/fixtures/typescript/type-alias/generic/expected.json +++ b/test/fixtures/typescript/type-alias/generic/expected.json @@ -59,23 +59,23 @@ }, "name": "T" }, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 7, - "end": 8, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 7, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 7 }, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 9 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 7, "end": 8, "loc": { @@ -86,13 +86,28 @@ "end": { "line": 1, "column": 8 - }, - "identifierName": "U" + } }, - "name": "U" + "name": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "U" + }, + "name": "U" + } } - } - ], + ] + }, "typeAnnotation": { "type": "TSTypeReference", "start": 12, diff --git a/test/fixtures/typescript/types/function-generic/expected.json b/test/fixtures/typescript/types/function-generic/expected.json index 8b8abdfa41..a8a85d7963 100644 --- a/test/fixtures/typescript/types/function-generic/expected.json +++ b/test/fixtures/typescript/types/function-generic/expected.json @@ -87,23 +87,23 @@ "column": 21 } }, - "typeParameters": [ - { - "type": "TSTypeParameterDeclaration", - "start": 8, - "end": 9, - "loc": { - "start": { - "line": 1, - "column": 8 - }, - "end": { - "line": 1, - "column": 9 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 8, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 8 }, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 10 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 8, "end": 9, "loc": { @@ -114,13 +114,28 @@ "end": { "line": 1, "column": 9 - }, - "identifierName": "T" + } }, - "name": "T" + "name": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "parameters": [ { "type": "Identifier", diff --git a/test/fixtures/typescript/types/mapped/expected.json b/test/fixtures/typescript/types/mapped/expected.json index f5bdb52a1c..3391375262 100644 --- a/test/fixtures/typescript/types/mapped/expected.json +++ b/test/fixtures/typescript/types/mapped/expected.json @@ -88,7 +88,7 @@ } }, "typeParameter": { - "type": "TSTypeParameterDeclaration", + "type": "TypeParameter", "start": 12, "end": 23, "loc": { @@ -217,7 +217,7 @@ }, "readonly": true, "typeParameter": { - "type": "TSTypeParameterDeclaration", + "type": "TypeParameter", "start": 58, "end": 69, "loc": { From ce3f89edaf061f49fe41842fe716e7f48dd8aa63 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 17 May 2017 10:53:07 -0700 Subject: [PATCH 07/55] Use TypeAnnotation node for type annotations --- src/plugins/typescript.js | 32 +- src/types.js | 17 +- .../arrow-function/annotated/expected.json | 17 +- .../async-generic/expected.json | 27 +- .../arrow-function/async/expected.json | 17 +- .../arrow-function/generic-tsx/expected.json | 27 +- .../arrow-function/generic/expected.json | 27 +- .../optional-parameter/expected.json | 17 +- .../predicate-types/expected.json | 55 ++- .../class/constructor/expected.json | 102 +++- .../typescript/class/declare/expected.json | 68 ++- .../typescript/class/generic/expected.json | 17 +- .../class/get-generic/expected.json | 17 +- .../class/index-signature/expected.json | 68 ++- .../members-with-modifier-names/expected.json | 51 +- .../members-with-reserved-names/expected.json | 17 +- .../class/method-computed/expected.json | 34 +- .../class/method-generic/expected.json | 143 ++++-- .../class/method-no-body/expected.json | 17 +- .../class/method-optional/expected.json | 17 +- .../class/method-return-type/expected.json | 17 +- .../class/parameter-properties/expected.json | 17 +- .../typescript/class/properties/expected.json | 34 +- .../class/property-computed/expected.json | 34 +- .../const/no-initializer/expected.json | 17 +- .../typescript/declare/const/expected.json | 34 +- .../declare/destructure/expected.json | 185 +++++--- .../typescript/declare/var/expected.json | 17 +- .../typescript/export/declare/expected.json | 34 +- .../function/annotated/expected.json | 54 ++- .../function/anonymous/expected.json | 54 ++- .../typescript/function/declare/expected.json | 44 +- .../function/overloads/expected.json | 68 ++- .../function/predicate-types/expected.json | 144 ++++-- .../interface/call-signature/expected.json | 34 +- .../construct-signature/expected.json | 34 +- .../interface/generic/expected.json | 17 +- .../interface/index-signature/expected.json | 34 +- .../interface/method-computed/expected.json | 34 +- .../interface/method-generic/expected.json | 44 +- .../interface/method-optional/expected.json | 17 +- .../interface/method-plain/expected.json | 59 ++- .../interface/modifiers/expected.json | 17 +- .../interface/properties/expected.json | 34 +- .../interface/property-computed/expected.json | 34 +- .../property-named-public/expected.json | 17 +- .../reserved-method-name/expected.json | 17 +- .../interface/separators/expected.json | 102 +++- .../body-declare/expected.json | 17 +- .../body-nested-declare/expected.json | 17 +- .../global-in-module/expected.json | 17 +- .../type-alias/generic-complex/expected.json | 17 +- .../typescript/types/array/expected.json | 29 +- .../types/function-generic/expected.json | 195 +++++--- .../types/function-in-generic/expected.json | 78 +++- .../types/function-with-this/expected.json | 107 +++-- .../typescript/types/function/expected.json | 265 +++++++---- .../typescript/types/indexed/expected.json | 73 +-- .../typescript/types/keywords/expected.json | 170 ++++++- .../types/literal-boolean/expected.json | 46 +- .../literal-number-negative/expected.json | 31 +- .../types/literal-number/expected.json | 31 +- .../types/literal-string/expected.json | 31 +- .../typescript/types/mapped/expected.json | 170 ++++--- .../reference-generic-nested/expected.json | 87 ++-- .../types/reference-generic/expected.json | 51 +- .../typescript/types/reference/expected.json | 27 +- .../types/type-literal/expected.json | 102 ++-- .../types/type-operator/expected.json | 35 +- .../typescript/types/typeof/expected.json | 69 +-- .../types/union-intersection/expected.json | 434 ++++++++++-------- 71 files changed, 3127 insertions(+), 1006 deletions(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index c1a961e7ad..de8d7e27c5 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -294,9 +294,9 @@ export default (superClass: Class): Class => class extends super signature.parameters = this.tsParseBindingListForSignature(); if (returnTokenRequired) { this.expect(returnToken); - signature.typeAnnotation = this.tsParseTypeOrTypePredicate(); + signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(); } else if (this.eat(returnToken)) { - signature.typeAnnotation = this.tsParseTypeOrTypePredicate(); + signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(); } } @@ -338,7 +338,7 @@ export default (superClass: Class): Class => class extends super this.expect(tt.bracketL); const id = this.parseIdentifier(); this.expect(tt.colon); - id.typeAnnotation = this.tsParseType(); + id.typeAnnotation = this.tsParseTypeAnnotation(); this.expect(tt.bracketR); node.parameters = [id]; @@ -447,7 +447,7 @@ export default (superClass: Class): Class => class extends super if (this.eat(tt.question)) { node.optional = true; } - node.typeAnnotation = this.tsTryParseTypeAnnotation(); + node.typeAnnotation = this.tsTryParseType(); this.semicolon(); this.expect(tt.braceR); @@ -654,6 +654,12 @@ export default (superClass: Class): Class => class extends super return false; } + tsParseTypeOrTypePredicateAnnotation(): N.TypeAnnotation { + const t: N.TypeAnnotation = this.startNode(); + t.typeAnnotation = this.tsParseTypeOrTypePredicate(); + return this.finishNode(t, "TypeAnnotation"); + } + tsParseTypeOrTypePredicate(): N.TsType { const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this)); @@ -668,7 +674,11 @@ export default (superClass: Class): Class => class extends super } } - tsTryParseTypeAnnotation(): ?N.TsType { + tsTryParseTypeAnnotation(): ?N.TypeAnnotation { + return this.eat(tt.colon) ? this.tsParseTypeAnnotation() : undefined; + } + + tsTryParseType(): ?N.TsType { return this.eat(tt.colon) ? this.tsParseType() : undefined; } @@ -680,6 +690,12 @@ export default (superClass: Class): Class => class extends super } } + tsParseTypeAnnotation(): N.TypeAnnotation { + const t: N.TypeAnnotation = this.startNode(); + t.typeAnnotation = this.tsParseType(); + return this.finishNode(t, "TypeAnnotation"); + } + tsParseType(): N.TsType { // Need to set `state.inType` so that we don't parse JSX in a type context. const oldInType = this.state.inType; @@ -1086,7 +1102,7 @@ export default (superClass: Class): Class => class extends super parseFunctionReturnType(node: N.Function): void { if (this.eat(tt.colon)) { - node.returnType = this.tsParseTypeOrTypePredicate(); + node.returnType = this.tsParseTypeOrTypePredicateAnnotation(); } } @@ -1253,7 +1269,7 @@ export default (superClass: Class): Class => class extends super types: ?$ReadOnlyArray): N.ArrowFunctionExpression { if (this.eat(tt.colon)) { - node.returnType = this.tsParseType(); + node.returnType = this.tsParseTypeAnnotation(); } return super.parseAsyncArrowFromCallExpression(node, call, types); } @@ -1351,7 +1367,7 @@ export default (superClass: Class): Class => class extends super const state = this.state.clone(); this.next(); try { - const returnType = this.tsParseTypeOrTypePredicate(); + const returnType = this.tsParseTypeOrTypePredicateAnnotation(); if (this.canInsertSemicolon()) this.unexpected(); if (!this.match(tt.arrow)) this.unexpected(); node.returnType = returnType; diff --git a/src/types.js b/src/types.js index cb835ce8b9..a4b848320a 100644 --- a/src/types.js +++ b/src/types.js @@ -134,7 +134,7 @@ export type FunctionBase = HasDecorators & { // TODO: All not in spec expression: boolean; typeParameters?: ?TypeParameterDeclaration; - returnType?: ?(TsType | FlowTypeAnnotation); + returnType?: ?TypeAnnotation; }; // Statements @@ -505,7 +505,7 @@ export type Accessibility = "public" | "protected" | "private"; export type PatternBase = HasDecorators & { // TODO: All not in spec // Flow/TypeScript only: - typeAnnotation?: ?(TsType | FlowTypeAnnotation); + typeAnnotation?: ?TypeAnnotation; // TypeScript only: optional?: ?true; accessibility?: ?Accessibility; // For parameter properties @@ -592,7 +592,7 @@ export type ClassProperty = ClassMemberBase & { key: Identifier; value: ?Expression; // TODO: Not in spec that this is nullable. - typeAnnotation?: ?(TsType | FlowTypeAnnotation); // TODO: Not in spec + typeAnnotation?: ?TypeAnnotation; // TODO: Not in spec variance?: ?FlowVariance; // TODO: Not in spec // TypeScript only: (TODO: Not in spec) @@ -696,7 +696,12 @@ export type JSXOpeningElement = Node; export type JSXClosingElement = Node; export type JSXElement = Node; -// Flow/TypeScript common +// Flow/TypeScript common (TODO: Not in spec) + +export type TypeAnnotation = NodeBase & { + type: "TypeAnnotation"; + typeAnnotation: TsType | FlowTypeAnnotation; +} export type TypeParameterDeclaration = NodeBase & { type: "TypeParameterDeclaration"; @@ -791,7 +796,7 @@ export type TsSignatureDeclaration = export type TsSignatureDeclarationOrIndexSignatureBase = { // Not using TypeScript's "ParameterDeclaration" here, since it's inconsistent with regular functions. parameters: $ReadOnlyArray; - typeAnnotation: ?TsType; + typeAnnotation: ?TypeAnnotation; } export type TsSignatureDeclarationBase = TsSignatureDeclarationOrIndexSignatureBase & { @@ -835,7 +840,7 @@ export type TsNamedTypeElementBase = TsTypeElementBase & { export type TsPropertySignature = TsNamedTypeElementBase & { type: "TSPropertySignature"; readonly?: true; - typeAnnotation?: TsType; + typeAnnotation?: TypeAnnotation; initializer?: Expression; }; diff --git a/test/fixtures/typescript/arrow-function/annotated/expected.json b/test/fixtures/typescript/arrow-function/annotated/expected.json index 4bc1b9b5bb..dadac86803 100644 --- a/test/fixtures/typescript/arrow-function/annotated/expected.json +++ b/test/fixtures/typescript/arrow-function/annotated/expected.json @@ -57,7 +57,7 @@ } }, "returnType": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 13, "end": 19, "loc": { @@ -69,6 +69,21 @@ "line": 1, "column": 19 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 13, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 19 + } + } } }, "id": null, diff --git a/test/fixtures/typescript/arrow-function/async-generic/expected.json b/test/fixtures/typescript/arrow-function/async-generic/expected.json index 14e40f4a1a..6e8f34ba2d 100644 --- a/test/fixtures/typescript/arrow-function/async-generic/expected.json +++ b/test/fixtures/typescript/arrow-function/async-generic/expected.json @@ -57,7 +57,7 @@ } }, "returnType": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 17, "end": 18, "loc": { @@ -70,8 +70,8 @@ "column": 18 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 17, "end": 18, "loc": { @@ -82,10 +82,25 @@ "end": { "line": 1, "column": 18 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "T" + }, + "name": "T" + } } }, "typeParameters": { diff --git a/test/fixtures/typescript/arrow-function/async/expected.json b/test/fixtures/typescript/arrow-function/async/expected.json index 073aacd594..7489457b21 100644 --- a/test/fixtures/typescript/arrow-function/async/expected.json +++ b/test/fixtures/typescript/arrow-function/async/expected.json @@ -57,7 +57,7 @@ } }, "returnType": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 20, "end": 23, "loc": { @@ -69,6 +69,21 @@ "line": 1, "column": 23 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 20, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 23 + } + } } }, "id": null, diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json index 9d166e6ae3..83f57befe0 100644 --- a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json +++ b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json @@ -57,7 +57,7 @@ } }, "returnType": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 72, "end": 73, "loc": { @@ -70,8 +70,8 @@ "column": 12 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 72, "end": 73, "loc": { @@ -82,10 +82,25 @@ "end": { "line": 2, "column": 12 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 72, + "end": 73, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + }, + "identifierName": "T" + }, + "name": "T" + } } }, "id": null, diff --git a/test/fixtures/typescript/arrow-function/generic/expected.json b/test/fixtures/typescript/arrow-function/generic/expected.json index 10a010ee14..ca6c7cef42 100644 --- a/test/fixtures/typescript/arrow-function/generic/expected.json +++ b/test/fixtures/typescript/arrow-function/generic/expected.json @@ -57,7 +57,7 @@ } }, "returnType": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 11, "end": 12, "loc": { @@ -70,8 +70,8 @@ "column": 12 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 11, "end": 12, "loc": { @@ -82,10 +82,25 @@ "end": { "line": 1, "column": 12 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "T" + }, + "name": "T" + } } }, "id": null, diff --git a/test/fixtures/typescript/arrow-function/optional-parameter/expected.json b/test/fixtures/typescript/arrow-function/optional-parameter/expected.json index afe06c383e..e349558e51 100644 --- a/test/fixtures/typescript/arrow-function/optional-parameter/expected.json +++ b/test/fixtures/typescript/arrow-function/optional-parameter/expected.json @@ -57,7 +57,7 @@ } }, "returnType": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 14, "end": 17, "loc": { @@ -69,6 +69,21 @@ "line": 1, "column": 17 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + } } }, "id": null, diff --git a/test/fixtures/typescript/arrow-function/predicate-types/expected.json b/test/fixtures/typescript/arrow-function/predicate-types/expected.json index c46fc887e8..5f0b36a4d5 100644 --- a/test/fixtures/typescript/arrow-function/predicate-types/expected.json +++ b/test/fixtures/typescript/arrow-function/predicate-types/expected.json @@ -57,7 +57,7 @@ } }, "returnType": { - "type": "TSTypePredicate", + "type": "TypeAnnotation", "start": 10, "end": 21, "loc": { @@ -70,10 +70,10 @@ "column": 21 } }, - "parameterName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypePredicate", "start": 10, - "end": 11, + "end": 21, "loc": { "start": { "line": 1, @@ -81,24 +81,39 @@ }, "end": { "line": 1, - "column": 11 - }, - "identifierName": "x" + "column": 21 + } }, - "name": "x" - }, - "typeAnnotation": { - "type": "TSStringKeyword", - "start": 15, - "end": 21, - "loc": { - "start": { - "line": 1, - "column": 15 + "parameterName": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "x" }, - "end": { - "line": 1, - "column": 21 + "name": "x" + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 21 + } } } } diff --git a/test/fixtures/typescript/class/constructor/expected.json b/test/fixtures/typescript/class/constructor/expected.json index c289bb055f..fb00c2ad46 100644 --- a/test/fixtures/typescript/class/constructor/expected.json +++ b/test/fixtures/typescript/class/constructor/expected.json @@ -131,7 +131,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 29, "end": 35, "loc": { @@ -143,6 +143,21 @@ "line": 2, "column": 25 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 29, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 25 + } + } } } }, @@ -163,7 +178,7 @@ }, "name": "y", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 40, "end": 46, "loc": { @@ -175,6 +190,21 @@ "line": 2, "column": 36 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 40, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } + } } } } @@ -236,7 +266,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 68, "end": 74, "loc": { @@ -248,6 +278,21 @@ "line": 3, "column": 25 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 68, + "end": 74, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 25 + } + } } } }, @@ -268,7 +313,7 @@ }, "name": "y", "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 79, "end": 85, "loc": { @@ -280,6 +325,21 @@ "line": 3, "column": 36 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 79, + "end": 85, + "loc": { + "start": { + "line": 3, + "column": 30 + }, + "end": { + "line": 3, + "column": 36 + } + } } } } @@ -341,7 +401,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 107, "end": 110, "loc": { @@ -353,6 +413,21 @@ "line": 4, "column": 22 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 107, + "end": 110, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 22 + } + } } } }, @@ -373,7 +448,7 @@ }, "name": "y", "typeAnnotation": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 115, "end": 118, "loc": { @@ -385,6 +460,21 @@ "line": 4, "column": 30 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 115, + "end": 118, + "loc": { + "start": { + "line": 4, + "column": 27 + }, + "end": { + "line": 4, + "column": 30 + } + } } } } diff --git a/test/fixtures/typescript/class/declare/expected.json b/test/fixtures/typescript/class/declare/expected.json index 653556e3ba..fcb934a0bd 100644 --- a/test/fixtures/typescript/class/declare/expected.json +++ b/test/fixtures/typescript/class/declare/expected.json @@ -107,7 +107,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 26, "end": 32, "loc": { @@ -119,12 +119,27 @@ "line": 2, "column": 14 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } } } } ], "typeAnnotation": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 35, "end": 38, "loc": { @@ -136,6 +151,21 @@ "line": 2, "column": 20 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 35, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + } } } }, @@ -208,7 +238,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 54, "end": 60, "loc": { @@ -220,6 +250,21 @@ "line": 4, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 54, + "end": 60, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + } } }, "value": null @@ -304,7 +349,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 80, "end": 84, "loc": { @@ -316,6 +361,21 @@ "line": 6, "column": 13 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 80, + "end": 84, + "loc": { + "start": { + "line": 6, + "column": 9 + }, + "end": { + "line": 6, + "column": 13 + } + } } } } diff --git a/test/fixtures/typescript/class/generic/expected.json b/test/fixtures/typescript/class/generic/expected.json index 60a566c59a..0189aa7881 100644 --- a/test/fixtures/typescript/class/generic/expected.json +++ b/test/fixtures/typescript/class/generic/expected.json @@ -168,7 +168,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 32, "end": 38, "loc": { @@ -180,6 +180,21 @@ "line": 1, "column": 38 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 38 + } + } } } } diff --git a/test/fixtures/typescript/class/get-generic/expected.json b/test/fixtures/typescript/class/get-generic/expected.json index 06dc9756e9..148be925c5 100644 --- a/test/fixtures/typescript/class/get-generic/expected.json +++ b/test/fixtures/typescript/class/get-generic/expected.json @@ -164,7 +164,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 32, "end": 36, "loc": { @@ -176,6 +176,21 @@ "line": 2, "column": 18 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 32, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 18 + } + } } } } diff --git a/test/fixtures/typescript/class/index-signature/expected.json b/test/fixtures/typescript/class/index-signature/expected.json index c822ecce6e..f3c1f7dc9f 100644 --- a/test/fixtures/typescript/class/index-signature/expected.json +++ b/test/fixtures/typescript/class/index-signature/expected.json @@ -107,7 +107,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 18, "end": 24, "loc": { @@ -119,12 +119,27 @@ "line": 2, "column": 14 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } } } } ], "typeAnnotation": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 27, "end": 30, "loc": { @@ -136,6 +151,21 @@ "line": 2, "column": 20 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + } + } } } }, @@ -172,7 +202,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 49, "end": 55, "loc": { @@ -184,12 +214,27 @@ "line": 3, "column": 23 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 49, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 23 + } + } } } } ], "typeAnnotation": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 58, "end": 61, "loc": { @@ -201,6 +246,21 @@ "line": 3, "column": 29 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 58, + "end": 61, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 29 + } + } } } } diff --git a/test/fixtures/typescript/class/members-with-modifier-names/expected.json b/test/fixtures/typescript/class/members-with-modifier-names/expected.json index 5403a3da26..4bf1257b7f 100644 --- a/test/fixtures/typescript/class/members-with-modifier-names/expected.json +++ b/test/fixtures/typescript/class/members-with-modifier-names/expected.json @@ -115,7 +115,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 24, "end": 28, "loc": { @@ -127,6 +127,21 @@ "line": 2, "column": 18 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 24, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 18 + } + } } } }, @@ -171,7 +186,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 51, "end": 55, "loc": { @@ -183,6 +198,21 @@ "line": 3, "column": 25 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 51, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 25 + } + } } } }, @@ -329,7 +359,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 91, "end": 95, "loc": { @@ -341,6 +371,21 @@ "line": 5, "column": 20 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 91, + "end": 95, + "loc": { + "start": { + "line": 5, + "column": 16 + }, + "end": { + "line": 5, + "column": 20 + } + } } } } diff --git a/test/fixtures/typescript/class/members-with-reserved-names/expected.json b/test/fixtures/typescript/class/members-with-reserved-names/expected.json index 5638646f3a..362d51067f 100644 --- a/test/fixtures/typescript/class/members-with-reserved-names/expected.json +++ b/test/fixtures/typescript/class/members-with-reserved-names/expected.json @@ -116,7 +116,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 31, "end": 35, "loc": { @@ -128,6 +128,21 @@ "line": 2, "column": 25 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 31, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 25 + } + } } } } diff --git a/test/fixtures/typescript/class/method-computed/expected.json b/test/fixtures/typescript/class/method-computed/expected.json index eb2b2cfb70..1f3ebb8df6 100644 --- a/test/fixtures/typescript/class/method-computed/expected.json +++ b/test/fixtures/typescript/class/method-computed/expected.json @@ -148,7 +148,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 35, "end": 39, "loc": { @@ -160,6 +160,21 @@ "line": 2, "column": 29 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 35, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + } + } } } }, @@ -237,7 +252,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 67, "end": 71, "loc": { @@ -249,6 +264,21 @@ "line": 3, "column": 30 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 67, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 30 + } + } } } } diff --git a/test/fixtures/typescript/class/method-generic/expected.json b/test/fixtures/typescript/class/method-generic/expected.json index bb3bdf073a..e2ee170760 100644 --- a/test/fixtures/typescript/class/method-generic/expected.json +++ b/test/fixtures/typescript/class/method-generic/expected.json @@ -180,7 +180,7 @@ }, "name": "a", "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 22, "end": 23, "loc": { @@ -193,8 +193,8 @@ "column": 13 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 22, "end": 23, "loc": { @@ -205,10 +205,25 @@ "end": { "line": 2, "column": 13 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 13 + }, + "identifierName": "T" + }, + "name": "T" + } } } }, @@ -230,7 +245,7 @@ "name": "b", "optional": true, "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 29, "end": 30, "loc": { @@ -243,8 +258,8 @@ "column": 20 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 29, "end": 30, "loc": { @@ -255,10 +270,25 @@ "end": { "line": 2, "column": 20 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } } } }, @@ -294,7 +324,7 @@ "name": "c" }, "typeAnnotation": { - "type": "TSArrayType", + "type": "TypeAnnotation", "start": 38, "end": 41, "loc": { @@ -307,10 +337,10 @@ "column": 31 } }, - "elementType": { - "type": "TSTypeReference", + "typeAnnotation": { + "type": "TSArrayType", "start": 38, - "end": 39, + "end": 41, "loc": { "start": { "line": 2, @@ -318,11 +348,11 @@ }, "end": { "line": 2, - "column": 29 + "column": 31 } }, - "typeName": { - "type": "Identifier", + "elementType": { + "type": "TSTypeReference", "start": 38, "end": 39, "loc": { @@ -333,17 +363,32 @@ "end": { "line": 2, "column": 29 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 29 + }, + "identifierName": "T" + }, + "name": "T" + } } } } } ], "returnType": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 44, "end": 45, "loc": { @@ -356,8 +401,8 @@ "column": 35 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 44, "end": 45, "loc": { @@ -368,10 +413,25 @@ "end": { "line": 2, "column": 35 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 2, + "column": 34 + }, + "end": { + "line": 2, + "column": 35 + }, + "identifierName": "T" + }, + "name": "T" + } } }, "body": { @@ -514,7 +574,7 @@ "async": false, "params": [], "returnType": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 77, "end": 78, "loc": { @@ -527,8 +587,8 @@ "column": 29 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 77, "end": 78, "loc": { @@ -539,10 +599,25 @@ "end": { "line": 3, "column": 29 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 77, + "end": 78, + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 29 + }, + "identifierName": "T" + }, + "name": "T" + } } }, "body": { diff --git a/test/fixtures/typescript/class/method-no-body/expected.json b/test/fixtures/typescript/class/method-no-body/expected.json index 4bbb26e551..ae05d52dbe 100644 --- a/test/fixtures/typescript/class/method-no-body/expected.json +++ b/test/fixtures/typescript/class/method-no-body/expected.json @@ -155,7 +155,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 28, "end": 32, "loc": { @@ -167,6 +167,21 @@ "line": 3, "column": 13 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 28, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 13 + } + } } } } diff --git a/test/fixtures/typescript/class/method-optional/expected.json b/test/fixtures/typescript/class/method-optional/expected.json index c5ec66f42c..b850b36d5a 100644 --- a/test/fixtures/typescript/class/method-optional/expected.json +++ b/test/fixtures/typescript/class/method-optional/expected.json @@ -116,7 +116,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 20, "end": 24, "loc": { @@ -128,6 +128,21 @@ "line": 2, "column": 14 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 20, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 14 + } + } } }, "body": { diff --git a/test/fixtures/typescript/class/method-return-type/expected.json b/test/fixtures/typescript/class/method-return-type/expected.json index e11905c45b..83cf345feb 100644 --- a/test/fixtures/typescript/class/method-return-type/expected.json +++ b/test/fixtures/typescript/class/method-return-type/expected.json @@ -115,7 +115,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 19, "end": 23, "loc": { @@ -127,6 +127,21 @@ "line": 2, "column": 13 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 13 + } + } } }, "body": { diff --git a/test/fixtures/typescript/class/parameter-properties/expected.json b/test/fixtures/typescript/class/parameter-properties/expected.json index 311aa0688c..0368e0449b 100644 --- a/test/fixtures/typescript/class/parameter-properties/expected.json +++ b/test/fixtures/typescript/class/parameter-properties/expected.json @@ -308,7 +308,7 @@ }, "name": "y", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 222, "end": 228, "loc": { @@ -320,6 +320,21 @@ "line": 10, "column": 24 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 222, + "end": 228, + "loc": { + "start": { + "line": 10, + "column": 18 + }, + "end": { + "line": 10, + "column": 24 + } + } } } }, diff --git a/test/fixtures/typescript/class/properties/expected.json b/test/fixtures/typescript/class/properties/expected.json index cd4b94d078..19849bef8e 100644 --- a/test/fixtures/typescript/class/properties/expected.json +++ b/test/fixtures/typescript/class/properties/expected.json @@ -180,7 +180,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 32, "end": 38, "loc": { @@ -192,6 +192,21 @@ "line": 4, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + } } }, "value": null @@ -230,7 +245,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 47, "end": 53, "loc": { @@ -242,6 +257,21 @@ "line": 5, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 47, + "end": 53, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 13 + } + } } }, "value": { diff --git a/test/fixtures/typescript/class/property-computed/expected.json b/test/fixtures/typescript/class/property-computed/expected.json index ff1b4c35f7..9b329685a8 100644 --- a/test/fixtures/typescript/class/property-computed/expected.json +++ b/test/fixtures/typescript/class/property-computed/expected.json @@ -142,7 +142,7 @@ "computed": false }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 33, "end": 39, "loc": { @@ -154,6 +154,21 @@ "line": 2, "column": 29 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 29 + } + } } }, "value": null @@ -226,7 +241,7 @@ }, "optional": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 65, "end": 71, "loc": { @@ -238,6 +253,21 @@ "line": 3, "column": 30 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 65, + "end": 71, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } + } } }, "value": null diff --git a/test/fixtures/typescript/const/no-initializer/expected.json b/test/fixtures/typescript/const/no-initializer/expected.json index 8f6d1953eb..6ab732e99e 100644 --- a/test/fixtures/typescript/const/no-initializer/expected.json +++ b/test/fixtures/typescript/const/no-initializer/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 9, "end": 15, "loc": { @@ -86,6 +86,21 @@ "line": 1, "column": 15 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } } } } diff --git a/test/fixtures/typescript/declare/const/expected.json b/test/fixtures/typescript/declare/const/expected.json index a6a9fa3e83..745fc35c98 100644 --- a/test/fixtures/typescript/declare/const/expected.json +++ b/test/fixtures/typescript/declare/const/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 17, "end": 23, "loc": { @@ -86,6 +86,21 @@ "line": 1, "column": 23 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } } } } @@ -121,7 +136,7 @@ }, "name": "y", "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 28, "end": 34, "loc": { @@ -133,6 +148,21 @@ "line": 1, "column": 34 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 34 + } + } } } } diff --git a/test/fixtures/typescript/declare/destructure/expected.json b/test/fixtures/typescript/declare/destructure/expected.json index 8d3b0abcd5..383a66aa46 100644 --- a/test/fixtures/typescript/declare/destructure/expected.json +++ b/test/fixtures/typescript/declare/destructure/expected.json @@ -184,7 +184,7 @@ } ], "typeAnnotation": { - "type": "TSTypeLiteral", + "type": "TypeAnnotation", "start": 24, "end": 48, "loc": { @@ -197,26 +197,25 @@ "column": 48 } }, - "members": [ - { - "type": "TSPropertySignature", - "start": 26, - "end": 36, - "loc": { - "start": { - "line": 1, - "column": 26 - }, - "end": { - "line": 1, - "column": 36 - } + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 24, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 24 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 48 + } + }, + "members": [ + { + "type": "TSPropertySignature", "start": 26, - "end": 27, + "end": 36, "loc": { "start": { "line": 1, @@ -224,47 +223,62 @@ }, "end": { "line": 1, - "column": 27 + "column": 36 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "x" }, - "identifierName": "x" + "name": "x" }, - "name": "x" - }, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 29, - "end": 35, - "loc": { - "start": { - "line": 1, - "column": 29 + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 29, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 35 + } }, - "end": { - "line": 1, - "column": 35 + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 29, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 35 + } + } } } - } - }, - { - "type": "TSPropertySignature", - "start": 37, - "end": 46, - "loc": { - "start": { - "line": 1, - "column": 37 - }, - "end": { - "line": 1, - "column": 46 - } }, - "computed": false, - "name": { - "type": "Identifier", + { + "type": "TSPropertySignature", "start": 37, - "end": 38, + "end": 46, "loc": { "start": { "line": 1, @@ -272,29 +286,60 @@ }, "end": { "line": 1, - "column": 38 + "column": 46 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 38 + }, + "identifierName": "y" }, - "identifierName": "y" + "name": "y" }, - "name": "y" - }, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 40, - "end": 46, - "loc": { - "start": { - "line": 1, - "column": 40 + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 40, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 40 + }, + "end": { + "line": 1, + "column": 46 + } }, - "end": { - "line": 1, - "column": 46 + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 40, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 40 + }, + "end": { + "line": 1, + "column": 46 + } + } } } } - } - ] + ] + } } } } diff --git a/test/fixtures/typescript/declare/var/expected.json b/test/fixtures/typescript/declare/var/expected.json index cab8ef29b5..5a8ce6961b 100644 --- a/test/fixtures/typescript/declare/var/expected.json +++ b/test/fixtures/typescript/declare/var/expected.json @@ -126,7 +126,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 30, "end": 33, "loc": { @@ -138,6 +138,21 @@ "line": 2, "column": 18 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 30, + "end": 33, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 18 + } + } } } }, diff --git a/test/fixtures/typescript/export/declare/expected.json b/test/fixtures/typescript/export/declare/expected.json index df05591583..9893599e50 100644 --- a/test/fixtures/typescript/export/declare/expected.json +++ b/test/fixtures/typescript/export/declare/expected.json @@ -90,7 +90,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 24, "end": 30, "loc": { @@ -102,6 +102,21 @@ "line": 1, "column": 30 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 24, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 30 + } + } } } } @@ -163,7 +178,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 61, "end": 65, "loc": { @@ -175,6 +190,21 @@ "line": 2, "column": 33 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 61, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 29 + }, + "end": { + "line": 2, + "column": 33 + } + } } }, "declare": true diff --git a/test/fixtures/typescript/function/annotated/expected.json b/test/fixtures/typescript/function/annotated/expected.json index 0b02abdc2a..517149cb5f 100644 --- a/test/fixtures/typescript/function/annotated/expected.json +++ b/test/fixtures/typescript/function/annotated/expected.json @@ -130,7 +130,7 @@ "name": "x", "optional": true, "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 18, "end": 19, "loc": { @@ -143,8 +143,8 @@ "column": 19 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 18, "end": 19, "loc": { @@ -155,16 +155,31 @@ "end": { "line": 1, "column": 19 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "T" + }, + "name": "T" + } } } } ], "returnType": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 22, "end": 23, "loc": { @@ -177,8 +192,8 @@ "column": 23 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 22, "end": 23, "loc": { @@ -189,10 +204,25 @@ "end": { "line": 1, "column": 23 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "T" + }, + "name": "T" + } } }, "body": { diff --git a/test/fixtures/typescript/function/anonymous/expected.json b/test/fixtures/typescript/function/anonymous/expected.json index eec2ce0fc6..fed1b9e90c 100644 --- a/test/fixtures/typescript/function/anonymous/expected.json +++ b/test/fixtures/typescript/function/anonymous/expected.json @@ -160,7 +160,7 @@ "name": "x", "optional": true, "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 26, "end": 27, "loc": { @@ -173,8 +173,8 @@ "column": 27 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 26, "end": 27, "loc": { @@ -185,16 +185,31 @@ "end": { "line": 1, "column": 27 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 27 + }, + "identifierName": "T" + }, + "name": "T" + } } } } ], "returnType": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 30, "end": 31, "loc": { @@ -207,8 +222,8 @@ "column": 31 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 30, "end": 31, "loc": { @@ -219,10 +234,25 @@ "end": { "line": 1, "column": 31 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 30, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 31 + }, + "identifierName": "T" + }, + "name": "T" + } } }, "body": { diff --git a/test/fixtures/typescript/function/declare/expected.json b/test/fixtures/typescript/function/declare/expected.json index c31d06dc99..0648b9d0ed 100644 --- a/test/fixtures/typescript/function/declare/expected.json +++ b/test/fixtures/typescript/function/declare/expected.json @@ -64,7 +64,7 @@ "async": false, "params": [], "returnType": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 22, "end": 26, "loc": { @@ -76,6 +76,21 @@ "line": 1, "column": 26 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 22, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 26 + } + } } }, "declare": true @@ -165,7 +180,7 @@ }, "params": [], "returnType": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 53, "end": 54, "loc": { @@ -178,8 +193,8 @@ "column": 26 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 53, "end": 54, "loc": { @@ -190,10 +205,25 @@ "end": { "line": 2, "column": 26 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 53, + "end": 54, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 26 + }, + "identifierName": "T" + }, + "name": "T" + } } }, "declare": true diff --git a/test/fixtures/typescript/function/overloads/expected.json b/test/fixtures/typescript/function/overloads/expected.json index 1ffe055e7a..d1c17481ab 100644 --- a/test/fixtures/typescript/function/overloads/expected.json +++ b/test/fixtures/typescript/function/overloads/expected.json @@ -96,7 +96,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 21, "end": 27, "loc": { @@ -108,12 +108,27 @@ "line": 1, "column": 27 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 27 + } + } } } } ], "returnType": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 30, "end": 36, "loc": { @@ -125,6 +140,21 @@ "line": 1, "column": 36 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 36 + } + } } } } @@ -197,7 +227,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 59, "end": 65, "loc": { @@ -209,12 +239,27 @@ "line": 2, "column": 27 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 59, + "end": 65, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 27 + } + } } } } ], "returnType": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 68, "end": 74, "loc": { @@ -226,6 +271,21 @@ "line": 2, "column": 36 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 68, + "end": 74, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } + } } } } diff --git a/test/fixtures/typescript/function/predicate-types/expected.json b/test/fixtures/typescript/function/predicate-types/expected.json index 45cd0b6073..77393469fe 100644 --- a/test/fixtures/typescript/function/predicate-types/expected.json +++ b/test/fixtures/typescript/function/predicate-types/expected.json @@ -80,7 +80,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 14, "end": 17, "loc": { @@ -92,12 +92,27 @@ "line": 1, "column": 17 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 14, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 17 + } + } } } } ], "returnType": { - "type": "TSTypePredicate", + "type": "TypeAnnotation", "start": 20, "end": 32, "loc": { @@ -110,10 +125,10 @@ "column": 32 } }, - "parameterName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypePredicate", "start": 20, - "end": 21, + "end": 32, "loc": { "start": { "line": 1, @@ -121,24 +136,39 @@ }, "end": { "line": 1, - "column": 21 - }, - "identifierName": "x" + "column": 32 + } }, - "name": "x" - }, - "typeAnnotation": { - "type": "TSBooleanKeyword", - "start": 25, - "end": 32, - "loc": { - "start": { - "line": 1, - "column": 25 + "parameterName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "x" }, - "end": { - "line": 1, - "column": 32 + "name": "x" + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 25, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 32 + } } } } @@ -211,7 +241,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 49, "end": 52, "loc": { @@ -223,12 +253,27 @@ "line": 2, "column": 16 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 49, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 16 + } + } } } } ], "returnType": { - "type": "TSTypePredicate", + "type": "TypeAnnotation", "start": 55, "end": 67, "loc": { @@ -241,10 +286,10 @@ "column": 31 } }, - "parameterName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypePredicate", "start": 55, - "end": 56, + "end": 67, "loc": { "start": { "line": 2, @@ -252,24 +297,39 @@ }, "end": { "line": 2, - "column": 20 - }, - "identifierName": "x" + "column": 31 + } }, - "name": "x" - }, - "typeAnnotation": { - "type": "TSBooleanKeyword", - "start": 60, - "end": 67, - "loc": { - "start": { - "line": 2, - "column": 24 + "parameterName": { + "type": "Identifier", + "start": 55, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "x" }, - "end": { - "line": 2, - "column": 31 + "name": "x" + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 60, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 31 + } } } } diff --git a/test/fixtures/typescript/interface/call-signature/expected.json b/test/fixtures/typescript/interface/call-signature/expected.json index 9a566496e1..6c2269e66f 100644 --- a/test/fixtures/typescript/interface/call-signature/expected.json +++ b/test/fixtures/typescript/interface/call-signature/expected.json @@ -92,7 +92,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 22, "end": 28, "loc": { @@ -104,12 +104,27 @@ "line": 2, "column": 14 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } } } } ], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 31, "end": 35, "loc": { @@ -121,6 +136,21 @@ "line": 2, "column": 21 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 31, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 21 + } + } } } } diff --git a/test/fixtures/typescript/interface/construct-signature/expected.json b/test/fixtures/typescript/interface/construct-signature/expected.json index 6000d12875..8c61fa5889 100644 --- a/test/fixtures/typescript/interface/construct-signature/expected.json +++ b/test/fixtures/typescript/interface/construct-signature/expected.json @@ -92,7 +92,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 26, "end": 32, "loc": { @@ -104,12 +104,27 @@ "line": 2, "column": 18 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + } } } } ], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 35, "end": 39, "loc": { @@ -121,6 +136,21 @@ "line": 2, "column": 25 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 35, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 25 + } + } } } } diff --git a/test/fixtures/typescript/interface/generic/expected.json b/test/fixtures/typescript/interface/generic/expected.json index c2ae14a250..c2dda7ee66 100644 --- a/test/fixtures/typescript/interface/generic/expected.json +++ b/test/fixtures/typescript/interface/generic/expected.json @@ -168,7 +168,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 36, "end": 42, "loc": { @@ -180,6 +180,21 @@ "line": 1, "column": 42 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 36, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 42 + } + } } } } diff --git a/test/fixtures/typescript/interface/index-signature/expected.json b/test/fixtures/typescript/interface/index-signature/expected.json index 02c36bf936..a91eeac4ab 100644 --- a/test/fixtures/typescript/interface/index-signature/expected.json +++ b/test/fixtures/typescript/interface/index-signature/expected.json @@ -92,7 +92,7 @@ }, "name": "s", "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 22, "end": 28, "loc": { @@ -104,12 +104,27 @@ "line": 2, "column": 14 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } } } } ], "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 31, "end": 37, "loc": { @@ -121,6 +136,21 @@ "line": 2, "column": 23 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } } } } diff --git a/test/fixtures/typescript/interface/method-computed/expected.json b/test/fixtures/typescript/interface/method-computed/expected.json index 8baebd2959..c18324e583 100644 --- a/test/fixtures/typescript/interface/method-computed/expected.json +++ b/test/fixtures/typescript/interface/method-computed/expected.json @@ -127,7 +127,7 @@ }, "parameters": [], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 39, "end": 43, "loc": { @@ -139,6 +139,21 @@ "line": 2, "column": 29 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 39, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + } + } } } }, @@ -210,7 +225,7 @@ "optional": true, "parameters": [], "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 71, "end": 77, "loc": { @@ -222,6 +237,21 @@ "line": 3, "column": 32 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 71, + "end": 77, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 32 + } + } } } } diff --git a/test/fixtures/typescript/interface/method-generic/expected.json b/test/fixtures/typescript/interface/method-generic/expected.json index f841b26a67..1c8bccf8fa 100644 --- a/test/fixtures/typescript/interface/method-generic/expected.json +++ b/test/fixtures/typescript/interface/method-generic/expected.json @@ -201,7 +201,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 44, "end": 50, "loc": { @@ -213,6 +213,21 @@ "line": 2, "column": 36 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 44, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } + } } } } @@ -223,7 +238,7 @@ }, "parameters": [], "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 57, "end": 58, "loc": { @@ -236,8 +251,8 @@ "column": 44 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 57, "end": 58, "loc": { @@ -248,10 +263,25 @@ "end": { "line": 2, "column": 44 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 43 + }, + "end": { + "line": 2, + "column": 44 + }, + "identifierName": "T" + }, + "name": "T" + } } } } diff --git a/test/fixtures/typescript/interface/method-optional/expected.json b/test/fixtures/typescript/interface/method-optional/expected.json index d6b75e8883..261ca03825 100644 --- a/test/fixtures/typescript/interface/method-optional/expected.json +++ b/test/fixtures/typescript/interface/method-optional/expected.json @@ -95,7 +95,7 @@ "optional": true, "parameters": [], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 24, "end": 28, "loc": { @@ -107,6 +107,21 @@ "line": 2, "column": 14 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 24, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 14 + } + } } } } diff --git a/test/fixtures/typescript/interface/method-plain/expected.json b/test/fixtures/typescript/interface/method-plain/expected.json index 1a75413e26..f83a5234ab 100644 --- a/test/fixtures/typescript/interface/method-plain/expected.json +++ b/test/fixtures/typescript/interface/method-plain/expected.json @@ -145,7 +145,7 @@ "name": "x", "optional": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 33, "end": 39, "loc": { @@ -157,6 +157,21 @@ "line": 3, "column": 16 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 16 + } + } } } }, @@ -192,7 +207,7 @@ "name": "y" }, "typeAnnotation": { - "type": "TSArrayType", + "type": "TypeAnnotation", "start": 47, "end": 55, "loc": { @@ -205,10 +220,10 @@ "column": 32 } }, - "elementType": { - "type": "TSNumberKeyword", + "typeAnnotation": { + "type": "TSArrayType", "start": 47, - "end": 53, + "end": 55, "loc": { "start": { "line": 3, @@ -216,7 +231,22 @@ }, "end": { "line": 3, - "column": 30 + "column": 32 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 47, + "end": 53, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } } } } @@ -224,7 +254,7 @@ } ], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 58, "end": 62, "loc": { @@ -236,6 +266,21 @@ "line": 3, "column": 39 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 58, + "end": 62, + "loc": { + "start": { + "line": 3, + "column": 35 + }, + "end": { + "line": 3, + "column": 39 + } + } } } } diff --git a/test/fixtures/typescript/interface/modifiers/expected.json b/test/fixtures/typescript/interface/modifiers/expected.json index be81ef67b2..9751090aa5 100644 --- a/test/fixtures/typescript/interface/modifiers/expected.json +++ b/test/fixtures/typescript/interface/modifiers/expected.json @@ -94,7 +94,7 @@ }, "readonly": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 30, "end": 36, "loc": { @@ -106,6 +106,21 @@ "line": 2, "column": 22 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 22 + } + } } } } diff --git a/test/fixtures/typescript/interface/properties/expected.json b/test/fixtures/typescript/interface/properties/expected.json index f72369911f..fe6582d8d3 100644 --- a/test/fixtures/typescript/interface/properties/expected.json +++ b/test/fixtures/typescript/interface/properties/expected.json @@ -126,7 +126,7 @@ "name": "y" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 28, "end": 34, "loc": { @@ -138,6 +138,21 @@ "line": 3, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 13 + } + } } } }, @@ -175,7 +190,7 @@ }, "optional": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 44, "end": 50, "loc": { @@ -187,6 +202,21 @@ "line": 4, "column": 14 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 44, + "end": 50, + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 14 + } + } } } } diff --git a/test/fixtures/typescript/interface/property-computed/expected.json b/test/fixtures/typescript/interface/property-computed/expected.json index e066168546..b0b9cb569c 100644 --- a/test/fixtures/typescript/interface/property-computed/expected.json +++ b/test/fixtures/typescript/interface/property-computed/expected.json @@ -126,7 +126,7 @@ "computed": false }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 37, "end": 43, "loc": { @@ -138,6 +138,21 @@ "line": 2, "column": 29 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 37, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 29 + } + } } } }, @@ -208,7 +223,7 @@ }, "optional": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 69, "end": 75, "loc": { @@ -220,6 +235,21 @@ "line": 3, "column": 30 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 69, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } + } } } } diff --git a/test/fixtures/typescript/interface/property-named-public/expected.json b/test/fixtures/typescript/interface/property-named-public/expected.json index 8eaedc0094..a808c420bb 100644 --- a/test/fixtures/typescript/interface/property-named-public/expected.json +++ b/test/fixtures/typescript/interface/property-named-public/expected.json @@ -93,7 +93,7 @@ "name": "public" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 23, "end": 29, "loc": { @@ -105,6 +105,21 @@ "line": 2, "column": 15 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 23, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 15 + } + } } } } diff --git a/test/fixtures/typescript/interface/reserved-method-name/expected.json b/test/fixtures/typescript/interface/reserved-method-name/expected.json index 4b745a912f..e986db879c 100644 --- a/test/fixtures/typescript/interface/reserved-method-name/expected.json +++ b/test/fixtures/typescript/interface/reserved-method-name/expected.json @@ -94,7 +94,7 @@ }, "parameters": [], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 27, "end": 31, "loc": { @@ -106,6 +106,21 @@ "line": 2, "column": 17 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 27, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 17 + } + } } } } diff --git a/test/fixtures/typescript/interface/separators/expected.json b/test/fixtures/typescript/interface/separators/expected.json index bae854af32..48aeb1aba5 100644 --- a/test/fixtures/typescript/interface/separators/expected.json +++ b/test/fixtures/typescript/interface/separators/expected.json @@ -93,7 +93,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 21, "end": 27, "loc": { @@ -105,6 +105,21 @@ "line": 1, "column": 27 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 27 + } + } } } }, @@ -141,7 +156,7 @@ "name": "y" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 32, "end": 38, "loc": { @@ -153,6 +168,21 @@ "line": 1, "column": 38 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 38 + } + } } } } @@ -223,7 +253,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 61, "end": 67, "loc": { @@ -235,6 +265,21 @@ "line": 2, "column": 26 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 61, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 26 + } + } } } }, @@ -271,7 +316,7 @@ "name": "y" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 72, "end": 78, "loc": { @@ -283,6 +328,21 @@ "line": 2, "column": 37 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 72, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 37 + } + } } } } @@ -353,7 +413,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 108, "end": 114, "loc": { @@ -365,6 +425,21 @@ "line": 4, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 108, + "end": 114, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + } } } }, @@ -401,7 +476,7 @@ "name": "y" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 122, "end": 128, "loc": { @@ -413,6 +488,21 @@ "line": 5, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 122, + "end": 128, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 13 + } + } } } } diff --git a/test/fixtures/typescript/module-namespace/body-declare/expected.json b/test/fixtures/typescript/module-namespace/body-declare/expected.json index 32154a93fb..99619eb165 100644 --- a/test/fixtures/typescript/module-namespace/body-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/body-declare/expected.json @@ -120,7 +120,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 35, "end": 41, "loc": { @@ -132,6 +132,21 @@ "line": 2, "column": 19 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 35, + "end": 41, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 19 + } + } } } } diff --git a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json index ce3aa7aa0c..6172ead122 100644 --- a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json @@ -166,7 +166,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 57, "end": 63, "loc": { @@ -178,6 +178,21 @@ "line": 3, "column": 23 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 57, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 23 + } + } } } } diff --git a/test/fixtures/typescript/module-namespace/global-in-module/expected.json b/test/fixtures/typescript/module-namespace/global-in-module/expected.json index acbfcdbc33..3e9e73988a 100644 --- a/test/fixtures/typescript/module-namespace/global-in-module/expected.json +++ b/test/fixtures/typescript/module-namespace/global-in-module/expected.json @@ -170,7 +170,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 49, "end": 55, "loc": { @@ -182,6 +182,21 @@ "line": 3, "column": 21 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 49, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 15 + }, + "end": { + "line": 3, + "column": 21 + } + } } } }, diff --git a/test/fixtures/typescript/type-alias/generic-complex/expected.json b/test/fixtures/typescript/type-alias/generic-complex/expected.json index 909818094c..adc71d3e72 100644 --- a/test/fixtures/typescript/type-alias/generic-complex/expected.json +++ b/test/fixtures/typescript/type-alias/generic-complex/expected.json @@ -168,7 +168,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 31, "end": 37, "loc": { @@ -180,6 +180,21 @@ "line": 1, "column": 37 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 37 + } + } } } } diff --git a/test/fixtures/typescript/types/array/expected.json b/test/fixtures/typescript/types/array/expected.json index 17ac9f8707..df9def1b23 100644 --- a/test/fixtures/typescript/types/array/expected.json +++ b/test/fixtures/typescript/types/array/expected.json @@ -74,7 +74,7 @@ }, "name": "arr", "typeAnnotation": { - "type": "TSArrayType", + "type": "TypeAnnotation", "start": 9, "end": 19, "loc": { @@ -87,10 +87,10 @@ "column": 19 } }, - "elementType": { + "typeAnnotation": { "type": "TSArrayType", "start": 9, - "end": 17, + "end": 19, "loc": { "start": { "line": 1, @@ -98,13 +98,13 @@ }, "end": { "line": 1, - "column": 17 + "column": 19 } }, "elementType": { - "type": "TSNumberKeyword", + "type": "TSArrayType", "start": 9, - "end": 15, + "end": 17, "loc": { "start": { "line": 1, @@ -112,7 +112,22 @@ }, "end": { "line": 1, - "column": 15 + "column": 17 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } } } } diff --git a/test/fixtures/typescript/types/function-generic/expected.json b/test/fixtures/typescript/types/function-generic/expected.json index a8a85d7963..9b848ba6d6 100644 --- a/test/fixtures/typescript/types/function-generic/expected.json +++ b/test/fixtures/typescript/types/function-generic/expected.json @@ -74,7 +74,7 @@ }, "name": "f", "typeAnnotation": { - "type": "TSFunctionType", + "type": "TypeAnnotation", "start": 7, "end": 21, "loc": { @@ -87,98 +87,175 @@ "column": 21 } }, - "typeParameters": { - "type": "TypeParameterDeclaration", - "start": 8, - "end": 10, + "typeAnnotation": { + "type": "TSFunctionType", + "start": 7, + "end": 21, "loc": { "start": { "line": 1, - "column": 8 + "column": 7 }, "end": { "line": 1, - "column": 10 + "column": 21 } }, - "params": [ + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 8, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + }, + "parameters": [ { - "type": "TypeParameter", - "start": 8, - "end": 9, + "type": "Identifier", + "start": 11, + "end": 15, "loc": { "start": { "line": 1, - "column": 8 + "column": 11 }, "end": { "line": 1, - "column": 9 - } + "column": 15 + }, + "identifierName": "a" }, - "name": { - "type": "Identifier", - "start": 8, - "end": 9, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 14, + "end": 15, "loc": { "start": { "line": 1, - "column": 8 + "column": 14 }, "end": { "line": 1, - "column": 9 - }, - "identifierName": "T" + "column": 15 + } }, - "name": "T" + "typeAnnotation": { + "type": "TSTypeReference", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "typeName": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "T" + }, + "name": "T" + } + } } } - ] - }, - "parameters": [ - { - "type": "Identifier", - "start": 11, - "end": 15, + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 20, + "end": 21, "loc": { "start": { "line": 1, - "column": 11 + "column": 20 }, "end": { "line": 1, - "column": 15 - }, - "identifierName": "a" + "column": 21 + } }, - "name": "a", "typeAnnotation": { "type": "TSTypeReference", - "start": 14, - "end": 15, + "start": 20, + "end": 21, "loc": { "start": { "line": 1, - "column": 14 + "column": 20 }, "end": { "line": 1, - "column": 15 + "column": 21 } }, "typeName": { "type": "Identifier", - "start": 14, - "end": 15, + "start": 20, + "end": 21, "loc": { "start": { "line": 1, - "column": 14 + "column": 20 }, "end": { "line": 1, - "column": 15 + "column": 21 }, "identifierName": "T" }, @@ -186,38 +263,6 @@ } } } - ], - "typeAnnotation": { - "type": "TSTypeReference", - "start": 20, - "end": 21, - "loc": { - "start": { - "line": 1, - "column": 20 - }, - "end": { - "line": 1, - "column": 21 - } - }, - "typeName": { - "type": "Identifier", - "start": 20, - "end": 21, - "loc": { - "start": { - "line": 1, - "column": 20 - }, - "end": { - "line": 1, - "column": 21 - }, - "identifierName": "T" - }, - "name": "T" - } } } }, diff --git a/test/fixtures/typescript/types/function-in-generic/expected.json b/test/fixtures/typescript/types/function-in-generic/expected.json index fede131143..dfc3d8a385 100644 --- a/test/fixtures/typescript/types/function-in-generic/expected.json +++ b/test/fixtures/typescript/types/function-in-generic/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 7, "end": 24, "loc": { @@ -87,10 +87,10 @@ "column": 24 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 7, - "end": 12, + "end": 24, "loc": { "start": { "line": 1, @@ -98,45 +98,75 @@ }, "end": { "line": 1, - "column": 12 - }, - "identifierName": "Array" + "column": 24 + } }, - "name": "Array" - }, - "typeArguments": [ - { - "type": "TSFunctionType", - "start": 13, - "end": 23, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 12, "loc": { "start": { "line": 1, - "column": 13 + "column": 7 }, "end": { "line": 1, - "column": 23 - } + "column": 12 + }, + "identifierName": "Array" }, - "parameters": [], - "typeAnnotation": { - "type": "TSVoidKeyword", - "start": 19, + "name": "Array" + }, + "typeArguments": [ + { + "type": "TSFunctionType", + "start": 13, "end": 23, "loc": { "start": { "line": 1, - "column": 19 + "column": 13 }, "end": { "line": 1, "column": 23 } + }, + "parameters": [], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 23 + } + } + } } } - } - ] + ] + } } }, "init": null diff --git a/test/fixtures/typescript/types/function-with-this/expected.json b/test/fixtures/typescript/types/function-with-this/expected.json index 6a617488b9..8cfe6f197d 100644 --- a/test/fixtures/typescript/types/function-with-this/expected.json +++ b/test/fixtures/typescript/types/function-with-this/expected.json @@ -74,7 +74,7 @@ }, "name": "f", "typeAnnotation": { - "type": "TSFunctionType", + "type": "TypeAnnotation", "start": 7, "end": 29, "loc": { @@ -87,54 +87,99 @@ "column": 29 } }, - "parameters": [ - { - "type": "Identifier", - "start": 8, - "end": 20, + "typeAnnotation": { + "type": "TSFunctionType", + "start": 7, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 8, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "this" + }, + "name": "this", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 20 + } + } + } + } + } + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 25, + "end": 29, "loc": { "start": { "line": 1, - "column": 8 + "column": 25 }, "end": { "line": 1, - "column": 20 - }, - "identifierName": "this" + "column": 29 + } }, - "name": "this", "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 14, - "end": 20, + "type": "TSVoidKeyword", + "start": 25, + "end": 29, "loc": { "start": { "line": 1, - "column": 14 + "column": 25 }, "end": { "line": 1, - "column": 20 + "column": 29 } } } } - ], - "typeAnnotation": { - "type": "TSVoidKeyword", - "start": 25, - "end": 29, - "loc": { - "start": { - "line": 1, - "column": 25 - }, - "end": { - "line": 1, - "column": 29 - } - } } } }, diff --git a/test/fixtures/typescript/types/function/expected.json b/test/fixtures/typescript/types/function/expected.json index a18456b555..32081abfbc 100644 --- a/test/fixtures/typescript/types/function/expected.json +++ b/test/fixtures/typescript/types/function/expected.json @@ -74,7 +74,7 @@ }, "name": "f", "typeAnnotation": { - "type": "TSFunctionType", + "type": "TypeAnnotation", "start": 7, "end": 54, "loc": { @@ -87,121 +87,151 @@ "column": 54 } }, - "parameters": [ - { - "type": "Identifier", - "start": 8, - "end": 17, - "loc": { - "start": { - "line": 1, - "column": 8 - }, - "end": { - "line": 1, - "column": 17 - }, - "identifierName": "a" + "typeAnnotation": { + "type": "TSFunctionType", + "start": 7, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 7 }, - "name": "a", - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 11, + "end": { + "line": 1, + "column": 54 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 8, "end": 17, "loc": { "start": { "line": 1, - "column": 11 + "column": 8 }, "end": { "line": 1, "column": 17 - } - } - } - }, - { - "type": "Identifier", - "start": 19, - "end": 29, - "loc": { - "start": { - "line": 1, - "column": 19 - }, - "end": { - "line": 1, - "column": 29 + }, + "identifierName": "a" }, - "identifierName": "b" - }, - "name": "b", - "optional": true, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 23, - "end": 29, - "loc": { - "start": { - "line": 1, - "column": 23 + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + } }, - "end": { - "line": 1, - "column": 29 + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + } + } } } - } - }, - { - "type": "RestElement", - "start": 31, - "end": 45, - "loc": { - "start": { - "line": 1, - "column": 31 - }, - "end": { - "line": 1, - "column": 45 - } }, - "argument": { + { "type": "Identifier", - "start": 34, - "end": 35, + "start": 19, + "end": 29, "loc": { "start": { "line": 1, - "column": 34 + "column": 19 }, "end": { "line": 1, - "column": 35 + "column": 29 }, - "identifierName": "c" + "identifierName": "b" }, - "name": "c" + "name": "b", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 23, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 23, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 29 + } + } + } + } }, - "typeAnnotation": { - "type": "TSArrayType", - "start": 37, + { + "type": "RestElement", + "start": 31, "end": 45, "loc": { "start": { "line": 1, - "column": 37 + "column": 31 }, "end": { "line": 1, "column": 45 } }, - "elementType": { - "type": "TSNumberKeyword", + "argument": { + "type": "Identifier", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 35 + }, + "identifierName": "c" + }, + "name": "c" + }, + "typeAnnotation": { + "type": "TypeAnnotation", "start": 37, - "end": 43, + "end": 45, "loc": { "start": { "line": 1, @@ -209,25 +239,70 @@ }, "end": { "line": 1, - "column": 43 + "column": 45 + } + }, + "typeAnnotation": { + "type": "TSArrayType", + "start": 37, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 37, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 43 + } + } } } } } - } - ], - "typeAnnotation": { - "type": "TSVoidKeyword", - "start": 50, - "end": 54, - "loc": { - "start": { - "line": 1, - "column": 50 + ], + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 50, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 50 + }, + "end": { + "line": 1, + "column": 54 + } }, - "end": { - "line": 1, - "column": 54 + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 50, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 50 + }, + "end": { + "line": 1, + "column": 54 + } + } } } } diff --git a/test/fixtures/typescript/types/indexed/expected.json b/test/fixtures/typescript/types/indexed/expected.json index a04ff81e27..1be0b67ff4 100644 --- a/test/fixtures/typescript/types/indexed/expected.json +++ b/test/fixtures/typescript/types/indexed/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSIndexedAccessType", + "type": "TypeAnnotation", "start": 7, "end": 11, "loc": { @@ -87,10 +87,10 @@ "column": 11 } }, - "objectType": { - "type": "TSTypeReference", + "typeAnnotation": { + "type": "TSIndexedAccessType", "start": 7, - "end": 8, + "end": 11, "loc": { "start": { "line": 1, @@ -98,11 +98,11 @@ }, "end": { "line": 1, - "column": 8 + "column": 11 } }, - "typeName": { - "type": "Identifier", + "objectType": { + "type": "TSTypeReference", "start": 7, "end": 8, "loc": { @@ -113,28 +113,28 @@ "end": { "line": 1, "column": 8 - }, - "identifierName": "T" - }, - "name": "T" - } - }, - "indexType": { - "type": "TSTypeReference", - "start": 9, - "end": 10, - "loc": { - "start": { - "line": 1, - "column": 9 + } }, - "end": { - "line": 1, - "column": 10 + "typeName": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" } }, - "typeName": { - "type": "Identifier", + "indexType": { + "type": "TSTypeReference", "start": 9, "end": 10, "loc": { @@ -145,10 +145,25 @@ "end": { "line": 1, "column": 10 - }, - "identifierName": "K" + } }, - "name": "K" + "typeName": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "K" + }, + "name": "K" + } } } } diff --git a/test/fixtures/typescript/types/keywords/expected.json b/test/fixtures/typescript/types/keywords/expected.json index e11add3b2a..58298cde14 100644 --- a/test/fixtures/typescript/types/keywords/expected.json +++ b/test/fixtures/typescript/types/keywords/expected.json @@ -74,7 +74,7 @@ }, "name": "a", "typeAnnotation": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 7, "end": 10, "loc": { @@ -86,6 +86,21 @@ "line": 1, "column": 10 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 7, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + } } } }, @@ -140,7 +155,7 @@ }, "name": "b", "typeAnnotation": { - "type": "TSBooleanKeyword", + "type": "TypeAnnotation", "start": 19, "end": 26, "loc": { @@ -152,6 +167,21 @@ "line": 2, "column": 14 } + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 19, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 14 + } + } } } }, @@ -206,7 +236,7 @@ }, "name": "ne", "typeAnnotation": { - "type": "TSNeverKeyword", + "type": "TypeAnnotation", "start": 36, "end": 41, "loc": { @@ -218,6 +248,21 @@ "line": 3, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNeverKeyword", + "start": 36, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 13 + } + } } } }, @@ -272,7 +317,7 @@ }, "name": "nul", "typeAnnotation": { - "type": "TSNullKeyword", + "type": "TypeAnnotation", "start": 52, "end": 56, "loc": { @@ -284,6 +329,21 @@ "line": 4, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNullKeyword", + "start": 52, + "end": 56, + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 13 + } + } } } }, @@ -338,7 +398,7 @@ }, "name": "num", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 67, "end": 73, "loc": { @@ -350,6 +410,21 @@ "line": 5, "column": 15 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 67, + "end": 73, + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 15 + } + } } } }, @@ -404,7 +479,7 @@ }, "name": "o", "typeAnnotation": { - "type": "TSObjectKeyword", + "type": "TypeAnnotation", "start": 82, "end": 88, "loc": { @@ -416,6 +491,21 @@ "line": 6, "column": 13 } + }, + "typeAnnotation": { + "type": "TSObjectKeyword", + "start": 82, + "end": 88, + "loc": { + "start": { + "line": 6, + "column": 7 + }, + "end": { + "line": 6, + "column": 13 + } + } } } }, @@ -470,7 +560,7 @@ }, "name": "st", "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 98, "end": 104, "loc": { @@ -482,6 +572,21 @@ "line": 7, "column": 14 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 98, + "end": 104, + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 14 + } + } } } }, @@ -536,7 +641,7 @@ }, "name": "sy", "typeAnnotation": { - "type": "TSSymbolKeyword", + "type": "TypeAnnotation", "start": 114, "end": 120, "loc": { @@ -548,6 +653,21 @@ "line": 8, "column": 14 } + }, + "typeAnnotation": { + "type": "TSSymbolKeyword", + "start": 114, + "end": 120, + "loc": { + "start": { + "line": 8, + "column": 8 + }, + "end": { + "line": 8, + "column": 14 + } + } } } }, @@ -602,7 +722,7 @@ }, "name": "u", "typeAnnotation": { - "type": "TSUndefinedKeyword", + "type": "TypeAnnotation", "start": 129, "end": 138, "loc": { @@ -614,6 +734,21 @@ "line": 9, "column": 16 } + }, + "typeAnnotation": { + "type": "TSUndefinedKeyword", + "start": 129, + "end": 138, + "loc": { + "start": { + "line": 9, + "column": 7 + }, + "end": { + "line": 9, + "column": 16 + } + } } } }, @@ -668,7 +803,7 @@ }, "name": "v", "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 147, "end": 151, "loc": { @@ -680,6 +815,21 @@ "line": 10, "column": 11 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 147, + "end": 151, + "loc": { + "start": { + "line": 10, + "column": 7 + }, + "end": { + "line": 10, + "column": 11 + } + } } } }, diff --git a/test/fixtures/typescript/types/literal-boolean/expected.json b/test/fixtures/typescript/types/literal-boolean/expected.json index fe855a59a7..407b568dc7 100644 --- a/test/fixtures/typescript/types/literal-boolean/expected.json +++ b/test/fixtures/typescript/types/literal-boolean/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSLiteralType", + "type": "TypeAnnotation", "start": 7, "end": 11, "loc": { @@ -87,8 +87,8 @@ "column": 11 } }, - "literal": { - "type": "BooleanLiteral", + "typeAnnotation": { + "type": "TSLiteralType", "start": 7, "end": 11, "loc": { @@ -101,7 +101,22 @@ "column": 11 } }, - "value": true + "literal": { + "type": "BooleanLiteral", + "start": 7, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "value": true + } } } }, @@ -156,7 +171,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSLiteralType", + "type": "TypeAnnotation", "start": 20, "end": 25, "loc": { @@ -169,8 +184,8 @@ "column": 12 } }, - "literal": { - "type": "BooleanLiteral", + "typeAnnotation": { + "type": "TSLiteralType", "start": 20, "end": 25, "loc": { @@ -183,7 +198,22 @@ "column": 12 } }, - "value": false + "literal": { + "type": "BooleanLiteral", + "start": 20, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "value": false + } } } }, diff --git a/test/fixtures/typescript/types/literal-number-negative/expected.json b/test/fixtures/typescript/types/literal-number-negative/expected.json index 4c43274c4d..5f4f8ea30f 100644 --- a/test/fixtures/typescript/types/literal-number-negative/expected.json +++ b/test/fixtures/typescript/types/literal-number-negative/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSLiteralType", + "type": "TypeAnnotation", "start": 7, "end": 9, "loc": { @@ -87,8 +87,8 @@ "column": 9 } }, - "literal": { - "type": "NumericLiteral", + "typeAnnotation": { + "type": "TSLiteralType", "start": 7, "end": 9, "loc": { @@ -101,11 +101,26 @@ "column": 9 } }, - "extra": { - "rawValue": -1, - "raw": "-1" - }, - "value": -1 + "literal": { + "type": "NumericLiteral", + "start": 7, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "extra": { + "rawValue": -1, + "raw": "-1" + }, + "value": -1 + } } } }, diff --git a/test/fixtures/typescript/types/literal-number/expected.json b/test/fixtures/typescript/types/literal-number/expected.json index 579c851ad2..bbe6431e6a 100644 --- a/test/fixtures/typescript/types/literal-number/expected.json +++ b/test/fixtures/typescript/types/literal-number/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSLiteralType", + "type": "TypeAnnotation", "start": 7, "end": 8, "loc": { @@ -87,8 +87,8 @@ "column": 8 } }, - "literal": { - "type": "NumericLiteral", + "typeAnnotation": { + "type": "TSLiteralType", "start": 7, "end": 8, "loc": { @@ -101,11 +101,26 @@ "column": 8 } }, - "extra": { - "rawValue": 0, - "raw": "0" - }, - "value": 0 + "literal": { + "type": "NumericLiteral", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } } } }, diff --git a/test/fixtures/typescript/types/literal-string/expected.json b/test/fixtures/typescript/types/literal-string/expected.json index 82cd450f65..d840cb8959 100644 --- a/test/fixtures/typescript/types/literal-string/expected.json +++ b/test/fixtures/typescript/types/literal-string/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSLiteralType", + "type": "TypeAnnotation", "start": 7, "end": 12, "loc": { @@ -87,8 +87,8 @@ "column": 12 } }, - "literal": { - "type": "StringLiteral", + "typeAnnotation": { + "type": "TSLiteralType", "start": 7, "end": 12, "loc": { @@ -101,11 +101,26 @@ "column": 12 } }, - "extra": { - "rawValue": "foo", - "raw": "\"foo\"" - }, - "value": "foo" + "literal": { + "type": "StringLiteral", + "start": 7, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } } } }, diff --git a/test/fixtures/typescript/types/mapped/expected.json b/test/fixtures/typescript/types/mapped/expected.json index 3391375262..23a66c8df3 100644 --- a/test/fixtures/typescript/types/mapped/expected.json +++ b/test/fixtures/typescript/types/mapped/expected.json @@ -74,7 +74,7 @@ }, "name": "map", "typeAnnotation": { - "type": "TSMappedType", + "type": "TypeAnnotation", "start": 9, "end": 35, "loc": { @@ -87,24 +87,24 @@ "column": 35 } }, - "typeParameter": { - "type": "TypeParameter", - "start": 12, - "end": 23, + "typeAnnotation": { + "type": "TSMappedType", + "start": 9, + "end": 35, "loc": { "start": { "line": 1, - "column": 12 + "column": 9 }, "end": { "line": 1, - "column": 23 + "column": 35 } }, - "name": { - "type": "Identifier", + "typeParameter": { + "type": "TypeParameter", "start": 12, - "end": 13, + "end": 23, "loc": { "start": { "line": 1, @@ -112,42 +112,57 @@ }, "end": { "line": 1, - "column": 13 + "column": 23 + } + }, + "name": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "P" }, - "identifierName": "P" + "name": "P" }, - "name": "P" + "constraint": { + "type": "TSStringKeyword", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 23 + } + } + } }, - "constraint": { - "type": "TSStringKeyword", - "start": 17, - "end": 23, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 26, + "end": 32, "loc": { "start": { "line": 1, - "column": 17 + "column": 26 }, "end": { "line": 1, - "column": 23 + "column": 32 } } } - }, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 26, - "end": 32, - "loc": { - "start": { - "line": 1, - "column": 26 - }, - "end": { - "line": 1, - "column": 32 - } - } } } }, @@ -202,7 +217,7 @@ }, "name": "map", "typeAnnotation": { - "type": "TSMappedType", + "type": "TypeAnnotation", "start": 46, "end": 82, "loc": { @@ -215,25 +230,25 @@ "column": 45 } }, - "readonly": true, - "typeParameter": { - "type": "TypeParameter", - "start": 58, - "end": 69, + "typeAnnotation": { + "type": "TSMappedType", + "start": 46, + "end": 82, "loc": { "start": { "line": 2, - "column": 21 + "column": 9 }, "end": { "line": 2, - "column": 32 + "column": 45 } }, - "name": { - "type": "Identifier", + "readonly": true, + "typeParameter": { + "type": "TypeParameter", "start": 58, - "end": 59, + "end": 69, "loc": { "start": { "line": 2, @@ -241,43 +256,58 @@ }, "end": { "line": 2, - "column": 22 + "column": 32 + } + }, + "name": { + "type": "Identifier", + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + }, + "identifierName": "P" }, - "identifierName": "P" + "name": "P" }, - "name": "P" + "constraint": { + "type": "TSStringKeyword", + "start": 63, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 26 + }, + "end": { + "line": 2, + "column": 32 + } + } + } }, - "constraint": { - "type": "TSStringKeyword", - "start": 63, - "end": 69, + "optional": true, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 73, + "end": 79, "loc": { "start": { "line": 2, - "column": 26 + "column": 36 }, "end": { "line": 2, - "column": 32 + "column": 42 } } } - }, - "optional": true, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 73, - "end": 79, - "loc": { - "start": { - "line": 2, - "column": 36 - }, - "end": { - "line": 2, - "column": 42 - } - } } } }, diff --git a/test/fixtures/typescript/types/reference-generic-nested/expected.json b/test/fixtures/typescript/types/reference-generic-nested/expected.json index d7663b125d..f3adcc0345 100644 --- a/test/fixtures/typescript/types/reference-generic-nested/expected.json +++ b/test/fixtures/typescript/types/reference-generic-nested/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 7, "end": 27, "loc": { @@ -87,10 +87,10 @@ "column": 27 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 7, - "end": 12, + "end": 27, "loc": { "start": { "line": 1, @@ -98,31 +98,31 @@ }, "end": { "line": 1, - "column": 12 - }, - "identifierName": "Array" + "column": 27 + } }, - "name": "Array" - }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 13, - "end": 26, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 12, "loc": { "start": { "line": 1, - "column": 13 + "column": 7 }, "end": { "line": 1, - "column": 26 - } + "column": 12 + }, + "identifierName": "Array" }, - "typeName": { - "type": "Identifier", + "name": "Array" + }, + "typeArguments": [ + { + "type": "TSTypeReference", "start": 13, - "end": 18, + "end": 26, "loc": { "start": { "line": 1, @@ -130,31 +130,46 @@ }, "end": { "line": 1, - "column": 18 - }, - "identifierName": "Array" + "column": 26 + } }, - "name": "Array" - }, - "typeArguments": [ - { - "type": "TSNumberKeyword", - "start": 19, - "end": 25, + "typeName": { + "type": "Identifier", + "start": 13, + "end": 18, "loc": { "start": { "line": 1, - "column": 19 + "column": 13 }, "end": { "line": 1, - "column": 25 + "column": 18 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeArguments": [ + { + "type": "TSNumberKeyword", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 25 + } } } - } - ] - } - ] + ] + } + ] + } } }, "init": null diff --git a/test/fixtures/typescript/types/reference-generic/expected.json b/test/fixtures/typescript/types/reference-generic/expected.json index be824ce58b..4d9f94f5d7 100644 --- a/test/fixtures/typescript/types/reference-generic/expected.json +++ b/test/fixtures/typescript/types/reference-generic/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 7, "end": 20, "loc": { @@ -87,10 +87,10 @@ "column": 20 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 7, - "end": 12, + "end": 20, "loc": { "start": { "line": 1, @@ -98,29 +98,44 @@ }, "end": { "line": 1, - "column": 12 - }, - "identifierName": "Array" + "column": 20 + } }, - "name": "Array" - }, - "typeArguments": [ - { - "type": "TSNumberKeyword", - "start": 13, - "end": 19, + "typeName": { + "type": "Identifier", + "start": 7, + "end": 12, "loc": { "start": { "line": 1, - "column": 13 + "column": 7 }, "end": { "line": 1, - "column": 19 + "column": 12 + }, + "identifierName": "Array" + }, + "name": "Array" + }, + "typeArguments": [ + { + "type": "TSNumberKeyword", + "start": 13, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 19 + } } } - } - ] + ] + } } }, "init": null diff --git a/test/fixtures/typescript/types/reference/expected.json b/test/fixtures/typescript/types/reference/expected.json index cc0181ee3f..157b23cbba 100644 --- a/test/fixtures/typescript/types/reference/expected.json +++ b/test/fixtures/typescript/types/reference/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 7, "end": 8, "loc": { @@ -87,8 +87,8 @@ "column": 8 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 7, "end": 8, "loc": { @@ -99,10 +99,25 @@ "end": { "line": 1, "column": 8 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } } } }, diff --git a/test/fixtures/typescript/types/type-literal/expected.json b/test/fixtures/typescript/types/type-literal/expected.json index 8b1c64358e..232f1590fd 100644 --- a/test/fixtures/typescript/types/type-literal/expected.json +++ b/test/fixtures/typescript/types/type-literal/expected.json @@ -74,7 +74,7 @@ }, "name": "obj", "typeAnnotation": { - "type": "TSTypeLiteral", + "type": "TypeAnnotation", "start": 9, "end": 22, "loc": { @@ -87,26 +87,25 @@ "column": 22 } }, - "members": [ - { - "type": "TSPropertySignature", - "start": 11, - "end": 20, - "loc": { - "start": { - "line": 1, - "column": 11 - }, - "end": { - "line": 1, - "column": 20 - } + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 9, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 9 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 22 + } + }, + "members": [ + { + "type": "TSPropertySignature", "start": 11, - "end": 12, + "end": 20, "loc": { "start": { "line": 1, @@ -114,29 +113,60 @@ }, "end": { "line": 1, - "column": 12 + "column": 20 + } + }, + "computed": false, + "name": { + "type": "Identifier", + "start": 11, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "x" }, - "identifierName": "x" + "name": "x" }, - "name": "x" - }, - "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 14, - "end": 20, - "loc": { - "start": { - "line": 1, - "column": 14 + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 20 + } }, - "end": { - "line": 1, - "column": 20 + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 14, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 20 + } + } } } } - } - ] + ] + } } }, "init": null diff --git a/test/fixtures/typescript/types/type-operator/expected.json b/test/fixtures/typescript/types/type-operator/expected.json index 9a906d4875..58080d57b3 100644 --- a/test/fixtures/typescript/types/type-operator/expected.json +++ b/test/fixtures/typescript/types/type-operator/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSTypeOperator", + "type": "TypeAnnotation", "start": 7, "end": 14, "loc": { @@ -87,23 +87,23 @@ "column": 14 } }, - "operator": "keyof", "typeAnnotation": { - "type": "TSTypeReference", - "start": 13, + "type": "TSTypeOperator", + "start": 7, "end": 14, "loc": { "start": { "line": 1, - "column": 13 + "column": 7 }, "end": { "line": 1, "column": 14 } }, - "typeName": { - "type": "Identifier", + "operator": "keyof", + "typeAnnotation": { + "type": "TSTypeReference", "start": 13, "end": 14, "loc": { @@ -114,10 +114,25 @@ "end": { "line": 1, "column": 14 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "T" + }, + "name": "T" + } } } } diff --git a/test/fixtures/typescript/types/typeof/expected.json b/test/fixtures/typescript/types/typeof/expected.json index 88f8848d31..edd14acd6a 100644 --- a/test/fixtures/typescript/types/typeof/expected.json +++ b/test/fixtures/typescript/types/typeof/expected.json @@ -74,7 +74,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSTypeQuery", + "type": "TypeAnnotation", "start": 7, "end": 17, "loc": { @@ -87,53 +87,68 @@ "column": 17 } }, - "exprName": { - "type": "TSQualifiedName", - "start": 14, + "typeAnnotation": { + "type": "TSTypeQuery", + "start": 7, "end": 17, "loc": { "start": { "line": 1, - "column": 14 + "column": 7 }, "end": { "line": 1, "column": 17 } }, - "left": { - "type": "Identifier", + "exprName": { + "type": "TSQualifiedName", "start": 14, - "end": 15, - "loc": { - "start": { - "line": 1, - "column": 14 - }, - "end": { - "line": 1, - "column": 15 - }, - "identifierName": "y" - }, - "name": "y" - }, - "right": { - "type": "Identifier", - "start": 16, "end": 17, "loc": { "start": { "line": 1, - "column": 16 + "column": 14 }, "end": { "line": 1, "column": 17 + } + }, + "left": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "y" }, - "identifierName": "z" + "name": "y" }, - "name": "z" + "right": { + "type": "Identifier", + "start": 16, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "z" + }, + "name": "z" + } } } } diff --git a/test/fixtures/typescript/types/union-intersection/expected.json b/test/fixtures/typescript/types/union-intersection/expected.json index 6b93c6d468..d54e2867dd 100644 --- a/test/fixtures/typescript/types/union-intersection/expected.json +++ b/test/fixtures/typescript/types/union-intersection/expected.json @@ -74,7 +74,7 @@ }, "name": "union", "typeAnnotation": { - "type": "TSUnionType", + "type": "TypeAnnotation", "start": 11, "end": 36, "loc": { @@ -87,53 +87,68 @@ "column": 36 } }, - "types": [ - { - "type": "TSNumberKeyword", - "start": 11, - "end": 17, - "loc": { - "start": { - "line": 1, - "column": 11 - }, - "end": { - "line": 1, - "column": 17 - } + "typeAnnotation": { + "type": "TSUnionType", + "start": 11, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 36 } }, - { - "type": "TSNullKeyword", - "start": 20, - "end": 24, - "loc": { - "start": { - "line": 1, - "column": 20 - }, - "end": { - "line": 1, - "column": 24 + "types": [ + { + "type": "TSNumberKeyword", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + } } - } - }, - { - "type": "TSUndefinedKeyword", - "start": 27, - "end": 36, - "loc": { - "start": { - "line": 1, - "column": 27 - }, - "end": { - "line": 1, - "column": 36 + }, + { + "type": "TSNullKeyword", + "start": 20, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "TSUndefinedKeyword", + "start": 27, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 36 + } } } - } - ] + ] + } } }, "init": null @@ -187,7 +202,7 @@ }, "name": "intersection", "typeAnnotation": { - "type": "TSIntersectionType", + "type": "TypeAnnotation", "start": 56, "end": 71, "loc": { @@ -200,38 +215,53 @@ "column": 33 } }, - "types": [ - { - "type": "TSNumberKeyword", - "start": 56, - "end": 62, - "loc": { - "start": { - "line": 2, - "column": 18 - }, - "end": { - "line": 2, - "column": 24 - } + "typeAnnotation": { + "type": "TSIntersectionType", + "start": 56, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 33 } }, - { - "type": "TSStringKeyword", - "start": 65, - "end": 71, - "loc": { - "start": { - "line": 2, - "column": 27 - }, - "end": { - "line": 2, - "column": 33 + "types": [ + { + "type": "TSNumberKeyword", + "start": 56, + "end": 62, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 24 + } + } + }, + { + "type": "TSStringKeyword", + "start": 65, + "end": 71, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 33 + } } } - } - ] + ] + } } }, "init": null @@ -285,7 +315,7 @@ }, "name": "precedence1", "typeAnnotation": { - "type": "TSUnionType", + "type": "TypeAnnotation", "start": 90, "end": 115, "loc": { @@ -298,70 +328,85 @@ "column": 42 } }, - "types": [ - { - "type": "TSNumberKeyword", - "start": 90, - "end": 96, - "loc": { - "start": { - "line": 3, - "column": 17 - }, - "end": { - "line": 3, - "column": 23 - } + "typeAnnotation": { + "type": "TSUnionType", + "start": 90, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 42 } }, - { - "type": "TSIntersectionType", - "start": 99, - "end": 115, - "loc": { - "start": { - "line": 3, - "column": 26 - }, - "end": { - "line": 3, - "column": 42 + "types": [ + { + "type": "TSNumberKeyword", + "start": 90, + "end": 96, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 23 + } } }, - "types": [ - { - "type": "TSStringKeyword", - "start": 99, - "end": 105, - "loc": { - "start": { - "line": 3, - "column": 26 - }, - "end": { - "line": 3, - "column": 32 - } + { + "type": "TSIntersectionType", + "start": 99, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 42 } }, - { - "type": "TSBooleanKeyword", - "start": 108, - "end": 115, - "loc": { - "start": { - "line": 3, - "column": 35 - }, - "end": { - "line": 3, - "column": 42 + "types": [ + { + "type": "TSStringKeyword", + "start": 99, + "end": 105, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 32 + } + } + }, + { + "type": "TSBooleanKeyword", + "start": 108, + "end": 115, + "loc": { + "start": { + "line": 3, + "column": 35 + }, + "end": { + "line": 3, + "column": 42 + } } } - } - ] - } - ] + ] + } + ] + } } }, "init": null @@ -415,7 +460,7 @@ }, "name": "precedence2", "typeAnnotation": { - "type": "TSUnionType", + "type": "TypeAnnotation", "start": 134, "end": 159, "loc": { @@ -428,70 +473,85 @@ "column": 42 } }, - "types": [ - { - "type": "TSIntersectionType", - "start": 134, - "end": 149, - "loc": { - "start": { - "line": 4, - "column": 17 - }, - "end": { - "line": 4, - "column": 32 - } + "typeAnnotation": { + "type": "TSUnionType", + "start": 134, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 17 }, - "types": [ - { - "type": "TSNumberKeyword", - "start": 134, - "end": 140, - "loc": { - "start": { - "line": 4, - "column": 17 - }, - "end": { - "line": 4, - "column": 23 - } + "end": { + "line": 4, + "column": 42 + } + }, + "types": [ + { + "type": "TSIntersectionType", + "start": 134, + "end": 149, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 32 } }, - { - "type": "TSStringKeyword", - "start": 143, - "end": 149, - "loc": { - "start": { - "line": 4, - "column": 26 - }, - "end": { - "line": 4, - "column": 32 + "types": [ + { + "type": "TSNumberKeyword", + "start": 134, + "end": 140, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 23 + } + } + }, + { + "type": "TSStringKeyword", + "start": 143, + "end": 149, + "loc": { + "start": { + "line": 4, + "column": 26 + }, + "end": { + "line": 4, + "column": 32 + } } } - } - ] - }, - { - "type": "TSBooleanKeyword", - "start": 152, - "end": 159, - "loc": { - "start": { - "line": 4, - "column": 35 - }, - "end": { - "line": 4, - "column": 42 + ] + }, + { + "type": "TSBooleanKeyword", + "start": 152, + "end": 159, + "loc": { + "start": { + "line": 4, + "column": 35 + }, + "end": { + "line": 4, + "column": 42 + } } } - } - ] + ] + } } }, "init": null From e40866806f8c28870265e511a6c39a6664ad13fd Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 17 May 2017 13:56:42 -0700 Subject: [PATCH 08/55] Remove optional "name" on TSTypeElementBase, because some type elements may never have "name", others are TSNamedTypeElementBase --- src/types.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/types.js b/src/types.js index a4b848320a..1b6ec2008f 100644 --- a/src/types.js +++ b/src/types.js @@ -817,7 +817,6 @@ export type TsTypeElement = export type TsTypeElementBase = NodeBase & { // TypeScript uses a `PropertyName` here, // but babylon just uses an `Expression` for object keys, so use that. - name?: Expression; optional?: true; } From c0b32cecebaeb631d8da543767be708a806abef4 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 17 May 2017 14:02:17 -0700 Subject: [PATCH 09/55] Add TSInterfaceBody intermediate node --- src/plugins/typescript.js | 4 +- src/types.js | 7 +- .../declare/interface/expected.json | 17 +- .../typescript/export/declare/expected.json | 17 +- .../interface/call-signature/expected.json | 125 +++-- .../construct-signature/expected.json | 125 +++-- .../typescript/interface/export/expected.json | 19 +- .../interface/extends/expected.json | 17 +- .../interface/generic/expected.json | 17 +- .../interface/index-signature/expected.json | 129 +++-- .../interface/method-computed/expected.json | 263 +++++----- .../interface/method-generic/expected.json | 307 +++++------ .../interface/method-optional/expected.json | 95 ++-- .../interface/method-plain/expected.json | 257 ++++----- .../interface/modifiers/expected.json | 97 ++-- .../interface/properties/expected.json | 211 ++++---- .../interface/property-computed/expected.json | 259 ++++----- .../property-named-public/expected.json | 91 ++-- .../reserved-method-name/expected.json | 93 ++-- .../interface/separators/expected.json | 495 ++++++++++-------- 20 files changed, 1477 insertions(+), 1168 deletions(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index de8d7e27c5..6dbc026a29 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -754,7 +754,9 @@ export default (superClass: Class): Class => class extends super if (this.eat(tt._extends)) { node.extends = this.tsParseHeritageClause(); } - node.members = this.tsParseObjectTypeMembers(); + const body: N.TSInterfaceBody = this.startNode(); + body.body = this.tsParseObjectTypeMembers(); + node.body = this.finishNode(body, "TSInterfaceBody"); return this.finishNode(node, "TSInterfaceDeclaration"); } diff --git a/src/types.js b/src/types.js index 1b6ec2008f..5a9ef78a1e 100644 --- a/src/types.js +++ b/src/types.js @@ -996,7 +996,12 @@ export type TsInterfaceDeclaration = DeclarationBase & { typeParameters: ?TypeParameterDeclaration; // TS uses "heritageClauses", but want this to resemble ClassBase. extends?: $ReadOnlyArray; - members: $ReadOnlyArray; + body: TSInterfaceBody; +} + +export type TSInterfaceBody = NodeBase & { + type: "TSInterfaceBody"; + body: $ReadOnlyArray; }; export type TsExpressionWithTypeArguments = TsTypeBase & { diff --git a/test/fixtures/typescript/declare/interface/expected.json b/test/fixtures/typescript/declare/interface/expected.json index f1860bb53e..bd786411c5 100644 --- a/test/fixtures/typescript/declare/interface/expected.json +++ b/test/fixtures/typescript/declare/interface/expected.json @@ -59,7 +59,22 @@ }, "name": "I" }, - "members": [], + "body": { + "type": "TSInterfaceBody", + "start": 20, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "body": [] + }, "declare": true } ], diff --git a/test/fixtures/typescript/export/declare/expected.json b/test/fixtures/typescript/export/declare/expected.json index 9893599e50..b978e9b0a4 100644 --- a/test/fixtures/typescript/export/declare/expected.json +++ b/test/fixtures/typescript/export/declare/expected.json @@ -324,7 +324,22 @@ }, "name": "I" }, - "members": [], + "body": { + "type": "TSInterfaceBody", + "start": 120, + "end": 122, + "loc": { + "start": { + "line": 4, + "column": 27 + }, + "end": { + "line": 4, + "column": 29 + } + }, + "body": [] + }, "declare": true } }, diff --git a/test/fixtures/typescript/interface/call-signature/expected.json b/test/fixtures/typescript/interface/call-signature/expected.json index 6c2269e66f..c144ea60c5 100644 --- a/test/fixtures/typescript/interface/call-signature/expected.json +++ b/test/fixtures/typescript/interface/call-signature/expected.json @@ -59,54 +59,54 @@ }, "name": "I" }, - "members": [ - { - "type": "TSCallSignatureDeclaration", - "start": 18, - "end": 36, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 22 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "parameters": [ - { - "type": "Identifier", - "start": 19, - "end": 28, - "loc": { - "start": { - "line": 2, - "column": 5 - }, - "end": { - "line": 2, - "column": 14 - }, - "identifierName": "x" + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSCallSignatureDeclaration", + "start": 18, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 4 }, - "name": "x", - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 22, + "end": { + "line": 2, + "column": 22 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 19, "end": 28, "loc": { "start": { "line": 2, - "column": 8 + "column": 5 }, "end": { "line": 2, "column": 14 - } + }, + "identifierName": "x" }, + "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 22, "end": 28, "loc": { @@ -118,27 +118,27 @@ "line": 2, "column": 14 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } } } } - } - ], - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 31, - "end": 35, - "loc": { - "start": { - "line": 2, - "column": 17 - }, - "end": { - "line": 2, - "column": 21 - } - }, + ], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 31, "end": 35, "loc": { @@ -150,11 +150,26 @@ "line": 2, "column": 21 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 31, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 21 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/construct-signature/expected.json b/test/fixtures/typescript/interface/construct-signature/expected.json index 8c61fa5889..b5e1ce72ba 100644 --- a/test/fixtures/typescript/interface/construct-signature/expected.json +++ b/test/fixtures/typescript/interface/construct-signature/expected.json @@ -59,54 +59,54 @@ }, "name": "I" }, - "members": [ - { - "type": "TSConstructSignatureDeclaration", - "start": 18, - "end": 40, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 26 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "parameters": [ - { - "type": "Identifier", - "start": 23, - "end": 32, - "loc": { - "start": { - "line": 2, - "column": 9 - }, - "end": { - "line": 2, - "column": 18 - }, - "identifierName": "x" + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSConstructSignatureDeclaration", + "start": 18, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 4 }, - "name": "x", - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 26, + "end": { + "line": 2, + "column": 26 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 23, "end": 32, "loc": { "start": { "line": 2, - "column": 12 + "column": 9 }, "end": { "line": 2, "column": 18 - } + }, + "identifierName": "x" }, + "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 26, "end": 32, "loc": { @@ -118,27 +118,27 @@ "line": 2, "column": 18 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 26, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 18 + } + } } } } - } - ], - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 35, - "end": 39, - "loc": { - "start": { - "line": 2, - "column": 21 - }, - "end": { - "line": 2, - "column": 25 - } - }, + ], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 35, "end": 39, "loc": { @@ -150,11 +150,26 @@ "line": 2, "column": 25 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 35, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 25 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/export/expected.json b/test/fixtures/typescript/interface/export/expected.json index 41c3acafc8..0ed26af20f 100644 --- a/test/fixtures/typescript/interface/export/expected.json +++ b/test/fixtures/typescript/interface/export/expected.json @@ -75,7 +75,24 @@ }, "name": "I" }, - "members": [], + "body": { + "type": "TSInterfaceBody", + "start": 19, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "body": [], + "leadingComments": null, + "trailingComments": null + }, "trailingComments": null }, "trailingComments": [ diff --git a/test/fixtures/typescript/interface/extends/expected.json b/test/fixtures/typescript/interface/extends/expected.json index 0e854c0bb7..8cc4bb33d9 100644 --- a/test/fixtures/typescript/interface/extends/expected.json +++ b/test/fixtures/typescript/interface/extends/expected.json @@ -159,7 +159,22 @@ ] } ], - "members": [] + "body": { + "type": "TSInterfaceBody", + "start": 27, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "body": [] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/generic/expected.json b/test/fixtures/typescript/interface/generic/expected.json index c2dda7ee66..a84fdd1617 100644 --- a/test/fixtures/typescript/interface/generic/expected.json +++ b/test/fixtures/typescript/interface/generic/expected.json @@ -203,7 +203,22 @@ } ] }, - "members": [] + "body": { + "type": "TSInterfaceBody", + "start": 46, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 46 + }, + "end": { + "line": 1, + "column": 48 + } + }, + "body": [] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/index-signature/expected.json b/test/fixtures/typescript/interface/index-signature/expected.json index a91eeac4ab..e6df7627e2 100644 --- a/test/fixtures/typescript/interface/index-signature/expected.json +++ b/test/fixtures/typescript/interface/index-signature/expected.json @@ -59,54 +59,54 @@ }, "name": "I" }, - "members": [ - { - "type": "TSIndexSignature", - "start": 18, - "end": 38, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 24 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "parameters": [ - { - "type": "Identifier", - "start": 19, - "end": 20, - "loc": { - "start": { - "line": 2, - "column": 5 - }, - "end": { - "line": 2, - "column": 6 - }, - "identifierName": "s" + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSIndexSignature", + "start": 18, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 4 }, - "name": "s", - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 22, - "end": 28, + "end": { + "line": 2, + "column": 24 + } + }, + "parameters": [ + { + "type": "Identifier", + "start": 19, + "end": 20, "loc": { "start": { "line": 2, - "column": 8 + "column": 5 }, "end": { "line": 2, - "column": 14 - } + "column": 6 + }, + "identifierName": "s" }, + "name": "s", "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 22, "end": 28, "loc": { @@ -118,27 +118,27 @@ "line": 2, "column": 14 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 14 + } + } } } } - } - ], - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 31, - "end": 37, - "loc": { - "start": { - "line": 2, - "column": 17 - }, - "end": { - "line": 2, - "column": 23 - } - }, + ], "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 31, "end": 37, "loc": { @@ -150,11 +150,26 @@ "line": 2, "column": 23 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 31, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 23 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/method-computed/expected.json b/test/fixtures/typescript/interface/method-computed/expected.json index c18324e583..f008e02301 100644 --- a/test/fixtures/typescript/interface/method-computed/expected.json +++ b/test/fixtures/typescript/interface/method-computed/expected.json @@ -59,89 +59,89 @@ }, "name": "I" }, - "members": [ - { - "type": "TSMethodSignature", - "start": 18, - "end": 44, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 30 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 80, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "computed": true, - "name": { - "type": "MemberExpression", - "start": 19, - "end": 34, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSMethodSignature", + "start": 18, + "end": 44, "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, - "column": 20 + "column": 30 } }, - "object": { - "type": "Identifier", + "computed": true, + "name": { + "type": "MemberExpression", "start": 19, - "end": 25, - "loc": { - "start": { - "line": 2, - "column": 5 - }, - "end": { - "line": 2, - "column": 11 - }, - "identifierName": "Symbol" - }, - "name": "Symbol" - }, - "property": { - "type": "Identifier", - "start": 26, "end": 34, "loc": { "start": { "line": 2, - "column": 12 + "column": 5 }, "end": { "line": 2, "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "Symbol" }, - "identifierName": "iterator" + "name": "Symbol" }, - "name": "iterator" - }, - "computed": false - }, - "parameters": [], - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 39, - "end": 43, - "loc": { - "start": { - "line": 2, - "column": 25 + "property": { + "type": "Identifier", + "start": 26, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" }, - "end": { - "line": 2, - "column": 29 - } + "computed": false }, + "parameters": [], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 39, "end": 43, "loc": { @@ -153,93 +153,93 @@ "line": 2, "column": 29 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 39, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 29 + } + } } } - } - }, - { - "type": "TSMethodSignature", - "start": 49, - "end": 78, - "loc": { - "start": { - "line": 3, - "column": 4 - }, - "end": { - "line": 3, - "column": 33 - } }, - "computed": true, - "name": { - "type": "MemberExpression", - "start": 50, - "end": 65, + { + "type": "TSMethodSignature", + "start": 49, + "end": 78, "loc": { "start": { "line": 3, - "column": 5 + "column": 4 }, "end": { "line": 3, - "column": 20 + "column": 33 } }, - "object": { - "type": "Identifier", + "computed": true, + "name": { + "type": "MemberExpression", "start": 50, - "end": 56, - "loc": { - "start": { - "line": 3, - "column": 5 - }, - "end": { - "line": 3, - "column": 11 - }, - "identifierName": "Symbol" - }, - "name": "Symbol" - }, - "property": { - "type": "Identifier", - "start": 57, "end": 65, "loc": { "start": { "line": 3, - "column": 12 + "column": 5 }, "end": { "line": 3, "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 50, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" }, - "identifierName": "iterator" + "name": "Symbol" }, - "name": "iterator" - }, - "computed": false - }, - "optional": true, - "parameters": [], - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 71, - "end": 77, - "loc": { - "start": { - "line": 3, - "column": 26 + "property": { + "type": "Identifier", + "start": 57, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" }, - "end": { - "line": 3, - "column": 32 - } + "computed": false }, + "optional": true, + "parameters": [], "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 71, "end": 77, "loc": { @@ -251,11 +251,26 @@ "line": 3, "column": 32 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 71, + "end": 77, + "loc": { + "start": { + "line": 3, + "column": 26 + }, + "end": { + "line": 3, + "column": 32 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/method-generic/expected.json b/test/fixtures/typescript/interface/method-generic/expected.json index 1c8bccf8fa..5f23b8a00c 100644 --- a/test/fixtures/typescript/interface/method-generic/expected.json +++ b/test/fixtures/typescript/interface/method-generic/expected.json @@ -59,26 +59,25 @@ }, "name": "I" }, - "members": [ - { - "type": "TSMethodSignature", - "start": 18, - "end": 59, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 45 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 61, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSMethodSignature", "start": 18, - "end": 19, + "end": 59, "loc": { "start": { "line": 2, @@ -86,45 +85,46 @@ }, "end": { "line": 2, - "column": 5 - }, - "identifierName": "m" + "column": 45 + } }, - "name": "m" - }, - "typeParameters": { - "type": "TypeParameterDeclaration", - "start": 20, - "end": 53, - "loc": { - "start": { - "line": 2, - "column": 6 + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" }, - "end": { - "line": 2, - "column": 39 - } + "name": "m" }, - "params": [ - { - "type": "TypeParameter", - "start": 20, - "end": 52, - "loc": { - "start": { - "line": 2, - "column": 6 - }, - "end": { - "line": 2, - "column": 38 - } + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 20, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 6 }, - "name": { - "type": "Identifier", + "end": { + "line": 2, + "column": 39 + } + }, + "params": [ + { + "type": "TypeParameter", "start": 20, - "end": 21, + "end": 52, "loc": { "start": { "line": 2, @@ -132,90 +132,90 @@ }, "end": { "line": 2, - "column": 7 - }, - "identifierName": "T" - }, - "name": "T" - }, - "constraint": { - "type": "TSObjectKeyword", - "start": 30, - "end": 36, - "loc": { - "start": { - "line": 2, - "column": 16 - }, - "end": { - "line": 2, - "column": 22 + "column": 38 } - } - }, - "default": { - "type": "TSTypeLiteral", - "start": 39, - "end": 52, - "loc": { - "start": { - "line": 2, - "column": 25 + }, + "name": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" }, - "end": { - "line": 2, - "column": 38 + "name": "T" + }, + "constraint": { + "type": "TSObjectKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 22 + } } }, - "members": [ - { - "type": "TSPropertySignature", - "start": 41, - "end": 50, - "loc": { - "start": { - "line": 2, - "column": 27 - }, - "end": { - "line": 2, - "column": 36 - } + "default": { + "type": "TSTypeLiteral", + "start": 39, + "end": 52, + "loc": { + "start": { + "line": 2, + "column": 25 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 2, + "column": 38 + } + }, + "members": [ + { + "type": "TSPropertySignature", "start": 41, - "end": 42, - "loc": { - "start": { - "line": 2, - "column": 27 - }, - "end": { - "line": 2, - "column": 28 - }, - "identifierName": "x" - }, - "name": "x" - }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 44, "end": 50, "loc": { "start": { "line": 2, - "column": 30 + "column": 27 }, "end": { "line": 2, "column": 36 } }, + "computed": false, + "name": { + "type": "Identifier", + "start": 41, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 28 + }, + "identifierName": "x" + }, + "name": "x" + }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 44, "end": 50, "loc": { @@ -227,32 +227,32 @@ "line": 2, "column": 36 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 44, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 36 + } + } } } } - } - ] + ] + } } - } - ] - }, - "parameters": [], - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 57, - "end": 58, - "loc": { - "start": { - "line": 2, - "column": 43 - }, - "end": { - "line": 2, - "column": 44 - } + ] }, + "parameters": [], "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 57, "end": 58, "loc": { @@ -265,8 +265,8 @@ "column": 44 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 57, "end": 58, "loc": { @@ -277,15 +277,30 @@ "end": { "line": 2, "column": 44 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 57, + "end": 58, + "loc": { + "start": { + "line": 2, + "column": 43 + }, + "end": { + "line": 2, + "column": 44 + }, + "identifierName": "T" + }, + "name": "T" + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/method-optional/expected.json b/test/fixtures/typescript/interface/method-optional/expected.json index 261ca03825..9c4a3deed7 100644 --- a/test/fixtures/typescript/interface/method-optional/expected.json +++ b/test/fixtures/typescript/interface/method-optional/expected.json @@ -59,26 +59,25 @@ }, "name": "I" }, - "members": [ - { - "type": "TSMethodSignature", - "start": 18, - "end": 29, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 15 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSMethodSignature", "start": 18, - "end": 19, + "end": 29, "loc": { "start": { "line": 2, @@ -86,30 +85,31 @@ }, "end": { "line": 2, - "column": 5 - }, - "identifierName": "m" + "column": 15 + } }, - "name": "m" - }, - "optional": true, - "parameters": [], - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 24, - "end": 28, - "loc": { - "start": { - "line": 2, - "column": 10 + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" }, - "end": { - "line": 2, - "column": 14 - } + "name": "m" }, + "optional": true, + "parameters": [], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 24, "end": 28, "loc": { @@ -121,11 +121,26 @@ "line": 2, "column": 14 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 24, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 14 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/method-plain/expected.json b/test/fixtures/typescript/interface/method-plain/expected.json index f83a5234ab..61e4d5d5a1 100644 --- a/test/fixtures/typescript/interface/method-plain/expected.json +++ b/test/fixtures/typescript/interface/method-plain/expected.json @@ -59,26 +59,25 @@ }, "name": "I" }, - "members": [ - { - "type": "TSMethodSignature", - "start": 18, - "end": 22, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 8 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 65, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSMethodSignature", "start": 18, - "end": 19, + "end": 22, "loc": { "start": { "line": 2, @@ -86,33 +85,33 @@ }, "end": { "line": 2, - "column": 5 - }, - "identifierName": "m" + "column": 8 + } }, - "name": "m" - }, - "parameters": [] - }, - { - "type": "TSMethodSignature", - "start": 27, - "end": 63, - "loc": { - "start": { - "line": 3, - "column": 4 + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "m" + }, + "name": "m" }, - "end": { - "line": 3, - "column": 40 - } + "parameters": [] }, - "computed": false, - "name": { - "type": "Identifier", + { + "type": "TSMethodSignature", "start": 27, - "end": 28, + "end": 63, "loc": { "start": { "line": 3, @@ -120,46 +119,47 @@ }, "end": { "line": 3, - "column": 5 - }, - "identifierName": "m" + "column": 40 + } }, - "name": "m" - }, - "parameters": [ - { + "computed": false, + "name": { "type": "Identifier", - "start": 29, - "end": 39, + "start": 27, + "end": 28, "loc": { "start": { "line": 3, - "column": 6 + "column": 4 }, "end": { "line": 3, - "column": 16 + "column": 5 }, - "identifierName": "x" + "identifierName": "m" }, - "name": "x", - "optional": true, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 33, + "name": "m" + }, + "parameters": [ + { + "type": "Identifier", + "start": 29, "end": 39, "loc": { "start": { "line": 3, - "column": 10 + "column": 6 }, "end": { "line": 3, "column": 16 - } + }, + "identifierName": "x" }, + "name": "x", + "optional": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 33, "end": 39, "loc": { @@ -171,57 +171,57 @@ "line": 3, "column": 16 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 33, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 10 + }, + "end": { + "line": 3, + "column": 16 + } + } } } - } - }, - { - "type": "RestElement", - "start": 41, - "end": 55, - "loc": { - "start": { - "line": 3, - "column": 18 - }, - "end": { - "line": 3, - "column": 32 - } - }, - "argument": { - "type": "Identifier", - "start": 44, - "end": 45, - "loc": { - "start": { - "line": 3, - "column": 21 - }, - "end": { - "line": 3, - "column": 22 - }, - "identifierName": "y" - }, - "name": "y" }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 47, + { + "type": "RestElement", + "start": 41, "end": 55, "loc": { "start": { "line": 3, - "column": 24 + "column": 18 }, "end": { "line": 3, "column": 32 } }, + "argument": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 22 + }, + "identifierName": "y" + }, + "name": "y" + }, "typeAnnotation": { - "type": "TSArrayType", + "type": "TypeAnnotation", "start": 47, "end": 55, "loc": { @@ -234,10 +234,10 @@ "column": 32 } }, - "elementType": { - "type": "TSNumberKeyword", + "typeAnnotation": { + "type": "TSArrayType", "start": 47, - "end": 53, + "end": 55, "loc": { "start": { "line": 3, @@ -245,30 +245,30 @@ }, "end": { "line": 3, - "column": 30 + "column": 32 + } + }, + "elementType": { + "type": "TSNumberKeyword", + "start": 47, + "end": 53, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } } } } } } - } - ], - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 58, - "end": 62, - "loc": { - "start": { - "line": 3, - "column": 35 - }, - "end": { - "line": 3, - "column": 39 - } - }, + ], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 58, "end": 62, "loc": { @@ -280,11 +280,26 @@ "line": 3, "column": 39 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 58, + "end": 62, + "loc": { + "start": { + "line": 3, + "column": 35 + }, + "end": { + "line": 3, + "column": 39 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/modifiers/expected.json b/test/fixtures/typescript/interface/modifiers/expected.json index 9751090aa5..86c51ed8fa 100644 --- a/test/fixtures/typescript/interface/modifiers/expected.json +++ b/test/fixtures/typescript/interface/modifiers/expected.json @@ -59,56 +59,56 @@ }, "name": "I" }, - "members": [ - { - "type": "TSPropertySignature", - "start": 18, - "end": 37, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 23 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "computed": false, - "name": { - "type": "Identifier", - "start": 27, - "end": 28, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 18, + "end": 37, "loc": { "start": { "line": 2, - "column": 13 + "column": 4 }, "end": { "line": 2, - "column": 14 - }, - "identifierName": "x" + "column": 23 + } }, - "name": "x" - }, - "readonly": true, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 30, - "end": 36, - "loc": { - "start": { - "line": 2, - "column": 16 + "computed": false, + "name": { + "type": "Identifier", + "start": 27, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "x" }, - "end": { - "line": 2, - "column": 22 - } + "name": "x" }, + "readonly": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 30, "end": 36, "loc": { @@ -120,11 +120,26 @@ "line": 2, "column": 22 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 22 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/properties/expected.json b/test/fixtures/typescript/interface/properties/expected.json index fe6582d8d3..019bc00090 100644 --- a/test/fixtures/typescript/interface/properties/expected.json +++ b/test/fixtures/typescript/interface/properties/expected.json @@ -59,26 +59,25 @@ }, "name": "I" }, - "members": [ - { - "type": "TSPropertySignature", - "start": 18, - "end": 20, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 6 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", "start": 18, - "end": 19, + "end": 20, "loc": { "start": { "line": 2, @@ -86,32 +85,32 @@ }, "end": { "line": 2, - "column": 5 - }, - "identifierName": "x" - }, - "name": "x" - } - }, - { - "type": "TSPropertySignature", - "start": 25, - "end": 35, - "loc": { - "start": { - "line": 3, - "column": 4 + "column": 6 + } }, - "end": { - "line": 3, - "column": 14 + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" } }, - "computed": false, - "name": { - "type": "Identifier", + { + "type": "TSPropertySignature", "start": 25, - "end": 26, + "end": 35, "loc": { "start": { "line": 3, @@ -119,28 +118,29 @@ }, "end": { "line": 3, - "column": 5 - }, - "identifierName": "y" + "column": 14 + } }, - "name": "y" - }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 28, - "end": 34, - "loc": { - "start": { - "line": 3, - "column": 7 + "computed": false, + "name": { + "type": "Identifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 5 + }, + "identifierName": "y" }, - "end": { - "line": 3, - "column": 13 - } + "name": "y" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 28, "end": 34, "loc": { @@ -152,29 +152,28 @@ "line": 3, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 3, + "column": 7 + }, + "end": { + "line": 3, + "column": 13 + } + } } } - } - }, - { - "type": "TSPropertySignature", - "start": 40, - "end": 51, - "loc": { - "start": { - "line": 4, - "column": 4 - }, - "end": { - "line": 4, - "column": 15 - } }, - "computed": false, - "name": { - "type": "Identifier", + { + "type": "TSPropertySignature", "start": 40, - "end": 41, + "end": 51, "loc": { "start": { "line": 4, @@ -182,29 +181,30 @@ }, "end": { "line": 4, - "column": 5 - }, - "identifierName": "z" + "column": 15 + } }, - "name": "z" - }, - "optional": true, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 44, - "end": 50, - "loc": { - "start": { - "line": 4, - "column": 8 + "computed": false, + "name": { + "type": "Identifier", + "start": 40, + "end": 41, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "z" }, - "end": { - "line": 4, - "column": 14 - } + "name": "z" }, + "optional": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 44, "end": 50, "loc": { @@ -216,11 +216,26 @@ "line": 4, "column": 14 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 44, + "end": 50, + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 14 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/property-computed/expected.json b/test/fixtures/typescript/interface/property-computed/expected.json index b0b9cb569c..cb41c591f3 100644 --- a/test/fixtures/typescript/interface/property-computed/expected.json +++ b/test/fixtures/typescript/interface/property-computed/expected.json @@ -59,88 +59,88 @@ }, "name": "I" }, - "members": [ - { - "type": "TSPropertySignature", - "start": 18, - "end": 44, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 30 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "computed": true, - "name": { - "type": "MemberExpression", - "start": 19, - "end": 34, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", + "start": 18, + "end": 44, "loc": { "start": { "line": 2, - "column": 5 + "column": 4 }, "end": { "line": 2, - "column": 20 + "column": 30 } }, - "object": { - "type": "Identifier", + "computed": true, + "name": { + "type": "MemberExpression", "start": 19, - "end": 25, - "loc": { - "start": { - "line": 2, - "column": 5 - }, - "end": { - "line": 2, - "column": 11 - }, - "identifierName": "Symbol" - }, - "name": "Symbol" - }, - "property": { - "type": "Identifier", - "start": 26, "end": 34, "loc": { "start": { "line": 2, - "column": 12 + "column": 5 }, "end": { "line": 2, "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "Symbol" }, - "identifierName": "iterator" + "name": "Symbol" }, - "name": "iterator" - }, - "computed": false - }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 37, - "end": 43, - "loc": { - "start": { - "line": 2, - "column": 23 + "property": { + "type": "Identifier", + "start": 26, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" }, - "end": { - "line": 2, - "column": 29 - } + "computed": false }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 37, "end": 43, "loc": { @@ -152,92 +152,92 @@ "line": 2, "column": 29 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 37, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 29 + } + } } } - } - }, - { - "type": "TSPropertySignature", - "start": 49, - "end": 76, - "loc": { - "start": { - "line": 3, - "column": 4 - }, - "end": { - "line": 3, - "column": 31 - } }, - "computed": true, - "name": { - "type": "MemberExpression", - "start": 50, - "end": 65, + { + "type": "TSPropertySignature", + "start": 49, + "end": 76, "loc": { "start": { "line": 3, - "column": 5 + "column": 4 }, "end": { "line": 3, - "column": 20 + "column": 31 } }, - "object": { - "type": "Identifier", + "computed": true, + "name": { + "type": "MemberExpression", "start": 50, - "end": 56, - "loc": { - "start": { - "line": 3, - "column": 5 - }, - "end": { - "line": 3, - "column": 11 - }, - "identifierName": "Symbol" - }, - "name": "Symbol" - }, - "property": { - "type": "Identifier", - "start": 57, "end": 65, "loc": { "start": { "line": 3, - "column": 12 + "column": 5 }, "end": { "line": 3, "column": 20 + } + }, + "object": { + "type": "Identifier", + "start": 50, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "Symbol" }, - "identifierName": "iterator" + "name": "Symbol" }, - "name": "iterator" - }, - "computed": false - }, - "optional": true, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 69, - "end": 75, - "loc": { - "start": { - "line": 3, - "column": 24 + "property": { + "type": "Identifier", + "start": 57, + "end": 65, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "iterator" + }, + "name": "iterator" }, - "end": { - "line": 3, - "column": 30 - } + "computed": false }, + "optional": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 69, "end": 75, "loc": { @@ -249,11 +249,26 @@ "line": 3, "column": 30 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 69, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 30 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/property-named-public/expected.json b/test/fixtures/typescript/interface/property-named-public/expected.json index a808c420bb..b6702e3f95 100644 --- a/test/fixtures/typescript/interface/property-named-public/expected.json +++ b/test/fixtures/typescript/interface/property-named-public/expected.json @@ -59,26 +59,25 @@ }, "name": "I" }, - "members": [ - { - "type": "TSPropertySignature", - "start": 15, - "end": 30, - "loc": { - "start": { - "line": 2, - "column": 1 - }, - "end": { - "line": 2, - "column": 16 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", "start": 15, - "end": 21, + "end": 30, "loc": { "start": { "line": 2, @@ -86,28 +85,29 @@ }, "end": { "line": 2, - "column": 7 - }, - "identifierName": "public" + "column": 16 + } }, - "name": "public" - }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 23, - "end": 29, - "loc": { - "start": { - "line": 2, - "column": 9 + "computed": false, + "name": { + "type": "Identifier", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "public" }, - "end": { - "line": 2, - "column": 15 - } + "name": "public" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 23, "end": 29, "loc": { @@ -119,11 +119,26 @@ "line": 2, "column": 15 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 23, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 15 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/reserved-method-name/expected.json b/test/fixtures/typescript/interface/reserved-method-name/expected.json index e986db879c..39d0408e8b 100644 --- a/test/fixtures/typescript/interface/reserved-method-name/expected.json +++ b/test/fixtures/typescript/interface/reserved-method-name/expected.json @@ -59,26 +59,25 @@ }, "name": "I" }, - "members": [ - { - "type": "TSMethodSignature", - "start": 18, - "end": 32, - "loc": { - "start": { - "line": 2, - "column": 4 - }, - "end": { - "line": 2, - "column": 18 - } + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "TSMethodSignature", "start": 18, - "end": 23, + "end": 32, "loc": { "start": { "line": 2, @@ -86,29 +85,30 @@ }, "end": { "line": 2, - "column": 9 - }, - "identifierName": "catch" + "column": 18 + } }, - "name": "catch" - }, - "parameters": [], - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 27, - "end": 31, - "loc": { - "start": { - "line": 2, - "column": 13 + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "catch" }, - "end": { - "line": 2, - "column": 17 - } + "name": "catch" }, + "parameters": [], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 27, "end": 31, "loc": { @@ -120,11 +120,26 @@ "line": 2, "column": 17 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 27, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 17 + } + } } } } - } - ] + ] + } } ], "directives": [] diff --git a/test/fixtures/typescript/interface/separators/expected.json b/test/fixtures/typescript/interface/separators/expected.json index 48aeb1aba5..fb48eb8fc9 100644 --- a/test/fixtures/typescript/interface/separators/expected.json +++ b/test/fixtures/typescript/interface/separators/expected.json @@ -59,26 +59,25 @@ }, "name": "Comma" }, - "members": [ - { - "type": "TSPropertySignature", - "start": 18, - "end": 28, - "loc": { - "start": { - "line": 1, - "column": 18 - }, - "end": { - "line": 1, - "column": 28 - } + "body": { + "type": "TSInterfaceBody", + "start": 16, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 16 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 1, + "column": 40 + } + }, + "body": [ + { + "type": "TSPropertySignature", "start": 18, - "end": 19, + "end": 28, "loc": { "start": { "line": 1, @@ -86,28 +85,29 @@ }, "end": { "line": 1, - "column": 19 - }, - "identifierName": "x" + "column": 28 + } }, - "name": "x" - }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 21, - "end": 27, - "loc": { - "start": { - "line": 1, - "column": 21 + "computed": false, + "name": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 19 + }, + "identifierName": "x" }, - "end": { - "line": 1, - "column": 27 - } + "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 21, "end": 27, "loc": { @@ -119,58 +119,58 @@ "line": 1, "column": 27 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 21, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 27 + } + } } } - } - }, - { - "type": "TSPropertySignature", - "start": 29, - "end": 38, - "loc": { - "start": { - "line": 1, - "column": 29 - }, - "end": { - "line": 1, - "column": 38 - } }, - "computed": false, - "name": { - "type": "Identifier", + { + "type": "TSPropertySignature", "start": 29, - "end": 30, - "loc": { - "start": { - "line": 1, - "column": 29 - }, - "end": { - "line": 1, - "column": 30 - }, - "identifierName": "y" - }, - "name": "y" - }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 32, "end": 38, "loc": { "start": { "line": 1, - "column": 32 + "column": 29 }, "end": { "line": 1, "column": 38 } }, + "computed": false, + "name": { + "type": "Identifier", + "start": 29, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 30 + }, + "identifierName": "y" + }, + "name": "y" + }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 32, "end": 38, "loc": { @@ -182,11 +182,26 @@ "line": 1, "column": 38 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 32, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 38 + } + } } } } - } - ] + ] + } }, { "type": "TSInterfaceDeclaration", @@ -219,26 +234,25 @@ }, "name": "Semi" }, - "members": [ - { - "type": "TSPropertySignature", - "start": 58, - "end": 68, - "loc": { - "start": { - "line": 2, - "column": 17 - }, - "end": { - "line": 2, - "column": 27 - } + "body": { + "type": "TSInterfaceBody", + "start": 56, + "end": 80, + "loc": { + "start": { + "line": 2, + "column": 15 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 2, + "column": 39 + } + }, + "body": [ + { + "type": "TSPropertySignature", "start": 58, - "end": 59, + "end": 68, "loc": { "start": { "line": 2, @@ -246,28 +260,29 @@ }, "end": { "line": 2, - "column": 18 - }, - "identifierName": "x" + "column": 27 + } }, - "name": "x" - }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 61, - "end": 67, - "loc": { - "start": { - "line": 2, - "column": 20 + "computed": false, + "name": { + "type": "Identifier", + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "x" }, - "end": { - "line": 2, - "column": 26 - } + "name": "x" }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 61, "end": 67, "loc": { @@ -279,58 +294,58 @@ "line": 2, "column": 26 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 61, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 26 + } + } } } - } - }, - { - "type": "TSPropertySignature", - "start": 69, - "end": 78, - "loc": { - "start": { - "line": 2, - "column": 28 - }, - "end": { - "line": 2, - "column": 37 - } }, - "computed": false, - "name": { - "type": "Identifier", + { + "type": "TSPropertySignature", "start": 69, - "end": 70, - "loc": { - "start": { - "line": 2, - "column": 28 - }, - "end": { - "line": 2, - "column": 29 - }, - "identifierName": "y" - }, - "name": "y" - }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 72, "end": 78, "loc": { "start": { "line": 2, - "column": 31 + "column": 28 }, "end": { "line": 2, "column": 37 } }, + "computed": false, + "name": { + "type": "Identifier", + "start": 69, + "end": 70, + "loc": { + "start": { + "line": 2, + "column": 28 + }, + "end": { + "line": 2, + "column": 29 + }, + "identifierName": "y" + }, + "name": "y" + }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 72, "end": 78, "loc": { @@ -342,11 +357,26 @@ "line": 2, "column": 37 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 72, + "end": 78, + "loc": { + "start": { + "line": 2, + "column": 31 + }, + "end": { + "line": 2, + "column": 37 + } + } } } } - } - ] + ] + } }, { "type": "TSInterfaceDeclaration", @@ -379,55 +409,55 @@ }, "name": "Newline" }, - "members": [ - { - "type": "TSPropertySignature", - "start": 105, - "end": 114, - "loc": { - "start": { - "line": 4, - "column": 4 - }, - "end": { - "line": 4, - "column": 13 - } + "body": { + "type": "TSInterfaceBody", + "start": 99, + "end": 130, + "loc": { + "start": { + "line": 3, + "column": 18 }, - "computed": false, - "name": { - "type": "Identifier", + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "TSPropertySignature", "start": 105, - "end": 106, - "loc": { - "start": { - "line": 4, - "column": 4 - }, - "end": { - "line": 4, - "column": 5 - }, - "identifierName": "x" - }, - "name": "x" - }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 108, "end": 114, "loc": { "start": { "line": 4, - "column": 7 + "column": 4 }, "end": { "line": 4, "column": 13 } }, + "computed": false, + "name": { + "type": "Identifier", + "start": 105, + "end": 106, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "x" + }, + "name": "x" + }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 108, "end": 114, "loc": { @@ -439,58 +469,58 @@ "line": 4, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 108, + "end": 114, + "loc": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 13 + } + } } } - } - }, - { - "type": "TSPropertySignature", - "start": 119, - "end": 128, - "loc": { - "start": { - "line": 5, - "column": 4 - }, - "end": { - "line": 5, - "column": 13 - } }, - "computed": false, - "name": { - "type": "Identifier", + { + "type": "TSPropertySignature", "start": 119, - "end": 120, - "loc": { - "start": { - "line": 5, - "column": 4 - }, - "end": { - "line": 5, - "column": 5 - }, - "identifierName": "y" - }, - "name": "y" - }, - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 122, "end": 128, "loc": { "start": { "line": 5, - "column": 7 + "column": 4 }, "end": { "line": 5, "column": 13 } }, + "computed": false, + "name": { + "type": "Identifier", + "start": 119, + "end": 120, + "loc": { + "start": { + "line": 5, + "column": 4 + }, + "end": { + "line": 5, + "column": 5 + }, + "identifierName": "y" + }, + "name": "y" + }, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 122, "end": 128, "loc": { @@ -502,11 +532,26 @@ "line": 5, "column": 13 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 122, + "end": 128, + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 13 + } + } } } } - } - ] + ] + } } ], "directives": [] From 65d90435ff782239bcd643dc9189aca41d73bbbf Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 17 May 2017 14:47:28 -0700 Subject: [PATCH 10/55] Change "name" fields to "id" or "key" --- src/parser/statement.js | 2 +- src/plugins/typescript.js | 20 +++++++++---------- src/types.js | 18 ++++++++--------- .../typescript/class/generic/expected.json | 2 +- .../declare/destructure/expected.json | 4 ++-- .../declare/interface/expected.json | 2 +- .../typescript/enum/const/expected.json | 2 +- .../enum/declare-const/expected.json | 2 +- .../typescript/enum/declare/expected.json | 2 +- .../enum/export-const/expected.json | 2 +- .../enum/export-declare-const/expected.json | 2 +- .../typescript/enum/export/expected.json | 2 +- .../enum/members-reserved-words/expected.json | 6 +++--- .../enum/members-strings/expected.json | 6 +++--- .../expected.json | 4 ++-- .../enum/members-trailing-comma/expected.json | 4 ++-- .../typescript/enum/members/expected.json | 6 +++--- .../export/as-namespace/expected.json | 2 +- .../typescript/export/declare/expected.json | 8 ++++---- .../import/equals-require/expected.json | 2 +- .../typescript/import/equals/expected.json | 2 +- .../export-import-require/expected.json | 2 +- .../import/export-import/expected.json | 2 +- .../import/not-top-level/expected.json | 2 +- .../interface/call-signature/expected.json | 2 +- .../construct-signature/expected.json | 2 +- .../typescript/interface/export/expected.json | 2 +- .../interface/extends/expected.json | 2 +- .../interface/generic/expected.json | 4 ++-- .../interface/index-signature/expected.json | 2 +- .../interface/method-computed/expected.json | 6 +++--- .../interface/method-generic/expected.json | 6 +++--- .../interface/method-optional/expected.json | 4 ++-- .../interface/method-plain/expected.json | 6 +++--- .../interface/modifiers/expected.json | 4 ++-- .../interface/properties/expected.json | 8 ++++---- .../interface/property-computed/expected.json | 6 +++--- .../property-named-public/expected.json | 4 ++-- .../reserved-method-name/expected.json | 4 ++-- .../interface/separators/expected.json | 18 ++++++++--------- .../body-declare/expected.json | 2 +- .../body-nested-declare/expected.json | 4 ++-- .../body-nested/expected.json | 4 ++-- .../module-namespace/body/expected.json | 2 +- .../declare-shorthand/expected.json | 2 +- .../global-in-module/expected.json | 4 ++-- .../head-declare/expected.json | 10 +++++----- .../head-export/expected.json | 6 +++--- .../module-namespace/head/expected.json | 12 +++++------ .../type-alias/declare/expected.json | 2 +- .../type-alias/export/expected.json | 2 +- .../type-alias/generic-complex/expected.json | 4 ++-- .../type-alias/generic/expected.json | 2 +- .../typescript/type-alias/plain/expected.json | 2 +- .../types/parenthesized/expected.json | 2 +- .../types/type-literal/expected.json | 2 +- 56 files changed, 124 insertions(+), 124 deletions(-) diff --git a/src/parser/statement.js b/src/parser/statement.js index 1a783470fb..79f1da4caf 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -968,7 +968,7 @@ export default class StatementParser extends ExpressionParser { const decl: N.TsNamespaceExportDeclaration = node; // See `parseNamespaceExportDeclaration` in TypeScript's own parser this.expectContextual("namespace"); - decl.name = this.parseIdentifier(); + decl.id = this.parseIdentifier(); this.semicolon(); return this.finishNode(decl, "TSNamespaceExportDeclaration"); } else if (this.shouldParseExportDeclaration()) { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 6dbc026a29..96929e66b0 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -354,7 +354,7 @@ export default (superClass: Class): Class => class extends super const { key, computed } = this.parsePropertyNameInfo(); node.computed = computed; - node.name = key; + node.key = key; if (this.eat(tt.question)) node.optional = true; const nodeAny: any = node; @@ -749,7 +749,7 @@ export default (superClass: Class): Class => class extends super } tsParseInterfaceDeclaration(node: N.TsInterfaceDeclaration): N.TsInterfaceDeclaration { - node.name = this.parseIdentifier(); + node.id = this.parseIdentifier(); node.typeParameters = this.tsTryParseTypeParameters(); if (this.eat(tt._extends)) { node.extends = this.tsParseHeritageClause(); @@ -761,7 +761,7 @@ export default (superClass: Class): Class => class extends super } tsParseTypeAliasDeclaration(node: N.TsTypeAliasDeclaration): N.TsTypeAliasDeclaration { - node.name = this.parseIdentifier(); + node.id = this.parseIdentifier(); node.typeParameters = this.tsTryParseTypeParameters(); this.expect(tt.eq); node.typeAnnotation = this.tsParseType(); @@ -772,7 +772,7 @@ export default (superClass: Class): Class => class extends super tsParseEnumMember(): N.TsEnumMember { const node: N.TsEnumMember = this.startNode(); // Computed property names are grammar errors in an enum, so accept just string literal or identifier. - node.name = this.match(tt.string) + node.id = this.match(tt.string) ? this.parseLiteral(this.state.value, "StringLiteral") : this.parseIdentifier(/* liberal */ true); if (this.eat(tt.eq)) { @@ -783,7 +783,7 @@ export default (superClass: Class): Class => class extends super tsParseEnumDeclaration(node: N.TsEnumDeclaration, isConst: boolean): N.TsEnumDeclaration { if (isConst) node.const = true; - node.name = this.parseIdentifier(); + node.id = this.parseIdentifier(); this.expect(tt.braceL); node.members = this.tsParseDelimitedList("EnumMembers", this.tsParseEnumMember.bind(this)); this.expect(tt.braceR); @@ -800,7 +800,7 @@ export default (superClass: Class): Class => class extends super } tsParseModuleOrNamespaceDeclaration(node: N.TsModuleDeclaration): N.TsModuleDeclaration { - node.name = this.parseIdentifier(); + node.id = this.parseIdentifier(); if (this.eat(tt.dot)) { const inner = this.startNode(); this.tsParseModuleOrNamespaceDeclaration(inner); @@ -814,9 +814,9 @@ export default (superClass: Class): Class => class extends super tsParseAmbientExternalModuleDeclaration(node: N.TsModuleDeclaration): N.TsModuleDeclaration { if (this.isContextual("global")) { node.global = true; - node.name = this.parseIdentifier(); + node.id = this.parseIdentifier(); } else if (this.match(tt.string)) { - node.name = this.parseLiteral(this.state.value, "StringLiteral"); + node.id = this.parseLiteral(this.state.value, "StringLiteral"); } else { this.unexpected(); } @@ -835,7 +835,7 @@ export default (superClass: Class): Class => class extends super isExport?: boolean): N.TsImportEqualsDeclaration { node.isExport = isExport || false; - node.name = this.parseIdentifier(); + node.id = this.parseIdentifier(); this.expect(tt.eq); node.moduleReference = this.tsParseModuleReference(); this.semicolon(); @@ -1008,7 +1008,7 @@ export default (superClass: Class): Class => class extends super if (this.match(tt.braceL)) { const mod: N.TsModuleDeclaration = node; mod.global = true; - mod.name = expr; + mod.id = expr; mod.body = this.tsParseModuleBlock(); return this.finishNode(mod, "TSModuleDeclaration"); } diff --git a/src/types.js b/src/types.js index 5a9ef78a1e..0d6f016f6e 100644 --- a/src/types.js +++ b/src/types.js @@ -831,7 +831,7 @@ export type TsConstructSignatureDeclaration = TsSignatureDeclarationBase & TsTyp export type TsNamedTypeElementBase = TsTypeElementBase & { // Not using TypeScript's `PropertyName` here since we don't have a `ComputedPropertyName` node type. // This is usually an Identifier but may be e.g. `Symbol.iterator` if `computed` is true. - name: Expression; + key: Expression; computed: boolean; optional?: true; } @@ -992,7 +992,7 @@ export type TsLiteralType = TsTypeBase & { export type TsInterfaceDeclaration = DeclarationBase & { type: "TSInterfaceDeclaration"; - name: Identifier; + id: Identifier; typeParameters: ?TypeParameterDeclaration; // TS uses "heritageClauses", but want this to resemble ClassBase. extends?: $ReadOnlyArray; @@ -1012,7 +1012,7 @@ export type TsExpressionWithTypeArguments = TsTypeBase & { export type TsTypeAliasDeclaration = DeclarationBase & { type: "TSTypeAliasDeclaration"; - name: Identifier; + id: Identifier; typeParameters: ?TypeParameterDeclaration; typeAnnotation: TsType; }; @@ -1020,20 +1020,20 @@ export type TsTypeAliasDeclaration = DeclarationBase & { export type TsEnumDeclaration = DeclarationBase & { type: "TSEnumDeclaration"; const?: true; - name: Identifier; + id: Identifier; members: $ReadOnlyArray; }; export type TsEnumMember = NodeBase & { type: "TSEnumMemodulmber"; - name: Identifier | StringLiteral; + id: Identifier | StringLiteral; initializer?: Expression; }; export type TsModuleDeclaration = DeclarationBase & { type: "TSModuleDeclaration"; global?: true; // In TypeScript, this is only available through `node.flags`. - name: TsModuleName; + id: TsModuleName; body: TsNamespaceBody; }; @@ -1046,7 +1046,7 @@ export type TsModuleBlock = NodeBase & { }; export type TsNamespaceDeclaration = TsModuleDeclaration & { - name: Identifier; + id: Identifier; body: TsNamespaceBody; }; @@ -1055,7 +1055,7 @@ export type TsModuleName = Identifier | StringLiteral; export type TsImportEqualsDeclaration = NodeBase & { type: "TSImportEqualsDeclaration"; isExport: boolean; - name: Identifier; + id: Identifier; moduleReference: TsModuleReference; }; @@ -1076,7 +1076,7 @@ export type TsExportAssignment = NodeBase & { export type TsNamespaceExportDeclaration = NodeBase & { type: "TSNamespaceExportDeclaration"; - name: Identifier; + id: Identifier; }; // ================ diff --git a/test/fixtures/typescript/class/generic/expected.json b/test/fixtures/typescript/class/generic/expected.json index 0189aa7881..ae152d0dca 100644 --- a/test/fixtures/typescript/class/generic/expected.json +++ b/test/fixtures/typescript/class/generic/expected.json @@ -150,7 +150,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 29, "end": 30, diff --git a/test/fixtures/typescript/declare/destructure/expected.json b/test/fixtures/typescript/declare/destructure/expected.json index 383a66aa46..54fce6821b 100644 --- a/test/fixtures/typescript/declare/destructure/expected.json +++ b/test/fixtures/typescript/declare/destructure/expected.json @@ -227,7 +227,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 26, "end": 27, @@ -290,7 +290,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 37, "end": 38, diff --git a/test/fixtures/typescript/declare/interface/expected.json b/test/fixtures/typescript/declare/interface/expected.json index bd786411c5..2ddce3fec9 100644 --- a/test/fixtures/typescript/declare/interface/expected.json +++ b/test/fixtures/typescript/declare/interface/expected.json @@ -42,7 +42,7 @@ "column": 22 } }, - "name": { + "id": { "type": "Identifier", "start": 18, "end": 19, diff --git a/test/fixtures/typescript/enum/const/expected.json b/test/fixtures/typescript/enum/const/expected.json index f387d86cc5..4e0e48a25f 100644 --- a/test/fixtures/typescript/enum/const/expected.json +++ b/test/fixtures/typescript/enum/const/expected.json @@ -43,7 +43,7 @@ } }, "const": true, - "name": { + "id": { "type": "Identifier", "start": 11, "end": 12, diff --git a/test/fixtures/typescript/enum/declare-const/expected.json b/test/fixtures/typescript/enum/declare-const/expected.json index 92ea1e4a07..cbb6b73653 100644 --- a/test/fixtures/typescript/enum/declare-const/expected.json +++ b/test/fixtures/typescript/enum/declare-const/expected.json @@ -43,7 +43,7 @@ } }, "const": true, - "name": { + "id": { "type": "Identifier", "start": 19, "end": 20, diff --git a/test/fixtures/typescript/enum/declare/expected.json b/test/fixtures/typescript/enum/declare/expected.json index 507a2f8f94..9ca983fb14 100644 --- a/test/fixtures/typescript/enum/declare/expected.json +++ b/test/fixtures/typescript/enum/declare/expected.json @@ -42,7 +42,7 @@ "column": 17 } }, - "name": { + "id": { "type": "Identifier", "start": 13, "end": 14, diff --git a/test/fixtures/typescript/enum/export-const/expected.json b/test/fixtures/typescript/enum/export-const/expected.json index 1866fda344..8d8043488c 100644 --- a/test/fixtures/typescript/enum/export-const/expected.json +++ b/test/fixtures/typescript/enum/export-const/expected.json @@ -59,7 +59,7 @@ } }, "const": true, - "name": { + "id": { "type": "Identifier", "start": 18, "end": 19, diff --git a/test/fixtures/typescript/enum/export-declare-const/expected.json b/test/fixtures/typescript/enum/export-declare-const/expected.json index 25339bc233..f83c34d4ac 100644 --- a/test/fixtures/typescript/enum/export-declare-const/expected.json +++ b/test/fixtures/typescript/enum/export-declare-const/expected.json @@ -59,7 +59,7 @@ } }, "const": true, - "name": { + "id": { "type": "Identifier", "start": 26, "end": 27, diff --git a/test/fixtures/typescript/enum/export/expected.json b/test/fixtures/typescript/enum/export/expected.json index d9527678fe..a06a074797 100644 --- a/test/fixtures/typescript/enum/export/expected.json +++ b/test/fixtures/typescript/enum/export/expected.json @@ -58,7 +58,7 @@ "column": 16 } }, - "name": { + "id": { "type": "Identifier", "start": 12, "end": 13, diff --git a/test/fixtures/typescript/enum/members-reserved-words/expected.json b/test/fixtures/typescript/enum/members-reserved-words/expected.json index 58f79e6d16..110b72a0ac 100644 --- a/test/fixtures/typescript/enum/members-reserved-words/expected.json +++ b/test/fixtures/typescript/enum/members-reserved-words/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 5, "end": 6, @@ -74,7 +74,7 @@ "column": 9 } }, - "name": { + "id": { "type": "Identifier", "start": 13, "end": 18, @@ -106,7 +106,7 @@ "column": 11 } }, - "name": { + "id": { "type": "Identifier", "start": 24, "end": 31, diff --git a/test/fixtures/typescript/enum/members-strings/expected.json b/test/fixtures/typescript/enum/members-strings/expected.json index 91b0af7255..f5089fa23a 100644 --- a/test/fixtures/typescript/enum/members-strings/expected.json +++ b/test/fixtures/typescript/enum/members-strings/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 5, "end": 6, @@ -74,7 +74,7 @@ "column": 9 } }, - "name": { + "id": { "type": "StringLiteral", "start": 13, "end": 18, @@ -109,7 +109,7 @@ "column": 13 } }, - "name": { + "id": { "type": "StringLiteral", "start": 24, "end": 29, diff --git a/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json b/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json index 2cb16a8d72..2ff7744992 100644 --- a/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json +++ b/test/fixtures/typescript/enum/members-trailing-comma-with-initializer/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 5, "end": 6, @@ -74,7 +74,7 @@ "column": 9 } }, - "name": { + "id": { "type": "Identifier", "start": 13, "end": 14, diff --git a/test/fixtures/typescript/enum/members-trailing-comma/expected.json b/test/fixtures/typescript/enum/members-trailing-comma/expected.json index 61fec59557..ccf1e3313b 100644 --- a/test/fixtures/typescript/enum/members-trailing-comma/expected.json +++ b/test/fixtures/typescript/enum/members-trailing-comma/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 5, "end": 6, @@ -74,7 +74,7 @@ "column": 5 } }, - "name": { + "id": { "type": "Identifier", "start": 13, "end": 14, diff --git a/test/fixtures/typescript/enum/members/expected.json b/test/fixtures/typescript/enum/members/expected.json index 0128196715..e2eb96a737 100644 --- a/test/fixtures/typescript/enum/members/expected.json +++ b/test/fixtures/typescript/enum/members/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 5, "end": 6, @@ -74,7 +74,7 @@ "column": 5 } }, - "name": { + "id": { "type": "Identifier", "start": 13, "end": 14, @@ -106,7 +106,7 @@ "column": 9 } }, - "name": { + "id": { "type": "Identifier", "start": 20, "end": 21, diff --git a/test/fixtures/typescript/export/as-namespace/expected.json b/test/fixtures/typescript/export/as-namespace/expected.json index abf438a2b3..3b91561944 100644 --- a/test/fixtures/typescript/export/as-namespace/expected.json +++ b/test/fixtures/typescript/export/as-namespace/expected.json @@ -42,7 +42,7 @@ "column": 22 } }, - "name": { + "id": { "type": "Identifier", "start": 20, "end": 21, diff --git a/test/fixtures/typescript/export/declare/expected.json b/test/fixtures/typescript/export/declare/expected.json index b978e9b0a4..eea05baaa7 100644 --- a/test/fixtures/typescript/export/declare/expected.json +++ b/test/fixtures/typescript/export/declare/expected.json @@ -307,7 +307,7 @@ "column": 29 } }, - "name": { + "id": { "type": "Identifier", "start": 118, "end": 119, @@ -373,7 +373,7 @@ "column": 31 } }, - "name": { + "id": { "type": "Identifier", "start": 143, "end": 144, @@ -438,7 +438,7 @@ "column": 26 } }, - "name": { + "id": { "type": "Identifier", "start": 177, "end": 178, @@ -504,7 +504,7 @@ "column": 29 } }, - "name": { + "id": { "type": "Identifier", "start": 207, "end": 208, diff --git a/test/fixtures/typescript/import/equals-require/expected.json b/test/fixtures/typescript/import/equals-require/expected.json index 438e3f1a14..3d3bcd0aa8 100644 --- a/test/fixtures/typescript/import/equals-require/expected.json +++ b/test/fixtures/typescript/import/equals-require/expected.json @@ -43,7 +43,7 @@ } }, "isExport": false, - "name": { + "id": { "type": "Identifier", "start": 7, "end": 8, diff --git a/test/fixtures/typescript/import/equals/expected.json b/test/fixtures/typescript/import/equals/expected.json index 437770f234..b8eec29841 100644 --- a/test/fixtures/typescript/import/equals/expected.json +++ b/test/fixtures/typescript/import/equals/expected.json @@ -43,7 +43,7 @@ } }, "isExport": false, - "name": { + "id": { "type": "Identifier", "start": 7, "end": 8, diff --git a/test/fixtures/typescript/import/export-import-require/expected.json b/test/fixtures/typescript/import/export-import-require/expected.json index 290b305bbd..548d031da4 100644 --- a/test/fixtures/typescript/import/export-import-require/expected.json +++ b/test/fixtures/typescript/import/export-import-require/expected.json @@ -43,7 +43,7 @@ } }, "isExport": true, - "name": { + "id": { "type": "Identifier", "start": 14, "end": 15, diff --git a/test/fixtures/typescript/import/export-import/expected.json b/test/fixtures/typescript/import/export-import/expected.json index e88bee97e1..f9a877ad29 100644 --- a/test/fixtures/typescript/import/export-import/expected.json +++ b/test/fixtures/typescript/import/export-import/expected.json @@ -43,7 +43,7 @@ } }, "isExport": true, - "name": { + "id": { "type": "Identifier", "start": 14, "end": 15, diff --git a/test/fixtures/typescript/import/not-top-level/expected.json b/test/fixtures/typescript/import/not-top-level/expected.json index 4642b6eefd..88f8968e1f 100644 --- a/test/fixtures/typescript/import/not-top-level/expected.json +++ b/test/fixtures/typescript/import/not-top-level/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "StringLiteral", "start": 15, "end": 18, diff --git a/test/fixtures/typescript/interface/call-signature/expected.json b/test/fixtures/typescript/interface/call-signature/expected.json index c144ea60c5..04965b80b5 100644 --- a/test/fixtures/typescript/interface/call-signature/expected.json +++ b/test/fixtures/typescript/interface/call-signature/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, diff --git a/test/fixtures/typescript/interface/construct-signature/expected.json b/test/fixtures/typescript/interface/construct-signature/expected.json index b5e1ce72ba..8722621e20 100644 --- a/test/fixtures/typescript/interface/construct-signature/expected.json +++ b/test/fixtures/typescript/interface/construct-signature/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, diff --git a/test/fixtures/typescript/interface/export/expected.json b/test/fixtures/typescript/interface/export/expected.json index 0ed26af20f..98373c9abf 100644 --- a/test/fixtures/typescript/interface/export/expected.json +++ b/test/fixtures/typescript/interface/export/expected.json @@ -58,7 +58,7 @@ "column": 21 } }, - "name": { + "id": { "type": "Identifier", "start": 17, "end": 18, diff --git a/test/fixtures/typescript/interface/extends/expected.json b/test/fixtures/typescript/interface/extends/expected.json index 8cc4bb33d9..e8f46e4313 100644 --- a/test/fixtures/typescript/interface/extends/expected.json +++ b/test/fixtures/typescript/interface/extends/expected.json @@ -42,7 +42,7 @@ "column": 29 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, diff --git a/test/fixtures/typescript/interface/generic/expected.json b/test/fixtures/typescript/interface/generic/expected.json index a84fdd1617..2efcbbd999 100644 --- a/test/fixtures/typescript/interface/generic/expected.json +++ b/test/fixtures/typescript/interface/generic/expected.json @@ -42,7 +42,7 @@ "column": 48 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -150,7 +150,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 33, "end": 34, diff --git a/test/fixtures/typescript/interface/index-signature/expected.json b/test/fixtures/typescript/interface/index-signature/expected.json index e6df7627e2..574f4f8673 100644 --- a/test/fixtures/typescript/interface/index-signature/expected.json +++ b/test/fixtures/typescript/interface/index-signature/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, diff --git a/test/fixtures/typescript/interface/method-computed/expected.json b/test/fixtures/typescript/interface/method-computed/expected.json index f008e02301..cfc2786e3b 100644 --- a/test/fixtures/typescript/interface/method-computed/expected.json +++ b/test/fixtures/typescript/interface/method-computed/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -89,7 +89,7 @@ } }, "computed": true, - "name": { + "key": { "type": "MemberExpression", "start": 19, "end": 34, @@ -186,7 +186,7 @@ } }, "computed": true, - "name": { + "key": { "type": "MemberExpression", "start": 50, "end": 65, diff --git a/test/fixtures/typescript/interface/method-generic/expected.json b/test/fixtures/typescript/interface/method-generic/expected.json index 5f23b8a00c..9e1a26fb27 100644 --- a/test/fixtures/typescript/interface/method-generic/expected.json +++ b/test/fixtures/typescript/interface/method-generic/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -89,7 +89,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 18, "end": 19, @@ -197,7 +197,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 41, "end": 42, diff --git a/test/fixtures/typescript/interface/method-optional/expected.json b/test/fixtures/typescript/interface/method-optional/expected.json index 9c4a3deed7..0964244246 100644 --- a/test/fixtures/typescript/interface/method-optional/expected.json +++ b/test/fixtures/typescript/interface/method-optional/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -89,7 +89,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 18, "end": 19, diff --git a/test/fixtures/typescript/interface/method-plain/expected.json b/test/fixtures/typescript/interface/method-plain/expected.json index 61e4d5d5a1..8d6317dd5e 100644 --- a/test/fixtures/typescript/interface/method-plain/expected.json +++ b/test/fixtures/typescript/interface/method-plain/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -89,7 +89,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 18, "end": 19, @@ -123,7 +123,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 27, "end": 28, diff --git a/test/fixtures/typescript/interface/modifiers/expected.json b/test/fixtures/typescript/interface/modifiers/expected.json index 86c51ed8fa..eedeb69ddc 100644 --- a/test/fixtures/typescript/interface/modifiers/expected.json +++ b/test/fixtures/typescript/interface/modifiers/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -89,7 +89,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 27, "end": 28, diff --git a/test/fixtures/typescript/interface/properties/expected.json b/test/fixtures/typescript/interface/properties/expected.json index 019bc00090..2ade19ecaf 100644 --- a/test/fixtures/typescript/interface/properties/expected.json +++ b/test/fixtures/typescript/interface/properties/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -89,7 +89,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 18, "end": 19, @@ -122,7 +122,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 25, "end": 26, @@ -185,7 +185,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 40, "end": 41, diff --git a/test/fixtures/typescript/interface/property-computed/expected.json b/test/fixtures/typescript/interface/property-computed/expected.json index cb41c591f3..c4adcbaf6f 100644 --- a/test/fixtures/typescript/interface/property-computed/expected.json +++ b/test/fixtures/typescript/interface/property-computed/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -89,7 +89,7 @@ } }, "computed": true, - "name": { + "key": { "type": "MemberExpression", "start": 19, "end": 34, @@ -185,7 +185,7 @@ } }, "computed": true, - "name": { + "key": { "type": "MemberExpression", "start": 50, "end": 65, diff --git a/test/fixtures/typescript/interface/property-named-public/expected.json b/test/fixtures/typescript/interface/property-named-public/expected.json index b6702e3f95..92531b56c3 100644 --- a/test/fixtures/typescript/interface/property-named-public/expected.json +++ b/test/fixtures/typescript/interface/property-named-public/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -89,7 +89,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 15, "end": 21, diff --git a/test/fixtures/typescript/interface/reserved-method-name/expected.json b/test/fixtures/typescript/interface/reserved-method-name/expected.json index 39d0408e8b..71cbbf880f 100644 --- a/test/fixtures/typescript/interface/reserved-method-name/expected.json +++ b/test/fixtures/typescript/interface/reserved-method-name/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -89,7 +89,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 18, "end": 23, diff --git a/test/fixtures/typescript/interface/separators/expected.json b/test/fixtures/typescript/interface/separators/expected.json index fb48eb8fc9..6bee518fdf 100644 --- a/test/fixtures/typescript/interface/separators/expected.json +++ b/test/fixtures/typescript/interface/separators/expected.json @@ -42,7 +42,7 @@ "column": 40 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 15, @@ -89,7 +89,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 18, "end": 19, @@ -152,7 +152,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 29, "end": 30, @@ -217,7 +217,7 @@ "column": 39 } }, - "name": { + "id": { "type": "Identifier", "start": 51, "end": 55, @@ -264,7 +264,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 58, "end": 59, @@ -327,7 +327,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 69, "end": 70, @@ -392,7 +392,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 91, "end": 98, @@ -439,7 +439,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 105, "end": 106, @@ -502,7 +502,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 119, "end": 120, diff --git a/test/fixtures/typescript/module-namespace/body-declare/expected.json b/test/fixtures/typescript/module-namespace/body-declare/expected.json index 99619eb165..24e64f88da 100644 --- a/test/fixtures/typescript/module-namespace/body-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/body-declare/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 18, "end": 19, diff --git a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json index 6172ead122..ced873b54e 100644 --- a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 18, "end": 19, @@ -88,7 +88,7 @@ "column": 5 } }, - "name": { + "id": { "type": "Identifier", "start": 36, "end": 37, diff --git a/test/fixtures/typescript/module-namespace/body-nested/expected.json b/test/fixtures/typescript/module-namespace/body-nested/expected.json index 80ec878cb8..d9da87ef39 100644 --- a/test/fixtures/typescript/module-namespace/body-nested/expected.json +++ b/test/fixtures/typescript/module-namespace/body-nested/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -88,7 +88,7 @@ "column": 5 } }, - "name": { + "id": { "type": "Identifier", "start": 28, "end": 29, diff --git a/test/fixtures/typescript/module-namespace/body/expected.json b/test/fixtures/typescript/module-namespace/body/expected.json index de38e956a7..478a7945b8 100644 --- a/test/fixtures/typescript/module-namespace/body/expected.json +++ b/test/fixtures/typescript/module-namespace/body/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, diff --git a/test/fixtures/typescript/module-namespace/declare-shorthand/expected.json b/test/fixtures/typescript/module-namespace/declare-shorthand/expected.json index ddde3e40cb..b03a91254c 100644 --- a/test/fixtures/typescript/module-namespace/declare-shorthand/expected.json +++ b/test/fixtures/typescript/module-namespace/declare-shorthand/expected.json @@ -42,7 +42,7 @@ "column": 19 } }, - "name": { + "id": { "type": "StringLiteral", "start": 15, "end": 18, diff --git a/test/fixtures/typescript/module-namespace/global-in-module/expected.json b/test/fixtures/typescript/module-namespace/global-in-module/expected.json index 3e9e73988a..ffae13bcf8 100644 --- a/test/fixtures/typescript/module-namespace/global-in-module/expected.json +++ b/test/fixtures/typescript/module-namespace/global-in-module/expected.json @@ -42,7 +42,7 @@ "column": 1 } }, - "name": { + "id": { "type": "StringLiteral", "start": 15, "end": 18, @@ -92,7 +92,7 @@ } }, "global": true, - "name": { + "id": { "type": "Identifier", "start": 25, "end": 31, diff --git a/test/fixtures/typescript/module-namespace/head-declare/expected.json b/test/fixtures/typescript/module-namespace/head-declare/expected.json index c44bcdd14b..afe7354fd4 100644 --- a/test/fixtures/typescript/module-namespace/head-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/head-declare/expected.json @@ -42,7 +42,7 @@ "column": 24 } }, - "name": { + "id": { "type": "Identifier", "start": 18, "end": 19, @@ -73,7 +73,7 @@ "column": 24 } }, - "name": { + "id": { "type": "Identifier", "start": 20, "end": 21, @@ -123,7 +123,7 @@ "column": 19 } }, - "name": { + "id": { "type": "Identifier", "start": 40, "end": 41, @@ -172,7 +172,7 @@ "column": 21 } }, - "name": { + "id": { "type": "StringLiteral", "start": 60, "end": 63, @@ -225,7 +225,7 @@ } }, "global": true, - "name": { + "id": { "type": "Identifier", "start": 75, "end": 81, diff --git a/test/fixtures/typescript/module-namespace/head-export/expected.json b/test/fixtures/typescript/module-namespace/head-export/expected.json index cec33f1c85..8476ad7ecd 100644 --- a/test/fixtures/typescript/module-namespace/head-export/expected.json +++ b/test/fixtures/typescript/module-namespace/head-export/expected.json @@ -58,7 +58,7 @@ "column": 23 } }, - "name": { + "id": { "type": "Identifier", "start": 17, "end": 18, @@ -89,7 +89,7 @@ "column": 23 } }, - "name": { + "id": { "type": "Identifier", "start": 19, "end": 20, @@ -155,7 +155,7 @@ "column": 18 } }, - "name": { + "id": { "type": "Identifier", "start": 38, "end": 39, diff --git a/test/fixtures/typescript/module-namespace/head/expected.json b/test/fixtures/typescript/module-namespace/head/expected.json index a60b2b9580..c3f419f4ca 100644 --- a/test/fixtures/typescript/module-namespace/head/expected.json +++ b/test/fixtures/typescript/module-namespace/head/expected.json @@ -42,7 +42,7 @@ "column": 14 } }, - "name": { + "id": { "type": "Identifier", "start": 10, "end": 11, @@ -90,7 +90,7 @@ "column": 18 } }, - "name": { + "id": { "type": "Identifier", "start": 25, "end": 26, @@ -121,7 +121,7 @@ "column": 18 } }, - "name": { + "id": { "type": "Identifier", "start": 27, "end": 28, @@ -152,7 +152,7 @@ "column": 18 } }, - "name": { + "id": { "type": "Identifier", "start": 29, "end": 30, @@ -202,7 +202,7 @@ "column": 11 } }, - "name": { + "id": { "type": "Identifier", "start": 41, "end": 42, @@ -250,7 +250,7 @@ "column": 13 } }, - "name": { + "id": { "type": "StringLiteral", "start": 53, "end": 56, diff --git a/test/fixtures/typescript/type-alias/declare/expected.json b/test/fixtures/typescript/type-alias/declare/expected.json index 05186b6446..342f2ca280 100644 --- a/test/fixtures/typescript/type-alias/declare/expected.json +++ b/test/fixtures/typescript/type-alias/declare/expected.json @@ -42,7 +42,7 @@ "column": 24 } }, - "name": { + "id": { "type": "Identifier", "start": 13, "end": 14, diff --git a/test/fixtures/typescript/type-alias/export/expected.json b/test/fixtures/typescript/type-alias/export/expected.json index 969254f6a9..503ea72ce1 100644 --- a/test/fixtures/typescript/type-alias/export/expected.json +++ b/test/fixtures/typescript/type-alias/export/expected.json @@ -58,7 +58,7 @@ "column": 23 } }, - "name": { + "id": { "type": "Identifier", "start": 12, "end": 13, diff --git a/test/fixtures/typescript/type-alias/generic-complex/expected.json b/test/fixtures/typescript/type-alias/generic-complex/expected.json index adc71d3e72..f771fb03e7 100644 --- a/test/fixtures/typescript/type-alias/generic-complex/expected.json +++ b/test/fixtures/typescript/type-alias/generic-complex/expected.json @@ -42,7 +42,7 @@ "column": 52 } }, - "name": { + "id": { "type": "Identifier", "start": 5, "end": 6, @@ -150,7 +150,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 28, "end": 29, diff --git a/test/fixtures/typescript/type-alias/generic/expected.json b/test/fixtures/typescript/type-alias/generic/expected.json index e81961a7be..8c22db2fa4 100644 --- a/test/fixtures/typescript/type-alias/generic/expected.json +++ b/test/fixtures/typescript/type-alias/generic/expected.json @@ -42,7 +42,7 @@ "column": 14 } }, - "name": { + "id": { "type": "Identifier", "start": 5, "end": 6, diff --git a/test/fixtures/typescript/type-alias/plain/expected.json b/test/fixtures/typescript/type-alias/plain/expected.json index dee2026e1e..bd7e4dd4ef 100644 --- a/test/fixtures/typescript/type-alias/plain/expected.json +++ b/test/fixtures/typescript/type-alias/plain/expected.json @@ -42,7 +42,7 @@ "column": 16 } }, - "name": { + "id": { "type": "Identifier", "start": 5, "end": 6, diff --git a/test/fixtures/typescript/types/parenthesized/expected.json b/test/fixtures/typescript/types/parenthesized/expected.json index dd4b167a5e..b92fa9d5d7 100644 --- a/test/fixtures/typescript/types/parenthesized/expected.json +++ b/test/fixtures/typescript/types/parenthesized/expected.json @@ -42,7 +42,7 @@ "column": 14 } }, - "name": { + "id": { "type": "Identifier", "start": 5, "end": 6, diff --git a/test/fixtures/typescript/types/type-literal/expected.json b/test/fixtures/typescript/types/type-literal/expected.json index 232f1590fd..c6ded591bc 100644 --- a/test/fixtures/typescript/types/type-literal/expected.json +++ b/test/fixtures/typescript/types/type-literal/expected.json @@ -117,7 +117,7 @@ } }, "computed": false, - "name": { + "key": { "type": "Identifier", "start": 11, "end": 12, From a339739342746c0bcb746ae847f471a2545ac3a7 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Wed, 17 May 2017 15:31:50 -0700 Subject: [PATCH 11/55] Introduce TSDeclareFunction and TSDeclareMethod node types --- src/parser/expression.js | 30 ++++++++++--------- src/parser/statement.js | 7 ++--- src/plugins/estree.js | 9 +++--- src/plugins/flow.js | 2 +- src/plugins/typescript.js | 2 +- src/types.js | 30 +++++++++++++++---- .../class/constructor/expected.json | 4 +-- .../typescript/class/declare/expected.json | 4 +-- .../class/get-generic/expected.json | 2 +- .../members-with-modifier-names/expected.json | 6 ++-- .../members-with-reserved-names/expected.json | 2 +- .../class/method-computed/expected.json | 4 +-- .../class/method-no-body/expected.json | 4 +-- .../expected.json | 4 +-- .../class/modifiers-accessors/expected.json | 4 +-- .../modifiers-methods-async/expected.json | 4 +-- .../typescript/class/static/expected.json | 8 ++--- .../typescript/export/declare/expected.json | 2 +- .../typescript/function/declare/expected.json | 4 +-- .../function/overloads/expected.json | 4 +-- 20 files changed, 79 insertions(+), 57 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 328b5b060a..962faa025a 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -951,19 +951,16 @@ export default class ExpressionParser extends LValParser { if (isPattern) this.unexpected(); prop.kind = "method"; prop.method = true; - this.parseMethod(prop, isGenerator, isAsync, /* isConstructor */ false); - - return this.finishNode(prop, "ObjectMethod"); + return this.parseMethod(prop, isGenerator, isAsync, /* isConstructor */ false, "ObjectMethod"); } if (this.isGetterOrSetterMethod(prop, isPattern)) { if (isGenerator || isAsync) this.unexpected(); prop.kind = prop.key.name; this.parsePropertyName(prop); - this.parseMethod(prop, /* isGenerator */false, /* isAsync */ false, /* isConstructor */ false); + this.parseMethod(prop, /* isGenerator */false, /* isAsync */ false, /* isConstructor */ false, "ObjectMethod"); this.checkGetterSetterParamCount(prop); - - return this.finishNode(prop, "ObjectMethod"); + return prop; } } @@ -1029,7 +1026,7 @@ export default class ExpressionParser extends LValParser { // Initialize empty function node. - initFunction(node: N.Function, isAsync: ?boolean): void { + initFunction(node: N.BodilessFunctionBase, isAsync: ?boolean): void { node.id = null; node.generator = false; node.expression = false; @@ -1038,7 +1035,7 @@ export default class ExpressionParser extends LValParser { // Parse object or class method. - parseMethod(node: N.MethodLike, isGenerator: boolean, isAsync: boolean, isConstructor: boolean): N.MethodLike { + parseMethod(node: T, isGenerator: boolean, isAsync: boolean, isConstructor: boolean, type: string): T { const oldInMethod = this.state.inMethod; this.state.inMethod = node.kind || true; this.initFunction(node, isAsync); @@ -1046,9 +1043,9 @@ export default class ExpressionParser extends LValParser { const allowModifiers = isConstructor; // For TypeScript parameter properties node.params = this.parseBindingList(tt.parenR, /* allowEmpty */ false, allowModifiers); node.generator = !!isGenerator; - this.maybeParseFunctionBody(node, /* allowExpression */ false); + const missingBody = this.maybeParseFunctionBody(node, /* allowExpression */ false, /* allowMissingBody */ type === "ClassMethod"); this.state.inMethod = oldInMethod; - return node; + return this.finishNode(node, missingBody ? "TSDeclareMethod" : type); } // Parse arrow function expression with given parameters. @@ -1072,7 +1069,8 @@ export default class ExpressionParser extends LValParser { return false; } - maybeParseFunctionBody(node: N.Function, allowExpression?: boolean): void { + // Returns true if the body is missing. + maybeParseFunctionBody(node: N.BodilessFunctionBase, allowExpression?: boolean, allowMissingBody: boolean): boolean { if (!allowExpression) { // For arrow functions, `parseArrow` handles the return type itself. this.parseFunctionReturnType(node); @@ -1080,14 +1078,18 @@ export default class ExpressionParser extends LValParser { // In TypeScript, a body may be missing on a declaration. // See `parseFunctionBlockOrSemicolon` in TS source. - if (!(this.hasPlugin("typescript") && !this.match(tt.braceL) && this.isLineTerminator())) { - this.parseFunctionBody(node, allowExpression); + if (allowMissingBody && this.hasPlugin("typescript") && !this.match(tt.braceL) && this.isLineTerminator()) { + return true; } + + // $FlowIgnore (node is not bodiless if we get here) + this.parseFunctionBody(node, allowExpression); + return false; } // TypeScript / Flow plugins will override this // eslint-disable-next-line no-unused-vars - parseFunctionReturnType(node: N.Function): void {} + parseFunctionReturnType(node: N.BodilessFunctionBase): void {} // Parse function body and check parameters. parseFunctionBody(node: N.Function, allowExpression: ?boolean): void { diff --git a/src/parser/statement.js b/src/parser/statement.js index 79f1da4caf..d8d0d40dc3 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -631,11 +631,11 @@ export default class StatementParser extends ExpressionParser { } this.parseFunctionParams(node); - this.maybeParseFunctionBody(node, allowExpressionBody); + const missingBody = this.maybeParseFunctionBody(node, allowExpressionBody, isStatement); this.state.inMethod = oldInMethod; - return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression"); + return this.finishNode(node, isStatement ? (missingBody ? "TSDeclareFunction" : "FunctionDeclaration") : "FunctionExpression"); } parseFunctionParams(node: N.NormalFunction): void { @@ -879,8 +879,7 @@ export default class StatementParser extends ExpressionParser { } parseClassMethod(classBody: N.ClassBody, method: N.ClassMethod, isGenerator: boolean, isAsync: boolean, isConstructor: boolean): void { - this.parseMethod(method, isGenerator, isAsync, isConstructor); - classBody.body.push(this.finishNode(method, "ClassMethod")); + classBody.body.push(this.parseMethod(method, isGenerator, isAsync, isConstructor, "ClassMethod")); } parseClassId(node: N.Class, isStatement: boolean, optionalId: ?boolean): void { diff --git a/src/plugins/estree.js b/src/plugins/estree.js index bc338ebfb1..053dcf3310 100644 --- a/src/plugins/estree.js +++ b/src/plugins/estree.js @@ -172,15 +172,16 @@ export default (superClass: Class): Class => class extends super return node; } - parseMethod(node: N.MethodLike, ...args): N.MethodLike { + parseMethod( + node: T, isGenerator: boolean, isAsync: boolean, isConstructor: boolean, type: string): T { let funcNode: any = this.startNode(); funcNode.kind = node.kind; // provide kind, so inner method correctly sets state - funcNode = super.parseMethod(funcNode, ...args); + funcNode = super.parseMethod(funcNode, isGenerator, isAsync, isConstructor, "FunctionExpression"); delete funcNode.kind; // $FlowIgnore - node.value = this.finishNode(funcNode, "FunctionExpression"); + node.value = funcNode; - return node; + return this.finishNode(node, type); } parseObjectMethod(...args): ?N.ObjectMethod { diff --git a/src/plugins/flow.js b/src/plugins/flow.js index be0107a216..9f052d2ae6 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -1006,7 +1006,7 @@ export default (superClass: Class): Class => class extends super // Overrides // ================================== - parseFunctionReturnType(node: N.Function): void { + parseFunctionReturnType(node: N.BodilessFunctionBase): void { if (this.match(tt.colon)) { const typeNode = this.startNode(); // $FlowFixMe diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 96929e66b0..8ebff26312 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1102,7 +1102,7 @@ export default (superClass: Class): Class => class extends super return { abstract, readonly }; } - parseFunctionReturnType(node: N.Function): void { + parseFunctionReturnType(node: N.BodilessFunctionBase): void { if (this.eat(tt.colon)) { node.returnType = this.tsParseTypeOrTypePredicateAnnotation(); } diff --git a/src/types.js b/src/types.js index 0d6f016f6e..12684cf5a3 100644 --- a/src/types.js +++ b/src/types.js @@ -54,6 +54,7 @@ export type Declaration = | TsModuleDeclaration; export type DeclarationBase = NodeBase & { // TypeScript allows declarations to be prefixed by `declare`. + //TODO: a FunctionDeclaration is never "declare", because it's a TSDeclareFunction instead. declare?: true; } @@ -124,7 +125,8 @@ export type Function = export type NormalFunction = FunctionDeclaration | FunctionExpression; -export type FunctionBase = HasDecorators & { +//TODO: name +export type BodilessFunctionBase = HasDecorators & { id: ?Identifier; params: $ReadOnlyArray; body: BlockStatement; @@ -135,6 +137,10 @@ export type FunctionBase = HasDecorators & { expression: boolean; typeParameters?: ?TypeParameterDeclaration; returnType?: ?TypeAnnotation; +} + +export type FunctionBase = BodilessFunctionBase & { + body: BlockStatement; }; // Statements @@ -274,7 +280,7 @@ export type ForOfStatement = ForInOfBase & { // Declarations -export type OptFunctionDeclaration = FunctionBase & DeclarationBase & HasDecorators & { +export type OptFunctionDeclaration = FunctionBase & DeclarationBase & { type: "FunctionDeclaration"; }; @@ -282,6 +288,15 @@ export type FunctionDeclaration = OptFunctionDeclaration & { id: Identifier; } +//test with `export default function(): void; +export type OptTSDeclareFunction = BodilessFunctionBase & DeclarationBase & { + type: "TSDeclareFunction"; +} + +export type TSDeclareFunction = OptTSDeclareFunction & { + id: Identifier; +}; + export type VariableDeclaration = DeclarationBase & HasDecorators & { type: "VariableDeclaration"; declarations: $ReadOnlyArray; @@ -569,14 +584,19 @@ export type ClassMemberBase = NodeBase & HasDecorators & { export type ClassMember = ClassMethod | ClassProperty | TsIndexSignature -export type MethodLike = ObjectMethod | FunctionExpression | ClassMethod; +export type MethodLike = ObjectMethod | FunctionExpression | ClassMethod | TSDeclareMethod; export type MethodBase = FunctionBase & { - +kind?: MethodKind; + +kind: MethodKind; }; export type MethodKind = "constructor" | "method" | "get" | "set"; +export type TSDeclareMethod = BodilessFunctionBase & ClassMemberBase & { + type: "TSDeclareMethod"; + +kind: MethodKind; +}; + export type ClassMethod = MethodBase & ClassMemberBase & { type: "ClassMethod"; key: Expression; @@ -675,7 +695,7 @@ export type ExportSpecifier = NodeBase & { export type ExportDefaultDeclaration = NodeBase & { type: "ExportDefaultDeclaration"; - declaration: OptFunctionDeclaration | OptClassDeclaration | Expression; + declaration: OptFunctionDeclaration | OptTSDeclareFunction | OptClassDeclaration | Expression; }; export type ExportAllDeclaration = NodeBase & { diff --git a/test/fixtures/typescript/class/constructor/expected.json b/test/fixtures/typescript/class/constructor/expected.json index fb00c2ad46..74867c1fc6 100644 --- a/test/fixtures/typescript/class/constructor/expected.json +++ b/test/fixtures/typescript/class/constructor/expected.json @@ -76,7 +76,7 @@ }, "body": [ { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 14, "end": 48, "loc": { @@ -211,7 +211,7 @@ ] }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 53, "end": 87, "loc": { diff --git a/test/fixtures/typescript/class/declare/expected.json b/test/fixtures/typescript/class/declare/expected.json index fcb934a0bd..b4ab86385d 100644 --- a/test/fixtures/typescript/class/declare/expected.json +++ b/test/fixtures/typescript/class/declare/expected.json @@ -270,7 +270,7 @@ "value": null }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 66, "end": 70, "loc": { @@ -310,7 +310,7 @@ "params": [] }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 75, "end": 85, "loc": { diff --git a/test/fixtures/typescript/class/get-generic/expected.json b/test/fixtures/typescript/class/get-generic/expected.json index 148be925c5..d98b89b2fc 100644 --- a/test/fixtures/typescript/class/get-generic/expected.json +++ b/test/fixtures/typescript/class/get-generic/expected.json @@ -76,7 +76,7 @@ }, "body": [ { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 22, "end": 37, "loc": { diff --git a/test/fixtures/typescript/class/members-with-modifier-names/expected.json b/test/fixtures/typescript/class/members-with-modifier-names/expected.json index 4bf1257b7f..6a31dfc899 100644 --- a/test/fixtures/typescript/class/members-with-modifier-names/expected.json +++ b/test/fixtures/typescript/class/members-with-modifier-names/expected.json @@ -76,7 +76,7 @@ }, "body": [ { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 14, "end": 29, "loc": { @@ -146,7 +146,7 @@ } }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 34, "end": 56, "loc": { @@ -271,7 +271,7 @@ } }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 79, "end": 96, "loc": { diff --git a/test/fixtures/typescript/class/members-with-reserved-names/expected.json b/test/fixtures/typescript/class/members-with-reserved-names/expected.json index 362d51067f..88ba3b71f4 100644 --- a/test/fixtures/typescript/class/members-with-reserved-names/expected.json +++ b/test/fixtures/typescript/class/members-with-reserved-names/expected.json @@ -76,7 +76,7 @@ }, "body": [ { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 14, "end": 36, "loc": { diff --git a/test/fixtures/typescript/class/method-computed/expected.json b/test/fixtures/typescript/class/method-computed/expected.json index 1f3ebb8df6..be01e76389 100644 --- a/test/fixtures/typescript/class/method-computed/expected.json +++ b/test/fixtures/typescript/class/method-computed/expected.json @@ -76,7 +76,7 @@ }, "body": [ { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 14, "end": 40, "loc": { @@ -179,7 +179,7 @@ } }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 45, "end": 72, "loc": { diff --git a/test/fixtures/typescript/class/method-no-body/expected.json b/test/fixtures/typescript/class/method-no-body/expected.json index ae05d52dbe..1a428191b0 100644 --- a/test/fixtures/typescript/class/method-no-body/expected.json +++ b/test/fixtures/typescript/class/method-no-body/expected.json @@ -76,7 +76,7 @@ }, "body": [ { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 14, "end": 18, "loc": { @@ -116,7 +116,7 @@ "params": [] }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 23, "end": 33, "loc": { diff --git a/test/fixtures/typescript/class/method-with-newline-without-body/expected.json b/test/fixtures/typescript/class/method-with-newline-without-body/expected.json index 9d0bd11572..5252a1bbd5 100644 --- a/test/fixtures/typescript/class/method-with-newline-without-body/expected.json +++ b/test/fixtures/typescript/class/method-with-newline-without-body/expected.json @@ -76,7 +76,7 @@ }, "body": [ { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 14, "end": 17, "loc": { @@ -116,7 +116,7 @@ "params": [] }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 22, "end": 25, "loc": { diff --git a/test/fixtures/typescript/class/modifiers-accessors/expected.json b/test/fixtures/typescript/class/modifiers-accessors/expected.json index 64f5ee3d96..953516b92d 100644 --- a/test/fixtures/typescript/class/modifiers-accessors/expected.json +++ b/test/fixtures/typescript/class/modifiers-accessors/expected.json @@ -77,7 +77,7 @@ }, "body": [ { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 63, "end": 80, "loc": { @@ -211,7 +211,7 @@ } }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 118, "end": 144, "loc": { diff --git a/test/fixtures/typescript/class/modifiers-methods-async/expected.json b/test/fixtures/typescript/class/modifiers-methods-async/expected.json index 41828f1684..d35e4a6d64 100644 --- a/test/fixtures/typescript/class/modifiers-methods-async/expected.json +++ b/test/fixtures/typescript/class/modifiers-methods-async/expected.json @@ -77,7 +77,7 @@ }, "body": [ { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 65, "end": 84, "loc": { @@ -175,7 +175,7 @@ } }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 113, "end": 141, "loc": { diff --git a/test/fixtures/typescript/class/static/expected.json b/test/fixtures/typescript/class/static/expected.json index 8c77a0575a..f8a95e4d32 100644 --- a/test/fixtures/typescript/class/static/expected.json +++ b/test/fixtures/typescript/class/static/expected.json @@ -76,7 +76,7 @@ }, "body": [ { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 14, "end": 25, "loc": { @@ -116,7 +116,7 @@ "params": [] }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 30, "end": 48, "loc": { @@ -157,7 +157,7 @@ "params": [] }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 53, "end": 74, "loc": { @@ -198,7 +198,7 @@ "params": [] }, { - "type": "ClassMethod", + "type": "TSDeclareMethod", "start": 79, "end": 98, "loc": { diff --git a/test/fixtures/typescript/export/declare/expected.json b/test/fixtures/typescript/export/declare/expected.json index eea05baaa7..e24f3ee6ec 100644 --- a/test/fixtures/typescript/export/declare/expected.json +++ b/test/fixtures/typescript/export/declare/expected.json @@ -143,7 +143,7 @@ "specifiers": [], "source": null, "declaration": { - "type": "FunctionDeclaration", + "type": "TSDeclareFunction", "start": 47, "end": 66, "loc": { diff --git a/test/fixtures/typescript/function/declare/expected.json b/test/fixtures/typescript/function/declare/expected.json index 0648b9d0ed..40c39d666f 100644 --- a/test/fixtures/typescript/function/declare/expected.json +++ b/test/fixtures/typescript/function/declare/expected.json @@ -29,7 +29,7 @@ "sourceType": "module", "body": [ { - "type": "FunctionDeclaration", + "type": "TSDeclareFunction", "start": 0, "end": 27, "loc": { @@ -96,7 +96,7 @@ "declare": true }, { - "type": "FunctionDeclaration", + "type": "TSDeclareFunction", "start": 28, "end": 55, "loc": { diff --git a/test/fixtures/typescript/function/overloads/expected.json b/test/fixtures/typescript/function/overloads/expected.json index d1c17481ab..5d9a028815 100644 --- a/test/fixtures/typescript/function/overloads/expected.json +++ b/test/fixtures/typescript/function/overloads/expected.json @@ -45,7 +45,7 @@ "specifiers": [], "source": null, "declaration": { - "type": "FunctionDeclaration", + "type": "TSDeclareFunction", "start": 7, "end": 37, "loc": { @@ -176,7 +176,7 @@ "specifiers": [], "source": null, "declaration": { - "type": "FunctionDeclaration", + "type": "TSDeclareFunction", "start": 45, "end": 75, "loc": { From ac4255cdaba23382b047e44627b770ca0eb92207 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 18 May 2017 12:39:42 -0700 Subject: [PATCH 12/55] Change TypeParameter name to a string to match flow --- src/parser/expression.js | 23 +++++++----- src/plugins/typescript.js | 4 +-- src/types.js | 2 +- .../arrow-function/generic-tsx/expected.json | 19 +--------- .../arrow-function/generic/expected.json | 18 +--------- .../class/expression-generic/expected.json | 36 ++----------------- .../typescript/class/generic/expected.json | 18 +--------- .../class/get-generic/expected.json | 18 +--------- .../members-with-modifier-names/expected.json | 18 +--------- .../class/method-generic/expected.json | 36 ++----------------- .../function/annotated/expected.json | 18 +--------- .../function/anonymous/expected.json | 18 +--------- .../typescript/function/declare/expected.json | 18 +--------- .../interface/generic/expected.json | 18 +--------- .../interface/method-generic/expected.json | 18 +--------- .../type-alias/generic-complex/expected.json | 18 +--------- .../type-alias/generic/expected.json | 18 +--------- .../types/function-generic/expected.json | 18 +--------- .../typescript/types/mapped/expected.json | 36 ++----------------- 19 files changed, 37 insertions(+), 335 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 962faa025a..d949bbe5b2 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -1182,26 +1182,33 @@ export default class ExpressionParser extends LValParser { parseIdentifier(liberal?: boolean): N.Identifier { const node = this.startNode(); + const name = this.parseIdentifierName(node.start, liberal); + node.name = name; + node.loc.identifierName = name; + return this.finishNode(node, "Identifier"); + } + + parseIdentifierName(pos: number, liberal?: boolean): string { if (!liberal) { this.checkReservedWord(this.state.value, this.state.start, !!this.state.type.keyword, false); } + let name: string; + if (this.match(tt.name)) { - node.name = this.state.value; + name = this.state.value; } else if (this.state.type.keyword) { - node.name = this.state.type.keyword; + name = this.state.type.keyword; } else { - this.unexpected(); + throw this.unexpected(); } - if (!liberal && node.name === "await" && this.state.inAsync) { - this.raise(node.start, "invalid use of await inside of an async function"); + if (!liberal && name === "await" && this.state.inAsync) { + this.raise(pos, "invalid use of await inside of an async function"); } - node.loc.identifierName = node.name; - this.next(); - return this.finishNode(node, "Identifier"); + return name; } checkReservedWord(word: string, startLoc: number, checkKeywords: boolean, isBinding: boolean): void { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 8ebff26312..c603844bd0 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -234,7 +234,7 @@ export default (superClass: Class): Class => class extends super tsParseTypeParameter(): N.TypeParameter { const node: N.TypeParameter = this.startNode(); - node.name = this.parseIdentifier(); + node.name = this.parseIdentifierName(node.start); if (this.eat(tt._extends)) { node.constraint = this.tsParseType(); } @@ -428,7 +428,7 @@ export default (superClass: Class): Class => class extends super tsParseMappedTypeParameter(): N.TypeParameter { const node: N.TypeParameter = this.startNode(); - node.name = this.parseIdentifier(); + node.name = this.parseIdentifierName(node.start); this.expect(tt._in); node.constraint = this.tsParseType(); return this.finishNode(node, "TypeParameter"); diff --git a/src/types.js b/src/types.js index 12684cf5a3..b4515cbc95 100644 --- a/src/types.js +++ b/src/types.js @@ -730,7 +730,7 @@ export type TypeParameterDeclaration = NodeBase & { export type TypeParameter = NodeBase & { type: "TypeParameter"; - name: Identifier; + name: string; constraint?: TsType; default?: TsType; }; diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json index 83f57befe0..d49759277a 100644 --- a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json +++ b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json @@ -204,24 +204,7 @@ "column": 2 } }, - "name": { - "type": "Identifier", - "start": 62, - "end": 63, - "loc": { - "start": { - "line": 2, - "column": 1 - }, - "end": { - "line": 2, - "column": 2 - }, - "identifierName": "T" - }, - "name": "T", - "leadingComments": null - }, + "name": "T", "leadingComments": null } ], diff --git a/test/fixtures/typescript/arrow-function/generic/expected.json b/test/fixtures/typescript/arrow-function/generic/expected.json index ca6c7cef42..735860986c 100644 --- a/test/fixtures/typescript/arrow-function/generic/expected.json +++ b/test/fixtures/typescript/arrow-function/generic/expected.json @@ -204,23 +204,7 @@ "column": 2 } }, - "name": { - "type": "Identifier", - "start": 1, - "end": 2, - "loc": { - "start": { - "line": 1, - "column": 1 - }, - "end": { - "line": 1, - "column": 2 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] } diff --git a/test/fixtures/typescript/class/expression-generic/expected.json b/test/fixtures/typescript/class/expression-generic/expected.json index 8a4ad66cbc..6082a36e18 100644 --- a/test/fixtures/typescript/class/expression-generic/expected.json +++ b/test/fixtures/typescript/class/expression-generic/expected.json @@ -86,23 +86,7 @@ "column": 8 } }, - "name": { - "type": "Identifier", - "start": 7, - "end": 8, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, @@ -203,23 +187,7 @@ "column": 10 } }, - "name": { - "type": "Identifier", - "start": 24, - "end": 25, - "loc": { - "start": { - "line": 2, - "column": 9 - }, - "end": { - "line": 2, - "column": 10 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, diff --git a/test/fixtures/typescript/class/generic/expected.json b/test/fixtures/typescript/class/generic/expected.json index ae152d0dca..f64a3d3295 100644 --- a/test/fixtures/typescript/class/generic/expected.json +++ b/test/fixtures/typescript/class/generic/expected.json @@ -88,23 +88,7 @@ "column": 40 } }, - "name": { - "type": "Identifier", - "start": 8, - "end": 9, - "loc": { - "start": { - "line": 1, - "column": 8 - }, - "end": { - "line": 1, - "column": 9 - }, - "identifierName": "T" - }, - "name": "T" - }, + "name": "T", "constraint": { "type": "TSObjectKeyword", "start": 18, diff --git a/test/fixtures/typescript/class/get-generic/expected.json b/test/fixtures/typescript/class/get-generic/expected.json index d98b89b2fc..ec6713b5e1 100644 --- a/test/fixtures/typescript/class/get-generic/expected.json +++ b/test/fixtures/typescript/class/get-generic/expected.json @@ -138,23 +138,7 @@ "column": 9 } }, - "name": { - "type": "Identifier", - "start": 26, - "end": 27, - "loc": { - "start": { - "line": 2, - "column": 8 - }, - "end": { - "line": 2, - "column": 9 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, diff --git a/test/fixtures/typescript/class/members-with-modifier-names/expected.json b/test/fixtures/typescript/class/members-with-modifier-names/expected.json index 6a31dfc899..65f583716d 100644 --- a/test/fixtures/typescript/class/members-with-modifier-names/expected.json +++ b/test/fixtures/typescript/class/members-with-modifier-names/expected.json @@ -333,23 +333,7 @@ "column": 11 } }, - "name": { - "type": "Identifier", - "start": 85, - "end": 86, - "loc": { - "start": { - "line": 5, - "column": 10 - }, - "end": { - "line": 5, - "column": 11 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, diff --git a/test/fixtures/typescript/class/method-generic/expected.json b/test/fixtures/typescript/class/method-generic/expected.json index e2ee170760..fb47a93450 100644 --- a/test/fixtures/typescript/class/method-generic/expected.json +++ b/test/fixtures/typescript/class/method-generic/expected.json @@ -138,23 +138,7 @@ "column": 7 } }, - "name": { - "type": "Identifier", - "start": 16, - "end": 17, - "loc": { - "start": { - "line": 2, - "column": 6 - }, - "end": { - "line": 2, - "column": 7 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, @@ -548,23 +532,7 @@ "column": 23 } }, - "name": { - "type": "Identifier", - "start": 71, - "end": 72, - "loc": { - "start": { - "line": 3, - "column": 22 - }, - "end": { - "line": 3, - "column": 23 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, diff --git a/test/fixtures/typescript/function/annotated/expected.json b/test/fixtures/typescript/function/annotated/expected.json index 517149cb5f..78fecb0685 100644 --- a/test/fixtures/typescript/function/annotated/expected.json +++ b/test/fixtures/typescript/function/annotated/expected.json @@ -91,23 +91,7 @@ "column": 12 } }, - "name": { - "type": "Identifier", - "start": 11, - "end": 12, - "loc": { - "start": { - "line": 1, - "column": 11 - }, - "end": { - "line": 1, - "column": 12 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, diff --git a/test/fixtures/typescript/function/anonymous/expected.json b/test/fixtures/typescript/function/anonymous/expected.json index fed1b9e90c..4957d5580f 100644 --- a/test/fixtures/typescript/function/anonymous/expected.json +++ b/test/fixtures/typescript/function/anonymous/expected.json @@ -121,23 +121,7 @@ "column": 20 } }, - "name": { - "type": "Identifier", - "start": 19, - "end": 20, - "loc": { - "start": { - "line": 1, - "column": 19 - }, - "end": { - "line": 1, - "column": 20 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, diff --git a/test/fixtures/typescript/function/declare/expected.json b/test/fixtures/typescript/function/declare/expected.json index 40c39d666f..5574c30e62 100644 --- a/test/fixtures/typescript/function/declare/expected.json +++ b/test/fixtures/typescript/function/declare/expected.json @@ -158,23 +158,7 @@ "column": 20 } }, - "name": { - "type": "Identifier", - "start": 47, - "end": 48, - "loc": { - "start": { - "line": 2, - "column": 19 - }, - "end": { - "line": 2, - "column": 20 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, diff --git a/test/fixtures/typescript/interface/generic/expected.json b/test/fixtures/typescript/interface/generic/expected.json index 2efcbbd999..7abcc3fb45 100644 --- a/test/fixtures/typescript/interface/generic/expected.json +++ b/test/fixtures/typescript/interface/generic/expected.json @@ -88,23 +88,7 @@ "column": 44 } }, - "name": { - "type": "Identifier", - "start": 12, - "end": 13, - "loc": { - "start": { - "line": 1, - "column": 12 - }, - "end": { - "line": 1, - "column": 13 - }, - "identifierName": "T" - }, - "name": "T" - }, + "name": "T", "constraint": { "type": "TSObjectKeyword", "start": 22, diff --git a/test/fixtures/typescript/interface/method-generic/expected.json b/test/fixtures/typescript/interface/method-generic/expected.json index 9e1a26fb27..14cc88e0d0 100644 --- a/test/fixtures/typescript/interface/method-generic/expected.json +++ b/test/fixtures/typescript/interface/method-generic/expected.json @@ -135,23 +135,7 @@ "column": 38 } }, - "name": { - "type": "Identifier", - "start": 20, - "end": 21, - "loc": { - "start": { - "line": 2, - "column": 6 - }, - "end": { - "line": 2, - "column": 7 - }, - "identifierName": "T" - }, - "name": "T" - }, + "name": "T", "constraint": { "type": "TSObjectKeyword", "start": 30, diff --git a/test/fixtures/typescript/type-alias/generic-complex/expected.json b/test/fixtures/typescript/type-alias/generic-complex/expected.json index f771fb03e7..9c7b9d4cd2 100644 --- a/test/fixtures/typescript/type-alias/generic-complex/expected.json +++ b/test/fixtures/typescript/type-alias/generic-complex/expected.json @@ -88,23 +88,7 @@ "column": 39 } }, - "name": { - "type": "Identifier", - "start": 7, - "end": 8, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - }, - "identifierName": "U" - }, - "name": "U" - }, + "name": "U", "constraint": { "type": "TSObjectKeyword", "start": 17, diff --git a/test/fixtures/typescript/type-alias/generic/expected.json b/test/fixtures/typescript/type-alias/generic/expected.json index 8c22db2fa4..d4ffab2901 100644 --- a/test/fixtures/typescript/type-alias/generic/expected.json +++ b/test/fixtures/typescript/type-alias/generic/expected.json @@ -88,23 +88,7 @@ "column": 8 } }, - "name": { - "type": "Identifier", - "start": 7, - "end": 8, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - }, - "identifierName": "U" - }, - "name": "U" - } + "name": "U" } ] }, diff --git a/test/fixtures/typescript/types/function-generic/expected.json b/test/fixtures/typescript/types/function-generic/expected.json index 9b848ba6d6..bc7d257bc2 100644 --- a/test/fixtures/typescript/types/function-generic/expected.json +++ b/test/fixtures/typescript/types/function-generic/expected.json @@ -130,23 +130,7 @@ "column": 9 } }, - "name": { - "type": "Identifier", - "start": 8, - "end": 9, - "loc": { - "start": { - "line": 1, - "column": 8 - }, - "end": { - "line": 1, - "column": 9 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, diff --git a/test/fixtures/typescript/types/mapped/expected.json b/test/fixtures/typescript/types/mapped/expected.json index 23a66c8df3..c8320ac82c 100644 --- a/test/fixtures/typescript/types/mapped/expected.json +++ b/test/fixtures/typescript/types/mapped/expected.json @@ -115,23 +115,7 @@ "column": 23 } }, - "name": { - "type": "Identifier", - "start": 12, - "end": 13, - "loc": { - "start": { - "line": 1, - "column": 12 - }, - "end": { - "line": 1, - "column": 13 - }, - "identifierName": "P" - }, - "name": "P" - }, + "name": "P", "constraint": { "type": "TSStringKeyword", "start": 17, @@ -259,23 +243,7 @@ "column": 32 } }, - "name": { - "type": "Identifier", - "start": 58, - "end": 59, - "loc": { - "start": { - "line": 2, - "column": 21 - }, - "end": { - "line": 2, - "column": 22 - }, - "identifierName": "P" - }, - "name": "P" - }, + "name": "P", "constraint": { "type": "TSStringKeyword", "start": 63, From 78df03fcf22450f02b47e53de4810d438715d263 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 18 May 2017 14:58:17 -0700 Subject: [PATCH 13/55] Use TypeParameterInstantiation nodes --- src/parser/expression.js | 2 +- src/plugins/flow.js | 4 +- src/plugins/typescript.js | 15 +- src/types.js | 13 +- .../expected.json | 228 +++++++++++------- .../class/expression-extends/expected.json | 114 +++++---- .../class/expression-implements/expected.json | 114 +++++---- .../class/extends-implements/expected.json | 114 +++++---- .../typescript/class/extends/expected.json | 57 +++-- .../typescript/class/implements/expected.json | 57 +++-- .../interface/extends/expected.json | 57 +++-- .../tsx/brace-is-block/expected.json | 57 +++-- .../type-alias/generic-complex/expected.json | 57 +++-- .../type-arguments/new/expected.json | 152 +++++++----- .../types/function-in-generic/expected.json | 59 +++-- .../reference-generic-nested/expected.json | 94 +++++--- .../types/reference-generic/expected.json | 45 ++-- 17 files changed, 777 insertions(+), 462 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index d949bbe5b2..5cf2833ea8 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -43,7 +43,7 @@ export default class ExpressionParser extends LValParser { +tsTypeArgumentsFromAmbiguous: (types: $ReadOnlyArray) => $ReadOnlyArray; +tsTypeParametersFromAmbiguous: ( types: $ReadOnlyArray) => N.TypeParameterDeclaration; - +tsParseTypeArguments: () => $ReadOnlyArray; + +tsParseTypeArguments: () => N.TypeParameterInstantiation; // Check if property name clashes with already added. // Object/class getters and setters are not allowed to clash — diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 9f052d2ae6..e89b3a73e5 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -372,7 +372,7 @@ export default (superClass: Class): Class => class extends super // Type annotations - flowParseTypeParameter(): N.FlowTypeParameter { + flowParseTypeParameter(): N.TypeParameter { const node = this.startNode(); const variance = this.flowParseVariance(); @@ -417,7 +417,7 @@ export default (superClass: Class): Class => class extends super return this.finishNode(node, "TypeParameterDeclaration"); } - flowParseTypeParameterInstantiation(): N.FlowTypeParameterInstantiation { + flowParseTypeParameterInstantiation(): N.TypeParameterInstantiation { const node = this.startNode(); const oldInType = this.state.inType; node.params = []; diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index c603844bd0..0667838c3e 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -201,12 +201,8 @@ export default (superClass: Class): Class => class extends super tsParseTypeReference(): N.TsTypeReference { const node: N.TsTypeReference = this.startNode(); node.typeName = this.tsParseEntityName(/* allowReservedWords */ false); - if (!this.hasPrecedingLineBreak() && this.eatRelational("<")) { - node.typeArguments = this.tsParseBracketedList( - "TypeParametersOrArguments", - this.tsParseType.bind(this), - /* bracket */ false, - /* skipFirstToken */ true); + if (!this.hasPrecedingLineBreak() && this.isRelational("<")) { + node.typeArguments = this.tsParseTypeArguments(); } return this.finishNode(node, "TSTypeReference"); } @@ -902,11 +898,12 @@ export default (superClass: Class): Class => class extends super } // Not in TypeScript's own parser. Called after seeing `new C<` in `parseNew`. - tsParseTypeArguments(): $ReadOnlyArray { + tsParseTypeArguments(): N.TypeParameterInstantiation { + const node = this.startNode(); this.expectRelational("<"); - const typeArguments = this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this)); + node.params = this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this)); this.expectRelational(">"); - return typeArguments; + return this.finishNode(node, "TypeParameterInstantiation"); } tsTypeArgumentsFromAmbiguous(types: $ReadOnlyArray): $ReadOnlyArray { diff --git a/src/types.js b/src/types.js index b4515cbc95..8c11df85da 100644 --- a/src/types.js +++ b/src/types.js @@ -564,7 +564,7 @@ export type ClassBase = HasDecorators & { // TODO: All not in spec typeParameters?: ?TypeParameterDeclaration; - superTypeParameters?: ?$ReadOnlyArray | FlowTypeParameterInstantiation; + superTypeParameters?: ?TypeParameterInstantiation; implements?: ?$ReadOnlyArray | $ReadOnlyArray; }; @@ -735,6 +735,11 @@ export type TypeParameter = NodeBase & { default?: TsType; }; +export type TypeParameterInstantiation = NodeBase & { + type: "TypeParameterInstantiation"; + params: $ReadOnlyArray | $ReadOnlyArray; +}; + // Flow (TODO: Not in spec) export type FlowType = Node; @@ -751,8 +756,6 @@ export type FlowDeclareInterface = Node; export type FlowInterface = Node; export type FlowInterfaceExtends = Node; export type FlowTypeAlias = Node; -export type FlowTypeParameter = Node; -export type FlowTypeParameterInstantiation = Node; export type FlowObjectTypeIndexer = Node; export type FlowFunctionTypeAnnotation = Node; export type FlowObjectTypeProperty = Node; @@ -932,7 +935,7 @@ export type TsConstructorType = TsTypeBase & TsSignatureDeclarationBase & { export type TsTypeReference = TsTypeBase & { type: "TSTypeReference"; typeName: TsEntityName; - typeArguments?: $ReadOnlyArray; // Should be non-empty if present. + typeArguments?: TypeParameterInstantiation; }; export type TsTypePredicate = TsTypeBase & { @@ -1027,7 +1030,7 @@ export type TSInterfaceBody = NodeBase & { export type TsExpressionWithTypeArguments = TsTypeBase & { type: "TSExpressionWithTypeArguments"; expression: TsEntityName; - typeArguments?: $ReadOnlyArray; // Must not be empty if present. + typeArguments?: TypeParameterInstantiation; }; export type TsTypeAliasDeclaration = DeclarationBase & { diff --git a/test/fixtures/typescript/class/expression-extends-implements/expected.json b/test/fixtures/typescript/class/expression-extends-implements/expected.json index a823c6c09f..8cd03c7f03 100644 --- a/test/fixtures/typescript/class/expression-extends-implements/expected.json +++ b/test/fixtures/typescript/class/expression-extends-implements/expected.json @@ -90,23 +90,23 @@ }, "arguments": [] }, - "superTypeParameters": [ - { - "type": "TSTypeReference", - "start": 19, - "end": 20, - "loc": { - "start": { - "line": 1, - "column": 19 - }, - "end": { - "line": 1, - "column": 20 - } + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 18, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 18 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 21 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 19, "end": 20, "loc": { @@ -117,13 +117,28 @@ "end": { "line": 1, "column": 20 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "implements": [ { "type": "TSExpressionWithTypeArguments", @@ -188,23 +203,23 @@ "name": "Y" } }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 37, - "end": 38, - "loc": { - "start": { - "line": 1, - "column": 37 - }, - "end": { - "line": 1, - "column": 38 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 36, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 36 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 39 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 37, "end": 38, "loc": { @@ -215,13 +230,28 @@ "end": { "line": 1, "column": 38 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 38 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ] + ] + } } ], "body": { @@ -324,23 +354,23 @@ }, "arguments": [] }, - "superTypeParameters": [ - { - "type": "TSTypeReference", - "start": 66, - "end": 67, - "loc": { - "start": { - "line": 2, - "column": 21 - }, - "end": { - "line": 2, - "column": 22 - } + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 65, + "end": 68, + "loc": { + "start": { + "line": 2, + "column": 20 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 2, + "column": 23 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 66, "end": 67, "loc": { @@ -351,13 +381,28 @@ "end": { "line": 2, "column": 22 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 66, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "implements": [ { "type": "TSExpressionWithTypeArguments", @@ -422,23 +467,23 @@ "name": "Y" } }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 84, - "end": 85, - "loc": { - "start": { - "line": 2, - "column": 39 - }, - "end": { - "line": 2, - "column": 40 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 83, + "end": 86, + "loc": { + "start": { + "line": 2, + "column": 38 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 2, + "column": 41 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 84, "end": 85, "loc": { @@ -449,13 +494,28 @@ "end": { "line": 2, "column": 40 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 84, + "end": 85, + "loc": { + "start": { + "line": 2, + "column": 39 + }, + "end": { + "line": 2, + "column": 40 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ] + ] + } } ], "body": { diff --git a/test/fixtures/typescript/class/expression-extends/expected.json b/test/fixtures/typescript/class/expression-extends/expected.json index a3f3d7a682..3e7c74ca7e 100644 --- a/test/fixtures/typescript/class/expression-extends/expected.json +++ b/test/fixtures/typescript/class/expression-extends/expected.json @@ -90,23 +90,23 @@ }, "arguments": [] }, - "superTypeParameters": [ - { - "type": "TSTypeReference", - "start": 19, - "end": 20, - "loc": { - "start": { - "line": 1, - "column": 19 - }, - "end": { - "line": 1, - "column": 20 - } + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 18, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 18 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 21 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 19, "end": 20, "loc": { @@ -117,13 +117,28 @@ "end": { "line": 1, "column": 20 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "body": { "type": "ClassBody", "start": 22, @@ -224,23 +239,23 @@ }, "arguments": [] }, - "superTypeParameters": [ - { - "type": "TSTypeReference", - "start": 48, - "end": 49, - "loc": { - "start": { - "line": 2, - "column": 21 - }, - "end": { - "line": 2, - "column": 22 - } + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 47, + "end": 50, + "loc": { + "start": { + "line": 2, + "column": 20 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 2, + "column": 23 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 48, "end": 49, "loc": { @@ -251,13 +266,28 @@ "end": { "line": 2, "column": 22 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 48, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 21 + }, + "end": { + "line": 2, + "column": 22 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "body": { "type": "ClassBody", "start": 51, diff --git a/test/fixtures/typescript/class/expression-implements/expected.json b/test/fixtures/typescript/class/expression-implements/expected.json index 0091779db2..ac2ce74175 100644 --- a/test/fixtures/typescript/class/expression-implements/expected.json +++ b/test/fixtures/typescript/class/expression-implements/expected.json @@ -121,23 +121,23 @@ "name": "Y" } }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 22, - "end": 23, - "loc": { - "start": { - "line": 1, - "column": 22 - }, - "end": { - "line": 1, - "column": 23 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 21 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 24 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 22, "end": 23, "loc": { @@ -148,13 +148,28 @@ "end": { "line": 1, "column": 23 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ] + ] + } } ], "body": { @@ -289,23 +304,23 @@ "name": "Y" } }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 54, - "end": 55, - "loc": { - "start": { - "line": 2, - "column": 24 - }, - "end": { - "line": 2, - "column": 25 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 53, + "end": 56, + "loc": { + "start": { + "line": 2, + "column": 23 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 2, + "column": 26 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 54, "end": 55, "loc": { @@ -316,13 +331,28 @@ "end": { "line": 2, "column": 25 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 25 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ] + ] + } } ], "body": { diff --git a/test/fixtures/typescript/class/extends-implements/expected.json b/test/fixtures/typescript/class/extends-implements/expected.json index 4580b1bf71..74fafc58dd 100644 --- a/test/fixtures/typescript/class/extends-implements/expected.json +++ b/test/fixtures/typescript/class/extends-implements/expected.json @@ -92,23 +92,23 @@ }, "arguments": [] }, - "superTypeParameters": [ - { - "type": "TSTypeReference", - "start": 20, - "end": 21, - "loc": { - "start": { - "line": 1, - "column": 20 - }, - "end": { - "line": 1, - "column": 21 - } + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 22 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 20, "end": 21, "loc": { @@ -119,13 +119,28 @@ "end": { "line": 1, "column": 21 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "implements": [ { "type": "TSExpressionWithTypeArguments", @@ -190,23 +205,23 @@ "name": "Y" } }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 38, - "end": 39, - "loc": { - "start": { - "line": 1, - "column": 38 - }, - "end": { - "line": 1, - "column": 39 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 37, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 37 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 40 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 38, "end": 39, "loc": { @@ -217,13 +232,28 @@ "end": { "line": 1, "column": 39 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 39 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ] + ] + } } ], "body": { diff --git a/test/fixtures/typescript/class/extends/expected.json b/test/fixtures/typescript/class/extends/expected.json index 79dc2619b5..d83c52adea 100644 --- a/test/fixtures/typescript/class/extends/expected.json +++ b/test/fixtures/typescript/class/extends/expected.json @@ -92,23 +92,23 @@ }, "arguments": [] }, - "superTypeParameters": [ - { - "type": "TSTypeReference", - "start": 20, - "end": 21, - "loc": { - "start": { - "line": 1, - "column": 20 - }, - "end": { - "line": 1, - "column": 21 - } + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 22 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 20, "end": 21, "loc": { @@ -119,13 +119,28 @@ "end": { "line": 1, "column": 21 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "body": { "type": "ClassBody", "start": 23, diff --git a/test/fixtures/typescript/class/implements/expected.json b/test/fixtures/typescript/class/implements/expected.json index e3fd8b4f71..326a741986 100644 --- a/test/fixtures/typescript/class/implements/expected.json +++ b/test/fixtures/typescript/class/implements/expected.json @@ -124,23 +124,23 @@ "name": "Y" } }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 23, - "end": 24, - "loc": { - "start": { - "line": 1, - "column": 23 - }, - "end": { - "line": 1, - "column": 24 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 22, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 22 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 25 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 23, "end": 24, "loc": { @@ -151,13 +151,28 @@ "end": { "line": 1, "column": 24 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ] + ] + } } ], "body": { diff --git a/test/fixtures/typescript/interface/extends/expected.json b/test/fixtures/typescript/interface/extends/expected.json index e8f46e4313..60b3d84728 100644 --- a/test/fixtures/typescript/interface/extends/expected.json +++ b/test/fixtures/typescript/interface/extends/expected.json @@ -123,23 +123,23 @@ "name": "Y" } }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 24, - "end": 25, - "loc": { - "start": { - "line": 1, - "column": 24 - }, - "end": { - "line": 1, - "column": 25 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 23, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 23 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 26 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 24, "end": 25, "loc": { @@ -150,13 +150,28 @@ "end": { "line": 1, "column": 25 - }, - "identifierName": "Z" + } }, - "name": "Z" + "typeName": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + }, + "identifierName": "Z" + }, + "name": "Z" + } } - } - ] + ] + } } ], "body": { diff --git a/test/fixtures/typescript/tsx/brace-is-block/expected.json b/test/fixtures/typescript/tsx/brace-is-block/expected.json index a6ecf227da..a264091a97 100644 --- a/test/fixtures/typescript/tsx/brace-is-block/expected.json +++ b/test/fixtures/typescript/tsx/brace-is-block/expected.json @@ -77,23 +77,23 @@ }, "name": "D" }, - "superTypeParameters": [ - { - "type": "TSTypeReference", - "start": 117, - "end": 118, - "loc": { - "start": { - "line": 2, - "column": 18 - }, - "end": { - "line": 2, - "column": 19 - } + "superTypeParameters": { + "type": "TypeParameterInstantiation", + "start": 116, + "end": 119, + "loc": { + "start": { + "line": 2, + "column": 17 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 2, + "column": 20 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 117, "end": 118, "loc": { @@ -104,13 +104,28 @@ "end": { "line": 2, "column": 19 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 117, + "end": 118, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 19 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "body": { "type": "ClassBody", "start": 120, diff --git a/test/fixtures/typescript/type-alias/generic-complex/expected.json b/test/fixtures/typescript/type-alias/generic-complex/expected.json index 9c7b9d4cd2..2a8adea5cf 100644 --- a/test/fixtures/typescript/type-alias/generic-complex/expected.json +++ b/test/fixtures/typescript/type-alias/generic-complex/expected.json @@ -218,23 +218,23 @@ }, "name": "Array" }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 49, - "end": 50, - "loc": { - "start": { - "line": 1, - "column": 49 - }, - "end": { - "line": 1, - "column": 50 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 48, + "end": 51, + "loc": { + "start": { + "line": 1, + "column": 48 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 51 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 49, "end": 50, "loc": { @@ -245,13 +245,28 @@ "end": { "line": 1, "column": 50 - }, - "identifierName": "U" + } }, - "name": "U" + "typeName": { + "type": "Identifier", + "start": 49, + "end": 50, + "loc": { + "start": { + "line": 1, + "column": 49 + }, + "end": { + "line": 1, + "column": 50 + }, + "identifierName": "U" + }, + "name": "U" + } } - } - ] + ] + } } } ], diff --git a/test/fixtures/typescript/type-arguments/new/expected.json b/test/fixtures/typescript/type-arguments/new/expected.json index 33908a10b6..58d88e4707 100644 --- a/test/fixtures/typescript/type-arguments/new/expected.json +++ b/test/fixtures/typescript/type-arguments/new/expected.json @@ -73,23 +73,23 @@ }, "name": "C" }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 6, - "end": 7, - "loc": { - "start": { - "line": 1, - "column": 6 - }, - "end": { - "line": 1, - "column": 7 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 5, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 5 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 8 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 6, "end": 7, "loc": { @@ -100,13 +100,28 @@ "end": { "line": 1, "column": 7 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ], + ] + }, "arguments": [] } }, @@ -155,23 +170,23 @@ }, "name": "C" }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 18, - "end": 19, - "loc": { - "start": { - "line": 2, - "column": 6 - }, - "end": { - "line": 2, - "column": 7 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 5 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 2, + "column": 11 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 18, "end": 19, "loc": { @@ -182,28 +197,28 @@ "end": { "line": 2, "column": 7 - }, - "identifierName": "T" + } }, - "name": "T" - } - }, - { - "type": "TSTypeReference", - "start": 21, - "end": 22, - "loc": { - "start": { - "line": 2, - "column": 9 - }, - "end": { - "line": 2, - "column": 10 + "typeName": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" } }, - "typeName": { - "type": "Identifier", + { + "type": "TSTypeReference", "start": 21, "end": 22, "loc": { @@ -214,13 +229,28 @@ "end": { "line": 2, "column": 10 - }, - "identifierName": "U" + } }, - "name": "U" + "typeName": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "U" + }, + "name": "U" + } } - } - ], + ] + }, "arguments": [] } } diff --git a/test/fixtures/typescript/types/function-in-generic/expected.json b/test/fixtures/typescript/types/function-in-generic/expected.json index dfc3d8a385..c4c2571134 100644 --- a/test/fixtures/typescript/types/function-in-generic/expected.json +++ b/test/fixtures/typescript/types/function-in-generic/expected.json @@ -118,38 +118,38 @@ }, "name": "Array" }, - "typeArguments": [ - { - "type": "TSFunctionType", - "start": 13, - "end": 23, - "loc": { - "start": { - "line": 1, - "column": 13 - }, - "end": { - "line": 1, - "column": 23 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 12, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "parameters": [], - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 19, + "end": { + "line": 1, + "column": 24 + } + }, + "params": [ + { + "type": "TSFunctionType", + "start": 13, "end": 23, "loc": { "start": { "line": 1, - "column": 19 + "column": 13 }, "end": { "line": 1, "column": 23 } }, + "parameters": [], "typeAnnotation": { - "type": "TSVoidKeyword", + "type": "TypeAnnotation", "start": 19, "end": 23, "loc": { @@ -161,11 +161,26 @@ "line": 1, "column": 23 } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 19, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 23 + } + } } } } - } - ] + ] + } } } }, diff --git a/test/fixtures/typescript/types/reference-generic-nested/expected.json b/test/fixtures/typescript/types/reference-generic-nested/expected.json index f3adcc0345..33ab435e17 100644 --- a/test/fixtures/typescript/types/reference-generic-nested/expected.json +++ b/test/fixtures/typescript/types/reference-generic-nested/expected.json @@ -118,25 +118,25 @@ }, "name": "Array" }, - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 13, - "end": 26, - "loc": { - "start": { - "line": 1, - "column": 13 - }, - "end": { - "line": 1, - "column": 26 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 12, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 12 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 27 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 13, - "end": 18, + "end": 26, "loc": { "start": { "line": 1, @@ -144,31 +144,61 @@ }, "end": { "line": 1, - "column": 18 + "column": 26 + } + }, + "typeName": { + "type": "Identifier", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "Array" }, - "identifierName": "Array" + "name": "Array" }, - "name": "Array" - }, - "typeArguments": [ - { - "type": "TSNumberKeyword", - "start": 19, - "end": 25, + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 18, + "end": 26, "loc": { "start": { "line": 1, - "column": 19 + "column": 18 }, "end": { "line": 1, - "column": 25 + "column": 26 } - } + }, + "params": [ + { + "type": "TSNumberKeyword", + "start": 19, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 25 + } + } + } + ] } - ] - } - ] + } + ] + } } } }, diff --git a/test/fixtures/typescript/types/reference-generic/expected.json b/test/fixtures/typescript/types/reference-generic/expected.json index 4d9f94f5d7..cd67c76858 100644 --- a/test/fixtures/typescript/types/reference-generic/expected.json +++ b/test/fixtures/typescript/types/reference-generic/expected.json @@ -118,23 +118,38 @@ }, "name": "Array" }, - "typeArguments": [ - { - "type": "TSNumberKeyword", - "start": 13, - "end": 19, - "loc": { - "start": { - "line": 1, - "column": 13 - }, - "end": { - "line": 1, - "column": 19 + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 12, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 20 + } + }, + "params": [ + { + "type": "TSNumberKeyword", + "start": 13, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 19 + } } } - } - ] + ] + } } } }, From b26c1bcd77bc6368b41247c9b04b897364058e7d Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 18 May 2017 15:16:46 -0700 Subject: [PATCH 14/55] Fix bug where TypeParameter.name was an Identifier instead of a string --- src/plugins/typescript.js | 2 +- src/types.js | 28 +++++++++---------- .../async-generic/expected.json | 18 +----------- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 0667838c3e..47acdfbf70 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -930,7 +930,7 @@ export default (superClass: Class): Class => class extends super const typeParam = this.nodeWithSamePosition(typeAmbiguous, "TypeParameter"); const { typeAnnotation: type, constraint, default: defaultType } = typeAmbiguous; if (type.type === "TSTypeReference" && !type.typeArguments && type.typeName.type === "Identifier") { - typeParam.name = type.typeName; + typeParam.name = type.typeName.name; } else { throw this.raise(type.start, "Type parameter must be a plain identifier."); } diff --git a/src/types.js b/src/types.js index 8c11df85da..1e5becba6a 100644 --- a/src/types.js +++ b/src/types.js @@ -288,15 +288,6 @@ export type FunctionDeclaration = OptFunctionDeclaration & { id: Identifier; } -//test with `export default function(): void; -export type OptTSDeclareFunction = BodilessFunctionBase & DeclarationBase & { - type: "TSDeclareFunction"; -} - -export type TSDeclareFunction = OptTSDeclareFunction & { - id: Identifier; -}; - export type VariableDeclaration = DeclarationBase & HasDecorators & { type: "VariableDeclaration"; declarations: $ReadOnlyArray; @@ -592,11 +583,6 @@ export type MethodBase = FunctionBase & { export type MethodKind = "constructor" | "method" | "get" | "set"; -export type TSDeclareMethod = BodilessFunctionBase & ClassMemberBase & { - type: "TSDeclareMethod"; - +kind: MethodKind; -}; - export type ClassMethod = MethodBase & ClassMemberBase & { type: "ClassMethod"; key: Expression; @@ -789,6 +775,20 @@ export type FlowClassImplements = Node; // Misc // ================ +//test with `export default function(): void; +export type OptTSDeclareFunction = BodilessFunctionBase & DeclarationBase & { + type: "TSDeclareFunction"; +} + +export type TSDeclareFunction = OptTSDeclareFunction & { + id: Identifier; +}; + +export type TSDeclareMethod = BodilessFunctionBase & ClassMemberBase & { + type: "TSDeclareMethod"; + +kind: MethodKind; +}; + export type TsQualifiedName = NodeBase & { type: "TSQualifiedName"; left: TsEntityName; diff --git a/test/fixtures/typescript/arrow-function/async-generic/expected.json b/test/fixtures/typescript/arrow-function/async-generic/expected.json index 6e8f34ba2d..47363b0d45 100644 --- a/test/fixtures/typescript/arrow-function/async-generic/expected.json +++ b/test/fixtures/typescript/arrow-function/async-generic/expected.json @@ -132,23 +132,7 @@ "column": 8 } }, - "name": { - "type": "Identifier", - "start": 7, - "end": 8, - "loc": { - "start": { - "line": 1, - "column": 7 - }, - "end": { - "line": 1, - "column": 8 - }, - "identifierName": "T" - }, - "name": "T" - } + "name": "T" } ] }, From a6055594a5ae4cd7072c7a90814d4a7f754e5a12 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 18 May 2017 15:30:31 -0700 Subject: [PATCH 15/55] Use TypeParameterInstantiation for call/new type arguments --- src/parser/expression.js | 5 +- src/plugins/typescript.js | 6 +- src/types.js | 2 +- .../type-arguments/call/expected.json | 152 +++++++++++------- 4 files changed, 98 insertions(+), 67 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 5cf2833ea8..cb72e15ca3 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -40,9 +40,8 @@ export default class ExpressionParser extends LValParser { base: N.Expression, startPos: number, startLoc: Position) => { newBase: N.Expression, shouldContinue: boolean }; - +tsTypeArgumentsFromAmbiguous: (types: $ReadOnlyArray) => $ReadOnlyArray; - +tsTypeParametersFromAmbiguous: ( - types: $ReadOnlyArray) => N.TypeParameterDeclaration; + +tsTypeArgumentsFromAmbiguous: (types: $ReadOnlyArray) => N.TypeParameterInstantiation; + +tsTypeParametersFromAmbiguous: (types: $ReadOnlyArray) => N.TypeParameterDeclaration; +tsParseTypeArguments: () => N.TypeParameterInstantiation; // Check if property name clashes with already added. diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 47acdfbf70..35d1c6c701 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -906,8 +906,9 @@ export default (superClass: Class): Class => class extends super return this.finishNode(node, "TypeParameterInstantiation"); } - tsTypeArgumentsFromAmbiguous(types: $ReadOnlyArray): $ReadOnlyArray { - return types.map(({ constraint, default: defaultType, typeAnnotation: type, start, end, loc }) => { + tsTypeArgumentsFromAmbiguous(types: $ReadOnlyArray): N.TypeParameterInstantiation { + const node: N.TypeParameterInstantiation = this.startNode(); + node.params = types.map(({ constraint, default: defaultType, typeAnnotation: type, start, end, loc }) => { if (constraint || defaultType) { throw this.raise(start, "Type argument may not have a constraint or default."); } @@ -920,6 +921,7 @@ export default (superClass: Class): Class => class extends super return type; }); + return this.finishNode(node, "TypeParameterInstantiation"); } tsTypeParametersFromAmbiguous( diff --git a/src/types.js b/src/types.js index 1e5becba6a..10415c17c0 100644 --- a/src/types.js +++ b/src/types.js @@ -464,7 +464,7 @@ export type ConditionalExpression = NodeBase & { export type CallOrNewBase = NodeBase & { callee: Expression | Super | Import; arguments: Array; // TODO: $ReadOnlyArray - typeArguments?: ?$ReadOnlyArray; // TODO: Not in spec + typeArguments?: ?TypeParameterInstantiation; // TODO: Not in spec }; export type CallExpression = CallOrNewBase & { diff --git a/test/fixtures/typescript/type-arguments/call/expected.json b/test/fixtures/typescript/type-arguments/call/expected.json index 1ea668f381..00aa437e7e 100644 --- a/test/fixtures/typescript/type-arguments/call/expected.json +++ b/test/fixtures/typescript/type-arguments/call/expected.json @@ -74,23 +74,23 @@ "name": "f" }, "arguments": [], - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 2, - "end": 3, - "loc": { - "start": { - "line": 1, - "column": 2 - }, - "end": { - "line": 1, - "column": 3 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 6, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 6 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 1, + "column": 6 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 2, "end": 3, "loc": { @@ -101,13 +101,28 @@ "end": { "line": 1, "column": 3 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 2, + "end": 3, + "loc": { + "start": { + "line": 1, + "column": 2 + }, + "end": { + "line": 1, + "column": 3 + }, + "identifierName": "T" + }, + "name": "T" + } } - } - ] + ] + } } }, { @@ -156,23 +171,23 @@ "name": "f" }, "arguments": [], - "typeArguments": [ - { - "type": "TSTypeReference", - "start": 10, - "end": 11, - "loc": { - "start": { - "line": 2, - "column": 2 - }, - "end": { - "line": 2, - "column": 3 - } + "typeArguments": { + "type": "TypeParameterInstantiation", + "start": 17, + "end": 17, + "loc": { + "start": { + "line": 2, + "column": 9 }, - "typeName": { - "type": "Identifier", + "end": { + "line": 2, + "column": 9 + } + }, + "params": [ + { + "type": "TSTypeReference", "start": 10, "end": 11, "loc": { @@ -183,28 +198,28 @@ "end": { "line": 2, "column": 3 - }, - "identifierName": "T" - }, - "name": "T" - } - }, - { - "type": "TSTypeReference", - "start": 13, - "end": 14, - "loc": { - "start": { - "line": 2, - "column": 5 + } }, - "end": { - "line": 2, - "column": 6 + "typeName": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 3 + }, + "identifierName": "T" + }, + "name": "T" } }, - "typeName": { - "type": "Identifier", + { + "type": "TSTypeReference", "start": 13, "end": 14, "loc": { @@ -215,13 +230,28 @@ "end": { "line": 2, "column": 6 - }, - "identifierName": "U" + } }, - "name": "U" + "typeName": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "U" + }, + "name": "U" + } } - } - ] + ] + } } } ], From 25f8779440d1277651dfe570736b5c3353718ed7 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 19 May 2017 09:59:46 -0700 Subject: [PATCH 16/55] Fix bug: arrow function parameter should have TypeAnnotation node --- src/plugins/typescript.js | 7 +++-- src/types.js | 6 +++++ .../arrow-function/annotated/expected.json | 17 +++++++++++- .../async-generic/expected.json | 27 ++++++++++++++----- .../arrow-function/async/expected.json | 17 +++++++++++- .../default-parameter-values/expected.json | 17 +++++++++++- .../arrow-function/generic-tsx/expected.json | 27 ++++++++++++++----- .../arrow-function/generic/expected.json | 27 ++++++++++++++----- .../optional-parameter/expected.json | 17 +++++++++++- .../predicate-types/expected.json | 17 +++++++++++- 10 files changed, 152 insertions(+), 27 deletions(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 35d1c6c701..049e335177 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -55,7 +55,6 @@ function keywordTypeFromName(value: string): N.TsKeywordTypeType | typeof undefi } export default (superClass: Class): Class => class extends superClass { - tsIsIdentifier(): boolean { // TODO: actually a bit more complex in TypeScript, but shouldn't matter. // See https://github.com/Microsoft/TypeScript/issues/15008 @@ -1172,9 +1171,9 @@ export default (superClass: Class): Class => class extends super } if (this.eat(tt.colon)) { - const typeCastNode = this.startNodeAt(startPos, startLoc); + const typeCastNode: N.TypeCastExpression = this.startNodeAt(startPos, startLoc); typeCastNode.expression = node; - typeCastNode.typeAnnotation = this.tsParseType(); + typeCastNode.typeAnnotation = this.tsParseTypeAnnotation(); return this.finishNode(typeCastNode, "TypeCastExpression"); } @@ -1454,7 +1453,7 @@ export default (superClass: Class): Class => class extends super return super.toAssignableList(exprList, isBinding, contextDescription); } - typeCastToParameter(node: N.Node): N.Node { + typeCastToParameter(node: N.TypeCastExpression): N.Node { node.expression.typeAnnotation = node.typeAnnotation; return this.finishNodeAt( diff --git a/src/types.js b/src/types.js index 10415c17c0..fdf9877d36 100644 --- a/src/types.js +++ b/src/types.js @@ -728,6 +728,12 @@ export type TypeParameterInstantiation = NodeBase & { // Flow (TODO: Not in spec) +export type TypeCastExpression = NodeBase & { + type: "TypeCastExpression"; + expression: Expression; + typeAnnotation: TypeAnnotation; +}; + export type FlowType = Node; export type FlowPredicate = Node; export type FlowDeclare = Node; diff --git a/test/fixtures/typescript/arrow-function/annotated/expected.json b/test/fixtures/typescript/arrow-function/annotated/expected.json index dadac86803..c920cf7d4b 100644 --- a/test/fixtures/typescript/arrow-function/annotated/expected.json +++ b/test/fixtures/typescript/arrow-function/annotated/expected.json @@ -108,7 +108,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 4, "end": 10, "loc": { @@ -120,6 +120,21 @@ "line": 1, "column": 10 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + } + } } } } diff --git a/test/fixtures/typescript/arrow-function/async-generic/expected.json b/test/fixtures/typescript/arrow-function/async-generic/expected.json index 47363b0d45..f47439f8f5 100644 --- a/test/fixtures/typescript/arrow-function/async-generic/expected.json +++ b/test/fixtures/typescript/arrow-function/async-generic/expected.json @@ -158,7 +158,7 @@ }, "name": "a", "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 13, "end": 14, "loc": { @@ -171,8 +171,8 @@ "column": 14 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 13, "end": 14, "loc": { @@ -183,10 +183,25 @@ "end": { "line": 1, "column": 14 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "T" + }, + "name": "T" + } } } } diff --git a/test/fixtures/typescript/arrow-function/async/expected.json b/test/fixtures/typescript/arrow-function/async/expected.json index 7489457b21..6849329ebe 100644 --- a/test/fixtures/typescript/arrow-function/async/expected.json +++ b/test/fixtures/typescript/arrow-function/async/expected.json @@ -109,7 +109,7 @@ "name": "x", "optional": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 11, "end": 17, "loc": { @@ -121,6 +121,21 @@ "line": 1, "column": 17 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 11, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 17 + } + } } } } diff --git a/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json b/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json index f822bebee4..6cd0894a4f 100644 --- a/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json +++ b/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json @@ -92,7 +92,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 4, "end": 10, "loc": { @@ -104,6 +104,21 @@ "line": 1, "column": 10 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 4, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 10 + } + } } } }, diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json index d49759277a..0437396c5b 100644 --- a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json +++ b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json @@ -125,7 +125,7 @@ }, "name": "a", "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 68, "end": 69, "loc": { @@ -138,8 +138,8 @@ "column": 8 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 68, "end": 69, "loc": { @@ -150,10 +150,25 @@ "end": { "line": 2, "column": 8 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 68, + "end": 69, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } } } } diff --git a/test/fixtures/typescript/arrow-function/generic/expected.json b/test/fixtures/typescript/arrow-function/generic/expected.json index 735860986c..9c4b47a9ab 100644 --- a/test/fixtures/typescript/arrow-function/generic/expected.json +++ b/test/fixtures/typescript/arrow-function/generic/expected.json @@ -125,7 +125,7 @@ }, "name": "a", "typeAnnotation": { - "type": "TSTypeReference", + "type": "TypeAnnotation", "start": 7, "end": 8, "loc": { @@ -138,8 +138,8 @@ "column": 8 } }, - "typeName": { - "type": "Identifier", + "typeAnnotation": { + "type": "TSTypeReference", "start": 7, "end": 8, "loc": { @@ -150,10 +150,25 @@ "end": { "line": 1, "column": 8 - }, - "identifierName": "T" + } }, - "name": "T" + "typeName": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "T" + }, + "name": "T" + } } } } diff --git a/test/fixtures/typescript/arrow-function/optional-parameter/expected.json b/test/fixtures/typescript/arrow-function/optional-parameter/expected.json index e349558e51..3da2ac662a 100644 --- a/test/fixtures/typescript/arrow-function/optional-parameter/expected.json +++ b/test/fixtures/typescript/arrow-function/optional-parameter/expected.json @@ -109,7 +109,7 @@ "name": "x", "optional": true, "typeAnnotation": { - "type": "TSNumberKeyword", + "type": "TypeAnnotation", "start": 5, "end": 11, "loc": { @@ -121,6 +121,21 @@ "line": 1, "column": 11 } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 5, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 11 + } + } } } } diff --git a/test/fixtures/typescript/arrow-function/predicate-types/expected.json b/test/fixtures/typescript/arrow-function/predicate-types/expected.json index 5f0b36a4d5..f47452b316 100644 --- a/test/fixtures/typescript/arrow-function/predicate-types/expected.json +++ b/test/fixtures/typescript/arrow-function/predicate-types/expected.json @@ -140,7 +140,7 @@ }, "name": "x", "typeAnnotation": { - "type": "TSAnyKeyword", + "type": "TypeAnnotation", "start": 4, "end": 7, "loc": { @@ -152,6 +152,21 @@ "line": 1, "column": 7 } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 4, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + } } } } From 30a9e47c52b18784f9efafd1e340ba0925e6b1a9 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 19 May 2017 13:38:00 -0700 Subject: [PATCH 17/55] Add TSParameterProperty --- src/parser/lval.js | 25 +- src/parser/node.js | 5 + src/plugins/typescript.js | 5 - src/types.js | 16 +- .../class/parameter-properties/actual.js | 8 +- .../class/parameter-properties/expected.json | 421 ++++++++++++------ 6 files changed, 333 insertions(+), 147 deletions(-) diff --git a/src/parser/lval.js b/src/parser/lval.js index 5b9c029690..86d8b0aacd 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -1,8 +1,8 @@ // @flow import { types as tt, type TokenType } from "../tokenizer/types"; -import type { Accessibility, Decorator, Expression, Identifier, Node, ObjectExpression, ObjectPattern, - Pattern, RestElement, SpreadElement } from "../types"; +import type { TSParameterProperty, Accessibility, Decorator, Expression, Identifier, Node, ObjectExpression, + ObjectPattern, Pattern, RestElement, SpreadElement } from "../types"; import type { Pos, Position } from "../util/location"; import { NodeUtils } from "./node"; @@ -29,6 +29,7 @@ export default class LValParser extends NodeUtils { case "ObjectPattern": case "ArrayPattern": case "AssignmentPattern": + case "TSParameterProperty": break; case "ObjectExpression": @@ -176,8 +177,8 @@ export default class LValParser extends NodeUtils { close: TokenType, allowEmpty?: boolean, allowModifiers?: boolean - ): $ReadOnlyArray { - const elts = []; + ): $ReadOnlyArray { + const elts: Array = []; let first = true; while (!this.eat(close)) { if (first) { @@ -206,9 +207,15 @@ export default class LValParser extends NodeUtils { } this.parseAssignableListItemTypes(left); const elt = this.parseMaybeDefault(left.start, left.loc.start, left); - if (accessibility) elt.accessibility = accessibility; - if (readonly) elt.readonly = readonly; - elts.push(elt); + if (accessibility || readonly) { + const pp: TSParameterProperty = this.startNodeAtNode(elt); + if (accessibility) pp.accessibility = accessibility; + if (readonly) pp.readonly = readonly; + pp.parameter = elt; + elts.push(this.finishNode(pp, "TSParameterProperty")); + } else { + elts.push(elt); + } } } return elts; @@ -297,6 +304,10 @@ export default class LValParser extends NodeUtils { this.checkLVal(expr.argument, isBinding, checkClashes, "rest element"); break; + case "TSParameterProperty": + this.checkLVal(expr.parameter, isBinding, checkClashes, "parameter property"); + break; + default: { const message = (isBinding ? /* istanbul ignore next */ "Binding invalid" : "Invalid") + " left-hand side" + diff --git a/src/parser/node.js b/src/parser/node.js index cc8f514366..586322e313 100644 --- a/src/parser/node.js +++ b/src/parser/node.js @@ -55,6 +55,11 @@ export class NodeUtils extends UtilParser { return new Node(this, pos, loc); } + /** Start a new node with a previous node's location. */ + startNodeAtNode(type: NodeType): T { + return this.startNodeAt(type.start, type.loc.start); + } + // Finish an AST node, adding `type` and `end` properties. finishNode(node: T, type: string): T { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 049e335177..2fa045d144 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -861,11 +861,6 @@ export default (superClass: Class): Class => class extends super // Utilities - /** Start a new node with a previous node's location. */ - startNodeAtNode(type: N.Node): T { - return this.startNodeAt(type.start, type.loc.start); - } - tsLookAhead(f: () => T): T { const state = this.state.clone(); const res = f(); diff --git a/src/types.js b/src/types.js index fdf9877d36..596f325cc0 100644 --- a/src/types.js +++ b/src/types.js @@ -128,7 +128,7 @@ export type NormalFunction = //TODO: name export type BodilessFunctionBase = HasDecorators & { id: ?Identifier; - params: $ReadOnlyArray; + params: $ReadOnlyArray; body: BlockStatement; generator: boolean; async: boolean; @@ -514,8 +514,6 @@ export type PatternBase = HasDecorators & { typeAnnotation?: ?TypeAnnotation; // TypeScript only: optional?: ?true; - accessibility?: ?Accessibility; // For parameter properties - readonly?: ?true; }; export type AssignmentProperty = ObjectProperty & { @@ -781,6 +779,14 @@ export type FlowClassImplements = Node; // Misc // ================ +export type TSParameterProperty = NodeBase & { + type: "TSParameterProperty"; + // At least one of `accessibility` or `readonly` must be set. + accessibility?: ?Accessibility; + readonly?: ?true; + parameter: Pattern; +} + //test with `export default function(): void; export type OptTSDeclareFunction = BodilessFunctionBase & DeclarationBase & { type: "TSDeclareFunction"; @@ -930,12 +936,12 @@ export type TsFunctionOrConstructorType = TsFunctionType | TsConstructorType; export type TsFunctionType = TsTypeBase & TsSignatureDeclarationBase & { type: "TSFunctionType"; - typeAnnotation: TsType; // not optional + typeAnnotation: TypeAnnotation; // not optional }; export type TsConstructorType = TsTypeBase & TsSignatureDeclarationBase & { type: "TSConstructorType"; - typeAnnotation: TsType; + typeAnnotation: TypeAnnotation; }; export type TsTypeReference = TsTypeBase & { diff --git a/test/fixtures/typescript/class/parameter-properties/actual.js b/test/fixtures/typescript/class/parameter-properties/actual.js index 846c2f1c6a..c9d6ec5927 100644 --- a/test/fixtures/typescript/class/parameter-properties/actual.js +++ b/test/fixtures/typescript/class/parameter-properties/actual.js @@ -1,11 +1,11 @@ class C { constructor( readonly r, - public pu, - protected po, - private pi, + public pu: number, + protected po?, + private pi?: number, public readonly pur, // Also works on AssignmentPattern readonly x = 0, - public y: number = 0) {} + public y?: number = 0) {} } diff --git a/test/fixtures/typescript/class/parameter-properties/expected.json b/test/fixtures/typescript/class/parameter-properties/expected.json index 0368e0449b..ae7a28cf75 100644 --- a/test/fixtures/typescript/class/parameter-properties/expected.json +++ b/test/fixtures/typescript/class/parameter-properties/expected.json @@ -1,7 +1,7 @@ { "type": "File", "start": 0, - "end": 238, + "end": 257, "loc": { "start": { "line": 1, @@ -15,7 +15,7 @@ "program": { "type": "Program", "start": 0, - "end": 238, + "end": 257, "loc": { "start": { "line": 1, @@ -31,7 +31,7 @@ { "type": "ClassDeclaration", "start": 0, - "end": 238, + "end": 257, "loc": { "start": { "line": 1, @@ -63,7 +63,7 @@ "body": { "type": "ClassBody", "start": 8, - "end": 238, + "end": 257, "loc": { "start": { "line": 1, @@ -78,7 +78,7 @@ { "type": "ClassMethod", "start": 14, - "end": 236, + "end": 255, "loc": { "start": { "line": 2, @@ -86,7 +86,7 @@ }, "end": { "line": 10, - "column": 32 + "column": 33 } }, "static": false, @@ -115,7 +115,7 @@ "async": false, "params": [ { - "type": "Identifier", + "type": "TSParameterProperty", "start": 44, "end": 45, "loc": { @@ -126,16 +126,31 @@ "end": { "line": 3, "column": 18 - }, - "identifierName": "r" + } }, - "name": "r", - "readonly": true + "readonly": true, + "parameter": { + "type": "Identifier", + "start": 44, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 18 + }, + "identifierName": "r" + }, + "name": "r" + } }, { - "type": "Identifier", + "type": "TSParameterProperty", "start": 62, - "end": 64, + "end": 72, "loc": { "start": { "line": 4, @@ -143,17 +158,62 @@ }, "end": { "line": 4, - "column": 17 - }, - "identifierName": "pu" + "column": 25 + } }, - "name": "pu", - "accessibility": "public" + "accessibility": "public", + "parameter": { + "type": "Identifier", + "start": 62, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 15 + }, + "end": { + "line": 4, + "column": 25 + }, + "identifierName": "pu" + }, + "name": "pu", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 66, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 66, + "end": 72, + "loc": { + "start": { + "line": 4, + "column": 19 + }, + "end": { + "line": 4, + "column": 25 + } + } + } + } + } }, { - "type": "Identifier", - "start": 84, - "end": 86, + "type": "TSParameterProperty", + "start": 92, + "end": 95, "loc": { "start": { "line": 5, @@ -161,17 +221,33 @@ }, "end": { "line": 5, - "column": 20 - }, - "identifierName": "po" + "column": 21 + } }, - "name": "po", - "accessibility": "protected" + "accessibility": "protected", + "parameter": { + "type": "Identifier", + "start": 92, + "end": 95, + "loc": { + "start": { + "line": 5, + "column": 18 + }, + "end": { + "line": 5, + "column": 21 + }, + "identifierName": "po" + }, + "name": "po", + "optional": true + } }, { - "type": "Identifier", - "start": 104, - "end": 106, + "type": "TSParameterProperty", + "start": 113, + "end": 124, "loc": { "start": { "line": 6, @@ -179,17 +255,63 @@ }, "end": { "line": 6, - "column": 18 - }, - "identifierName": "pi" + "column": 27 + } }, - "name": "pi", - "accessibility": "private" + "accessibility": "private", + "parameter": { + "type": "Identifier", + "start": 113, + "end": 124, + "loc": { + "start": { + "line": 6, + "column": 16 + }, + "end": { + "line": 6, + "column": 27 + }, + "identifierName": "pi" + }, + "name": "pi", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 118, + "end": 124, + "loc": { + "start": { + "line": 6, + "column": 21 + }, + "end": { + "line": 6, + "column": 27 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 118, + "end": 124, + "loc": { + "start": { + "line": 6, + "column": 21 + }, + "end": { + "line": 6, + "column": 27 + } + } + } + } + } }, { - "type": "Identifier", - "start": 132, - "end": 135, + "type": "TSParameterProperty", + "start": 150, + "end": 153, "loc": { "start": { "line": 7, @@ -198,17 +320,32 @@ "end": { "line": 7, "column": 27 - }, - "identifierName": "pur" + } }, - "name": "pur", "accessibility": "public", - "readonly": true + "readonly": true, + "parameter": { + "type": "Identifier", + "start": 150, + "end": 153, + "loc": { + "start": { + "line": 7, + "column": 24 + }, + "end": { + "line": 7, + "column": 27 + }, + "identifierName": "pur" + }, + "name": "pur" + } }, { - "type": "AssignmentPattern", - "start": 197, - "end": 202, + "type": "TSParameterProperty", + "start": 215, + "end": 220, "loc": { "start": { "line": 9, @@ -219,50 +356,67 @@ "column": 22 } }, - "left": { - "type": "Identifier", - "start": 197, - "end": 198, + "readonly": true, + "parameter": { + "type": "AssignmentPattern", + "start": 215, + "end": 220, "loc": { "start": { "line": 9, "column": 17 }, - "end": { - "line": 9, - "column": 18 - }, - "identifierName": "x" - }, - "name": "x", - "leadingComments": null - }, - "right": { - "type": "NumericLiteral", - "start": 201, - "end": 202, - "loc": { - "start": { - "line": 9, - "column": 21 - }, "end": { "line": 9, "column": 22 } }, - "extra": { - "rawValue": 0, - "raw": "0" + "left": { + "type": "Identifier", + "start": 215, + "end": 216, + "loc": { + "start": { + "line": 9, + "column": 17 + }, + "end": { + "line": 9, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x", + "leadingComments": null }, - "value": 0 + "right": { + "type": "NumericLiteral", + "start": 219, + "end": 220, + "loc": { + "start": { + "line": 9, + "column": 21 + }, + "end": { + "line": 9, + "column": 22 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + }, + "leadingComments": null }, "leadingComments": [ { "type": "CommentLine", "value": " Also works on AssignmentPattern", - "start": 145, - "end": 179, + "start": 163, + "end": 197, "loc": { "start": { "line": 8, @@ -274,13 +428,12 @@ } } } - ], - "readonly": true + ] }, { - "type": "AssignmentPattern", - "start": 219, - "end": 232, + "type": "TSParameterProperty", + "start": 237, + "end": 251, "loc": { "start": { "line": 10, @@ -288,13 +441,14 @@ }, "end": { "line": 10, - "column": 28 + "column": 29 } }, - "left": { - "type": "Identifier", - "start": 219, - "end": 228, + "accessibility": "public", + "parameter": { + "type": "AssignmentPattern", + "start": 237, + "end": 251, "loc": { "start": { "line": 10, @@ -302,77 +456,92 @@ }, "end": { "line": 10, - "column": 24 - }, - "identifierName": "y" + "column": 29 + } }, - "name": "y", - "typeAnnotation": { - "type": "TypeAnnotation", - "start": 222, - "end": 228, + "left": { + "type": "Identifier", + "start": 237, + "end": 247, "loc": { "start": { "line": 10, - "column": 18 + "column": 15 }, "end": { "line": 10, - "column": 24 - } + "column": 25 + }, + "identifierName": "y" }, + "name": "y", + "optional": true, "typeAnnotation": { - "type": "TSNumberKeyword", - "start": 222, - "end": 228, + "type": "TypeAnnotation", + "start": 241, + "end": 247, "loc": { "start": { "line": 10, - "column": 18 + "column": 19 }, "end": { "line": 10, - "column": 24 + "column": 25 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 241, + "end": 247, + "loc": { + "start": { + "line": 10, + "column": 19 + }, + "end": { + "line": 10, + "column": 25 + } } } } - } - }, - "right": { - "type": "NumericLiteral", - "start": 231, - "end": 232, - "loc": { - "start": { - "line": 10, - "column": 27 - }, - "end": { - "line": 10, - "column": 28 - } - }, - "extra": { - "rawValue": 0, - "raw": "0" }, - "value": 0 - }, - "accessibility": "public" + "right": { + "type": "NumericLiteral", + "start": 250, + "end": 251, + "loc": { + "start": { + "line": 10, + "column": 28 + }, + "end": { + "line": 10, + "column": 29 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } } ], "body": { "type": "BlockStatement", - "start": 234, - "end": 236, + "start": 253, + "end": 255, "loc": { "start": { "line": 10, - "column": 30 + "column": 31 }, "end": { "line": 10, - "column": 32 + "column": 33 } }, "body": [], @@ -389,8 +558,8 @@ { "type": "CommentLine", "value": " Also works on AssignmentPattern", - "start": 145, - "end": 179, + "start": 163, + "end": 197, "loc": { "start": { "line": 8, From d2619b9a7ce987453b2d79c484bb4c7a9b9b133d Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 19 May 2017 14:12:19 -0700 Subject: [PATCH 18/55] Use options object for maybeParseFunctionBody --- src/parser/expression.js | 8 ++++---- src/parser/statement.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index cb72e15ca3..f372e92092 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -1042,7 +1042,7 @@ export default class ExpressionParser extends LValParser { const allowModifiers = isConstructor; // For TypeScript parameter properties node.params = this.parseBindingList(tt.parenR, /* allowEmpty */ false, allowModifiers); node.generator = !!isGenerator; - const missingBody = this.maybeParseFunctionBody(node, /* allowExpression */ false, /* allowMissingBody */ type === "ClassMethod"); + const missingBody = this.maybeParseFunctionBody(node, { allowMissingBody: type === "ClassMethod" }); this.state.inMethod = oldInMethod; return this.finishNode(node, missingBody ? "TSDeclareMethod" : type); } @@ -1069,8 +1069,8 @@ export default class ExpressionParser extends LValParser { } // Returns true if the body is missing. - maybeParseFunctionBody(node: N.BodilessFunctionBase, allowExpression?: boolean, allowMissingBody: boolean): boolean { - if (!allowExpression) { + maybeParseFunctionBody(node: N.BodilessFunctionBase, { allowExpressionBody, allowMissingBody }: { allowExpressionBody?: boolean, allowMissingBody: boolean }): boolean { + if (!allowExpressionBody) { // For arrow functions, `parseArrow` handles the return type itself. this.parseFunctionReturnType(node); } @@ -1082,7 +1082,7 @@ export default class ExpressionParser extends LValParser { } // $FlowIgnore (node is not bodiless if we get here) - this.parseFunctionBody(node, allowExpression); + this.parseFunctionBody(node, allowExpressionBody); return false; } diff --git a/src/parser/statement.js b/src/parser/statement.js index d8d0d40dc3..1af7548763 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -631,7 +631,7 @@ export default class StatementParser extends ExpressionParser { } this.parseFunctionParams(node); - const missingBody = this.maybeParseFunctionBody(node, allowExpressionBody, isStatement); + const missingBody = this.maybeParseFunctionBody(node, { allowExpressionBody, allowMissingBody: isStatement }); this.state.inMethod = oldInMethod; From 5569e15e33442a6800c5b227f52d1bba9246fba6 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 19 May 2017 15:13:06 -0700 Subject: [PATCH 19/55] Attach decorators to the TSParameterProperty, not to the Identifier --- src/parser/lval.js | 9 +- src/types.js | 3 +- .../actual.js | 3 + .../expected.json | 239 ++++++++++++++++++ .../options.json | 3 + 5 files changed, 253 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/typescript/class/parameter-properties-with-decorators/actual.js create mode 100644 test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json create mode 100644 test/fixtures/typescript/class/parameter-properties-with-decorators/options.json diff --git a/src/parser/lval.js b/src/parser/lval.js index 86d8b0aacd..bb2376b5e4 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -202,18 +202,21 @@ export default class LValParser extends NodeUtils { } const { accessibility, readonly } = allowModifiers ? this.parseParameterModifiers() : {}; const left = this.parseMaybeDefault(); - if (decorators.length) { - left.decorators = decorators; - } this.parseAssignableListItemTypes(left); const elt = this.parseMaybeDefault(left.start, left.loc.start, left); if (accessibility || readonly) { const pp: TSParameterProperty = this.startNodeAtNode(elt); + if (decorators.length) { + pp.decorators = decorators; + } if (accessibility) pp.accessibility = accessibility; if (readonly) pp.readonly = readonly; pp.parameter = elt; elts.push(this.finishNode(pp, "TSParameterProperty")); } else { + if (decorators.length) { + left.decorators = decorators; + } elts.push(elt); } } diff --git a/src/types.js b/src/types.js index 596f325cc0..72f4c05c53 100644 --- a/src/types.js +++ b/src/types.js @@ -779,7 +779,8 @@ export type FlowClassImplements = Node; // Misc // ================ -export type TSParameterProperty = NodeBase & { +export type TSParameterProperty = HasDecorators & { + // Note: This has decorators instead of its parameter. type: "TSParameterProperty"; // At least one of `accessibility` or `readonly` must be set. accessibility?: ?Accessibility; diff --git a/test/fixtures/typescript/class/parameter-properties-with-decorators/actual.js b/test/fixtures/typescript/class/parameter-properties-with-decorators/actual.js new file mode 100644 index 0000000000..c5e8d315db --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties-with-decorators/actual.js @@ -0,0 +1,3 @@ +class C { + constructor(@foo readonly x: number) {} +} diff --git a/test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json b/test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json new file mode 100644 index 0000000000..94b8b91d2f --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json @@ -0,0 +1,239 @@ +{ + "type": "File", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 55, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 14, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 43 + } + }, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 14, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "constructor" + }, + "name": "constructor" + }, + "kind": "constructor", + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "TSParameterProperty", + "start": 40, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 39 + } + }, + "decorators": [ + { + "type": "Decorator", + "start": 26, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 16 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "expression": { + "type": "Identifier", + "start": 27, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "readonly": true, + "parameter": { + "type": "Identifier", + "start": 40, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 39 + }, + "identifierName": "x" + }, + "name": "x", + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 43, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 33 + }, + "end": { + "line": 2, + "column": 39 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 43, + "end": 49, + "loc": { + "start": { + "line": 2, + "column": 33 + }, + "end": { + "line": 2, + "column": 39 + } + } + } + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 51, + "end": 53, + "loc": { + "start": { + "line": 2, + "column": 41 + }, + "end": { + "line": 2, + "column": 43 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/class/parameter-properties-with-decorators/options.json b/test/fixtures/typescript/class/parameter-properties-with-decorators/options.json new file mode 100644 index 0000000000..bb696a1918 --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties-with-decorators/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["typescript", "decorators"] +} From c75745f071e9361bf3abe608466b31802c026934 Mon Sep 17 00:00:00 2001 From: andy-ms Date: Sun, 21 May 2017 07:47:20 -0700 Subject: [PATCH 20/55] Make TSTypePredicate store a TypeAnnotation node instead of directly storing a type --- src/plugins/typescript.js | 24 ++++++------- src/types.js | 2 +- .../predicate-types/expected.json | 17 +++++++++- .../function/predicate-types/expected.json | 34 +++++++++++++++++-- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 2fa045d144..3df8f7b65c 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -210,7 +210,7 @@ export default (superClass: Class): Class => class extends super this.next(); const node: N.TsTypePredicate = this.startNode(); node.parameterName = lhs; - node.typeAnnotation = this.tsParseType(); + node.typeAnnotation = this.tsParseTypeAnnotation(); return this.finishNode(node, "TSTypePredicate"); } @@ -650,23 +650,19 @@ export default (superClass: Class): Class => class extends super } tsParseTypeOrTypePredicateAnnotation(): N.TypeAnnotation { - const t: N.TypeAnnotation = this.startNode(); - t.typeAnnotation = this.tsParseTypeOrTypePredicate(); - return this.finishNode(t, "TypeAnnotation"); - } - - tsParseTypeOrTypePredicate(): N.TsType { const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this)); - const type = this.tsParseType(); - if (typePredicateVariable) { - const node: N.TsTypePredicate = this.startNodeAtNode(typePredicateVariable); - node.parameterName = typePredicateVariable; - node.typeAnnotation = type; - return this.finishNode(node, "TSTypePredicate"); - } else { + const type = this.tsParseTypeAnnotation(); + if (!typePredicateVariable) { return type; } + + const t: N.TypeAnnotation = this.startNodeAtNode(typePredicateVariable); + const node: N.TsTypePredicate = this.startNodeAtNode(typePredicateVariable); + node.parameterName = typePredicateVariable; + node.typeAnnotation = type; + t.typeAnnotation = this.finishNode(node, "TSTypePredicate"); + return this.finishNode(t, "TypeAnnotation"); } tsTryParseTypeAnnotation(): ?N.TypeAnnotation { diff --git a/src/types.js b/src/types.js index 72f4c05c53..adcf0b925a 100644 --- a/src/types.js +++ b/src/types.js @@ -954,7 +954,7 @@ export type TsTypeReference = TsTypeBase & { export type TsTypePredicate = TsTypeBase & { type: "TSTypePredicate"; parameterName: Identifier | TsThisType; - typeAnnotation: TsType; + typeAnnotation: TypeAnnotation; }; // `typeof` operator diff --git a/test/fixtures/typescript/arrow-function/predicate-types/expected.json b/test/fixtures/typescript/arrow-function/predicate-types/expected.json index f47452b316..529fec9593 100644 --- a/test/fixtures/typescript/arrow-function/predicate-types/expected.json +++ b/test/fixtures/typescript/arrow-function/predicate-types/expected.json @@ -102,7 +102,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSStringKeyword", + "type": "TypeAnnotation", "start": 15, "end": 21, "loc": { @@ -114,6 +114,21 @@ "line": 1, "column": 21 } + }, + "typeAnnotation": { + "type": "TSStringKeyword", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 21 + } + } } } } diff --git a/test/fixtures/typescript/function/predicate-types/expected.json b/test/fixtures/typescript/function/predicate-types/expected.json index 77393469fe..09bb67f9c8 100644 --- a/test/fixtures/typescript/function/predicate-types/expected.json +++ b/test/fixtures/typescript/function/predicate-types/expected.json @@ -157,7 +157,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSBooleanKeyword", + "type": "TypeAnnotation", "start": 25, "end": 32, "loc": { @@ -169,6 +169,21 @@ "line": 1, "column": 32 } + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 25, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 32 + } + } } } } @@ -318,7 +333,7 @@ "name": "x" }, "typeAnnotation": { - "type": "TSBooleanKeyword", + "type": "TypeAnnotation", "start": 60, "end": 67, "loc": { @@ -330,6 +345,21 @@ "line": 2, "column": 31 } + }, + "typeAnnotation": { + "type": "TSBooleanKeyword", + "start": 60, + "end": 67, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 31 + } + } } } } From 733943a4001745000d1025fd3d26baf0be0dceb6 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 22 May 2017 08:56:25 -0700 Subject: [PATCH 21/55] parseExprOp: Factor out typescript-specific code to overrideable methods --- src/parser/expression.js | 23 +++++++++++++---------- src/plugins/typescript.js | 12 ++++++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index f372e92092..47c67b9b60 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -204,10 +204,9 @@ export default class ExpressionParser extends LValParser { parseExprOp(left: N.Expression, leftStartPos: number, leftStartLoc: Position, minPrec: number, noIn: ?boolean): N.Expression { let prec = this.state.type.binop; - let isAs = false; - if (this.hasPlugin("typescript") && prec == null && this.isContextual("as") && !this.hasPrecedingLineBreak()) { - prec = tt._in.binop; - isAs = true; + const specialPrec = this.getSpecialBinaryOperator(); + if (specialPrec != null) { + prec = specialPrec; } if (prec != null && (!noIn || !this.match(tt._in))) { @@ -228,13 +227,11 @@ export default class ExpressionParser extends LValParser { const op = this.state.type; this.next(); - const startPos = this.state.start; - const startLoc = this.state.startLoc; - if (isAs) { - node.expression = left; - node.typeAnnotation = this.tsParseType(); - this.finishNode(node, "TSAsExpression"); + if (specialPrec != null) { + this.handleSpecialBinaryOperator(node, left); } else { + const startPos = this.state.start; + const startLoc = this.state.startLoc; node.left = left; node.operator = operator; node.right = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn); @@ -247,6 +244,12 @@ export default class ExpressionParser extends LValParser { return left; } + getSpecialBinaryOperator(): ?number { return null; } + // eslint-disable-next-line no-unused-vars + handleSpecialBinaryOperator(node: N.Node, left: N.Expression): void { + throw new Error("Must be overridden if a plugin defines special binary operators."); + } + // Parse unary operators, both prefix and postfix. parseMaybeUnary(refShorthandDefaultPos: ?Pos): N.Expression { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 3df8f7b65c..8226e79621 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1063,6 +1063,18 @@ export default (superClass: Class): Class => class extends super // OVERRIDES // ====================================================== + getSpecialBinaryOperator(): ?number { + if (this.isContextual("as") && !this.hasPrecedingLineBreak()) { + return tt._in.binop; + } + } + + handleSpecialBinaryOperator(node: N.Node, left: N.Expression): void { + node.expression = left; + node.typeAnnotation = this.tsParseType(); + this.finishNode(node, "TSAsExpression"); + } + parseAccessModifier(): ?N.Accessibility { return this.tsParseModifier(["public", "protected", "private"]); } From e6210f705d36d8cd35531660704a60139e14f84b Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 22 May 2017 13:00:06 -0700 Subject: [PATCH 22/55] expression.js: Remove forward-declarations and use overrides instead --- src/parser/expression.js | 67 ++++++++++---------- src/plugins/typescript.js | 127 ++++++++++++++++++-------------------- src/types.js | 24 +++---- 3 files changed, 107 insertions(+), 111 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 6a018c3c3d..be4f78f4dd 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -34,16 +34,6 @@ export default class ExpressionParser extends LValParser { +parseFunction: (node: T, isStatement: boolean, allowExpressionBody?: boolean, isAsync?: boolean, optionalId?: boolean) => T; +takeDecorators: (node: N.HasDecorators) => void; - // Forward-declarations: defined in typescript.js - +tsParseType: () => N.TsType; - +tsTryParseCallExpressionWithTypeArguments: ( - base: N.Expression, - startPos: number, - startLoc: Position) => { newBase: N.Expression, shouldContinue: boolean }; - +tsTypeArgumentsFromAmbiguous: (types: $ReadOnlyArray) => N.TypeParameterInstantiation; - +tsTypeParametersFromAmbiguous: (types: $ReadOnlyArray) => N.TypeParameterDeclaration; - +tsParseTypeArguments: () => N.TypeParameterInstantiation; - // Check if property name clashes with already added. // Object/class getters and setters are not allowed to clash — // either with each other or with an init property — and in @@ -246,6 +236,7 @@ export default class ExpressionParser extends LValParser { getSpecialBinaryOperator(): ?number { return null; } // eslint-disable-next-line no-unused-vars handleSpecialBinaryOperator(node: N.Node, left: N.Expression): void { + // istanbul ignore next throw new Error("Must be overridden if a plugin defines special binary operators."); } @@ -336,15 +327,7 @@ export default class ExpressionParser extends LValParser { node.computed = true; this.expect(tt.bracketR); base = this.finishNode(node, "MemberExpression"); - } else if (!noCalls && this.hasPlugin("typescript") && this.isRelational("<")) { - // May be a call `f()`. But may be a less-than expression. - const { newBase, shouldContinue } = this.tsTryParseCallExpressionWithTypeArguments(base, startPos, startLoc); - if (shouldContinue) { - base = newBase; - } else { - return newBase; - } - } else if (!noCalls && this.match(tt.parenL)) { + } else if (!noCalls && (this.match(tt.parenL) || this.isRelational("<"))) { const { newBase, shouldContinue } = this.parseCallExpression(base, startPos, startLoc); if (shouldContinue) { base = newBase; @@ -368,11 +351,13 @@ export default class ExpressionParser extends LValParser { // But in the case of `async(`, both `async();` (call expression) and // `async () => { await foo() };` (generic async arrow function) are valid syntax. // So, types must be ambiguous, as they could be either parameters or arguments. - parseCallExpression( - base: N.Expression, - startPos: number, - startLoc: Position, - typeAmbiguous?: ?$ReadOnlyArray): { newBase: N.Expression, shouldContinue: boolean } { + parseCallExpression(base: N.Expression, startPos: number, startLoc: Position): { newBase: N.Expression, shouldContinue: boolean } { + let typeAmbiguous; + if (this.isRelational("<")) { + // May be a call `f()`. But may be a less-than expression. + typeAmbiguous = this.parseCallExpressionTypeAmbiguous(); + if (!typeAmbiguous) return { newBase: base, shouldContinue: false }; + } const possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); this.next(); @@ -397,16 +382,29 @@ export default class ExpressionParser extends LValParser { return { newBase: arrow, shouldContinue: false }; } else { if (typeAmbiguous) { - if (!this.hasPlugin("typescript")) { - throw new Error("typeAmbiguous should only be passed in typescript mode."); - } - node.typeArguments = this.tsTypeArgumentsFromAmbiguous(typeAmbiguous); + node.typeArguments = this.typeArgumentsFromAmbiguous(typeAmbiguous); } this.toReferencedList(node.arguments); return { newBase: base, shouldContinue: true }; } } + parseCallExpressionTypeAmbiguous(): ?$ReadOnlyArray { + return null; + } + + // eslint-disable-next-line no-unused-vars + typeArgumentsFromAmbiguous(types: $ReadOnlyArray): N.TypeParameterInstantiation { + // istanbul ignore next + throw new Error("Plugins should override typeArgumentsFromAmbiguous if overriding parseCallExpressionTypeArguments"); + } + + // eslint-disable-next-line no-unused-vars + typeParametersFromAmbiguous(types: $ReadOnlyArray): N.TypeParameterDeclaration { + // istanbul ignore next + throw new Error("Plugins should override typeParametersFromAmbiguous if overriding parseCallExpressionTypeArguments"); + } + parseCallExpressionArguments(close: TokenType, possibleAsyncArrow: boolean): $ReadOnlyArray { const elts = []; let innerParenStart; @@ -440,10 +438,10 @@ export default class ExpressionParser extends LValParser { return this.match(tt.arrow); } - parseAsyncArrowFromCallExpression(node: N.ArrowFunctionExpression, call: N.CallExpression, types: ?$ReadOnlyArray): N.ArrowFunctionExpression { + parseAsyncArrowFromCallExpression(node: N.ArrowFunctionExpression, call: N.CallExpression, types: ?$ReadOnlyArray): N.ArrowFunctionExpression { this.expect(tt.arrow); if (types) { - node.typeParameters = this.tsTypeParametersFromAmbiguous(types); + node.typeParameters = this.typeParametersFromAmbiguous(types); } return this.parseArrowExpression(node, call.arguments, true); } @@ -786,8 +784,9 @@ export default class ExpressionParser extends LValParser { node.callee = this.parseNoCallExpr(); - if (this.hasPlugin("typescript") && this.isRelational("<")) { - node.typeArguments = this.tsParseTypeArguments(); + if (this.isRelational("<")) { + const typeArguments = this.parseTypeArguments(); + if (typeArguments) node.typeArguments = typeArguments; } if (this.eat(tt.parenL)) { @@ -800,6 +799,10 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "NewExpression"); } + parseTypeArguments(): ?N.TypeParameterInstantiation { + return null; + } + // Parse template expression. parseTemplateElement(isTagged: boolean): N.TemplateElement { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 8226e79621..9bef47f293 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -201,7 +201,7 @@ export default (superClass: Class): Class => class extends super const node: N.TsTypeReference = this.startNode(); node.typeName = this.tsParseEntityName(/* allowReservedWords */ false); if (!this.hasPrecedingLineBreak() && this.isRelational("<")) { - node.typeArguments = this.tsParseTypeArguments(); + node.typeArguments = this.parseTypeArguments(); } return this.finishNode(node, "TSTypeReference"); } @@ -242,8 +242,8 @@ export default (superClass: Class): Class => class extends super } // Parse something that may be a type parameter or a type argument. - tsParseTypeAmbiguous(): ?N.TsTypeAmbiguous { - const node: N.TsTypeAmbiguous = this.startNode(); + tsParseTypeAmbiguous(): ?N.TypeAmbiguous { + const node: N.TypeAmbiguous = this.startNode(); try { node.typeAnnotation = this.tsParseType(); if (this.eat(tt._extends)) { @@ -260,7 +260,7 @@ export default (superClass: Class): Class => class extends super throw err; } - return this.finishNode(node, "TSTypeAmbiguous"); + return this.finishNode(node, "TypeAmbiguous"); } tsTryParseTypeParameters(): ?N.TypeParameterDeclaration { @@ -713,7 +713,7 @@ export default (superClass: Class): Class => class extends super } // NOTE: this is only called within a `tryParse`. So it shouldn't fail, it should just return `undefined`. - tsParseTypeArgumentsInExpression(): ?$ReadOnlyArray { + tsParseTypeArgumentsInExpression(): ?$ReadOnlyArray { this.expectRelational("<"); const typeArguments = this.tsTryParseDelimitedList( "TypeParametersOrArguments", @@ -733,7 +733,7 @@ export default (superClass: Class): Class => class extends super // then has grammar errors later if it's not an EntityName. node.expression = this.tsParseEntityName(/* allowReservedWords */ false); if (this.isRelational("<")) { - node.typeArguments = this.tsParseTypeArguments(); + node.typeArguments = this.parseTypeArguments(); } return this.finishNode(node, "TSExpressionWithTypeArguments"); @@ -875,65 +875,6 @@ export default (superClass: Class): Class => class extends super } } - // Not in TypeScript's own parser, but similar to its `parseCallExpressionRest`. - tsTryParseCallExpressionWithTypeArguments( - base: N.Expression, - startPos: number, - startLoc: Position): { newBase: N.Expression, shouldContinue: boolean } { - - const typeArguments = this.tsTryParse(this.tsParseTypeArgumentsInExpression.bind(this)); - return typeArguments - ? this.parseCallExpression(base, startPos, startLoc, typeArguments) - : { newBase: base, shouldContinue: false }; - } - - // Not in TypeScript's own parser. Called after seeing `new C<` in `parseNew`. - tsParseTypeArguments(): N.TypeParameterInstantiation { - const node = this.startNode(); - this.expectRelational("<"); - node.params = this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this)); - this.expectRelational(">"); - return this.finishNode(node, "TypeParameterInstantiation"); - } - - tsTypeArgumentsFromAmbiguous(types: $ReadOnlyArray): N.TypeParameterInstantiation { - const node: N.TypeParameterInstantiation = this.startNode(); - node.params = types.map(({ constraint, default: defaultType, typeAnnotation: type, start, end, loc }) => { - if (constraint || defaultType) { - throw this.raise(start, "Type argument may not have a constraint or default."); - } - - // Set location - type.start = start; - type.end = end; - type.loc.start = loc.start; - type.loc.end = loc.end; - - return type; - }); - return this.finishNode(node, "TypeParameterInstantiation"); - } - - tsTypeParametersFromAmbiguous( - types: $ReadOnlyArray): N.TypeParameterDeclaration { - - const node: N.TypeParameterDeclaration = this.startNode(); - node.params = types.map((typeAmbiguous) => { - const typeParam = this.nodeWithSamePosition(typeAmbiguous, "TypeParameter"); - const { typeAnnotation: type, constraint, default: defaultType } = typeAmbiguous; - if (type.type === "TSTypeReference" && !type.typeArguments && type.typeName.type === "Identifier") { - typeParam.name = type.typeName.name; - } else { - throw this.raise(type.start, "Type parameter must be a plain identifier."); - } - - if (constraint) typeParam.constriant = constraint; - if (defaultType) typeParam.default = defaultType; - return typeParam; - }); - return this.finishNode(node, "TypeParameterDeclaration"); - } - nodeWithSamePosition(original: N.Node, type: string): T { const node = this.startNodeAtNode(original); node.type = type; @@ -1063,6 +1004,58 @@ export default (superClass: Class): Class => class extends super // OVERRIDES // ====================================================== + // Similar to TypeScript's own parser's `parseCallExpressionRest`. + parseCallExpressionTypeAmbiguous(): ?$ReadOnlyArray { + return this.tsTryParse(this.tsParseTypeArgumentsInExpression.bind(this)); + } + + typeArgumentsFromAmbiguous(types: $ReadOnlyArray): N.TypeParameterInstantiation { + const node: N.TypeParameterInstantiation = this.startNode(); + node.params = types.map(({ constraint, default: defaultType, typeAnnotation: type, start, end, loc }) => { + if (constraint || defaultType) { + throw this.raise(start, "Type argument may not have a constraint or default."); + } + + // Set location + type.start = start; + type.end = end; + type.loc.start = loc.start; + type.loc.end = loc.end; + + return type; + }); + return this.finishNode(node, "TypeParameterInstantiation"); + } + + typeParametersFromAmbiguous( + types: $ReadOnlyArray): N.TypeParameterDeclaration { + + const node: N.TypeParameterDeclaration = this.startNode(); + node.params = types.map((typeAmbiguous) => { + const typeParam = this.nodeWithSamePosition(typeAmbiguous, "TypeParameter"); + const { typeAnnotation: type, constraint, default: defaultType } = typeAmbiguous; + if (type.type === "TSTypeReference" && !type.typeArguments && type.typeName.type === "Identifier") { + typeParam.name = type.typeName.name; + } else { + throw this.raise(type.start, "Type parameter must be a plain identifier."); + } + + if (constraint) typeParam.constriant = constraint; + if (defaultType) typeParam.default = defaultType; + return typeParam; + }); + return this.finishNode(node, "TypeParameterDeclaration"); + } + + // Not in TypeScript's own parser. Called after seeing `new C<` in `parseNew`. + parseTypeArguments(): N.TypeParameterInstantiation { + const node = this.startNode(); + this.expectRelational("<"); + node.params = this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this)); + this.expectRelational(">"); + return this.finishNode(node, "TypeParameterInstantiation"); + } + getSpecialBinaryOperator(): ?number { if (this.isContextual("as") && !this.hasPrecedingLineBreak()) { return tt._in.binop; @@ -1234,7 +1227,7 @@ export default (superClass: Class): Class => class extends super parseClassSuper(node: N.Class, isStatement?: boolean): void { super.parseClassSuper(node, isStatement); if (node.superClass && this.isRelational("<")) { - node.superTypeParameters = this.tsParseTypeArguments(); + node.superTypeParameters = this.parseTypeArguments(); } if (this.eatContextual("implements")) { node.implements = this.tsParseHeritageClause(); @@ -1269,7 +1262,7 @@ export default (superClass: Class): Class => class extends super parseAsyncArrowFromCallExpression( node: N.ArrowFunctionExpression, call: N.CallExpression, - types: ?$ReadOnlyArray): N.ArrowFunctionExpression { + types: ?$ReadOnlyArray): N.ArrowFunctionExpression { if (this.eat(tt.colon)) { node.returnType = this.tsParseTypeAnnotation(); diff --git a/src/types.js b/src/types.js index 1a28c5e05f..01d82ad665 100644 --- a/src/types.js +++ b/src/types.js @@ -721,6 +721,18 @@ export type JSXElement = Node; // Flow/TypeScript common (TODO: Not in spec) +/** + * For internal use only. + * This is for something which may be a type parameter or type argument, but which is not yet known. + * These shouldn't appear in the output AST. + */ +export type TypeAmbiguous = NodeBase & { + type: "TypeAmbiguous"; + typeAnnotation: TsType; + constraint?: TsType; + default?: TsType; +} + export type TypeAnnotation = NodeBase & { type: "TypeAnnotation"; typeAnnotation: TsType | FlowTypeAnnotation; @@ -829,18 +841,6 @@ export type TsQualifiedName = NodeBase & { export type TsEntityName = Identifier | TsQualifiedName; -/** - * For internal use only. - * This is for something which may be a type parameter or type argument, but which is not yet known. - * These shouldn't appear in the output AST. - */ -export type TsTypeAmbiguous = NodeBase & { - type: "TSTypeAmbiguous"; - typeAnnotation: TsType; - constraint?: TsType; - default?: TsType; -} - export type TsSignatureDeclaration = | TsCallSignatureDeclaration | TsConstructSignatureDeclaration From 7834a35a48bb9c35792da199750803b7ff729853 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 22 May 2017 13:13:58 -0700 Subject: [PATCH 23/55] Inline parseCallExpression --- src/parser/expression.js | 80 +++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 46 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index be4f78f4dd..5baf9e49ab 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -328,11 +328,41 @@ export default class ExpressionParser extends LValParser { this.expect(tt.bracketR); base = this.finishNode(node, "MemberExpression"); } else if (!noCalls && (this.match(tt.parenL) || this.isRelational("<"))) { - const { newBase, shouldContinue } = this.parseCallExpression(base, startPos, startLoc); - if (shouldContinue) { - base = newBase; + let typeAmbiguous; + if (this.isRelational("<")) { + // May be a call `f()`. But may be a less-than expression. + // In the case of `async(`, both `async();` (call expression) and + // `async () => { await foo() };` (generic async arrow function) are valid syntax. + // So, types parsed now must be ambiguous, as they could be either parameters or arguments. + typeAmbiguous = this.parseCallExpressionTypeAmbiguous(); + if (!typeAmbiguous) return base; + } + + const possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); + this.next(); + + const node = this.startNodeAt(startPos, startLoc); + node.callee = base; + node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); + if (node.callee.type === "Import") { + if (node.arguments.length !== 1) { + this.raise(node.start, "import() requires exactly one argument"); + } + + const importArg = node.arguments[0]; + if (importArg && importArg.type === "SpreadElement") { + this.raise(importArg.start, "... is not allowed in import()"); + } + } + base = this.finishNode(node, "CallExpression"); + + if (possibleAsync && this.shouldParseAsyncArrow()) { + return this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node, typeAmbiguous); } else { - return newBase; + if (typeAmbiguous) { + node.typeArguments = this.typeArgumentsFromAmbiguous(typeAmbiguous); + } + this.toReferencedList(node.arguments); } } else if (this.match(tt.backQuote)) { const node = this.startNodeAt(startPos, startLoc); @@ -347,48 +377,6 @@ export default class ExpressionParser extends LValParser { throw new Error("Unreachable"); } - // In TypeScript, when we see `f(`, it is usually a call expression. - // But in the case of `async(`, both `async();` (call expression) and - // `async () => { await foo() };` (generic async arrow function) are valid syntax. - // So, types must be ambiguous, as they could be either parameters or arguments. - parseCallExpression(base: N.Expression, startPos: number, startLoc: Position): { newBase: N.Expression, shouldContinue: boolean } { - let typeAmbiguous; - if (this.isRelational("<")) { - // May be a call `f()`. But may be a less-than expression. - typeAmbiguous = this.parseCallExpressionTypeAmbiguous(); - if (!typeAmbiguous) return { newBase: base, shouldContinue: false }; - } - - const possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); - this.next(); - - const node = this.startNodeAt(startPos, startLoc); - node.callee = base; - node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); - if (node.callee.type === "Import") { - if (node.arguments.length !== 1) { - this.raise(node.start, "import() requires exactly one argument"); - } - - const importArg = node.arguments[0]; - if (importArg && importArg.type === "SpreadElement") { - this.raise(importArg.start, "... is not allowed in import()"); - } - } - base = this.finishNode(node, "CallExpression"); - - if (possibleAsync && this.shouldParseAsyncArrow()) { - const arrow = this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node, typeAmbiguous); - return { newBase: arrow, shouldContinue: false }; - } else { - if (typeAmbiguous) { - node.typeArguments = this.typeArgumentsFromAmbiguous(typeAmbiguous); - } - this.toReferencedList(node.arguments); - return { newBase: base, shouldContinue: true }; - } - } - parseCallExpressionTypeAmbiguous(): ?$ReadOnlyArray { return null; } From 345930d36f9dee582c0ee6a70ce66fe78217ec72 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 22 May 2017 13:26:34 -0700 Subject: [PATCH 24/55] Remove `parsePropertyNameInfo`, no longer needed --- src/parser/expression.js | 18 ++++++------------ src/plugins/flow.js | 3 ++- src/plugins/typescript.js | 4 +--- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 5baf9e49ab..81fbbe7635 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -1014,25 +1014,19 @@ export default class ExpressionParser extends LValParser { return node; } - parsePropertyName(prop: N.ObjectOrClassMember): N.Expression { - const { computed, key } = this.parsePropertyNameInfo(); - prop.computed = computed; - prop.key = key; - return key; - } - - parsePropertyNameInfo(): { computed: boolean; key: N.Expression } { + parsePropertyName(prop: N.ObjectOrClassMember | N.TsNamedTypeElementBase): N.Identifier { if (this.eat(tt.bracketL)) { - const key = this.parseMaybeAssign(); + prop.computed = true; + prop.key = this.parseMaybeAssign(); this.expect(tt.bracketR); - return { computed: true, key }; } else { + prop.computed = false; const oldInPropertyName = this.state.inPropertyName; this.state.inPropertyName = true; - const key = (this.match(tt.num) || this.match(tt.string)) ? this.parseExprAtom() : this.parseIdentifier(true); + prop.key = (this.match(tt.num) || this.match(tt.string)) ? this.parseExprAtom() : this.parseIdentifier(true); this.state.inPropertyName = oldInPropertyName; - return { computed: false, key }; } + return prop.key; } // Initialize empty function node. diff --git a/src/plugins/flow.js b/src/plugins/flow.js index dfe15ef994..ce4460b3c6 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -1275,9 +1275,10 @@ export default (superClass: Class): Class => class extends super } } - parsePropertyName(node: N.ObjectOrClassMember): N.Identifier { + parsePropertyName(node: N.ObjectOrClassMember | N.TsNamedTypeElementBase): N.Identifier { const variance = this.flowParseVariance(); const key = super.parsePropertyName(node); + // $FlowIgnore ("variance" not defined on TsNamedTypeElementBase) node.variance = variance; return key; } diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 9bef47f293..f698fef0b5 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -347,9 +347,7 @@ export default (superClass: Class): Class => class extends super node: N.TsPropertySignature | N.TsMethodSignature, readonly: boolean): N.TsPropertySignature | N.TsMethodSignature { - const { key, computed } = this.parsePropertyNameInfo(); - node.computed = computed; - node.key = key; + this.parsePropertyName(node); if (this.eat(tt.question)) node.optional = true; const nodeAny: any = node; From c01500c29ea5b7069fe94ce16f4f070de961cadd Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 22 May 2017 13:37:00 -0700 Subject: [PATCH 25/55] Remove TsTypeElementBase. Call/construct/index signatures are never optional. --- src/plugins/typescript.js | 4 +++- src/types.js | 16 +++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index f698fef0b5..17a5777cb8 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -310,7 +310,9 @@ export default (superClass: Class): Class => class extends super } } - tsParseSignatureMember(kind: "TSCallSignatureDeclaration" | "TSConstructSignatureDeclaration") { + tsParseSignatureMember( + kind: "TSCallSignatureDeclaration" | "TSConstructSignatureDeclaration", + ): N.TsCallSignatureDeclaration | N.TsConstructSignatureDeclaration { const node: N.TsCallSignatureDeclaration | N.TsConstructSignatureDeclaration = this.startNode(); if (kind === "TSConstructSignatureDeclaration") { this.expect(tt._new); diff --git a/src/types.js b/src/types.js index 01d82ad665..82f6dd7541 100644 --- a/src/types.js +++ b/src/types.js @@ -848,7 +848,7 @@ export type TsSignatureDeclaration = | TsFunctionType | TsConstructorType; -export type TsSignatureDeclarationOrIndexSignatureBase = { +export type TsSignatureDeclarationOrIndexSignatureBase = NodeBase & { // Not using TypeScript's "ParameterDeclaration" here, since it's inconsistent with regular functions. parameters: $ReadOnlyArray; typeAnnotation: ?TypeAnnotation; @@ -869,21 +869,15 @@ export type TsTypeElement = | TsMethodSignature | TsIndexSignature; -export type TsTypeElementBase = NodeBase & { - // TypeScript uses a `PropertyName` here, - // but babylon just uses an `Expression` for object keys, so use that. - optional?: true; -} - -export type TsCallSignatureDeclaration = TsSignatureDeclarationBase & TsTypeElementBase & { +export type TsCallSignatureDeclaration = TsSignatureDeclarationBase & { type: "TSCallSignatureDeclaration"; } -export type TsConstructSignatureDeclaration = TsSignatureDeclarationBase & TsTypeElementBase & { +export type TsConstructSignatureDeclaration = TsSignatureDeclarationBase & { type: "TSConstructSignature"; } -export type TsNamedTypeElementBase = TsTypeElementBase & { +export type TsNamedTypeElementBase = NodeBase & { // Not using TypeScript's `PropertyName` here since we don't have a `ComputedPropertyName` node type. // This is usually an Identifier but may be e.g. `Symbol.iterator` if `computed` is true. key: Expression; @@ -903,7 +897,7 @@ export type TsMethodSignature = TsSignatureDeclarationBase & TsNamedTypeElementB } // *Not* a ClassMemberBase: Can't have accessibility, can't be abstract, can't be optional. -export type TsIndexSignature = TsSignatureDeclarationOrIndexSignatureBase & TsTypeElementBase & { +export type TsIndexSignature = TsSignatureDeclarationOrIndexSignatureBase & { readonly?: true; type: "TSIndexSignature"; // Note: parameters.length must be 1. From c20dfa7c08e93444af49c9a38c2dbc3f456b375c Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 22 May 2017 14:38:52 -0700 Subject: [PATCH 26/55] Introduce BodilessFunctionOrMethodBase type --- src/parser/expression.js | 6 +- src/plugins/flow.js | 2 +- src/plugins/typescript.js | 2 +- src/types.js | 13 +- .../function/export-default/actual.js | 1 + .../function/export-default/expected.json | 148 ++++++++++++++++++ 6 files changed, 163 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/typescript/function/export-default/actual.js create mode 100644 test/fixtures/typescript/function/export-default/expected.json diff --git a/src/parser/expression.js b/src/parser/expression.js index 81fbbe7635..334f61d9f9 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -1031,7 +1031,7 @@ export default class ExpressionParser extends LValParser { // Initialize empty function node. - initFunction(node: N.BodilessFunctionBase, isAsync: ?boolean): void { + initFunction(node: N.BodilessFunctionOrMethodBase, isAsync: ?boolean): void { node.id = null; node.generator = false; node.expression = false; @@ -1075,7 +1075,7 @@ export default class ExpressionParser extends LValParser { } // Returns true if the body is missing. - maybeParseFunctionBody(node: N.BodilessFunctionBase, { allowExpressionBody, allowMissingBody }: { allowExpressionBody?: boolean, allowMissingBody: boolean }): boolean { + maybeParseFunctionBody(node: N.BodilessFunctionOrMethodBase, { allowExpressionBody, allowMissingBody }: { allowExpressionBody?: boolean, allowMissingBody: boolean }): boolean { if (!allowExpressionBody) { // For arrow functions, `parseArrow` handles the return type itself. this.parseFunctionReturnType(node); @@ -1094,7 +1094,7 @@ export default class ExpressionParser extends LValParser { // TypeScript / Flow plugins will override this // eslint-disable-next-line no-unused-vars - parseFunctionReturnType(node: N.BodilessFunctionBase): void {} + parseFunctionReturnType(node: N.BodilessFunctionOrMethodBase): void {} // Parse function body and check parameters. parseFunctionBody(node: N.Function, allowExpression: ?boolean): void { diff --git a/src/plugins/flow.js b/src/plugins/flow.js index ce4460b3c6..973d4a49f2 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -1006,7 +1006,7 @@ export default (superClass: Class): Class => class extends super // Overrides // ================================== - parseFunctionReturnType(node: N.BodilessFunctionBase): void { + parseFunctionReturnType(node: N.BodilessFunctionOrMethodBase): void { if (this.match(tt.colon)) { const typeNode = this.startNode(); // $FlowFixMe diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 17a5777cb8..a6631ed748 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1096,7 +1096,7 @@ export default (superClass: Class): Class => class extends super return { abstract, readonly }; } - parseFunctionReturnType(node: N.BodilessFunctionBase): void { + parseFunctionReturnType(node: N.BodilessFunctionOrMethodBase): void { if (this.eat(tt.colon)) { node.returnType = this.tsParseTypeOrTypePredicateAnnotation(); } diff --git a/src/types.js b/src/types.js index 82f6dd7541..70ec970d8d 100644 --- a/src/types.js +++ b/src/types.js @@ -132,9 +132,11 @@ export type Function = export type NormalFunction = FunctionDeclaration | FunctionExpression; -//TODO: name -export type BodilessFunctionBase = HasDecorators & { +export type BodilessFunctionOrMethodBase = HasDecorators & { + // TODO: Remove this. Should not assign "id" to methods. + // https://github.com/babel/babylon/issues/535 id: ?Identifier; + params: $ReadOnlyArray; body: BlockStatement; generator: boolean; @@ -146,6 +148,10 @@ export type BodilessFunctionBase = HasDecorators & { returnType?: ?TypeAnnotation; } +export type BodilessFunctionBase = BodilessFunctionOrMethodBase & { + id: ?Identifier; +} + export type FunctionBase = BodilessFunctionBase & { body: BlockStatement; }; @@ -819,7 +825,6 @@ export type TSParameterProperty = HasDecorators & { parameter: Pattern; } -//test with `export default function(): void; export type OptTSDeclareFunction = BodilessFunctionBase & DeclarationBase & { type: "TSDeclareFunction"; } @@ -828,7 +833,7 @@ export type TSDeclareFunction = OptTSDeclareFunction & { id: Identifier; }; -export type TSDeclareMethod = BodilessFunctionBase & ClassMemberBase & { +export type TSDeclareMethod = BodilessFunctionOrMethodBase & ClassMemberBase & { type: "TSDeclareMethod"; +kind: MethodKind; }; diff --git a/test/fixtures/typescript/function/export-default/actual.js b/test/fixtures/typescript/function/export-default/actual.js new file mode 100644 index 0000000000..37e894cdaa --- /dev/null +++ b/test/fixtures/typescript/function/export-default/actual.js @@ -0,0 +1 @@ +export default function(x?: number): void; diff --git a/test/fixtures/typescript/function/export-default/expected.json b/test/fixtures/typescript/function/export-default/expected.json new file mode 100644 index 0000000000..6cfd14e943 --- /dev/null +++ b/test/fixtures/typescript/function/export-default/expected.json @@ -0,0 +1,148 @@ +{ + "type": "File", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportDefaultDeclaration", + "start": 0, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "declaration": { + "type": "TSDeclareFunction", + "start": 15, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "id": null, + "generator": false, + "expression": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 24, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 34 + }, + "identifierName": "x" + }, + "name": "x", + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 34 + } + } + } + } + } + ], + "returnType": { + "type": "TypeAnnotation", + "start": 37, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 41 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 37, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 41 + } + } + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file From 7ebe5dc8705e727415e6bcff089e90b649870f46 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 22 May 2017 14:50:49 -0700 Subject: [PATCH 27/55] Don't expose tsParseEnumDeclaration; instead, override `parseStatementContent` --- src/parser/statement.js | 23 +++++++---------------- src/plugins/typescript.js | 13 +++++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/parser/statement.js b/src/parser/statement.js index c2827a380b..c2f7b5dbb5 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -16,7 +16,6 @@ const loopLabel = { kind: "loop" }, switchLabel = { kind: "switch" }; export default class StatementParser extends ExpressionParser { // Forward-declarations: Code defined in typescript.js - +tsParseEnumDeclaration: (node: N.TsEnumDeclaration, isConst: boolean) => N.TsEnumDeclaration; +tsTryParseIndexSignature: (node: N.TsIndexSignature) => ?N.TsIndexSignature +tsParseImportEqualsDeclaration: ( node: N.TsImportEqualsDeclaration, @@ -71,14 +70,17 @@ export default class StatementParser extends ExpressionParser { if (this.match(tt.at)) { this.parseDecorators(true); } + return this.parseStatementContent(declaration, topLevel); + } - const starttype = this.state.type; - const node = this.startNode(); - + parseStatementContent(declaration: boolean, topLevel: ?boolean): N.Statement { // Most types of statements are recognized by the keyword they // start with. Many are trivial to parse, some require a bit of // complexity. + const starttype = this.state.type; + const node = this.startNode(); + switch (starttype) { // $FlowFixMe case tt._break: case tt._continue: return this.parseBreakContinueStatement(node, starttype.keyword); @@ -99,19 +101,8 @@ export default class StatementParser extends ExpressionParser { case tt._throw: return this.parseThrowStatement(node); case tt._try: return this.parseTryStatement(node); - case tt._const: - // Handle `const enum` - if (this.hasPlugin("typescript")) { - const ahead = this.lookahead(); - if (ahead.type === tt.name && ahead.value === "enum") { - this.expect(tt._const); - this.expectContextual("enum"); - return this.tsParseEnumDeclaration(node, /* isConst */ true); - } - } - // fall through - case tt._let: + case tt._const: if (!declaration) this.unexpected(); // NOTE: falls through to _var case tt._var: diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index a6631ed748..95c532ebbd 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1004,6 +1004,19 @@ export default (superClass: Class): Class => class extends super // OVERRIDES // ====================================================== + parseStatementContent(declaration: boolean, topLevel: ?boolean): N.Statement { + if (this.state.type === tt._const) { + const ahead = this.lookahead(); + if (ahead.type === tt.name && ahead.value === "enum") { + const node: N.TsEnumDeclaration = this.startNode(); //! + this.expect(tt._const); + this.expectContextual("enum"); + return this.tsParseEnumDeclaration(node, /* isConst */ true); + } + } + return super.parseStatementContent(declaration, topLevel); + } + // Similar to TypeScript's own parser's `parseCallExpressionRest`. parseCallExpressionTypeAmbiguous(): ?$ReadOnlyArray { return this.tsTryParse(this.tsParseTypeArgumentsInExpression.bind(this)); From 5a88758b453e0c655066e99a59a85848bc3b3911 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 22 May 2017 14:59:39 -0700 Subject: [PATCH 28/55] Prefer switch statements to long if...else-if chains --- src/parser/statement.js | 2 +- src/parser/util.js | 6 -- src/plugins/typescript.js | 132 +++++++++++++++++++++----------------- 3 files changed, 73 insertions(+), 67 deletions(-) diff --git a/src/parser/statement.js b/src/parser/statement.js index c2f7b5dbb5..ea815d1aea 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -258,7 +258,7 @@ export default class StatementParser extends ExpressionParser { return this.parseFor(node, null); } - if (this.matchVarLetOrConst()) { + if (this.match(tt._var) || this.match(tt._let) || this.match(tt._const)) { const init = this.startNode(); const varKind = this.state.type; this.next(); diff --git a/src/parser/util.js b/src/parser/util.js index c4147b7912..7f7531ac27 100644 --- a/src/parser/util.js +++ b/src/parser/util.js @@ -17,12 +17,6 @@ export default class UtilParser extends Tokenizer { extra[key] = val; } - // Tests whether we are at the beginning of a variable declaration: at "var", "let", or "const". - - matchVarLetOrConst(): boolean { - return this.match(tt._var) || this.match(tt._let) || this.match(tt._const); - } - // TODO isRelational(op: "<" | ">"): boolean { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 95c532ebbd..41336f37d7 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -497,48 +497,57 @@ export default (superClass: Class): Class => class extends super } tsParseNonArrayType(): N.TsType { - if (this.match(tt.name) || this.match(tt._void) || this.match(tt._null)) { - const type = this.match(tt._void) - ? "TSVoidKeyword" - : this.match(tt._null) - ? "TSNullKeyword" - : keywordTypeFromName(this.state.value); - if (type !== undefined && this.lookahead().type !== tt.dot) { - const node: N.TsKeywordType = this.startNode(); - this.next(); - return this.finishNode(node, type); - } - return this.tsParseTypeReference(); - } else if (this.match(tt.string) || this.match(tt.num) || this.match(tt._true) || this.match(tt._false)) { - return this.tsParseLiteralTypeNode(); - } else if (this.match(tt.plusMin) && this.state.value === "-") { - const node: N.TsLiteralType = this.startNode(); - this.next(); - if (!this.match(tt.num)) { - throw this.unexpected(); - } - node.literal = this.parseLiteral(-this.state.value, "NumericLiteral", node.start, node.loc.start); - return this.finishNode(node, "TSLiteralType"); - } else if (this.match(tt._this)) { - const thisKeyword = this.tsParseThisTypeNode(); - if (this.isContextual("is") && !this.hasPrecedingLineBreak()) { - return this.tsParseThisTypePredicate(thisKeyword); - } else { - return thisKeyword; - } - } else if (this.match(tt._typeof)) { - return this.tsParseTypeQuery(); - } else if (this.match(tt.braceL)) { - return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) - ? this.tsParseMappedType() - : this.tsParseTypeLiteral(); - } else if (this.match(tt.bracketL)) { - return this.tsParseTupleType(); - } else if (this.match(tt.parenL)) { - return this.tsParseParenthesizedType(); - } else { - return this.tsParseTypeReference(); + switch (this.state.type) { + case tt.name: + case tt._void: + case tt._null: + const type = this.match(tt._void) + ? "TSVoidKeyword" + : this.match(tt._null) + ? "TSNullKeyword" + : keywordTypeFromName(this.state.value); + if (type !== undefined && this.lookahead().type !== tt.dot) { + const node: N.TsKeywordType = this.startNode(); + this.next(); + return this.finishNode(node, type); + } + return this.tsParseTypeReference(); + case tt.string: + case tt.num: + case tt._true: + case tt._false: + return this.tsParseLiteralTypeNode(); + case tt.plusMin: + if (this.state.value === "-") { + const node: N.TsLiteralType = this.startNode(); + this.next(); + if (!this.match(tt.num)) { + throw this.unexpected(); + } + node.literal = this.parseLiteral(-this.state.value, "NumericLiteral", node.start, node.loc.start); + return this.finishNode(node, "TSLiteralType"); + } + break; + case tt._this: + const thisKeyword = this.tsParseThisTypeNode(); + if (this.isContextual("is") && !this.hasPrecedingLineBreak()) { + return this.tsParseThisTypePredicate(thisKeyword); + } else { + return thisKeyword; + } + case tt._typeof: + return this.tsParseTypeQuery(); + case tt.braceL: + return this.tsLookAhead(this.tsIsStartOfMappedType.bind(this)) + ? this.tsParseMappedType() + : this.tsParseTypeLiteral(); + case tt.bracketL: + return this.tsParseTupleType(); + case tt.parenL: + return this.tsParseParenthesizedType(); } + + throw this.unexpected(); } tsParseArrayTypeOrHigher(): N.TsType { @@ -889,26 +898,29 @@ export default (superClass: Class): Class => class extends super } tsTryParseDeclare(nany: any): ?N.Declaration { - if (this.eat(tt._function)) { - return this.parseFunction(nany, /* isStatement */ true); - } else if (this.match(tt._class)) { - return this.parseClass(nany, /* isStatement */ true, /* optionalId */ false); - } else if (this.matchVarLetOrConst()) { - if (this.match(tt._const) && this.lookaheadIsContextual("enum")) { - // `const enum = 0;` not allowed because "enum" is a strict mode reserved word. - this.expect(tt._const); - this.expectContextual("enum"); - return this.tsParseEnumDeclaration(nany, /* isConst */ true); - } else { + switch (this.state.type) { + case tt._function: + this.next(); + return this.parseFunction(nany, /* isStatement */ true); + case tt._class: + return this.parseClass(nany, /* isStatement */ true, /* optionalId */ false); + case tt._const: + if (this.match(tt._const) && this.lookaheadIsContextual("enum")) { + // `const enum = 0;` not allowed because "enum" is a strict mode reserved word. + this.expect(tt._const); + this.expectContextual("enum"); + return this.tsParseEnumDeclaration(nany, /* isConst */ true); + } + // falls through + case tt._var: case tt._let: return this.parseVarStatement(nany, this.state.type); - } - } else if (this.match(tt.name)) { - const value = this.state.value; - if (value === "global") { - return this.tsParseAmbientExternalModuleDeclaration(nany); - } else { - return this.tsParseDeclaration(nany, value, /* next */ true); - } + case tt.name: + const value = this.state.value; + if (value === "global") { + return this.tsParseAmbientExternalModuleDeclaration(nany); + } else { + return this.tsParseDeclaration(nany, value, /* next */ true); + } } } From 6a10fd4b8288a4e40452115d7e79307bc421bf0a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 22 May 2017 15:14:16 -0700 Subject: [PATCH 29/55] Clean up some statement.js changes --- src/parser/statement.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/parser/statement.js b/src/parser/statement.js index ea815d1aea..bf76f8778c 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -4,9 +4,9 @@ import * as N from "../types"; import { types as tt, type TokenType } from "../tokenizer/types"; +import ExpressionParser from "./expression"; import type { Position } from "../util/location"; import { lineBreak } from "../util/whitespace"; -import ExpressionParser from "./expression"; // Reused empty array added for node fields that are always empty. @@ -74,13 +74,13 @@ export default class StatementParser extends ExpressionParser { } parseStatementContent(declaration: boolean, topLevel: ?boolean): N.Statement { + const starttype = this.state.type; + const node = this.startNode(); + // Most types of statements are recognized by the keyword they // start with. Many are trivial to parse, some require a bit of // complexity. - const starttype = this.state.type; - const node = this.startNode(); - switch (starttype) { // $FlowFixMe case tt._break: case tt._continue: return this.parseBreakContinueStatement(node, starttype.keyword); @@ -483,9 +483,9 @@ export default class StatementParser extends ExpressionParser { } parseBlockBody(node: N.BlockStatementLike, allowDirectives: ?boolean, topLevel: boolean, end: TokenType): void { - node.body = []; - node.directives = []; - this.parseBlockOrModuleBlockBody(node.body, allowDirectives ? node.directives : undefined, topLevel, end); + const body = node.body = []; + const directives = node.directives = []; + this.parseBlockOrModuleBlockBody(body, allowDirectives ? directives : undefined, topLevel, end); } // Undefined directives means that directives are not allowed. @@ -871,18 +871,17 @@ export default class StatementParser extends ExpressionParser { return this.finishNode(node, "ClassPrivateProperty"); } - parseClassProperty(node: N.ClassProperty): N.ClassProperty { - const allowProperties = this.hasPlugin("classProperties") || this.hasPlugin("typescript"); + const hasPlugin = this.hasPlugin("classProperties") || this.hasPlugin("typescript"); const noPluginMsg = "You can only use Class Properties when the 'classProperties' plugin is enabled."; - if (!node.typeAnnotation && !allowProperties) { + if (!node.typeAnnotation && !hasPlugin) { this.raise(node.start, noPluginMsg); } this.state.inClassProperty = true; if (this.match(tt.eq)) { - if (!allowProperties) this.raise(this.state.start, noPluginMsg); + if (!hasPlugin) this.raise(this.state.start, noPluginMsg); this.next(); node.value = this.parseMaybeAssign(); } else { From fe13a1e4e25e77d5fee07c5e38a498065ae1a4e7 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 25 May 2017 11:14:16 -0700 Subject: [PATCH 30/55] Implicit return -> explicit return --- src/plugins/typescript.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 41336f37d7..d32d65f330 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1085,6 +1085,7 @@ export default (superClass: Class): Class => class extends super if (this.isContextual("as") && !this.hasPrecedingLineBreak()) { return tt._in.binop; } + return null; } handleSpecialBinaryOperator(node: N.Node, left: N.Expression): void { From 8524c4850f982c0559b395589048d73c24b1a10b Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 12 Jun 2017 11:24:42 -0700 Subject: [PATCH 31/55] Move handling of special imports/exports to typescript.js --- src/parser/statement.js | 32 +++++++------------------------- src/plugins/flow.js | 2 +- src/plugins/typescript.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/parser/statement.js b/src/parser/statement.js index 3c6b1b1114..ec1c420b36 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -17,9 +17,6 @@ const loopLabel = { kind: "loop" }, switchLabel = { kind: "switch" }; export default class StatementParser extends ExpressionParser { // Forward-declarations: Code defined in typescript.js +tsTryParseIndexSignature: (node: N.TsIndexSignature) => ?N.TsIndexSignature - +tsParseImportEqualsDeclaration: ( - node: N.TsImportEqualsDeclaration, - isExport?: boolean) => N.TsImportEqualsDeclaration // ### Statement parsing @@ -126,7 +123,13 @@ export default class StatementParser extends ExpressionParser { this.raise(this.state.start, "'import' and 'export' may appear only with 'sourceType: module'"); } } - return starttype === tt._import ? this.parseImport(node) : this.parseExport(node); + + this.next(); + if (starttype == tt._import) { + return this.parseImport(node); + } else { + return this.parseExport(node); + } case tt.name: if (this.state.value === "async") { @@ -918,8 +921,6 @@ export default class StatementParser extends ExpressionParser { // TODO: better type. Node is an N.AnyExport. parseExport(node: N.Node): N.Node { - this.eat(tt._export); - // export * from '...' if (this.match(tt.star)) { const specifier = this.startNode(); @@ -971,21 +972,6 @@ export default class StatementParser extends ExpressionParser { if (needsSemi) this.semicolon(); this.checkExport(node, true, true); return this.finishNode(node, "ExportDefaultDeclaration"); - } else if (this.hasPlugin("typescript") && this.match(tt._import)) { // `export import A = B;` - this.expect(tt._import); - return this.tsParseImportEqualsDeclaration(node, /* isExport */ true); - } else if (this.hasPlugin("typescript") && this.eat(tt.eq)) { // `export = x;` - const assign: N.TsExportAssignment = node; - assign.expression = this.parseExpression(); - this.semicolon(); - return this.finishNode(assign, "TSExportAssignment"); - } else if (this.hasPlugin("typescript") && this.eatContextual("as")) { // `export as namespace A;` - const decl: N.TsNamespaceExportDeclaration = node; - // See `parseNamespaceExportDeclaration` in TypeScript's own parser - this.expectContextual("namespace"); - decl.id = this.parseIdentifier(); - this.semicolon(); - return this.finishNode(decl, "TSNamespaceExportDeclaration"); } else if (this.shouldParseExportDeclaration()) { node.specifiers = []; node.source = null; @@ -1157,14 +1143,10 @@ export default class StatementParser extends ExpressionParser { // Parses import declaration. parseImport(node: N.Node): N.ImportDeclaration | N.TsImportEqualsDeclaration { - this.expect(tt._import); - // import '...' if (this.match(tt.string)) { node.specifiers = []; node.source = this.parseExprAtom(); - } else if (this.hasPlugin("typescript") && this.match(tt.name) && this.lookahead().type === tt.eq) { - return this.tsParseImportEqualsDeclaration(node); } else { node.specifiers = []; this.parseImportSpecifiers(node); diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 31d88925ec..a2d69a1b08 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -178,7 +178,7 @@ export default (superClass: Class): Class => class extends super if (lookahead.value !== "type" && lookahead.value !== "typeof") { this.unexpected(null, "Imports within a `declare module` body must always be `import type` or `import typeof`"); } - + this.next(); this.parseImport(bodyNode); } else { this.expectContextual("declare", "Only declares and type imports are allowed inside declare module"); diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 35421b9fb8..f3608d1938 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1016,6 +1016,34 @@ export default (superClass: Class): Class => class extends super // OVERRIDES // ====================================================== + parseImport(node: N.Node): N.ImportDeclaration | N.TsImportEqualsDeclaration { + if (this.match(tt.name) && this.lookahead().type === tt.eq) { + return this.tsParseImportEqualsDeclaration(node); + } + return super.parseImport(node); + } + + parseExport(node: N.Node): N.Node { + if (this.hasPlugin("typescript") && this.match(tt._import)) { // `export import A = B;` + this.expect(tt._import); + return this.tsParseImportEqualsDeclaration(node, /* isExport */ true); + } else if (this.hasPlugin("typescript") && this.eat(tt.eq)) { // `export = x;` + const assign: N.TsExportAssignment = node; + assign.expression = this.parseExpression(); + this.semicolon(); + return this.finishNode(assign, "TSExportAssignment"); + } else if (this.hasPlugin("typescript") && this.eatContextual("as")) { // `export as namespace A;` + const decl: N.TsNamespaceExportDeclaration = node; + // See `parseNamespaceExportDeclaration` in TypeScript's own parser + this.expectContextual("namespace"); + decl.id = this.parseIdentifier(); + this.semicolon(); + return this.finishNode(decl, "TSNamespaceExportDeclaration"); + } else { + return super.parseExport(node); + } + } + parseStatementContent(declaration: boolean, topLevel: ?boolean): N.Statement { if (this.state.type === tt._const) { const ahead = this.lookahead(); From 42fb31e59f1a486269a676b63978e411b837c863 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 12 Jun 2017 11:27:04 -0700 Subject: [PATCH 32/55] No need to disable export checks --- src/parser/statement.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/parser/statement.js b/src/parser/statement.js index ec1c420b36..a0accd929c 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -1034,11 +1034,6 @@ export default class StatementParser extends ExpressionParser { } checkExport(node: N.ExportNamedDeclaration, checkNames: ?boolean, isDefault?: boolean): void { - if (this.hasPlugin("typescript")) { - // TS will check exports. - return; - } - if (checkNames) { // Check for duplicate exports if (isDefault) { From 307de59e530ffb24d125ef83eec988dfea729e59 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 12 Jun 2017 11:30:55 -0700 Subject: [PATCH 33/55] Always set VariableDeclaration.init --- src/parser/statement.js | 15 ++++++++------- .../members-with-modifier-names/expected.json | 2 +- .../class/modifiers-properties/expected.json | 8 ++++---- .../typescript/const/no-initializer/expected.json | 3 ++- .../typescript/declare/const/expected.json | 6 ++++-- .../typescript/declare/destructure/expected.json | 3 ++- .../typescript/export/declare/expected.json | 3 ++- .../module-namespace/body-declare/expected.json | 3 ++- .../body-nested-declare/expected.json | 3 ++- 9 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/parser/statement.js b/src/parser/statement.js index a0accd929c..19bd16bac3 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -578,14 +578,15 @@ export default class StatementParser extends ExpressionParser { this.parseVarHead(decl); if (this.eat(tt.eq)) { decl.init = this.parseMaybeAssign(isFor); - } else if (kind === tt._const && !(this.match(tt._in) || this.isContextual("of"))) { - // `const` with no initializer is allowed in TypeScript. It could be a declaration `const x: number;`. - if (!this.hasPlugin("typescript")) { - this.unexpected(); - } - } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(tt._in) || this.isContextual("of")))) { - this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value"); } else { + if (kind === tt._const && !(this.match(tt._in) || this.isContextual("of"))) { + // `const` with no initializer is allowed in TypeScript. It could be a declaration `const x: number;`. + if (!this.hasPlugin("typescript")) { + this.unexpected(); + } + } else if (decl.id.type !== "Identifier" && !(isFor && (this.match(tt._in) || this.isContextual("of")))) { + this.raise(this.state.lastTokEnd, "Complex binding patterns require an initialization value"); + } decl.init = null; } declarations.push(this.finishNode(decl, "VariableDeclarator")); diff --git a/test/fixtures/typescript/class/members-with-modifier-names/expected.json b/test/fixtures/typescript/class/members-with-modifier-names/expected.json index 65f583716d..6d8287c3e5 100644 --- a/test/fixtures/typescript/class/members-with-modifier-names/expected.json +++ b/test/fixtures/typescript/class/members-with-modifier-names/expected.json @@ -160,7 +160,6 @@ } }, "accessibility": "public", - "static": false, "kind": "method", "computed": false, "key": { @@ -180,6 +179,7 @@ }, "name": "static" }, + "static": false, "id": null, "generator": false, "expression": false, diff --git a/test/fixtures/typescript/class/modifiers-properties/expected.json b/test/fixtures/typescript/class/modifiers-properties/expected.json index 105f235590..f7894ed6c3 100644 --- a/test/fixtures/typescript/class/modifiers-properties/expected.json +++ b/test/fixtures/typescript/class/modifiers-properties/expected.json @@ -305,8 +305,8 @@ "column": 25 } }, - "readonly": true, "abstract": true, + "readonly": true, "static": false, "computed": false, "key": { @@ -342,8 +342,8 @@ "column": 25 } }, - "readonly": true, "abstract": true, + "readonly": true, "static": false, "computed": false, "key": { @@ -526,8 +526,8 @@ } }, "accessibility": "public", - "readonly": true, "abstract": true, + "readonly": true, "static": false, "computed": false, "key": { @@ -564,8 +564,8 @@ } }, "accessibility": "public", - "readonly": true, "abstract": true, + "readonly": true, "static": false, "computed": false, "key": { diff --git a/test/fixtures/typescript/const/no-initializer/expected.json b/test/fixtures/typescript/const/no-initializer/expected.json index 6ab732e99e..380e4e88b1 100644 --- a/test/fixtures/typescript/const/no-initializer/expected.json +++ b/test/fixtures/typescript/const/no-initializer/expected.json @@ -103,7 +103,8 @@ } } } - } + }, + "init": null } ], "kind": "const" diff --git a/test/fixtures/typescript/declare/const/expected.json b/test/fixtures/typescript/declare/const/expected.json index 745fc35c98..43e1ea827a 100644 --- a/test/fixtures/typescript/declare/const/expected.json +++ b/test/fixtures/typescript/declare/const/expected.json @@ -103,7 +103,8 @@ } } } - } + }, + "init": null }, { "type": "VariableDeclarator", @@ -165,7 +166,8 @@ } } } - } + }, + "init": null } ], "kind": "const", diff --git a/test/fixtures/typescript/declare/destructure/expected.json b/test/fixtures/typescript/declare/destructure/expected.json index 54fce6821b..0a5dc26b88 100644 --- a/test/fixtures/typescript/declare/destructure/expected.json +++ b/test/fixtures/typescript/declare/destructure/expected.json @@ -341,7 +341,8 @@ ] } } - } + }, + "init": null } ], "kind": "const", diff --git a/test/fixtures/typescript/export/declare/expected.json b/test/fixtures/typescript/export/declare/expected.json index e24f3ee6ec..4481ac1082 100644 --- a/test/fixtures/typescript/export/declare/expected.json +++ b/test/fixtures/typescript/export/declare/expected.json @@ -119,7 +119,8 @@ } } } - } + }, + "init": null } ], "kind": "const", diff --git a/test/fixtures/typescript/module-namespace/body-declare/expected.json b/test/fixtures/typescript/module-namespace/body-declare/expected.json index 24e64f88da..5376141777 100644 --- a/test/fixtures/typescript/module-namespace/body-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/body-declare/expected.json @@ -149,7 +149,8 @@ } } } - } + }, + "init": null } ], "kind": "const" diff --git a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json index ced873b54e..3172253e59 100644 --- a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json @@ -195,7 +195,8 @@ } } } - } + }, + "init": null } ], "kind": "const" From 0424c7999b12145e39b541b660dc39f9e7209f04 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 12 Jun 2017 13:11:52 -0700 Subject: [PATCH 34/55] Move TypeScript-specific code out of `parseClassMember` --- src/parser/statement.js | 80 +++-- src/plugins/typescript.js | 46 ++- .../class/method-readonly/actual.js | 3 + .../class/method-readonly/options.json | 3 + .../class/modifiers-properties/actual.js | 1 + .../class/modifiers-properties/expected.json | 299 +++++++++++------- 6 files changed, 269 insertions(+), 163 deletions(-) create mode 100644 test/fixtures/typescript/class/method-readonly/actual.js create mode 100644 test/fixtures/typescript/class/method-readonly/options.json diff --git a/src/parser/statement.js b/src/parser/statement.js index 19bd16bac3..2d9d456bef 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -15,9 +15,6 @@ const empty = []; const loopLabel = { kind: "loop" }, switchLabel = { kind: "switch" }; export default class StatementParser extends ExpressionParser { - // Forward-declarations: Code defined in typescript.js - +tsTryParseIndexSignature: (node: N.TsIndexSignature) => ?N.TsIndexSignature - // ### Statement parsing // Parse a program. Initializes the parser, reads any number of @@ -717,7 +714,6 @@ export default class StatementParser extends ExpressionParser { parseClassMember(classBody: N.ClassBody, member: N.ClassMember, state: { hadConstructor: boolean }): void { // Use the appropriate variable to represent `member` once a more specific type is known. const memberAny: any = member; - const methodOrProp: N.ClassMethod | N.ClassProperty = memberAny; const method: N.ClassMethod = memberAny; const prop: N.ClassProperty = memberAny; @@ -729,9 +725,6 @@ export default class StatementParser extends ExpressionParser { return; } - const accessibility = this.parseAccessModifier(); - if (accessibility) methodOrProp.accessibility = accessibility; - let isStatic = false; if (this.match(tt.name) && this.state.value === "static") { const key = this.parseIdentifier(true); // eats 'static' @@ -755,21 +748,18 @@ export default class StatementParser extends ExpressionParser { isStatic = true; } - const { abstract, readonly } = this.parseClassMemberModifiers(); - if (abstract) methodOrProp.abstract = true; - if (readonly) prop.readonly = true; + this.parseClassMemberWithIsStatic(classBody, member, state, isStatic); + } - if (this.hasPlugin("typescript") && !abstract && !methodOrProp.static && !accessibility) { - const idx = this.tsTryParseIndexSignature(memberAny); - if (idx) { - classBody.body.push(idx); - return; - } - } + parseClassMemberWithIsStatic(classBody: N.ClassBody, member: N.ClassMember, state: { hadConstructor: boolean }, isStatic: boolean) { + const memberAny: any = member; + const methodOrProp: N.ClassMethod | N.ClassProperty = memberAny; + const method: N.ClassMethod = memberAny; + const prop: N.ClassProperty = memberAny; methodOrProp.static = isStatic; - if (!readonly && this.eat(tt.star)) { + if (this.eat(tt.star)) { // a generator method.kind = "method"; this.parsePropertyName(method); @@ -784,17 +774,11 @@ export default class StatementParser extends ExpressionParser { } const isSimple = this.match(tt.name); - const key = this.parsePropertyName(methodOrProp); - if (!methodOrProp.computed && methodOrProp.static && (methodOrProp.key.name === "prototype" || methodOrProp.key.value === "prototype")) { - this.raise(methodOrProp.key.start, "Classes may not have static property named prototype"); - } + const key = this.parseClassPropertyName(methodOrProp); - if (this.hasPlugin("typescript")) { - const optional = this.eat(tt.question); - if (optional) method.optional = true; - } + this.parsePostMemberNameModifiers(methodOrProp); - if (!readonly && this.isClassMethod()) { + if (this.isClassMethod()) { // a normal method const isConstructor = this.isNonstaticConstructor(method); if (isConstructor) { @@ -803,27 +787,21 @@ export default class StatementParser extends ExpressionParser { method.kind = "method"; } - if (isConstructor && !this.hasPlugin("typescript")) { + if (isConstructor) { if (method.decorators) { this.raise(method.start, "You can't attach decorators to a class constructor"); } // TypeScript allows multiple overloaded constructor declarations. - if (!this.hasPlugin("typescript")) { - if (state.hadConstructor) { - this.raise(key.start, "Duplicate constructor in the same class"); - } - state.hadConstructor = true; + if (state.hadConstructor && !this.hasPlugin("typescript")) { + this.raise(key.start, "Duplicate constructor in the same class"); } + state.hadConstructor = true; } this.parseClassMethod(classBody, method, false, false, isConstructor); - } else if (readonly || this.isClassProperty()) { - // a normal property - if (this.isNonstaticConstructor(prop)) { - this.raise(prop.key.start, "Classes may not have a non-static field named 'constructor'"); - } - classBody.body.push(this.parseClassProperty(prop)); + } else if (this.isClassProperty()) { + this.pushClassProperty(classBody, prop); } else if (isSimple && key.name === "async" && !this.isLineTerminator()) { // an async method const isGenerator = this.hasPlugin("asyncGenerators") && this.eat(tt.star); @@ -853,14 +831,28 @@ export default class StatementParser extends ExpressionParser { } } - // Overridden in typescript.js - parseAccessModifier(): ?N.Accessibility { - return undefined; + parseClassPropertyName(methodOrProp: N.ClassMethod | N.ClassProperty): N.Expression { + const key = this.parsePropertyName(methodOrProp); + if (!methodOrProp.computed && methodOrProp.static && (methodOrProp.key.name === "prototype" || methodOrProp.key.value === "prototype")) { + this.raise(methodOrProp.key.start, "Classes may not have static property named prototype"); + } + return key; + } + + pushClassProperty(classBody: N.ClassBody, prop: N.ClassProperty) { + if (this.isNonstaticConstructor(prop)) { + this.raise(prop.key.start, "Classes may not have a non-static field named 'constructor'"); + } + classBody.body.push(this.parseClassProperty(prop)); } // Overridden in typescript.js - parseClassMemberModifiers(): { abstract: boolean, readonly: boolean } { - return { abstract: false, readonly: false }; + // eslint-disable-next-line no-unused-vars + parsePostMemberNameModifiers(methodOrProp: N.ClassMethod | N.ClassProperty): void {} + + // Overridden in typescript.js + parseAccessModifier(): ?N.Accessibility { + return undefined; } parsePrivateClassProperty(node: N.ClassPrivateProperty): N.ClassPrivateProperty { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index f3608d1938..1c30ced856 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1048,7 +1048,7 @@ export default (superClass: Class): Class => class extends super if (this.state.type === tt._const) { const ahead = this.lookahead(); if (ahead.type === tt.name && ahead.value === "enum") { - const node: N.TsEnumDeclaration = this.startNode(); //! + const node: N.TsEnumDeclaration = this.startNode(); this.expect(tt._const); this.expectContextual("enum"); return this.tsParseEnumDeclaration(node, /* isConst */ true); @@ -1132,7 +1132,22 @@ export default (superClass: Class): Class => class extends super return { accessibility, readonly }; } - parseClassMemberModifiers(): { abstract: boolean, readonly: boolean } { + parseClassMember(classBody: N.ClassBody, member: any, state: { hadConstructor: boolean }): void { + const accessibility = this.parseAccessModifier(); + if (accessibility) member.accessibility = accessibility; + + super.parseClassMember(classBody, member, state); + } + + parseClassMemberWithIsStatic( + classBody: N.ClassBody, + member: any, + state: { hadConstructor: boolean }, + isStatic: boolean): void { + const methodOrProp: N.ClassMethod | N.ClassProperty = member; + const prop: N.ClassProperty = member; + const propOrIdx: N.ClassProperty | N.TsIndexSignature = member; + let abstract = false, readonly = false; const mod = this.tsParseModifier(["abstract", "readonly"]); @@ -1147,7 +1162,32 @@ export default (superClass: Class): Class => class extends super break; } - return { abstract, readonly }; + if (abstract) methodOrProp.abstract = true; + if (readonly) propOrIdx.readonly = true; + + if (!abstract && !isStatic && !methodOrProp.accessibility) { + const idx = this.tsTryParseIndexSignature(member); + if (idx) { + classBody.body.push(idx); + return; + } + } + + if (readonly) { + // Must be a property (if not an index signature). + methodOrProp.static = isStatic; + this.parseClassPropertyName(prop); + this.parsePostMemberNameModifiers(methodOrProp); + this.pushClassProperty(classBody, prop); + return; + } + + super.parseClassMemberWithIsStatic(classBody, member, state, isStatic); + } + + parsePostMemberNameModifiers(methodOrProp: N.ClassMethod | N.ClassProperty): void { + const optional = this.eat(tt.question); + if (optional) methodOrProp.optional = true; } parseFunctionReturnType(node: N.BodilessFunctionOrMethodBase): void { diff --git a/test/fixtures/typescript/class/method-readonly/actual.js b/test/fixtures/typescript/class/method-readonly/actual.js new file mode 100644 index 0000000000..393ccdb37b --- /dev/null +++ b/test/fixtures/typescript/class/method-readonly/actual.js @@ -0,0 +1,3 @@ +class C { + readonly m() {} +} diff --git a/test/fixtures/typescript/class/method-readonly/options.json b/test/fixtures/typescript/class/method-readonly/options.json new file mode 100644 index 0000000000..897764181d --- /dev/null +++ b/test/fixtures/typescript/class/method-readonly/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected ; (2:14)" +} diff --git a/test/fixtures/typescript/class/modifiers-properties/actual.js b/test/fixtures/typescript/class/modifiers-properties/actual.js index 874a23ca80..ab1a705653 100644 --- a/test/fixtures/typescript/class/modifiers-properties/actual.js +++ b/test/fixtures/typescript/class/modifiers-properties/actual.js @@ -1,5 +1,6 @@ abstract class C { readonly r; + readonly r2?: number; abstract a; static s; diff --git a/test/fixtures/typescript/class/modifiers-properties/expected.json b/test/fixtures/typescript/class/modifiers-properties/expected.json index f7894ed6c3..2b6c0c0818 100644 --- a/test/fixtures/typescript/class/modifiers-properties/expected.json +++ b/test/fixtures/typescript/class/modifiers-properties/expected.json @@ -1,28 +1,28 @@ { "type": "File", "start": 0, - "end": 370, + "end": 396, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 20, + "line": 21, "column": 1 } }, "program": { "type": "Program", "start": 0, - "end": 370, + "end": 396, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 20, + "line": 21, "column": 1 } }, @@ -31,14 +31,14 @@ { "type": "ClassDeclaration", "start": 0, - "end": 370, + "end": 396, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 20, + "line": 21, "column": 1 } }, @@ -64,14 +64,14 @@ "body": { "type": "ClassBody", "start": 17, - "end": 370, + "end": 396, "loc": { "start": { "line": 1, "column": 17 }, "end": { - "line": 20, + "line": 21, "column": 1 } }, @@ -115,7 +115,7 @@ { "type": "ClassProperty", "start": 39, - "end": 50, + "end": 60, "loc": { "start": { "line": 3, @@ -123,16 +123,16 @@ }, "end": { "line": 3, - "column": 15 + "column": 25 } }, - "abstract": true, + "readonly": true, "static": false, "computed": false, "key": { "type": "Identifier", "start": 48, - "end": 49, + "end": 50, "loc": { "start": { "line": 3, @@ -140,6 +140,73 @@ }, "end": { "line": 3, + "column": 15 + }, + "identifierName": "r2" + }, + "name": "r2" + }, + "optional": true, + "typeAnnotation": { + "type": "TypeAnnotation", + "start": 53, + "end": 59, + "loc": { + "start": { + "line": 3, + "column": 18 + }, + "end": { + "line": 3, + "column": 24 + } + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 53, + "end": 59, + "loc": { + "start": { + "line": 3, + "column": 18 + }, + "end": { + "line": 3, + "column": 24 + } + } + } + }, + "value": null + }, + { + "type": "ClassProperty", + "start": 65, + "end": 76, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 15 + } + }, + "abstract": true, + "static": false, + "computed": false, + "key": { + "type": "Identifier", + "start": 74, + "end": 75, + "loc": { + "start": { + "line": 4, + "column": 13 + }, + "end": { + "line": 4, "column": 14 }, "identifierName": "a" @@ -150,15 +217,15 @@ }, { "type": "ClassProperty", - "start": 55, - "end": 64, + "start": 81, + "end": 90, "loc": { "start": { - "line": 4, + "line": 5, "column": 4 }, "end": { - "line": 4, + "line": 5, "column": 13 } }, @@ -166,15 +233,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 62, - "end": 63, + "start": 88, + "end": 89, "loc": { "start": { - "line": 4, + "line": 5, "column": 11 }, "end": { - "line": 4, + "line": 5, "column": 12 }, "identifierName": "s" @@ -185,15 +252,15 @@ }, { "type": "ClassProperty", - "start": 70, - "end": 80, + "start": 96, + "end": 106, "loc": { "start": { - "line": 6, + "line": 7, "column": 4 }, "end": { - "line": 6, + "line": 7, "column": 14 } }, @@ -202,15 +269,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 77, - "end": 79, + "start": 103, + "end": 105, "loc": { "start": { - "line": 6, + "line": 7, "column": 11 }, "end": { - "line": 6, + "line": 7, "column": 13 }, "identifierName": "pu" @@ -221,15 +288,15 @@ }, { "type": "ClassProperty", - "start": 85, - "end": 98, + "start": 111, + "end": 124, "loc": { "start": { - "line": 7, + "line": 8, "column": 4 }, "end": { - "line": 7, + "line": 8, "column": 17 } }, @@ -238,15 +305,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 95, - "end": 97, + "start": 121, + "end": 123, "loc": { "start": { - "line": 7, + "line": 8, "column": 14 }, "end": { - "line": 7, + "line": 8, "column": 16 }, "identifierName": "po" @@ -257,15 +324,15 @@ }, { "type": "ClassProperty", - "start": 103, - "end": 114, + "start": 129, + "end": 140, "loc": { "start": { - "line": 8, + "line": 9, "column": 4 }, "end": { - "line": 8, + "line": 9, "column": 15 } }, @@ -274,15 +341,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 111, - "end": 113, + "start": 137, + "end": 139, "loc": { "start": { - "line": 8, + "line": 9, "column": 12 }, "end": { - "line": 8, + "line": 9, "column": 14 }, "identifierName": "pi" @@ -293,15 +360,15 @@ }, { "type": "ClassProperty", - "start": 120, - "end": 141, + "start": 146, + "end": 167, "loc": { "start": { - "line": 10, + "line": 11, "column": 4 }, "end": { - "line": 10, + "line": 11, "column": 25 } }, @@ -311,15 +378,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 138, - "end": 140, + "start": 164, + "end": 166, "loc": { "start": { - "line": 10, + "line": 11, "column": 22 }, "end": { - "line": 10, + "line": 11, "column": 24 }, "identifierName": "ra" @@ -330,15 +397,15 @@ }, { "type": "ClassProperty", - "start": 146, - "end": 167, + "start": 172, + "end": 193, "loc": { "start": { - "line": 11, + "line": 12, "column": 4 }, "end": { - "line": 11, + "line": 12, "column": 25 } }, @@ -348,15 +415,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 164, - "end": 166, + "start": 190, + "end": 192, "loc": { "start": { - "line": 11, + "line": 12, "column": 22 }, "end": { - "line": 11, + "line": 12, "column": 24 }, "identifierName": "ar" @@ -367,15 +434,15 @@ }, { "type": "ClassProperty", - "start": 172, - "end": 191, + "start": 198, + "end": 217, "loc": { "start": { - "line": 12, + "line": 13, "column": 4 }, "end": { - "line": 12, + "line": 13, "column": 23 } }, @@ -384,15 +451,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 188, - "end": 190, + "start": 214, + "end": 216, "loc": { "start": { - "line": 12, + "line": 13, "column": 20 }, "end": { - "line": 12, + "line": 13, "column": 22 }, "identifierName": "sr" @@ -403,15 +470,15 @@ }, { "type": "ClassProperty", - "start": 197, - "end": 217, + "start": 223, + "end": 243, "loc": { "start": { - "line": 14, + "line": 15, "column": 4 }, "end": { - "line": 14, + "line": 15, "column": 24 } }, @@ -421,15 +488,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 213, - "end": 216, + "start": 239, + "end": 242, "loc": { "start": { - "line": 14, + "line": 15, "column": 20 }, "end": { - "line": 14, + "line": 15, "column": 23 }, "identifierName": "pur" @@ -440,15 +507,15 @@ }, { "type": "ClassProperty", - "start": 222, - "end": 242, + "start": 248, + "end": 268, "loc": { "start": { - "line": 15, + "line": 16, "column": 4 }, "end": { - "line": 15, + "line": 16, "column": 24 } }, @@ -458,15 +525,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 238, - "end": 241, + "start": 264, + "end": 267, "loc": { "start": { - "line": 15, + "line": 16, "column": 20 }, "end": { - "line": 15, + "line": 16, "column": 23 }, "identifierName": "pua" @@ -477,15 +544,15 @@ }, { "type": "ClassProperty", - "start": 247, - "end": 265, + "start": 273, + "end": 291, "loc": { "start": { - "line": 16, + "line": 17, "column": 4 }, "end": { - "line": 16, + "line": 17, "column": 22 } }, @@ -494,15 +561,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 261, - "end": 264, + "start": 287, + "end": 290, "loc": { "start": { - "line": 16, + "line": 17, "column": 18 }, "end": { - "line": 16, + "line": 17, "column": 21 }, "identifierName": "pus" @@ -513,15 +580,15 @@ }, { "type": "ClassProperty", - "start": 270, - "end": 300, + "start": 296, + "end": 326, "loc": { "start": { - "line": 17, + "line": 18, "column": 4 }, "end": { - "line": 17, + "line": 18, "column": 34 } }, @@ -532,15 +599,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 295, - "end": 299, + "start": 321, + "end": 325, "loc": { "start": { - "line": 17, + "line": 18, "column": 29 }, "end": { - "line": 17, + "line": 18, "column": 33 }, "identifierName": "pura" @@ -551,15 +618,15 @@ }, { "type": "ClassProperty", - "start": 305, - "end": 335, + "start": 331, + "end": 361, "loc": { "start": { - "line": 18, + "line": 19, "column": 4 }, "end": { - "line": 18, + "line": 19, "column": 34 } }, @@ -570,15 +637,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 330, - "end": 334, + "start": 356, + "end": 360, "loc": { "start": { - "line": 18, + "line": 19, "column": 29 }, "end": { - "line": 18, + "line": 19, "column": 33 }, "identifierName": "puar" @@ -589,15 +656,15 @@ }, { "type": "ClassProperty", - "start": 340, - "end": 368, + "start": 366, + "end": 394, "loc": { "start": { - "line": 19, + "line": 20, "column": 4 }, "end": { - "line": 19, + "line": 20, "column": 32 } }, @@ -607,15 +674,15 @@ "computed": false, "key": { "type": "Identifier", - "start": 363, - "end": 367, + "start": 389, + "end": 393, "loc": { "start": { - "line": 19, + "line": 20, "column": 27 }, "end": { - "line": 19, + "line": 20, "column": 31 }, "identifierName": "pusr" From b6b2eb67cb33932945d1e42a511d34712222521b Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 12 Jun 2017 13:27:30 -0700 Subject: [PATCH 35/55] Rename typeArguments to typeParameters --- src/parser/expression.js | 4 ++-- src/plugins/typescript.js | 6 +++--- src/types.js | 6 +++--- .../class/expression-extends-implements/expected.json | 4 ++-- .../typescript/class/expression-implements/expected.json | 4 ++-- .../typescript/class/extends-implements/expected.json | 2 +- test/fixtures/typescript/class/implements/expected.json | 2 +- test/fixtures/typescript/interface/extends/expected.json | 2 +- .../typescript/type-alias/generic-complex/expected.json | 2 +- test/fixtures/typescript/type-arguments/call/expected.json | 4 ++-- test/fixtures/typescript/type-arguments/new/expected.json | 4 ++-- .../typescript/types/function-in-generic/expected.json | 2 +- .../typescript/types/reference-generic-nested/expected.json | 4 ++-- .../typescript/types/reference-generic/expected.json | 2 +- 14 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 116abc4bd0..2ebecc626e 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -405,7 +405,7 @@ export default class ExpressionParser extends LValParser { return this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node, typeAmbiguous); } else { if (typeAmbiguous) { - node.typeArguments = this.typeArgumentsFromAmbiguous(typeAmbiguous); + node.typeParameters = this.typeArgumentsFromAmbiguous(typeAmbiguous); } this.toReferencedList(node.arguments); } @@ -834,7 +834,7 @@ export default class ExpressionParser extends LValParser { if (this.isRelational("<")) { const typeArguments = this.parseTypeArguments(); - if (typeArguments) node.typeArguments = typeArguments; + if (typeArguments) node.typeParameters = typeArguments; } if (this.eat(tt.parenL)) { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 1c30ced856..b8177563c6 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -201,7 +201,7 @@ export default (superClass: Class): Class => class extends super const node: N.TsTypeReference = this.startNode(); node.typeName = this.tsParseEntityName(/* allowReservedWords */ false); if (!this.hasPrecedingLineBreak() && this.isRelational("<")) { - node.typeArguments = this.parseTypeArguments(); + node.typeParameters = this.parseTypeArguments(); } return this.finishNode(node, "TSTypeReference"); } @@ -742,7 +742,7 @@ export default (superClass: Class): Class => class extends super // then has grammar errors later if it's not an EntityName. node.expression = this.tsParseEntityName(/* allowReservedWords */ false); if (this.isRelational("<")) { - node.typeArguments = this.parseTypeArguments(); + node.typeParameters = this.parseTypeArguments(); } return this.finishNode(node, "TSExpressionWithTypeArguments"); @@ -1087,7 +1087,7 @@ export default (superClass: Class): Class => class extends super node.params = types.map((typeAmbiguous) => { const typeParam = this.nodeWithSamePosition(typeAmbiguous, "TypeParameter"); const { typeAnnotation: type, constraint, default: defaultType } = typeAmbiguous; - if (type.type === "TSTypeReference" && !type.typeArguments && type.typeName.type === "Identifier") { + if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") { typeParam.name = type.typeName.name; } else { throw this.raise(type.start, "Type parameter must be a plain identifier."); diff --git a/src/types.js b/src/types.js index 475c2c77df..de1dd36df2 100644 --- a/src/types.js +++ b/src/types.js @@ -475,7 +475,7 @@ export type ConditionalExpression = NodeBase & { export type CallOrNewBase = NodeBase & { callee: Expression | Super | Import; arguments: Array; // TODO: $ReadOnlyArray - typeArguments?: ?TypeParameterInstantiation; // TODO: Not in spec + typeParameters?: ?TypeParameterInstantiation; // TODO: Not in spec }; export type CallExpression = CallOrNewBase & { @@ -958,7 +958,7 @@ export type TsConstructorType = TsTypeBase & TsSignatureDeclarationBase & { export type TsTypeReference = TsTypeBase & { type: "TSTypeReference"; typeName: TsEntityName; - typeArguments?: TypeParameterInstantiation; + typeParameters?: TypeParameterInstantiation; }; export type TsTypePredicate = TsTypeBase & { @@ -1053,7 +1053,7 @@ export type TSInterfaceBody = NodeBase & { export type TsExpressionWithTypeArguments = TsTypeBase & { type: "TSExpressionWithTypeArguments"; expression: TsEntityName; - typeArguments?: TypeParameterInstantiation; + typeParameters?: TypeParameterInstantiation; }; export type TsTypeAliasDeclaration = DeclarationBase & { diff --git a/test/fixtures/typescript/class/expression-extends-implements/expected.json b/test/fixtures/typescript/class/expression-extends-implements/expected.json index 8cd03c7f03..ab219ddf0a 100644 --- a/test/fixtures/typescript/class/expression-extends-implements/expected.json +++ b/test/fixtures/typescript/class/expression-extends-implements/expected.json @@ -203,7 +203,7 @@ "name": "Y" } }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 36, "end": 39, @@ -467,7 +467,7 @@ "name": "Y" } }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 83, "end": 86, diff --git a/test/fixtures/typescript/class/expression-implements/expected.json b/test/fixtures/typescript/class/expression-implements/expected.json index ac2ce74175..a3ec0e0b6e 100644 --- a/test/fixtures/typescript/class/expression-implements/expected.json +++ b/test/fixtures/typescript/class/expression-implements/expected.json @@ -121,7 +121,7 @@ "name": "Y" } }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 21, "end": 24, @@ -304,7 +304,7 @@ "name": "Y" } }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 53, "end": 56, diff --git a/test/fixtures/typescript/class/extends-implements/expected.json b/test/fixtures/typescript/class/extends-implements/expected.json index 74fafc58dd..c8dee7a629 100644 --- a/test/fixtures/typescript/class/extends-implements/expected.json +++ b/test/fixtures/typescript/class/extends-implements/expected.json @@ -205,7 +205,7 @@ "name": "Y" } }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 37, "end": 40, diff --git a/test/fixtures/typescript/class/implements/expected.json b/test/fixtures/typescript/class/implements/expected.json index 326a741986..4af4f8f4d0 100644 --- a/test/fixtures/typescript/class/implements/expected.json +++ b/test/fixtures/typescript/class/implements/expected.json @@ -124,7 +124,7 @@ "name": "Y" } }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 22, "end": 25, diff --git a/test/fixtures/typescript/interface/extends/expected.json b/test/fixtures/typescript/interface/extends/expected.json index 60b3d84728..ef4064864f 100644 --- a/test/fixtures/typescript/interface/extends/expected.json +++ b/test/fixtures/typescript/interface/extends/expected.json @@ -123,7 +123,7 @@ "name": "Y" } }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 23, "end": 26, diff --git a/test/fixtures/typescript/type-alias/generic-complex/expected.json b/test/fixtures/typescript/type-alias/generic-complex/expected.json index 2a8adea5cf..f27221db96 100644 --- a/test/fixtures/typescript/type-alias/generic-complex/expected.json +++ b/test/fixtures/typescript/type-alias/generic-complex/expected.json @@ -218,7 +218,7 @@ }, "name": "Array" }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 48, "end": 51, diff --git a/test/fixtures/typescript/type-arguments/call/expected.json b/test/fixtures/typescript/type-arguments/call/expected.json index 00aa437e7e..6272c2073e 100644 --- a/test/fixtures/typescript/type-arguments/call/expected.json +++ b/test/fixtures/typescript/type-arguments/call/expected.json @@ -74,7 +74,7 @@ "name": "f" }, "arguments": [], - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 6, "end": 6, @@ -171,7 +171,7 @@ "name": "f" }, "arguments": [], - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 17, "end": 17, diff --git a/test/fixtures/typescript/type-arguments/new/expected.json b/test/fixtures/typescript/type-arguments/new/expected.json index 58d88e4707..333b5faa84 100644 --- a/test/fixtures/typescript/type-arguments/new/expected.json +++ b/test/fixtures/typescript/type-arguments/new/expected.json @@ -73,7 +73,7 @@ }, "name": "C" }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 5, "end": 8, @@ -170,7 +170,7 @@ }, "name": "C" }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 17, "end": 23, diff --git a/test/fixtures/typescript/types/function-in-generic/expected.json b/test/fixtures/typescript/types/function-in-generic/expected.json index c4c2571134..946b34e4de 100644 --- a/test/fixtures/typescript/types/function-in-generic/expected.json +++ b/test/fixtures/typescript/types/function-in-generic/expected.json @@ -118,7 +118,7 @@ }, "name": "Array" }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 12, "end": 24, diff --git a/test/fixtures/typescript/types/reference-generic-nested/expected.json b/test/fixtures/typescript/types/reference-generic-nested/expected.json index 33ab435e17..32a5d1c7b2 100644 --- a/test/fixtures/typescript/types/reference-generic-nested/expected.json +++ b/test/fixtures/typescript/types/reference-generic-nested/expected.json @@ -118,7 +118,7 @@ }, "name": "Array" }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 12, "end": 27, @@ -164,7 +164,7 @@ }, "name": "Array" }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 18, "end": 26, diff --git a/test/fixtures/typescript/types/reference-generic/expected.json b/test/fixtures/typescript/types/reference-generic/expected.json index cd67c76858..9f720f041b 100644 --- a/test/fixtures/typescript/types/reference-generic/expected.json +++ b/test/fixtures/typescript/types/reference-generic/expected.json @@ -118,7 +118,7 @@ }, "name": "Array" }, - "typeArguments": { + "typeParameters": { "type": "TypeParameterInstantiation", "start": 12, "end": 20, From a4cb112605ee9617aeae688958c4bb1f10d372b7 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 12 Jun 2017 13:50:23 -0700 Subject: [PATCH 36/55] Be more strict about when optional properties are allowed --- src/parser/lval.js | 3 +++ src/plugins/flow.js | 6 ++++++ src/plugins/typescript.js | 6 ++++++ src/types.js | 7 ++++--- .../class/parameter-properties-binding-patterns/actual.js | 3 +++ .../parameter-properties-binding-patterns/options.json | 3 +++ .../typescript/function/pattern-parameters/actual.js | 1 + .../typescript/function/pattern-parameters/options.json | 3 +++ 8 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/typescript/class/parameter-properties-binding-patterns/actual.js create mode 100644 test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json create mode 100644 test/fixtures/typescript/function/pattern-parameters/actual.js create mode 100644 test/fixtures/typescript/function/pattern-parameters/options.json diff --git a/src/parser/lval.js b/src/parser/lval.js index d9de1ccba7..b70892cc58 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -212,6 +212,9 @@ export default class LValParser extends NodeUtils { } if (accessibility) pp.accessibility = accessibility; if (readonly) pp.readonly = readonly; + if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") { + throw this.raise(pp.start, "A parameter property may not be declared using a binding pattern."); + } pp.parameter = elt; elts.push(this.finishNode(pp, "TSParameterProperty")); } else { diff --git a/src/plugins/flow.js b/src/plugins/flow.js index a2d69a1b08..01eb1fad52 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -1336,6 +1336,12 @@ export default (superClass: Class): Class => class extends super parseAssignableListItemTypes(param: N.Pattern): N.Pattern { if (this.eat(tt.question)) { + if (param.type !== "Identifier") { + throw this.raise( + param.start, + "A binding pattern parameter cannot be optional in an implementation signature."); + } + param.optional = true; } if (this.match(tt.colon)) { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index b8177563c6..83dd6d952d 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1477,6 +1477,12 @@ export default (superClass: Class): Class => class extends super // Allow type annotations inside of a parameter list. parseAssignableListItemTypes(param: N.Pattern) { if (this.eat(tt.question)) { + if (param.type !== "Identifier") { + throw this.raise( + param.start, + "A binding pattern parameter cannot be optional in an implementation signature."); + } + param.optional = true; } const type = this.tsTryParseTypeAnnotation(); diff --git a/src/types.js b/src/types.js index de1dd36df2..44aa8334bf 100644 --- a/src/types.js +++ b/src/types.js @@ -68,6 +68,9 @@ export type Identifier = PatternBase & { name: string; __clone(): Identifier; + + // TypeScript only. Used in case of an optional parameter. + optional?: ?true; }; export type PrivateName = NodeBase & { @@ -523,8 +526,6 @@ export type PatternBase = HasDecorators & { // TODO: All not in spec // Flow/TypeScript only: typeAnnotation?: ?TypeAnnotation; - // TypeScript only: - optional?: ?true; }; export type AssignmentProperty = ObjectProperty & { @@ -814,7 +815,7 @@ export type TSParameterProperty = HasDecorators & { // At least one of `accessibility` or `readonly` must be set. accessibility?: ?Accessibility; readonly?: ?true; - parameter: Pattern; + parameter: Identifier | AssignmentPattern; } export type OptTSDeclareFunction = BodilessFunctionBase & DeclarationBase & { diff --git a/test/fixtures/typescript/class/parameter-properties-binding-patterns/actual.js b/test/fixtures/typescript/class/parameter-properties-binding-patterns/actual.js new file mode 100644 index 0000000000..412241a327 --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties-binding-patterns/actual.js @@ -0,0 +1,3 @@ +class C { + constructor(public []) {} +} diff --git a/test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json b/test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json new file mode 100644 index 0000000000..67137f2107 --- /dev/null +++ b/test/fixtures/typescript/class/parameter-properties-binding-patterns/options.json @@ -0,0 +1,3 @@ +{ + "throws": "A parameter property may not be declared using a binding pattern. (2:23)" +} diff --git a/test/fixtures/typescript/function/pattern-parameters/actual.js b/test/fixtures/typescript/function/pattern-parameters/actual.js new file mode 100644 index 0000000000..d7fa2d78dc --- /dev/null +++ b/test/fixtures/typescript/function/pattern-parameters/actual.js @@ -0,0 +1 @@ +function f([]?, {}) {} diff --git a/test/fixtures/typescript/function/pattern-parameters/options.json b/test/fixtures/typescript/function/pattern-parameters/options.json new file mode 100644 index 0000000000..fb2831d618 --- /dev/null +++ b/test/fixtures/typescript/function/pattern-parameters/options.json @@ -0,0 +1,3 @@ +{ + "throws": "A binding pattern parameter cannot be optional in an implementation signature. (1:11)" +} From dd115dea7db5b1926431aa0cfc72aca0908e1731 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 12 Jun 2017 15:15:12 -0700 Subject: [PATCH 37/55] Don't check for duplicate exports --- src/plugins/typescript.js | 11 + .../export/nested-same-name/actual.js | 4 + .../export/nested-same-name/expected.json | 274 ++++++++++++++++++ 3 files changed, 289 insertions(+) create mode 100644 test/fixtures/typescript/export/nested-same-name/actual.js create mode 100644 test/fixtures/typescript/export/nested-same-name/expected.json diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 83dd6d952d..9609d97541 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1016,6 +1016,17 @@ export default (superClass: Class): Class => class extends super // OVERRIDES // ====================================================== + /* + Don't bother doing this check in TypeScript code because: + 1. We may have a nested export statement with the same name: + export const x = 0; + export namespace N { + export const x = 1; + } + 2. We have a type checker to warn us about this sort of thing. + */ + checkDuplicateExports() {} + parseImport(node: N.Node): N.ImportDeclaration | N.TsImportEqualsDeclaration { if (this.match(tt.name) && this.lookahead().type === tt.eq) { return this.tsParseImportEqualsDeclaration(node); diff --git a/test/fixtures/typescript/export/nested-same-name/actual.js b/test/fixtures/typescript/export/nested-same-name/actual.js new file mode 100644 index 0000000000..e0c638df32 --- /dev/null +++ b/test/fixtures/typescript/export/nested-same-name/actual.js @@ -0,0 +1,4 @@ +export const x = 0; +export namespace N { + export const x = 1; +} diff --git a/test/fixtures/typescript/export/nested-same-name/expected.json b/test/fixtures/typescript/export/nested-same-name/expected.json new file mode 100644 index 0000000000..fb69187bd9 --- /dev/null +++ b/test/fixtures/typescript/export/nested-same-name/expected.json @@ -0,0 +1,274 @@ +{ + "type": "File", + "start": 0, + "end": 66, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 66, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start": 7, + "end": 19, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 19 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 13, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "NumericLiteral", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + ], + "kind": "const" + } + }, + { + "type": "ExportNamedDeclaration", + "start": 20, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "TSModuleDeclaration", + "start": 27, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 17 + }, + "end": { + "line": 2, + "column": 18 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 39, + "end": 66, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 45, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start": 52, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 23 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 58, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "id": { + "type": "Identifier", + "start": 58, + "end": 59, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 18 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "NumericLiteral", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + ], + "kind": "const" + } + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file From 07cd896be4c13482be8e6a37273a02f9a26834ca Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 09:44:40 -0700 Subject: [PATCH 38/55] Fix type of TSDeclareMethod --- src/types.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/types.js b/src/types.js index 44aa8334bf..856017e76d 100644 --- a/src/types.js +++ b/src/types.js @@ -593,13 +593,15 @@ export type MethodBase = FunctionBase & { export type MethodKind = "constructor" | "method" | "get" | "set"; -export type ClassMethod = MethodBase & ClassMemberBase & { +export type ClassMethodOrDeclareMethodCommon = ClassMemberBase & { type: "ClassMethod"; key: Expression; kind: MethodKind; static: boolean; decorators: $ReadOnlyArray; +}; +export type ClassMethod = MethodBase & ClassMethodOrDeclareMethodCommon & { variance?: ?FlowVariance; // TODO: Not in spec }; @@ -826,7 +828,7 @@ export type TSDeclareFunction = OptTSDeclareFunction & { id: Identifier; }; -export type TSDeclareMethod = BodilessFunctionOrMethodBase & ClassMemberBase & { +export type TSDeclareMethod = BodilessFunctionOrMethodBase & ClassMethodOrDeclareMethodCommon & { type: "TSDeclareMethod"; +kind: MethodKind; }; From a127a06128f091eaf0160bb125682e5e9d5ee369 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 09:46:33 -0700 Subject: [PATCH 39/55] Move checkReservedWord handling to override --- src/parser/expression.js | 7 ------- src/plugins/typescript.js | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 2ebecc626e..1a825f6f8d 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -1281,13 +1281,6 @@ export default class ExpressionParser extends LValParser { } checkReservedWord(word: string, startLoc: number, checkKeywords: boolean, isBinding: boolean): void { - if (this.hasPlugin("typescript")) { - // Don't bother checking for TypeScript code. - // Strict mode words may be allowed as in `declare namespace N { const static: number; }`. - // And we have a type checker anyway, so don't bother having the parser do it. - return; - } - if (this.isReservedWord(word) || (checkKeywords && this.isKeyword(word))) { this.raise(startLoc, word + " is a reserved word"); } diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 9609d97541..a8f82a8529 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1016,6 +1016,13 @@ export default (superClass: Class): Class => class extends super // OVERRIDES // ====================================================== + // eslint-disable-next-line no-unused-vars + checkReservedWord(word: string, startLoc: number, checkKeywords: boolean, isBinding: boolean): void { + // Don't bother checking for TypeScript code. + // Strict mode words may be allowed as in `declare namespace N { const static: number; }`. + // And we have a type checker anyway, so don't bother having the parser do it. + } + /* Don't bother doing this check in TypeScript code because: 1. We may have a nested export statement with the same name: From bee125d51ced2d80b12b2cc111c9d1f31c456bbd Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 10:00:36 -0700 Subject: [PATCH 40/55] Use an override for parseExprOp --- src/parser/expression.js | 33 +--- src/plugins/typescript.js | 30 ++-- test/fixtures/typescript/cast/as/actual.js | 1 + .../fixtures/typescript/cast/as/expected.json | 142 +++++++++++++++++- 4 files changed, 162 insertions(+), 44 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 1a825f6f8d..c5408fec63 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -200,19 +200,15 @@ export default class ExpressionParser extends LValParser { // operator that has a lower precedence than the set it is parsing. parseExprOp(left: N.Expression, leftStartPos: number, leftStartLoc: Position, minPrec: number, noIn: ?boolean): N.Expression { - let prec = this.state.type.binop; - const specialPrec = this.getSpecialBinaryOperator(); - if (specialPrec != null) { - prec = specialPrec; - } - + const prec = this.state.type.binop; if (prec != null && (!noIn || !this.match(tt._in))) { if (prec > minPrec) { const node = this.startNodeAt(leftStartPos, leftStartLoc); - const operator = this.state.value; + node.left = left; + node.operator = this.state.value; if ( - operator === "**" && + node.operator === "**" && left.type === "UnaryExpression" && left.extra && !left.extra.parenthesizedArgument && @@ -224,30 +220,17 @@ export default class ExpressionParser extends LValParser { const op = this.state.type; this.next(); - if (specialPrec != null) { - this.handleSpecialBinaryOperator(node, left); - } else { - const startPos = this.state.start; - const startLoc = this.state.startLoc; - node.left = left; - node.operator = operator; - node.right = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn); - this.finishNode(node, (op === tt.logicalOR || op === tt.logicalAND) ? "LogicalExpression" : "BinaryExpression"); - } + const startPos = this.state.start; + const startLoc = this.state.startLoc; + node.right = this.parseExprOp(this.parseMaybeUnary(), startPos, startLoc, op.rightAssociative ? prec - 1 : prec, noIn); + this.finishNode(node, (op === tt.logicalOR || op === tt.logicalAND) ? "LogicalExpression" : "BinaryExpression"); return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn); } } return left; } - getSpecialBinaryOperator(): ?number { return null; } - // eslint-disable-next-line no-unused-vars - handleSpecialBinaryOperator(node: N.Node, left: N.Expression): void { - // istanbul ignore next - throw new Error("Must be overridden if a plugin defines special binary operators."); - } - // Parse unary operators, both prefix and postfix. parseMaybeUnary(refShorthandDefaultPos: ?Pos): N.Expression { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index a8f82a8529..c060a587c2 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1016,6 +1016,23 @@ export default (superClass: Class): Class => class extends super // OVERRIDES // ====================================================== + parseExprOp( + left: N.Expression, + leftStartPos: number, + leftStartLoc: Position, + minPrec: number, + noIn: ?boolean) { + if (nonNull(tt._in.binop) > minPrec && !this.hasPrecedingLineBreak() && this.eatContextual("as")) { + const node: N.TsAsExpression = this.startNodeAt(leftStartPos, leftStartLoc); + node.expression = left; + node.typeAnnotation = this.tsParseType(); + this.finishNode(node, "TSAsExpression"); + return this.parseExprOp(node, leftStartPos, leftStartLoc, minPrec, noIn); + } + + return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec, noIn); + } + // eslint-disable-next-line no-unused-vars checkReservedWord(word: string, startLoc: number, checkKeywords: boolean, isBinding: boolean): void { // Don't bother checking for TypeScript code. @@ -1127,19 +1144,6 @@ export default (superClass: Class): Class => class extends super return this.finishNode(node, "TypeParameterInstantiation"); } - getSpecialBinaryOperator(): ?number { - if (this.isContextual("as") && !this.hasPrecedingLineBreak()) { - return tt._in.binop; - } - return null; - } - - handleSpecialBinaryOperator(node: N.Node, left: N.Expression): void { - node.expression = left; - node.typeAnnotation = this.tsParseType(); - this.finishNode(node, "TSAsExpression"); - } - parseAccessModifier(): ?N.Accessibility { return this.tsParseModifier(["public", "protected", "private"]); } diff --git a/test/fixtures/typescript/cast/as/actual.js b/test/fixtures/typescript/cast/as/actual.js index 10c643e062..4ecdb2197e 100644 --- a/test/fixtures/typescript/cast/as/actual.js +++ b/test/fixtures/typescript/cast/as/actual.js @@ -1,3 +1,4 @@ x as T; x < y as boolean; // (x < y) as boolean; x === 1 as number; // x === (1 as number); +x as any as T; diff --git a/test/fixtures/typescript/cast/as/expected.json b/test/fixtures/typescript/cast/as/expected.json index 7b1fd54278..89db7218dd 100644 --- a/test/fixtures/typescript/cast/as/expected.json +++ b/test/fixtures/typescript/cast/as/expected.json @@ -1,29 +1,29 @@ { "type": "File", "start": 0, - "end": 91, + "end": 106, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 3, - "column": 42 + "line": 4, + "column": 14 } }, "program": { "type": "Program", "start": 0, - "end": 91, + "end": 106, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 3, - "column": 42 + "line": 4, + "column": 14 } }, "sourceType": "module", @@ -355,6 +355,136 @@ } } ] + }, + { + "type": "ExpressionStatement", + "start": 92, + "end": 106, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 14 + } + }, + "expression": { + "type": "TSAsExpression", + "start": 92, + "end": 105, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "expression": { + "type": "TSAsExpression", + "start": 92, + "end": 100, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 8 + } + }, + "expression": { + "type": "Identifier", + "start": 92, + "end": 93, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + }, + "identifierName": "x" + }, + "name": "x", + "leadingComments": null + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 97, + "end": 100, + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 8 + } + } + }, + "leadingComments": null + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 104, + "end": 105, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 13 + } + }, + "typeName": { + "type": "Identifier", + "start": 104, + "end": 105, + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 13 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "leadingComments": null + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " x === (1 as number);", + "start": 68, + "end": 91, + "loc": { + "start": { + "line": 3, + "column": 19 + }, + "end": { + "line": 3, + "column": 42 + } + } + } + ] } ], "directives": [] From 024f18e4b788a727a75728d9e1a0bb1f7773c3ab Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 12:53:08 -0700 Subject: [PATCH 41/55] Factor typescript-specific code out of parseSubscript --- src/parser/expression.js | 205 +++++++-------- src/parser/statement.js | 8 +- src/plugins/typescript.js | 170 +++++++------ src/types.js | 12 - .../async-generic-false-positive/actual.js | 2 + .../expected.json | 235 ++++++++++++++++++ .../async-generic/expected.json | 8 +- .../type-arguments/call/expected.json | 16 +- test/helpers/runFixtureTests.js | 3 + 9 files changed, 450 insertions(+), 209 deletions(-) create mode 100644 test/fixtures/typescript/arrow-function/async-generic-false-positive/actual.js create mode 100644 test/fixtures/typescript/arrow-function/async-generic-false-positive/expected.json diff --git a/src/parser/expression.js b/src/parser/expression.js index c5408fec63..775f4dfe87 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -294,131 +294,121 @@ export default class ExpressionParser extends LValParser { return this.parseSubscripts(expr, startPos, startLoc); } - parseSubscripts(base: N.Expression, startPos: number, startLoc: Position, noCalls?: boolean): N.Expression { + parseSubscripts(base: N.Expression, startPos: number, startLoc: Position, noCalls?: ?boolean): N.Expression { + const state = { stop: false }; for (;;) { - if (!noCalls && this.eat(tt.doubleColon)) { - const node = this.startNodeAt(startPos, startLoc); - node.object = base; - node.callee = this.parseNoCallExpr(); - return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls); - } else if (this.hasPlugin("typescript") && this.eat(tt.bang)) { - const nonNullExpression: N.TsNonNullExpression = this.startNodeAt(startPos, startLoc); - nonNullExpression.expression = base; - base = this.finishNode(nonNullExpression, "TSNonNullExpression"); - } else if (this.match(tt.questionDot)) { - if (!this.hasPlugin("optionalChaining")) { - this.raise(startPos, "You can only use optional-chaining when the 'optionalChaining' plugin is enabled."); - } + const node = this.parseSubscript(base, startPos, startLoc, noCalls, state); + if (state.stop) { + return node; + } + base = node; + } - if (noCalls && this.lookahead().type == tt.parenL) { - return base; - } - this.next(); + // instanbul ignore next + throw new Error("Unreachable"); + } - const node = this.startNodeAt(startPos, startLoc); - - if (this.eat(tt.bracketL)) { - node.object = base; - node.property = this.parseExpression(); - node.computed = true; - node.optional = true; - this.expect(tt.bracketR); - base = this.finishNode(node, "MemberExpression"); - } else if (this.eat(tt.parenL)) { - const possibleAsync = this.state.potentialArrowAt === base.start && - base.type === "Identifier" && - base.name === "async" && - !this.canInsertSemicolon(); - - node.callee = base; - node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); - node.optional = true; - - base = this.finishNode(node, "CallExpression"); - } else { - node.object = base; - node.property = this.parseIdentifier(true); - node.computed = false; - node.optional = true; - base = this.finishNode(node, "MemberExpression"); - } - } else if (this.eat(tt.dot)) { - const node = this.startNodeAt(startPos, startLoc); - node.object = base; - node.property = this.hasPlugin("classPrivateProperties") ? this.parseMaybePrivateName() : this.parseIdentifier(true); - node.computed = false; - base = this.finishNode(node, "MemberExpression"); - } else if (this.eat(tt.bracketL)) { - const node = this.startNodeAt(startPos, startLoc); + parseSubscript(base: N.Expression, startPos: number, startLoc: Position, noCalls: ?boolean, state: { stop: boolean }): N.Expression { + if (!noCalls && this.eat(tt.doubleColon)) { + const node = this.startNodeAt(startPos, startLoc); + node.object = base; + node.callee = this.parseNoCallExpr(); + state.stop = true; + return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls); + } else if (this.match(tt.questionDot)) { + if (!this.hasPlugin("optionalChaining")) { + this.raise(startPos, "You can only use optional-chaining when the 'optionalChaining' plugin is enabled."); + } + + if (noCalls && this.lookahead().type == tt.parenL) { + state.stop = true; + return base; + } + this.next(); + + const node = this.startNodeAt(startPos, startLoc); + + if (this.eat(tt.bracketL)) { node.object = base; node.property = this.parseExpression(); node.computed = true; + node.optional = true; this.expect(tt.bracketR); - base = this.finishNode(node, "MemberExpression"); - } else if (!noCalls && (this.match(tt.parenL) || this.isRelational("<"))) { - let typeAmbiguous; - if (this.isRelational("<")) { - // May be a call `f()`. But may be a less-than expression. - // In the case of `async(`, both `async();` (call expression) and - // `async () => { await foo() };` (generic async arrow function) are valid syntax. - // So, types parsed now must be ambiguous, as they could be either parameters or arguments. - typeAmbiguous = this.parseCallExpressionTypeAmbiguous(); - if (!typeAmbiguous) return base; - } - - const possibleAsync = this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); - this.next(); + return this.finishNode(node, "MemberExpression"); + } else if (this.eat(tt.parenL)) { + const possibleAsync = this.state.potentialArrowAt === base.start && + base.type === "Identifier" && + base.name === "async" && + !this.canInsertSemicolon(); - const node = this.startNodeAt(startPos, startLoc); node.callee = base; node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); - if (node.callee.type === "Import") { - if (node.arguments.length !== 1) { - this.raise(node.start, "import() requires exactly one argument"); - } + node.optional = true; - const importArg = node.arguments[0]; - if (importArg && importArg.type === "SpreadElement") { - this.raise(importArg.start, "... is not allowed in import()"); - } - } - base = this.finishNode(node, "CallExpression"); + return this.finishNode(node, "CallExpression"); + } else { + node.object = base; + node.property = this.parseIdentifier(true); + node.computed = false; + node.optional = true; + return this.finishNode(node, "MemberExpression"); + } + } else if (this.eat(tt.dot)) { + const node = this.startNodeAt(startPos, startLoc); + node.object = base; + node.property = this.hasPlugin("classPrivateProperties") ? this.parseMaybePrivateName() : this.parseIdentifier(true); + node.computed = false; + return this.finishNode(node, "MemberExpression"); + } else if (this.eat(tt.bracketL)) { + const node = this.startNodeAt(startPos, startLoc); + node.object = base; + node.property = this.parseExpression(); + node.computed = true; + this.expect(tt.bracketR); + return this.finishNode(node, "MemberExpression"); + } else if (!noCalls && this.match(tt.parenL)) { + const possibleAsync = this.atPossibleAsync(base); + this.next(); - if (possibleAsync && this.shouldParseAsyncArrow()) { - return this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node, typeAmbiguous); - } else { - if (typeAmbiguous) { - node.typeParameters = this.typeArgumentsFromAmbiguous(typeAmbiguous); - } - this.toReferencedList(node.arguments); - } - } else if (this.match(tt.backQuote)) { - const node = this.startNodeAt(startPos, startLoc); - node.tag = base; - node.quasi = this.parseTemplate(true); - base = this.finishNode(node, "TaggedTemplateExpression"); + const node = this.startNodeAt(startPos, startLoc); + node.callee = base; + node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); + base = this.finishCallExpression(node); + + if (possibleAsync && this.shouldParseAsyncArrow()) { + state.stop = true; + return this.parseAsyncArrowFromCallExpression(this.startNodeAt(startPos, startLoc), node); } else { - return base; + this.toReferencedList(node.arguments); + return node; } + } else if (this.match(tt.backQuote)) { + const node = this.startNodeAt(startPos, startLoc); + node.tag = base; + node.quasi = this.parseTemplate(true); + return this.finishNode(node, "TaggedTemplateExpression"); + } else { + state.stop = true; + return base; } - // istanbul ignore next - throw new Error("Unreachable"); } - parseCallExpressionTypeAmbiguous(): ?$ReadOnlyArray { - return null; + atPossibleAsync(base: N.Expression): boolean { + return this.state.potentialArrowAt === base.start && base.type === "Identifier" && base.name === "async" && !this.canInsertSemicolon(); } - // eslint-disable-next-line no-unused-vars - typeArgumentsFromAmbiguous(types: $ReadOnlyArray): N.TypeParameterInstantiation { - // istanbul ignore next - throw new Error("Plugins should override typeArgumentsFromAmbiguous if overriding parseCallExpressionTypeArguments"); - } + finishCallExpression(node: N.CallExpression): N.CallExpression { + if (node.callee.type === "Import") { + if (node.arguments.length !== 1) { + this.raise(node.start, "import() requires exactly one argument"); + } - // eslint-disable-next-line no-unused-vars - typeParametersFromAmbiguous(types: $ReadOnlyArray): N.TypeParameterDeclaration { - // istanbul ignore next - throw new Error("Plugins should override typeParametersFromAmbiguous if overriding parseCallExpressionTypeArguments"); + const importArg = node.arguments[0]; + if (importArg && importArg.type === "SpreadElement") { + this.raise(importArg.start, "... is not allowed in import()"); + } + } + return this.finishNode(node, "CallExpression"); } parseCallExpressionArguments(close: TokenType, possibleAsyncArrow: boolean): $ReadOnlyArray { @@ -454,11 +444,8 @@ export default class ExpressionParser extends LValParser { return this.match(tt.arrow); } - parseAsyncArrowFromCallExpression(node: N.ArrowFunctionExpression, call: N.CallExpression, types: ?$ReadOnlyArray): N.ArrowFunctionExpression { + parseAsyncArrowFromCallExpression(node: N.ArrowFunctionExpression, call: N.CallExpression): N.ArrowFunctionExpression { this.expect(tt.arrow); - if (types) { - node.typeParameters = this.typeParametersFromAmbiguous(types); - } return this.parseArrowExpression(node, call.arguments, true); } diff --git a/src/parser/statement.js b/src/parser/statement.js index 2d9d456bef..ea5ba00c79 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -632,8 +632,14 @@ export default class StatementParser extends ExpressionParser { } parseFunctionParams(node: N.NormalFunction): void { + node.params = this.doParseFunctionParams(); + } + + doParseFunctionParams(): $ReadOnlyArray { this.expect(tt.parenL); - node.params = this.parseBindingList(tt.parenR); + // $FlowIgnore (won't be TSParameterProperty) + const x: $ReadOnlyArray = this.parseBindingList(tt.parenR); + return x; } // Parse a class declaration or literal (depending on the diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index c060a587c2..74ac5db147 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -241,28 +241,6 @@ export default (superClass: Class): Class => class extends super return this.finishNode(node, "TypeParameter"); } - // Parse something that may be a type parameter or a type argument. - tsParseTypeAmbiguous(): ?N.TypeAmbiguous { - const node: N.TypeAmbiguous = this.startNode(); - try { - node.typeAnnotation = this.tsParseType(); - if (this.eat(tt._extends)) { - node.constraint = this.tsParseType(); - } - - if (this.eat(tt.eq)) { - node.default = this.tsParseType(); - } - } catch (err) { - if (err instanceof SyntaxError) { - return undefined; - } - throw err; - } - - return this.finishNode(node, "TypeAmbiguous"); - } - tsTryParseTypeParameters(): ?N.TypeParameterDeclaration { if (this.eatRelational("<")) { return this.tsParseTypeParameters(); @@ -674,6 +652,10 @@ export default (superClass: Class): Class => class extends super return this.finishNode(t, "TypeAnnotation"); } + tsTryParseTypeOrTypePredicateAnnotation(): ?N.TypeAnnotation { + return this.eat(tt.colon) ? this.tsParseTypeOrTypePredicateAnnotation() : undefined; + } + tsTryParseTypeAnnotation(): ?N.TypeAnnotation { return this.eat(tt.colon) ? this.tsParseTypeAnnotation() : undefined; } @@ -721,13 +703,19 @@ export default (superClass: Class): Class => class extends super return this.finishNode(node, "TSTypeAssertion"); } - // NOTE: this is only called within a `tryParse`. So it shouldn't fail, it should just return `undefined`. - tsParseTypeArgumentsInExpression(): ?$ReadOnlyArray { - this.expectRelational("<"); - const typeArguments = this.tsTryParseDelimitedList( - "TypeParametersOrArguments", - this.tsParseTypeAmbiguous.bind(this)); - return typeArguments && this.eatRelational(">") && this.match(tt.parenL) ? typeArguments : undefined; + tsTryParseTypeArgumentsInExpression(): ?N.TypeParameterInstantiation { + return this.tsTryParseAndCatch(() => { + const res: N.TypeParameterInstantiation = this.startNode(); + this.expectRelational("<"); + const typeArguments = this.tsParseDelimitedList( + "TypeParametersOrArguments", + this.tsParseType.bind(this)); + this.expectRelational(">"); + res.params = typeArguments; + this.finishNode(res, "TypeParameterInstantiation"); + this.expect(tt.parenL); + return res; + }); } tsParseHeritageClause(): $ReadOnlyArray { @@ -873,6 +861,19 @@ export default (superClass: Class): Class => class extends super return res; } + tsTryParseAndCatch(f: () => T): ?T { + const state = this.state.clone(); + try { + return f(); + } catch (e) { + if (e instanceof SyntaxError) { + this.state = state; + return undefined; + } + throw e; + } + } + tsTryParse(f: () => ?T): ?T { const state = this.state.clone(); const result = f(); @@ -1012,10 +1013,73 @@ export default (superClass: Class): Class => class extends super } } + tsTryParseGenericAsyncArrowFunction(startPos: number, startLoc: Position): ?N.ArrowFunctionExpression { + const res: ?N.ArrowFunctionExpression = this.tsTryParseAndCatch(() => { + const node: N.ArrowFunctionExpression = this.startNodeAt(startPos, startLoc); + this.expectRelational("<"); + node.typeParameters = this.tsParseTypeParameters(); + node.params = this.doParseFunctionParams(); + node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation(); + this.expect(tt.arrow); + return node; + }); + + if (!res) { + return undefined; + } + + res.id = null; + res.generator = false; + res.expression = true; // May be set again by parseFunctionBody. + res.async = true; + this.parseFunctionBody(res, true); + return this.finishNode(res, "ArrowFunctionExpression"); + } + // ====================================================== // OVERRIDES // ====================================================== + parseSubscript( + base: N.Expression, + startPos: number, + startLoc: Position, + noCalls: ?boolean, + state: { stop: boolean }): N.Expression { + + if (this.eat(tt.bang)) { + const nonNullExpression: N.TsNonNullExpression = this.startNodeAt(startPos, startLoc); + nonNullExpression.expression = base; + return this.finishNode(nonNullExpression, "TSNonNullExpression"); + } + + if (!noCalls && this.isRelational("<")) { + if (this.atPossibleAsync(base)) { + // Almost certainly this is a generic async function `async () => ... + // But it might be a call with a type argument `async();` + const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction(startPos, startLoc); + if (asyncArrowFn) { + return asyncArrowFn; + } + } + + const node: N.CallExpression = this.startNodeAt(startPos, startLoc); + node.callee = base; + + // May be passing type arguments. But may just be the `<` operator. + const typeArguments = this.tsTryParseTypeArgumentsInExpression(); // Also eats the "(" + if (typeArguments) { + // possibleAsync always false here, because we would have handled it above. + // $FlowIgnore (won't be any undefined arguments) + node.arguments = this.parseCallExpressionArguments(tt.parenR, /* possibleAsync */ false); + node.typeParameters = typeArguments; + return this.finishCallExpression(node); + } + } + + return super.parseSubscript(base, startPos, startLoc, noCalls, state); + } + parseExprOp( left: N.Expression, leftStartPos: number, @@ -1092,49 +1156,6 @@ export default (superClass: Class): Class => class extends super return super.parseStatementContent(declaration, topLevel); } - // Similar to TypeScript's own parser's `parseCallExpressionRest`. - parseCallExpressionTypeAmbiguous(): ?$ReadOnlyArray { - return this.tsTryParse(this.tsParseTypeArgumentsInExpression.bind(this)); - } - - typeArgumentsFromAmbiguous(types: $ReadOnlyArray): N.TypeParameterInstantiation { - const node: N.TypeParameterInstantiation = this.startNode(); - node.params = types.map(({ constraint, default: defaultType, typeAnnotation: type, start, end, loc }) => { - if (constraint || defaultType) { - throw this.raise(start, "Type argument may not have a constraint or default."); - } - - // Set location - type.start = start; - type.end = end; - type.loc.start = loc.start; - type.loc.end = loc.end; - - return type; - }); - return this.finishNode(node, "TypeParameterInstantiation"); - } - - typeParametersFromAmbiguous( - types: $ReadOnlyArray): N.TypeParameterDeclaration { - - const node: N.TypeParameterDeclaration = this.startNode(); - node.params = types.map((typeAmbiguous) => { - const typeParam = this.nodeWithSamePosition(typeAmbiguous, "TypeParameter"); - const { typeAnnotation: type, constraint, default: defaultType } = typeAmbiguous; - if (type.type === "TSTypeReference" && !type.typeParameters && type.typeName.type === "Identifier") { - typeParam.name = type.typeName.name; - } else { - throw this.raise(type.start, "Type parameter must be a plain identifier."); - } - - if (constraint) typeParam.constriant = constraint; - if (defaultType) typeParam.default = defaultType; - return typeParam; - }); - return this.finishNode(node, "TypeParameterDeclaration"); - } - // Not in TypeScript's own parser. Called after seeing `new C<` in `parseNew`. parseTypeArguments(): N.TypeParameterInstantiation { const node = this.startNode(); @@ -1377,13 +1398,12 @@ export default (superClass: Class): Class => class extends super // parse the return type of an async arrow function - let foo = (async (): number => {}); parseAsyncArrowFromCallExpression( node: N.ArrowFunctionExpression, - call: N.CallExpression, - types: ?$ReadOnlyArray): N.ArrowFunctionExpression { + call: N.CallExpression): N.ArrowFunctionExpression { if (this.eat(tt.colon)) { node.returnType = this.tsParseTypeAnnotation(); } - return super.parseAsyncArrowFromCallExpression(node, call, types); + return super.parseAsyncArrowFromCallExpression(node, call); } parseMaybeAssign(...args): N.Expression { diff --git a/src/types.js b/src/types.js index 856017e76d..11800bb24f 100644 --- a/src/types.js +++ b/src/types.js @@ -722,18 +722,6 @@ export type JSXElement = Node; // Flow/TypeScript common (TODO: Not in spec) -/** - * For internal use only. - * This is for something which may be a type parameter or type argument, but which is not yet known. - * These shouldn't appear in the output AST. - */ -export type TypeAmbiguous = NodeBase & { - type: "TypeAmbiguous"; - typeAnnotation: TsType; - constraint?: TsType; - default?: TsType; -} - export type TypeAnnotation = NodeBase & { type: "TypeAnnotation"; typeAnnotation: TsType | FlowTypeAnnotation; diff --git a/test/fixtures/typescript/arrow-function/async-generic-false-positive/actual.js b/test/fixtures/typescript/arrow-function/async-generic-false-positive/actual.js new file mode 100644 index 0000000000..29986ed4d0 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async-generic-false-positive/actual.js @@ -0,0 +1,2 @@ +async < 1; +async() == 0; diff --git a/test/fixtures/typescript/arrow-function/async-generic-false-positive/expected.json b/test/fixtures/typescript/arrow-function/async-generic-false-positive/expected.json new file mode 100644 index 0000000000..f04d318bc2 --- /dev/null +++ b/test/fixtures/typescript/arrow-function/async-generic-false-positive/expected.json @@ -0,0 +1,235 @@ +{ + "type": "File", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "left": { + "type": "Identifier", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + }, + "identifierName": "async" + }, + "name": "async" + }, + "operator": "<", + "right": { + "type": "NumericLiteral", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + }, + { + "type": "ExpressionStatement", + "start": 11, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 11, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "left": { + "type": "CallExpression", + "start": 11, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "callee": { + "type": "Identifier", + "start": 11, + "end": 16, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "async" + }, + "name": "async" + }, + "arguments": [], + "typeParameters": { + "type": "TypeParameterInstantiation", + "start": 16, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "params": [ + { + "type": "TSTypeReference", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "typeName": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "T" + }, + "name": "T" + } + } + ] + } + }, + "operator": "==", + "right": { + "type": "NumericLiteral", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/test/fixtures/typescript/arrow-function/async-generic/expected.json b/test/fixtures/typescript/arrow-function/async-generic/expected.json index f47439f8f5..7edff6b60f 100644 --- a/test/fixtures/typescript/arrow-function/async-generic/expected.json +++ b/test/fixtures/typescript/arrow-function/async-generic/expected.json @@ -105,16 +105,16 @@ }, "typeParameters": { "type": "TypeParameterDeclaration", - "start": 22, - "end": 21, + "start": 7, + "end": 9, "loc": { "start": { "line": 1, - "column": 22 + "column": 7 }, "end": { "line": 1, - "column": 21 + "column": 9 } }, "params": [ diff --git a/test/fixtures/typescript/type-arguments/call/expected.json b/test/fixtures/typescript/type-arguments/call/expected.json index 6272c2073e..a644b276ea 100644 --- a/test/fixtures/typescript/type-arguments/call/expected.json +++ b/test/fixtures/typescript/type-arguments/call/expected.json @@ -76,16 +76,16 @@ "arguments": [], "typeParameters": { "type": "TypeParameterInstantiation", - "start": 6, - "end": 6, + "start": 1, + "end": 4, "loc": { "start": { "line": 1, - "column": 6 + "column": 1 }, "end": { "line": 1, - "column": 6 + "column": 4 } }, "params": [ @@ -173,16 +173,16 @@ "arguments": [], "typeParameters": { "type": "TypeParameterInstantiation", - "start": 17, - "end": 17, + "start": 9, + "end": 15, "loc": { "start": { "line": 2, - "column": 9 + "column": 1 }, "end": { "line": 2, - "column": 9 + "column": 7 } }, "params": [ diff --git a/test/helpers/runFixtureTests.js b/test/helpers/runFixtureTests.js index c37edcff04..0ec9479d1f 100644 --- a/test/helpers/runFixtureTests.js +++ b/test/helpers/runFixtureTests.js @@ -94,6 +94,9 @@ function runTest(test, parseFunction) { var mis = misMatch(JSON.parse(test.expect.code), ast); if (mis) { + console.log("ACTUAL:"); + console.log(JSON.stringify(ast, undefined, 2)); + throw new Error(mis); } } From 78e8061a2711bc1d45bdabe1a418ae47508a08d7 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 13:14:10 -0700 Subject: [PATCH 42/55] Refactor maybeParseFunctionBody to avoid checking hasPlugin("typescript") --- src/parser/expression.js | 17 +++++++---------- src/parser/statement.js | 6 ++---- src/plugins/typescript.js | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 775f4dfe87..7539cfb8e7 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -1082,9 +1082,9 @@ export default class ExpressionParser extends LValParser { const allowModifiers = isConstructor; // For TypeScript parameter properties node.params = this.parseBindingList(tt.parenR, /* allowEmpty */ false, allowModifiers); node.generator = !!isGenerator; - const missingBody = this.maybeParseFunctionBody(node, { allowMissingBody: type === "ClassMethod" }); + this.parseFunctionReturnTypeAndBodyAndFinish(node, type); this.state.inMethod = oldInMethod; - return this.finishNode(node, missingBody ? "TSDeclareMethod" : type); + return node; } // Parse arrow function expression with given parameters. @@ -1108,22 +1108,19 @@ export default class ExpressionParser extends LValParser { return false; } - // Returns true if the body is missing. - maybeParseFunctionBody(node: N.BodilessFunctionOrMethodBase, { allowExpressionBody, allowMissingBody }: { allowExpressionBody?: boolean, allowMissingBody: boolean }): boolean { + parseFunctionReturnTypeAndBodyAndFinish(node: N.BodilessFunctionOrMethodBase, type: string, allowExpressionBody?: boolean): void { if (!allowExpressionBody) { // For arrow functions, `parseArrow` handles the return type itself. this.parseFunctionReturnType(node); } - // In TypeScript, a body may be missing on a declaration. - // See `parseFunctionBlockOrSemicolon` in TS source. - if (allowMissingBody && this.hasPlugin("typescript") && !this.match(tt.braceL) && this.isLineTerminator()) { - return true; - } + this.parseFunctionBodyAndFinish(node, type, allowExpressionBody); + } + parseFunctionBodyAndFinish(node: N.BodilessFunctionOrMethodBase, type: string, allowExpressionBody?: boolean): void { // $FlowIgnore (node is not bodiless if we get here) this.parseFunctionBody(node, allowExpressionBody); - return false; + this.finishNode(node, type); } // TypeScript / Flow plugins will override this diff --git a/src/parser/statement.js b/src/parser/statement.js index ea5ba00c79..bc45c215c6 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -624,11 +624,9 @@ export default class StatementParser extends ExpressionParser { } this.parseFunctionParams(node); - const missingBody = this.maybeParseFunctionBody(node, { allowExpressionBody, allowMissingBody: isStatement }); - + this.parseFunctionReturnTypeAndBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression", allowExpressionBody); this.state.inMethod = oldInMethod; - - return this.finishNode(node, isStatement ? (missingBody ? "TSDeclareFunction" : "FunctionDeclaration") : "FunctionExpression"); + return node; } parseFunctionParams(node: N.NormalFunction): void { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 74ac5db147..02845c73d4 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1040,6 +1040,23 @@ export default (superClass: Class): Class => class extends super // OVERRIDES // ====================================================== + parseFunctionBodyAndFinish( + node: N.BodilessFunctionOrMethodBase, + type: string, + allowExpressionBody?: boolean): void { + const bodilessType = type === "FunctionDeclaration" + ? "TSDeclareFunction" + : type === "ClassMethod" + ? "TSDeclareMethod" + : undefined; + if (bodilessType && !this.match(tt.braceL) && this.isLineTerminator()) { + this.finishNode(node, bodilessType); + return; + } + + super.parseFunctionBodyAndFinish(node, type, allowExpressionBody); + } + parseSubscript( base: N.Expression, startPos: number, From 434ab3e164e5bc74e9a74b5e6d22533b1c941c4c Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 13:15:57 -0700 Subject: [PATCH 43/55] Remove useless hasPlugin("typescript") from within typescript plugin --- src/plugins/typescript.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 02845c73d4..44e2f53f95 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1140,15 +1140,15 @@ export default (superClass: Class): Class => class extends super } parseExport(node: N.Node): N.Node { - if (this.hasPlugin("typescript") && this.match(tt._import)) { // `export import A = B;` + if (this.match(tt._import)) { // `export import A = B;` this.expect(tt._import); return this.tsParseImportEqualsDeclaration(node, /* isExport */ true); - } else if (this.hasPlugin("typescript") && this.eat(tt.eq)) { // `export = x;` + } else if (this.eat(tt.eq)) { // `export = x;` const assign: N.TsExportAssignment = node; assign.expression = this.parseExpression(); this.semicolon(); return this.finishNode(assign, "TSExportAssignment"); - } else if (this.hasPlugin("typescript") && this.eatContextual("as")) { // `export as namespace A;` + } else if (this.eatContextual("as")) { // `export as namespace A;` const decl: N.TsNamespaceExportDeclaration = node; // See `parseNamespaceExportDeclaration` in TypeScript's own parser this.expectContextual("namespace"); From 53d3683b277a37f857be8afd86eda89a8536093a Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 13:21:41 -0700 Subject: [PATCH 44/55] Remove doParseFunctionParams --- src/parser/statement.js | 10 ++-------- src/plugins/flow.js | 2 +- src/plugins/typescript.js | 5 +++-- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/parser/statement.js b/src/parser/statement.js index bc45c215c6..99d16891be 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -629,15 +629,9 @@ export default class StatementParser extends ExpressionParser { return node; } - parseFunctionParams(node: N.NormalFunction): void { - node.params = this.doParseFunctionParams(); - } - - doParseFunctionParams(): $ReadOnlyArray { + parseFunctionParams(node: N.Function): void { this.expect(tt.parenL); - // $FlowIgnore (won't be TSParameterProperty) - const x: $ReadOnlyArray = this.parseBindingList(tt.parenR); - return x; + node.params = this.parseBindingList(tt.parenR); } // Parse a class declaration or literal (depending on the diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 01eb1fad52..38958d74a0 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -1440,7 +1440,7 @@ export default (superClass: Class): Class => class extends super } // parse function type parameters - function foo() {} - parseFunctionParams(node: N.NormalFunction): void { + parseFunctionParams(node: N.Function): void { if (this.isRelational("<")) { node.typeParameters = this.flowParseTypeParameterDeclaration(); } diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 44e2f53f95..2c717d26f1 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1018,7 +1018,8 @@ export default (superClass: Class): Class => class extends super const node: N.ArrowFunctionExpression = this.startNodeAt(startPos, startLoc); this.expectRelational("<"); node.typeParameters = this.tsParseTypeParameters(); - node.params = this.doParseFunctionParams(); + // Don't use overloaded parseFunctionParams which would look for "<" again. + super.parseFunctionParams(node); node.returnType = this.tsTryParseTypeOrTypePredicateAnnotation(); this.expect(tt.arrow); return node; @@ -1396,7 +1397,7 @@ export default (superClass: Class): Class => class extends super super.parseObjPropValue(prop, ...args); } - parseFunctionParams(node: N.NormalFunction): void { + parseFunctionParams(node: N.Function): void { const typeParameters = this.tsTryParseTypeParameters(); if (typeParameters) node.typeParameters = typeParameters; super.parseFunctionParams(node); From d95aec6dced03bda5f287d598b2dae56f15648e3 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 13:24:17 -0700 Subject: [PATCH 45/55] Use atPossibleAsync in one more place --- src/parser/expression.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 7539cfb8e7..b9686b7ccc 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -336,10 +336,7 @@ export default class ExpressionParser extends LValParser { this.expect(tt.bracketR); return this.finishNode(node, "MemberExpression"); } else if (this.eat(tt.parenL)) { - const possibleAsync = this.state.potentialArrowAt === base.start && - base.type === "Identifier" && - base.name === "async" && - !this.canInsertSemicolon(); + const possibleAsync = this.atPossibleAsync(base); node.callee = base; node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync); From 7fd24e6b22fba67d29cd6166fddbfb2530bc495d Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 14:04:58 -0700 Subject: [PATCH 46/55] Combine parseFunctionReturnTypeAndBodyAndFinish with parseFunctionBodyAndFinish --- src/parser/expression.js | 15 +-------------- src/parser/statement.js | 2 +- src/plugins/flow.js | 7 +++++-- src/plugins/typescript.js | 11 +++++------ 4 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index b9686b7ccc..aad703fda5 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -1079,7 +1079,7 @@ export default class ExpressionParser extends LValParser { const allowModifiers = isConstructor; // For TypeScript parameter properties node.params = this.parseBindingList(tt.parenR, /* allowEmpty */ false, allowModifiers); node.generator = !!isGenerator; - this.parseFunctionReturnTypeAndBodyAndFinish(node, type); + this.parseFunctionBodyAndFinish(node, type); this.state.inMethod = oldInMethod; return node; } @@ -1105,25 +1105,12 @@ export default class ExpressionParser extends LValParser { return false; } - parseFunctionReturnTypeAndBodyAndFinish(node: N.BodilessFunctionOrMethodBase, type: string, allowExpressionBody?: boolean): void { - if (!allowExpressionBody) { - // For arrow functions, `parseArrow` handles the return type itself. - this.parseFunctionReturnType(node); - } - - this.parseFunctionBodyAndFinish(node, type, allowExpressionBody); - } - parseFunctionBodyAndFinish(node: N.BodilessFunctionOrMethodBase, type: string, allowExpressionBody?: boolean): void { // $FlowIgnore (node is not bodiless if we get here) this.parseFunctionBody(node, allowExpressionBody); this.finishNode(node, type); } - // TypeScript / Flow plugins will override this - // eslint-disable-next-line no-unused-vars - parseFunctionReturnType(node: N.BodilessFunctionOrMethodBase): void {} - // Parse function body and check parameters. parseFunctionBody(node: N.Function, allowExpression: ?boolean): void { const isExpression = allowExpression && !this.match(tt.braceL); diff --git a/src/parser/statement.js b/src/parser/statement.js index 99d16891be..9c23747b04 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -624,7 +624,7 @@ export default class StatementParser extends ExpressionParser { } this.parseFunctionParams(node); - this.parseFunctionReturnTypeAndBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression", allowExpressionBody); + this.parseFunctionBodyAndFinish(node, isStatement ? "FunctionDeclaration" : "FunctionExpression", allowExpressionBody); this.state.inMethod = oldInMethod; return node; } diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 38958d74a0..3ead852fdc 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -1006,8 +1006,9 @@ export default (superClass: Class): Class => class extends super // Overrides // ================================== - parseFunctionReturnType(node: N.BodilessFunctionOrMethodBase): void { - if (this.match(tt.colon)) { + parseFunctionBodyAndFinish(node: N.BodilessFunctionOrMethodBase, type: string, allowExpressionBody?: boolean): void { + // For arrow functions, `parseArrow` handles the return type itself. + if (!allowExpressionBody && this.match(tt.colon)) { const typeNode = this.startNode(); // $FlowFixMe [typeNode.typeAnnotation, node.predicate] = this.flowParseTypeAndPredicateInitialiser(); @@ -1015,6 +1016,8 @@ export default (superClass: Class): Class => class extends super ? this.finishNode(typeNode, "TypeAnnotation") : null; } + + super.parseFunctionBodyAndFinish(node, type, allowExpressionBody); } // interfaces diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 2c717d26f1..67d676bbb0 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1045,6 +1045,11 @@ export default (superClass: Class): Class => class extends super node: N.BodilessFunctionOrMethodBase, type: string, allowExpressionBody?: boolean): void { + // For arrow functions, `parseArrow` handles the return type itself. + if (!allowExpressionBody && this.eat(tt.colon)) { + node.returnType = this.tsParseTypeOrTypePredicateAnnotation(); + } + const bodilessType = type === "FunctionDeclaration" ? "TSDeclareFunction" : type === "ClassMethod" @@ -1251,12 +1256,6 @@ export default (superClass: Class): Class => class extends super if (optional) methodOrProp.optional = true; } - parseFunctionReturnType(node: N.BodilessFunctionOrMethodBase): void { - if (this.eat(tt.colon)) { - node.returnType = this.tsParseTypeOrTypePredicateAnnotation(); - } - } - // Note: The reason we do this in `parseExpressionStatement` and not `parseStatement` // is that e.g. `type()` is valid JS, so we must try parsing that first. // If it's really a type, we will parse `type` as the statement, and can correct it here From 8280b957e205de9259e7783f4dfbd178e8250b95 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 13 Jun 2017 15:13:40 -0700 Subject: [PATCH 47/55] Handle false positive for `new` with type arguments --- src/parser/expression.js | 10 +- src/plugins/typescript.js | 35 ++++-- .../new-false-positive/actual.js | 1 + .../new-false-positive/expected.json | 115 ++++++++++++++++++ 4 files changed, 142 insertions(+), 19 deletions(-) create mode 100644 test/fixtures/typescript/type-arguments/new-false-positive/actual.js create mode 100644 test/fixtures/typescript/type-arguments/new-false-positive/expected.json diff --git a/src/parser/expression.js b/src/parser/expression.js index aad703fda5..d94f693b17 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -799,10 +799,7 @@ export default class ExpressionParser extends LValParser { node.callee = this.parseNoCallExpr(); const optional = this.eat(tt.questionDot); - if (this.isRelational("<")) { - const typeArguments = this.parseTypeArguments(); - if (typeArguments) node.typeParameters = typeArguments; - } + this.parseNewTypeArguments(node); if (this.eat(tt.parenL)) { node.arguments = this.parseExprList(tt.parenR); @@ -817,9 +814,8 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "NewExpression"); } - parseTypeArguments(): ?N.TypeParameterInstantiation { - return null; - } + // eslint-disable-next-line no-unused-vars + parseNewTypeArguments(node: N.NewExpression) {} // Parse template expression. diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 67d676bbb0..a0634db7d3 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -201,7 +201,7 @@ export default (superClass: Class): Class => class extends super const node: N.TsTypeReference = this.startNode(); node.typeName = this.tsParseEntityName(/* allowReservedWords */ false); if (!this.hasPrecedingLineBreak() && this.isRelational("<")) { - node.typeParameters = this.parseTypeArguments(); + node.typeParameters = this.tsParseTypeArguments(); } return this.finishNode(node, "TSTypeReference"); } @@ -730,7 +730,7 @@ export default (superClass: Class): Class => class extends super // then has grammar errors later if it's not an EntityName. node.expression = this.tsParseEntityName(/* allowReservedWords */ false); if (this.isRelational("<")) { - node.typeParameters = this.parseTypeArguments(); + node.typeParameters = this.tsParseTypeArguments(); } return this.finishNode(node, "TSExpressionWithTypeArguments"); @@ -1037,6 +1037,14 @@ export default (superClass: Class): Class => class extends super return this.finishNode(res, "ArrowFunctionExpression"); } + tsParseTypeArguments(): N.TypeParameterInstantiation { + const node = this.startNode(); + this.expectRelational("<"); + node.params = this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this)); + this.expectRelational(">"); + return this.finishNode(node, "TypeParameterInstantiation"); + } + // ====================================================== // OVERRIDES // ====================================================== @@ -1103,6 +1111,18 @@ export default (superClass: Class): Class => class extends super return super.parseSubscript(base, startPos, startLoc, noCalls, state); } + parseNewTypeArguments(node: N.NewExpression): void { + if (!this.isRelational("<")) { // tsTryParseAndCatch is expensive, so avoid if not necessary. + return; + } + + // 99% certain this is `new C();`. But may be `new C < T;`, which is also legal. + const typeParameters = this.tsTryParseAndCatch(this.tsParseTypeArguments.bind(this)); + if (typeParameters) { + node.typeParameters = typeParameters; + } + } + parseExprOp( left: N.Expression, leftStartPos: number, @@ -1179,15 +1199,6 @@ export default (superClass: Class): Class => class extends super return super.parseStatementContent(declaration, topLevel); } - // Not in TypeScript's own parser. Called after seeing `new C<` in `parseNew`. - parseTypeArguments(): N.TypeParameterInstantiation { - const node = this.startNode(); - this.expectRelational("<"); - node.params = this.tsParseDelimitedList("TypeParametersOrArguments", this.tsParseType.bind(this)); - this.expectRelational(">"); - return this.finishNode(node, "TypeParameterInstantiation"); - } - parseAccessModifier(): ?N.Accessibility { return this.tsParseModifier(["public", "protected", "private"]); } @@ -1381,7 +1392,7 @@ export default (superClass: Class): Class => class extends super parseClassSuper(node: N.Class): void { super.parseClassSuper(node); if (node.superClass && this.isRelational("<")) { - node.superTypeParameters = this.parseTypeArguments(); + node.superTypeParameters = this.tsParseTypeArguments(); } if (this.eatContextual("implements")) { node.implements = this.tsParseHeritageClause(); diff --git a/test/fixtures/typescript/type-arguments/new-false-positive/actual.js b/test/fixtures/typescript/type-arguments/new-false-positive/actual.js new file mode 100644 index 0000000000..f703174469 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new-false-positive/actual.js @@ -0,0 +1 @@ +new A < T; diff --git a/test/fixtures/typescript/type-arguments/new-false-positive/expected.json b/test/fixtures/typescript/type-arguments/new-false-positive/expected.json new file mode 100644 index 0000000000..721f294462 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new-false-positive/expected.json @@ -0,0 +1,115 @@ +{ + "type": "File", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "sourceType": "module", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "left": { + "type": "NewExpression", + "start": 0, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "callee": { + "type": "Identifier", + "start": 4, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + }, + "arguments": [] + }, + "operator": "<", + "right": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file From db0b2f5d8bd5a281a56aee7700f11c8d73efab1b Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 15 Jun 2017 10:20:45 -0700 Subject: [PATCH 48/55] Undo runFixtureTests change --- test/helpers/runFixtureTests.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/helpers/runFixtureTests.js b/test/helpers/runFixtureTests.js index 0ec9479d1f..c37edcff04 100644 --- a/test/helpers/runFixtureTests.js +++ b/test/helpers/runFixtureTests.js @@ -94,9 +94,6 @@ function runTest(test, parseFunction) { var mis = misMatch(JSON.parse(test.expect.code), ast); if (mis) { - console.log("ACTUAL:"); - console.log(JSON.stringify(ast, undefined, 2)); - throw new Error(mis); } } From f494cab269b5c27d95840e7b7dc1a85c546f3813 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 16 Jun 2017 13:01:45 -0700 Subject: [PATCH 49/55] Move several changes out of lval.js and into overloads --- src/parser/lval.js | 11 -------- src/plugins/typescript.js | 58 +++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/parser/lval.js b/src/parser/lval.js index b70892cc58..fafbd8a233 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -30,7 +30,6 @@ export default class LValParser extends NodeUtils { case "ObjectPattern": case "ArrayPattern": case "AssignmentPattern": - case "TSParameterProperty": break; case "ObjectExpression": @@ -163,12 +162,6 @@ export default class LValParser extends NodeUtils { case tt.braceL: return this.parseObj(true); - case tt._this: - if (this.hasPlugin("typescript")) { - // "this" may be the name of a parameter, so allow it. - return this.parseIdentifier(/* liberal */ true); - } - default: throw this.unexpected(); } @@ -312,10 +305,6 @@ export default class LValParser extends NodeUtils { this.checkLVal(expr.argument, isBinding, checkClashes, "rest element"); break; - case "TSParameterProperty": - this.checkLVal(expr.parameter, isBinding, checkClashes, "parameter property"); - break; - default: { const message = (isBinding ? /* istanbul ignore next */ "Binding invalid" : "Invalid") + " left-hand side" + diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index a0634db7d3..6002ce8a0f 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1560,6 +1560,47 @@ export default (superClass: Class): Class => class extends super return this.finishNode(param, param.type); } + toAssignable(node: N.Node, isBinding: ?boolean, contextDescription: string): N.Node { + switch (node.type) { + case "TypeCastExpression": + return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription); + case "TSParameterProperty": + return super.toAssignable(node, isBinding, contextDescription); + default: + return super.toAssignable(node, isBinding, contextDescription); + } + } + + checkLVal( + expr: N.Expression, + isBinding: ?boolean, + checkClashes: ?{ [key: string]: boolean }, + contextDescription: string): void { + switch (expr.type) { + case "TypeCastExpression": + // Allow "typecasts" to appear on the left of assignment expressions, + // because it may be in an arrow function. + // e.g. `const f = (foo: number = 0) => foo;` + return; + case "TSParameterProperty": + this.checkLVal(expr.parameter, isBinding, checkClashes, "parameter property"); + return; + default: + super.checkLVal(expr, isBinding, checkClashes, contextDescription); + return; + } + } + + parseBindingAtom(): N.Pattern { + switch (this.state.type) { + case tt._this: + // "this" may be the name of a parameter, so allow it. + return this.parseIdentifier(/* liberal */ true); + default: + return super.parseBindingAtom(); + } + } + // === === === === === === === === === === === === === === === === // Note: All below methods are duplicates of something in flow.js. // Not sure what the best way to combine these is. @@ -1597,14 +1638,6 @@ export default (superClass: Class): Class => class extends super } } - toAssignable(node: N.Node, isBinding: ?boolean, contextDescription: string): N.Node { - if (node.type === "TypeCastExpression") { - return super.toAssignable(this.typeCastToParameter(node), isBinding, contextDescription); - } else { - return super.toAssignable(node, isBinding, contextDescription); - } - } - toAssignableList( exprList: N.Expression[], isBinding: ?boolean, @@ -1641,15 +1674,6 @@ export default (superClass: Class): Class => class extends super return exprList; } - checkLVal(node: N.Expression): void { - // Allow "typecasts" to appear on the left of assignment expressions, - // because it may be in an arrow function. - // e.g. `const f = (foo: number = 0) => foo;` - if (node.type !== "TypeCastExpression") { - return super.checkLVal(...arguments); - } - } - shouldParseArrow() { return this.match(tt.colon) || super.shouldParseArrow(); } From 5b56e5925600a6529cc06c3249e7f857392d362e Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 16 Jun 2017 13:16:38 -0700 Subject: [PATCH 50/55] Move parseBindingList changes out of lval.js and into an override --- src/parser/lval.js | 33 +++++++++++---------------------- src/plugins/typescript.js | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/parser/lval.js b/src/parser/lval.js index fafbd8a233..bedabd74cc 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -194,33 +194,22 @@ export default class LValParser extends NodeUtils { while (this.match(tt.at)) { decorators.push(this.parseDecorator()); } - const { accessibility, readonly } = allowModifiers ? this.parseParameterModifiers() : {}; - const left = this.parseMaybeDefault(); - this.parseAssignableListItemTypes(left); - const elt = this.parseMaybeDefault(left.start, left.loc.start, left); - if (accessibility || readonly) { - const pp: TSParameterProperty = this.startNodeAtNode(elt); - if (decorators.length) { - pp.decorators = decorators; - } - if (accessibility) pp.accessibility = accessibility; - if (readonly) pp.readonly = readonly; - if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") { - throw this.raise(pp.start, "A parameter property may not be declared using a binding pattern."); - } - pp.parameter = elt; - elts.push(this.finishNode(pp, "TSParameterProperty")); - } else { - if (decorators.length) { - left.decorators = decorators; - } - elts.push(elt); - } + elts.push(this.parseAssignableListItem(allowModifiers, decorators)); } } return elts; } + parseAssignableListItem(allowModifiers: ?boolean, decorators: Decorator[]): Pattern | TSParameterProperty { + const left = this.parseMaybeDefault(); + this.parseAssignableListItemTypes(left); + const elt = this.parseMaybeDefault(left.start, left.loc.start, left); + if (decorators.length) { + left.decorators = decorators; + } + return elt; + } + // Overridden in typescript.js parseParameterModifiers(): { accessibility: ?Accessibility, readonly: boolean } { return { accessibility: undefined, readonly: false }; diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 6002ce8a0f..022445686a 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1049,6 +1049,39 @@ export default (superClass: Class): Class => class extends super // OVERRIDES // ====================================================== + parseAssignableListItem( + allowModifiers: ?boolean, + decorators: N.Decorator[]): N.Pattern | N.TSParameterProperty { + let accessibility: ?N.Accessibility; + let readonly = false; + if (allowModifiers) { + accessibility = this.parseAccessModifier(); + readonly = !!this.tsParseModifier(["readonly"]); + } + + const left = this.parseMaybeDefault(); + this.parseAssignableListItemTypes(left); + const elt = this.parseMaybeDefault(left.start, left.loc.start, left); + if (accessibility || readonly) { + const pp: N.TSParameterProperty = this.startNodeAtNode(elt); + if (decorators.length) { + pp.decorators = decorators; + } + if (accessibility) pp.accessibility = accessibility; + if (readonly) pp.readonly = readonly; + if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") { + throw this.raise(pp.start, "A parameter property may not be declared using a binding pattern."); + } + pp.parameter = elt; + return this.finishNode(pp, "TSParameterProperty"); + } else { + if (decorators.length) { + left.decorators = decorators; + } + return elt; + } + } + parseFunctionBodyAndFinish( node: N.BodilessFunctionOrMethodBase, type: string, @@ -1203,12 +1236,6 @@ export default (superClass: Class): Class => class extends super return this.tsParseModifier(["public", "protected", "private"]); } - parseParameterModifiers(): { accessibility: ?N.Accessibility, readonly: boolean } { - const accessibility = this.parseAccessModifier(); - const readonly = !!this.tsParseModifier(["readonly"]); - return { accessibility, readonly }; - } - parseClassMember(classBody: N.ClassBody, member: any, state: { hadConstructor: boolean }): void { const accessibility = this.parseAccessModifier(); if (accessibility) member.accessibility = accessibility; From eb5e8dc65c6d6a53aa7484aeecb5fda31ea620bb Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 20 Jun 2017 12:33:59 -0700 Subject: [PATCH 51/55] Make type annotation range start at the ':' or '=>' --- src/plugins/typescript.js | 42 +++---- .../arrow-function/annotated/expected.json | 8 +- .../async-generic/expected.json | 106 +++++++++--------- .../arrow-function/async/expected.json | 8 +- .../default-parameter-values/expected.json | 4 +- .../arrow-function/generic-tsx/expected.json | 8 +- .../arrow-function/generic/expected.json | 8 +- .../optional-parameter/expected.json | 8 +- .../predicate-types/expected.json | 8 +- .../class/constructor/expected.json | 24 ++-- .../typescript/class/declare/expected.json | 12 +- .../typescript/class/generic/expected.json | 4 +- .../class/get-generic/expected.json | 4 +- .../class/index-signature/expected.json | 8 +- .../members-with-modifier-names/expected.json | 12 +- .../members-with-reserved-names/expected.json | 4 +- .../class/method-computed/expected.json | 8 +- .../class/method-generic/expected.json | 20 ++-- .../class/method-no-body/expected.json | 4 +- .../class/method-optional/expected.json | 4 +- .../class/method-return-type/expected.json | 4 +- .../class/modifiers-properties/expected.json | 4 +- .../expected.json | 4 +- .../class/parameter-properties/expected.json | 12 +- .../typescript/class/properties/expected.json | 8 +- .../class/property-computed/expected.json | 8 +- .../const/no-initializer/expected.json | 4 +- .../typescript/declare/const/expected.json | 8 +- .../declare/destructure/expected.json | 12 +- .../typescript/declare/var/expected.json | 4 +- .../typescript/export/declare/expected.json | 8 +- .../function/annotated/expected.json | 8 +- .../function/anonymous/expected.json | 8 +- .../typescript/function/declare/expected.json | 8 +- .../function/export-default/expected.json | 8 +- .../function/overloads/expected.json | 16 +-- .../function/predicate-types/expected.json | 16 +-- .../interface/call-signature/expected.json | 8 +- .../construct-signature/expected.json | 8 +- .../interface/generic/expected.json | 4 +- .../interface/index-signature/expected.json | 4 +- .../interface/method-computed/expected.json | 8 +- .../interface/method-generic/expected.json | 8 +- .../interface/method-optional/expected.json | 4 +- .../interface/method-plain/expected.json | 12 +- .../interface/modifiers/expected.json | 4 +- .../interface/properties/expected.json | 8 +- .../interface/property-computed/expected.json | 8 +- .../property-named-public/expected.json | 4 +- .../reserved-method-name/expected.json | 4 +- .../interface/separators/expected.json | 24 ++-- .../body-declare/expected.json | 4 +- .../body-nested-declare/expected.json | 4 +- .../global-in-module/expected.json | 4 +- .../type-alias/generic-complex/expected.json | 4 +- .../typescript/types/array/expected.json | 4 +- .../types/function-generic/expected.json | 12 +- .../types/function-in-generic/expected.json | 8 +- .../types/function-with-this/expected.json | 12 +- .../typescript/types/function/expected.json | 20 ++-- .../typescript/types/indexed/expected.json | 4 +- .../typescript/types/keywords/expected.json | 40 +++---- .../types/literal-boolean/expected.json | 8 +- .../literal-number-negative/expected.json | 4 +- .../types/literal-number/expected.json | 4 +- .../types/literal-string/expected.json | 4 +- .../typescript/types/mapped/expected.json | 8 +- .../reference-generic-nested/expected.json | 4 +- .../types/reference-generic/expected.json | 4 +- .../typescript/types/reference/expected.json | 4 +- .../types/type-literal/expected.json | 8 +- .../types/type-operator/expected.json | 4 +- .../typescript/types/typeof/expected.json | 4 +- .../types/union-intersection/expected.json | 16 +-- 74 files changed, 371 insertions(+), 369 deletions(-) diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 022445686a..16a6d45e03 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -210,7 +210,7 @@ export default (superClass: Class): Class => class extends super this.next(); const node: N.TsTypePredicate = this.startNode(); node.parameterName = lhs; - node.typeAnnotation = this.tsParseTypeAnnotation(); + node.typeAnnotation = this.tsParseTypeAnnotation(/* eatColon */ false); return this.finishNode(node, "TSTypePredicate"); } @@ -266,10 +266,9 @@ export default (superClass: Class): Class => class extends super this.expect(tt.parenL); signature.parameters = this.tsParseBindingListForSignature(); if (returnTokenRequired) { - this.expect(returnToken); - signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(); - } else if (this.eat(returnToken)) { - signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(); + signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken); + } else if (this.match(returnToken)) { + signature.typeAnnotation = this.tsParseTypeOrTypePredicateAnnotation(returnToken); } } @@ -313,7 +312,7 @@ export default (superClass: Class): Class => class extends super this.expect(tt.bracketL); const id = this.parseIdentifier(); this.expect(tt.colon); - id.typeAnnotation = this.tsParseTypeAnnotation(); + id.typeAnnotation = this.tsParseTypeAnnotation(/* eatColon */ false); this.expect(tt.bracketR); node.parameters = [id]; @@ -636,15 +635,19 @@ export default (superClass: Class): Class => class extends super return false; } - tsParseTypeOrTypePredicateAnnotation(): N.TypeAnnotation { + tsParseTypeOrTypePredicateAnnotation(returnToken: TokenType): N.TypeAnnotation { + const t: N.TypeAnnotation = this.startNode(); + this.expect(returnToken); + const typePredicateVariable = this.tsIsIdentifier() && this.tsTryParse(this.tsParseTypePredicatePrefix.bind(this)); - const type = this.tsParseTypeAnnotation(); + if (!typePredicateVariable) { - return type; + return this.tsParseTypeAnnotation(/* eatColon */ false, t); } - const t: N.TypeAnnotation = this.startNodeAtNode(typePredicateVariable); + const type = this.tsParseTypeAnnotation(/* eatColon */ false); + const node: N.TsTypePredicate = this.startNodeAtNode(typePredicateVariable); node.parameterName = typePredicateVariable; node.typeAnnotation = type; @@ -653,11 +656,11 @@ export default (superClass: Class): Class => class extends super } tsTryParseTypeOrTypePredicateAnnotation(): ?N.TypeAnnotation { - return this.eat(tt.colon) ? this.tsParseTypeOrTypePredicateAnnotation() : undefined; + return this.match(tt.colon) ? this.tsParseTypeOrTypePredicateAnnotation(tt.colon) : undefined; } tsTryParseTypeAnnotation(): ?N.TypeAnnotation { - return this.eat(tt.colon) ? this.tsParseTypeAnnotation() : undefined; + return this.match(tt.colon) ? this.tsParseTypeAnnotation() : undefined; } tsTryParseType(): ?N.TsType { @@ -672,8 +675,8 @@ export default (superClass: Class): Class => class extends super } } - tsParseTypeAnnotation(): N.TypeAnnotation { - const t: N.TypeAnnotation = this.startNode(); + tsParseTypeAnnotation(eatColon = true, t: N.TypeAnnotation = this.startNode()): N.TypeAnnotation { + if (eatColon) this.expect(tt.colon); t.typeAnnotation = this.tsParseType(); return this.finishNode(t, "TypeAnnotation"); } @@ -1087,8 +1090,8 @@ export default (superClass: Class): Class => class extends super type: string, allowExpressionBody?: boolean): void { // For arrow functions, `parseArrow` handles the return type itself. - if (!allowExpressionBody && this.eat(tt.colon)) { - node.returnType = this.tsParseTypeOrTypePredicateAnnotation(); + if (!allowExpressionBody && this.match(tt.colon)) { + node.returnType = this.tsParseTypeOrTypePredicateAnnotation(tt.colon); } const bodilessType = type === "FunctionDeclaration" @@ -1358,7 +1361,7 @@ export default (superClass: Class): Class => class extends super node.optional = true; } - if (this.eat(tt.colon)) { + if (this.match(tt.colon)) { const typeCastNode: N.TypeCastExpression = this.startNodeAt(startPos, startLoc); typeCastNode.expression = node; typeCastNode.typeAnnotation = this.tsParseTypeAnnotation(); @@ -1455,7 +1458,7 @@ export default (superClass: Class): Class => class extends super node: N.ArrowFunctionExpression, call: N.CallExpression): N.ArrowFunctionExpression { - if (this.eat(tt.colon)) { + if (this.match(tt.colon)) { node.returnType = this.tsParseTypeAnnotation(); } return super.parseAsyncArrowFromCallExpression(node, call); @@ -1552,9 +1555,8 @@ export default (superClass: Class): Class => class extends super // This is different from how the TS parser does it. // TS uses lookahead. Babylon parses it as a parenthesized expression and converts. const state = this.state.clone(); - this.next(); try { - const returnType = this.tsParseTypeOrTypePredicateAnnotation(); + const returnType = this.tsParseTypeOrTypePredicateAnnotation(tt.colon); if (this.canInsertSemicolon()) this.unexpected(); if (!this.match(tt.arrow)) this.unexpected(); node.returnType = returnType; diff --git a/test/fixtures/typescript/arrow-function/annotated/expected.json b/test/fixtures/typescript/arrow-function/annotated/expected.json index c920cf7d4b..4c9939e4a9 100644 --- a/test/fixtures/typescript/arrow-function/annotated/expected.json +++ b/test/fixtures/typescript/arrow-function/annotated/expected.json @@ -58,12 +58,12 @@ }, "returnType": { "type": "TypeAnnotation", - "start": 13, + "start": 11, "end": 19, "loc": { "start": { "line": 1, - "column": 13 + "column": 11 }, "end": { "line": 1, @@ -109,12 +109,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 4, + "start": 2, "end": 10, "loc": { "start": { "line": 1, - "column": 4 + "column": 2 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/arrow-function/async-generic/expected.json b/test/fixtures/typescript/arrow-function/async-generic/expected.json index 7edff6b60f..86035a3967 100644 --- a/test/fixtures/typescript/arrow-function/async-generic/expected.json +++ b/test/fixtures/typescript/arrow-function/async-generic/expected.json @@ -56,53 +56,6 @@ "column": 23 } }, - "returnType": { - "type": "TypeAnnotation", - "start": 17, - "end": 18, - "loc": { - "start": { - "line": 1, - "column": 17 - }, - "end": { - "line": 1, - "column": 18 - } - }, - "typeAnnotation": { - "type": "TSTypeReference", - "start": 17, - "end": 18, - "loc": { - "start": { - "line": 1, - "column": 17 - }, - "end": { - "line": 1, - "column": 18 - } - }, - "typeName": { - "type": "Identifier", - "start": 17, - "end": 18, - "loc": { - "start": { - "line": 1, - "column": 17 - }, - "end": { - "line": 1, - "column": 18 - }, - "identifierName": "T" - }, - "name": "T" - } - } - }, "typeParameters": { "type": "TypeParameterDeclaration", "start": 7, @@ -136,10 +89,6 @@ } ] }, - "id": null, - "generator": false, - "expression": true, - "async": true, "params": [ { "type": "Identifier", @@ -159,12 +108,12 @@ "name": "a", "typeAnnotation": { "type": "TypeAnnotation", - "start": 13, + "start": 11, "end": 14, "loc": { "start": { "line": 1, - "column": 13 + "column": 11 }, "end": { "line": 1, @@ -206,6 +155,57 @@ } } ], + "returnType": { + "type": "TypeAnnotation", + "start": 15, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "typeName": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + }, + "identifierName": "T" + }, + "name": "T" + } + } + }, + "id": null, + "generator": false, + "expression": true, + "async": true, "body": { "type": "Identifier", "start": 22, diff --git a/test/fixtures/typescript/arrow-function/async/expected.json b/test/fixtures/typescript/arrow-function/async/expected.json index 6849329ebe..424ce9c58d 100644 --- a/test/fixtures/typescript/arrow-function/async/expected.json +++ b/test/fixtures/typescript/arrow-function/async/expected.json @@ -58,12 +58,12 @@ }, "returnType": { "type": "TypeAnnotation", - "start": 20, + "start": 18, "end": 23, "loc": { "start": { "line": 1, - "column": 20 + "column": 18 }, "end": { "line": 1, @@ -110,12 +110,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 11, + "start": 9, "end": 17, "loc": { "start": { "line": 1, - "column": 11 + "column": 9 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json b/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json index 6cd0894a4f..dd80d9124b 100644 --- a/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json +++ b/test/fixtures/typescript/arrow-function/default-parameter-values/expected.json @@ -93,12 +93,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 4, + "start": 2, "end": 10, "loc": { "start": { "line": 1, - "column": 4 + "column": 2 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json index 0437396c5b..b61d4ea254 100644 --- a/test/fixtures/typescript/arrow-function/generic-tsx/expected.json +++ b/test/fixtures/typescript/arrow-function/generic-tsx/expected.json @@ -58,12 +58,12 @@ }, "returnType": { "type": "TypeAnnotation", - "start": 72, + "start": 70, "end": 73, "loc": { "start": { "line": 2, - "column": 11 + "column": 9 }, "end": { "line": 2, @@ -126,12 +126,12 @@ "name": "a", "typeAnnotation": { "type": "TypeAnnotation", - "start": 68, + "start": 66, "end": 69, "loc": { "start": { "line": 2, - "column": 7 + "column": 5 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/arrow-function/generic/expected.json b/test/fixtures/typescript/arrow-function/generic/expected.json index 9c4b47a9ab..6c5f53a23d 100644 --- a/test/fixtures/typescript/arrow-function/generic/expected.json +++ b/test/fixtures/typescript/arrow-function/generic/expected.json @@ -58,12 +58,12 @@ }, "returnType": { "type": "TypeAnnotation", - "start": 11, + "start": 9, "end": 12, "loc": { "start": { "line": 1, - "column": 11 + "column": 9 }, "end": { "line": 1, @@ -126,12 +126,12 @@ "name": "a", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 8, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/arrow-function/optional-parameter/expected.json b/test/fixtures/typescript/arrow-function/optional-parameter/expected.json index 3da2ac662a..0fa880dc0b 100644 --- a/test/fixtures/typescript/arrow-function/optional-parameter/expected.json +++ b/test/fixtures/typescript/arrow-function/optional-parameter/expected.json @@ -58,12 +58,12 @@ }, "returnType": { "type": "TypeAnnotation", - "start": 14, + "start": 12, "end": 17, "loc": { "start": { "line": 1, - "column": 14 + "column": 12 }, "end": { "line": 1, @@ -110,12 +110,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 5, + "start": 3, "end": 11, "loc": { "start": { "line": 1, - "column": 5 + "column": 3 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/arrow-function/predicate-types/expected.json b/test/fixtures/typescript/arrow-function/predicate-types/expected.json index 529fec9593..db5590e5ba 100644 --- a/test/fixtures/typescript/arrow-function/predicate-types/expected.json +++ b/test/fixtures/typescript/arrow-function/predicate-types/expected.json @@ -58,12 +58,12 @@ }, "returnType": { "type": "TypeAnnotation", - "start": 10, + "start": 8, "end": 21, "loc": { "start": { "line": 1, - "column": 10 + "column": 8 }, "end": { "line": 1, @@ -156,12 +156,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 4, + "start": 2, "end": 7, "loc": { "start": { "line": 1, - "column": 4 + "column": 2 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/class/constructor/expected.json b/test/fixtures/typescript/class/constructor/expected.json index 74867c1fc6..260165a33e 100644 --- a/test/fixtures/typescript/class/constructor/expected.json +++ b/test/fixtures/typescript/class/constructor/expected.json @@ -132,12 +132,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 29, + "start": 27, "end": 35, "loc": { "start": { "line": 2, - "column": 19 + "column": 17 }, "end": { "line": 2, @@ -179,12 +179,12 @@ "name": "y", "typeAnnotation": { "type": "TypeAnnotation", - "start": 40, + "start": 38, "end": 46, "loc": { "start": { "line": 2, - "column": 30 + "column": 28 }, "end": { "line": 2, @@ -267,12 +267,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 68, + "start": 66, "end": 74, "loc": { "start": { "line": 3, - "column": 19 + "column": 17 }, "end": { "line": 3, @@ -314,12 +314,12 @@ "name": "y", "typeAnnotation": { "type": "TypeAnnotation", - "start": 79, + "start": 77, "end": 85, "loc": { "start": { "line": 3, - "column": 30 + "column": 28 }, "end": { "line": 3, @@ -402,12 +402,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 107, + "start": 105, "end": 110, "loc": { "start": { "line": 4, - "column": 19 + "column": 17 }, "end": { "line": 4, @@ -449,12 +449,12 @@ "name": "y", "typeAnnotation": { "type": "TypeAnnotation", - "start": 115, + "start": 113, "end": 118, "loc": { "start": { "line": 4, - "column": 27 + "column": 25 }, "end": { "line": 4, diff --git a/test/fixtures/typescript/class/declare/expected.json b/test/fixtures/typescript/class/declare/expected.json index b4ab86385d..ef56cc4a7c 100644 --- a/test/fixtures/typescript/class/declare/expected.json +++ b/test/fixtures/typescript/class/declare/expected.json @@ -140,12 +140,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 35, + "start": 33, "end": 38, "loc": { "start": { "line": 2, - "column": 17 + "column": 15 }, "end": { "line": 2, @@ -239,12 +239,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 54, + "start": 52, "end": 60, "loc": { "start": { "line": 4, - "column": 7 + "column": 5 }, "end": { "line": 4, @@ -350,12 +350,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 80, + "start": 78, "end": 84, "loc": { "start": { "line": 6, - "column": 9 + "column": 7 }, "end": { "line": 6, diff --git a/test/fixtures/typescript/class/generic/expected.json b/test/fixtures/typescript/class/generic/expected.json index f64a3d3295..2a84f7e0c1 100644 --- a/test/fixtures/typescript/class/generic/expected.json +++ b/test/fixtures/typescript/class/generic/expected.json @@ -153,12 +153,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 32, + "start": 30, "end": 38, "loc": { "start": { "line": 1, - "column": 32 + "column": 30 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/class/get-generic/expected.json b/test/fixtures/typescript/class/get-generic/expected.json index ec6713b5e1..d8733a2b3e 100644 --- a/test/fixtures/typescript/class/get-generic/expected.json +++ b/test/fixtures/typescript/class/get-generic/expected.json @@ -149,12 +149,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 32, + "start": 30, "end": 36, "loc": { "start": { "line": 2, - "column": 14 + "column": 12 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/class/index-signature/expected.json b/test/fixtures/typescript/class/index-signature/expected.json index f3c1f7dc9f..3c2298fc51 100644 --- a/test/fixtures/typescript/class/index-signature/expected.json +++ b/test/fixtures/typescript/class/index-signature/expected.json @@ -140,12 +140,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 27, + "start": 25, "end": 30, "loc": { "start": { "line": 2, - "column": 17 + "column": 15 }, "end": { "line": 2, @@ -235,12 +235,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 58, + "start": 56, "end": 61, "loc": { "start": { "line": 3, - "column": 26 + "column": 24 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/class/members-with-modifier-names/expected.json b/test/fixtures/typescript/class/members-with-modifier-names/expected.json index 6d8287c3e5..e61ec54c61 100644 --- a/test/fixtures/typescript/class/members-with-modifier-names/expected.json +++ b/test/fixtures/typescript/class/members-with-modifier-names/expected.json @@ -116,12 +116,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 24, + "start": 22, "end": 28, "loc": { "start": { "line": 2, - "column": 14 + "column": 12 }, "end": { "line": 2, @@ -187,12 +187,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 51, + "start": 49, "end": 55, "loc": { "start": { "line": 3, - "column": 21 + "column": 19 }, "end": { "line": 3, @@ -344,12 +344,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 91, + "start": 89, "end": 95, "loc": { "start": { "line": 5, - "column": 16 + "column": 14 }, "end": { "line": 5, diff --git a/test/fixtures/typescript/class/members-with-reserved-names/expected.json b/test/fixtures/typescript/class/members-with-reserved-names/expected.json index 88ba3b71f4..b23d374bce 100644 --- a/test/fixtures/typescript/class/members-with-reserved-names/expected.json +++ b/test/fixtures/typescript/class/members-with-reserved-names/expected.json @@ -117,12 +117,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 31, + "start": 29, "end": 35, "loc": { "start": { "line": 2, - "column": 21 + "column": 19 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/class/method-computed/expected.json b/test/fixtures/typescript/class/method-computed/expected.json index be01e76389..4f2b905560 100644 --- a/test/fixtures/typescript/class/method-computed/expected.json +++ b/test/fixtures/typescript/class/method-computed/expected.json @@ -149,12 +149,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 35, + "start": 33, "end": 39, "loc": { "start": { "line": 2, - "column": 25 + "column": 23 }, "end": { "line": 2, @@ -253,12 +253,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 67, + "start": 65, "end": 71, "loc": { "start": { "line": 3, - "column": 26 + "column": 24 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/class/method-generic/expected.json b/test/fixtures/typescript/class/method-generic/expected.json index fb47a93450..99c4ee7ffe 100644 --- a/test/fixtures/typescript/class/method-generic/expected.json +++ b/test/fixtures/typescript/class/method-generic/expected.json @@ -165,12 +165,12 @@ "name": "a", "typeAnnotation": { "type": "TypeAnnotation", - "start": 22, + "start": 20, "end": 23, "loc": { "start": { "line": 2, - "column": 12 + "column": 10 }, "end": { "line": 2, @@ -230,12 +230,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 29, + "start": 27, "end": 30, "loc": { "start": { "line": 2, - "column": 19 + "column": 17 }, "end": { "line": 2, @@ -309,12 +309,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 38, + "start": 36, "end": 41, "loc": { "start": { "line": 2, - "column": 28 + "column": 26 }, "end": { "line": 2, @@ -373,12 +373,12 @@ ], "returnType": { "type": "TypeAnnotation", - "start": 44, + "start": 42, "end": 45, "loc": { "start": { "line": 2, - "column": 34 + "column": 32 }, "end": { "line": 2, @@ -543,12 +543,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 77, + "start": 75, "end": 78, "loc": { "start": { "line": 3, - "column": 28 + "column": 26 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/class/method-no-body/expected.json b/test/fixtures/typescript/class/method-no-body/expected.json index 1a428191b0..71a82e87ca 100644 --- a/test/fixtures/typescript/class/method-no-body/expected.json +++ b/test/fixtures/typescript/class/method-no-body/expected.json @@ -156,12 +156,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 28, + "start": 26, "end": 32, "loc": { "start": { "line": 3, - "column": 9 + "column": 7 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/class/method-optional/expected.json b/test/fixtures/typescript/class/method-optional/expected.json index b850b36d5a..c96beed6c2 100644 --- a/test/fixtures/typescript/class/method-optional/expected.json +++ b/test/fixtures/typescript/class/method-optional/expected.json @@ -117,12 +117,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 20, + "start": 18, "end": 24, "loc": { "start": { "line": 2, - "column": 10 + "column": 8 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/class/method-return-type/expected.json b/test/fixtures/typescript/class/method-return-type/expected.json index 83cf345feb..483cb98a04 100644 --- a/test/fixtures/typescript/class/method-return-type/expected.json +++ b/test/fixtures/typescript/class/method-return-type/expected.json @@ -116,12 +116,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 19, + "start": 17, "end": 23, "loc": { "start": { "line": 2, - "column": 9 + "column": 7 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/class/modifiers-properties/expected.json b/test/fixtures/typescript/class/modifiers-properties/expected.json index 2b6c0c0818..ebdc3a5bcd 100644 --- a/test/fixtures/typescript/class/modifiers-properties/expected.json +++ b/test/fixtures/typescript/class/modifiers-properties/expected.json @@ -149,12 +149,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 53, + "start": 51, "end": 59, "loc": { "start": { "line": 3, - "column": 18 + "column": 16 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json b/test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json index 94b8b91d2f..e0ff119eb4 100644 --- a/test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json +++ b/test/fixtures/typescript/class/parameter-properties-with-decorators/expected.json @@ -181,12 +181,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 43, + "start": 41, "end": 49, "loc": { "start": { "line": 2, - "column": 33 + "column": 31 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/class/parameter-properties/expected.json b/test/fixtures/typescript/class/parameter-properties/expected.json index ae7a28cf75..606cfe31be 100644 --- a/test/fixtures/typescript/class/parameter-properties/expected.json +++ b/test/fixtures/typescript/class/parameter-properties/expected.json @@ -180,12 +180,12 @@ "name": "pu", "typeAnnotation": { "type": "TypeAnnotation", - "start": 66, + "start": 64, "end": 72, "loc": { "start": { "line": 4, - "column": 19 + "column": 17 }, "end": { "line": 4, @@ -278,12 +278,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 118, + "start": 116, "end": 124, "loc": { "start": { "line": 6, - "column": 21 + "column": 19 }, "end": { "line": 6, @@ -478,12 +478,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 241, + "start": 239, "end": 247, "loc": { "start": { "line": 10, - "column": 19 + "column": 17 }, "end": { "line": 10, diff --git a/test/fixtures/typescript/class/properties/expected.json b/test/fixtures/typescript/class/properties/expected.json index 19849bef8e..8cc2487abd 100644 --- a/test/fixtures/typescript/class/properties/expected.json +++ b/test/fixtures/typescript/class/properties/expected.json @@ -181,12 +181,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 32, + "start": 30, "end": 38, "loc": { "start": { "line": 4, - "column": 7 + "column": 5 }, "end": { "line": 4, @@ -246,12 +246,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 47, + "start": 45, "end": 53, "loc": { "start": { "line": 5, - "column": 7 + "column": 5 }, "end": { "line": 5, diff --git a/test/fixtures/typescript/class/property-computed/expected.json b/test/fixtures/typescript/class/property-computed/expected.json index 9b329685a8..7978804e58 100644 --- a/test/fixtures/typescript/class/property-computed/expected.json +++ b/test/fixtures/typescript/class/property-computed/expected.json @@ -143,12 +143,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 33, + "start": 31, "end": 39, "loc": { "start": { "line": 2, - "column": 23 + "column": 21 }, "end": { "line": 2, @@ -242,12 +242,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 65, + "start": 63, "end": 71, "loc": { "start": { "line": 3, - "column": 24 + "column": 22 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/const/no-initializer/expected.json b/test/fixtures/typescript/const/no-initializer/expected.json index 380e4e88b1..58ccff5e4a 100644 --- a/test/fixtures/typescript/const/no-initializer/expected.json +++ b/test/fixtures/typescript/const/no-initializer/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 9, + "start": 7, "end": 15, "loc": { "start": { "line": 1, - "column": 9 + "column": 7 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/declare/const/expected.json b/test/fixtures/typescript/declare/const/expected.json index 43e1ea827a..2d0eb59bff 100644 --- a/test/fixtures/typescript/declare/const/expected.json +++ b/test/fixtures/typescript/declare/const/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 17, + "start": 15, "end": 23, "loc": { "start": { "line": 1, - "column": 17 + "column": 15 }, "end": { "line": 1, @@ -138,12 +138,12 @@ "name": "y", "typeAnnotation": { "type": "TypeAnnotation", - "start": 28, + "start": 26, "end": 34, "loc": { "start": { "line": 1, - "column": 28 + "column": 26 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/declare/destructure/expected.json b/test/fixtures/typescript/declare/destructure/expected.json index 0a5dc26b88..8d28d26e0b 100644 --- a/test/fixtures/typescript/declare/destructure/expected.json +++ b/test/fixtures/typescript/declare/destructure/expected.json @@ -185,12 +185,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 24, + "start": 22, "end": 48, "loc": { "start": { "line": 1, - "column": 24 + "column": 22 }, "end": { "line": 1, @@ -246,12 +246,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 29, + "start": 27, "end": 35, "loc": { "start": { "line": 1, - "column": 29 + "column": 27 }, "end": { "line": 1, @@ -309,12 +309,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 40, + "start": 38, "end": 46, "loc": { "start": { "line": 1, - "column": 40 + "column": 38 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/declare/var/expected.json b/test/fixtures/typescript/declare/var/expected.json index 5a8ce6961b..a790e494e6 100644 --- a/test/fixtures/typescript/declare/var/expected.json +++ b/test/fixtures/typescript/declare/var/expected.json @@ -127,12 +127,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 30, + "start": 28, "end": 33, "loc": { "start": { "line": 2, - "column": 15 + "column": 13 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/export/declare/expected.json b/test/fixtures/typescript/export/declare/expected.json index 4481ac1082..d20d41cbdd 100644 --- a/test/fixtures/typescript/export/declare/expected.json +++ b/test/fixtures/typescript/export/declare/expected.json @@ -91,12 +91,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 24, + "start": 22, "end": 30, "loc": { "start": { "line": 1, - "column": 24 + "column": 22 }, "end": { "line": 1, @@ -180,12 +180,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 61, + "start": 59, "end": 65, "loc": { "start": { "line": 2, - "column": 29 + "column": 27 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/function/annotated/expected.json b/test/fixtures/typescript/function/annotated/expected.json index 78fecb0685..206cd1c3a3 100644 --- a/test/fixtures/typescript/function/annotated/expected.json +++ b/test/fixtures/typescript/function/annotated/expected.json @@ -115,12 +115,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 18, + "start": 16, "end": 19, "loc": { "start": { "line": 1, - "column": 18 + "column": 16 }, "end": { "line": 1, @@ -164,12 +164,12 @@ ], "returnType": { "type": "TypeAnnotation", - "start": 22, + "start": 20, "end": 23, "loc": { "start": { "line": 1, - "column": 22 + "column": 20 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/function/anonymous/expected.json b/test/fixtures/typescript/function/anonymous/expected.json index 4957d5580f..ba875b73e8 100644 --- a/test/fixtures/typescript/function/anonymous/expected.json +++ b/test/fixtures/typescript/function/anonymous/expected.json @@ -145,12 +145,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 26, + "start": 24, "end": 27, "loc": { "start": { "line": 1, - "column": 26 + "column": 24 }, "end": { "line": 1, @@ -194,12 +194,12 @@ ], "returnType": { "type": "TypeAnnotation", - "start": 30, + "start": 28, "end": 31, "loc": { "start": { "line": 1, - "column": 30 + "column": 28 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/function/declare/expected.json b/test/fixtures/typescript/function/declare/expected.json index 5574c30e62..1b115529a9 100644 --- a/test/fixtures/typescript/function/declare/expected.json +++ b/test/fixtures/typescript/function/declare/expected.json @@ -65,12 +65,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 22, + "start": 20, "end": 26, "loc": { "start": { "line": 1, - "column": 22 + "column": 20 }, "end": { "line": 1, @@ -165,12 +165,12 @@ "params": [], "returnType": { "type": "TypeAnnotation", - "start": 53, + "start": 51, "end": 54, "loc": { "start": { "line": 2, - "column": 25 + "column": 23 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/function/export-default/expected.json b/test/fixtures/typescript/function/export-default/expected.json index 6cfd14e943..26302894ff 100644 --- a/test/fixtures/typescript/function/export-default/expected.json +++ b/test/fixtures/typescript/function/export-default/expected.json @@ -80,12 +80,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 28, + "start": 26, "end": 34, "loc": { "start": { "line": 1, - "column": 28 + "column": 26 }, "end": { "line": 1, @@ -112,12 +112,12 @@ ], "returnType": { "type": "TypeAnnotation", - "start": 37, + "start": 35, "end": 41, "loc": { "start": { "line": 1, - "column": 37 + "column": 35 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/function/overloads/expected.json b/test/fixtures/typescript/function/overloads/expected.json index 5d9a028815..04801d51b3 100644 --- a/test/fixtures/typescript/function/overloads/expected.json +++ b/test/fixtures/typescript/function/overloads/expected.json @@ -97,12 +97,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 21, + "start": 19, "end": 27, "loc": { "start": { "line": 1, - "column": 21 + "column": 19 }, "end": { "line": 1, @@ -129,12 +129,12 @@ ], "returnType": { "type": "TypeAnnotation", - "start": 30, + "start": 28, "end": 36, "loc": { "start": { "line": 1, - "column": 30 + "column": 28 }, "end": { "line": 1, @@ -228,12 +228,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 59, + "start": 57, "end": 65, "loc": { "start": { "line": 2, - "column": 21 + "column": 19 }, "end": { "line": 2, @@ -260,12 +260,12 @@ ], "returnType": { "type": "TypeAnnotation", - "start": 68, + "start": 66, "end": 74, "loc": { "start": { "line": 2, - "column": 30 + "column": 28 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/function/predicate-types/expected.json b/test/fixtures/typescript/function/predicate-types/expected.json index 09bb67f9c8..6053c0ef64 100644 --- a/test/fixtures/typescript/function/predicate-types/expected.json +++ b/test/fixtures/typescript/function/predicate-types/expected.json @@ -81,12 +81,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 14, + "start": 12, "end": 17, "loc": { "start": { "line": 1, - "column": 14 + "column": 12 }, "end": { "line": 1, @@ -113,12 +113,12 @@ ], "returnType": { "type": "TypeAnnotation", - "start": 20, + "start": 18, "end": 32, "loc": { "start": { "line": 1, - "column": 20 + "column": 18 }, "end": { "line": 1, @@ -257,12 +257,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 49, + "start": 47, "end": 52, "loc": { "start": { "line": 2, - "column": 13 + "column": 11 }, "end": { "line": 2, @@ -289,12 +289,12 @@ ], "returnType": { "type": "TypeAnnotation", - "start": 55, + "start": 53, "end": 67, "loc": { "start": { "line": 2, - "column": 19 + "column": 17 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/interface/call-signature/expected.json b/test/fixtures/typescript/interface/call-signature/expected.json index 04965b80b5..61e5359a15 100644 --- a/test/fixtures/typescript/interface/call-signature/expected.json +++ b/test/fixtures/typescript/interface/call-signature/expected.json @@ -107,12 +107,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 22, + "start": 20, "end": 28, "loc": { "start": { "line": 2, - "column": 8 + "column": 6 }, "end": { "line": 2, @@ -139,12 +139,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 31, + "start": 29, "end": 35, "loc": { "start": { "line": 2, - "column": 17 + "column": 15 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/interface/construct-signature/expected.json b/test/fixtures/typescript/interface/construct-signature/expected.json index 8722621e20..15e105e1d0 100644 --- a/test/fixtures/typescript/interface/construct-signature/expected.json +++ b/test/fixtures/typescript/interface/construct-signature/expected.json @@ -107,12 +107,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 26, + "start": 24, "end": 32, "loc": { "start": { "line": 2, - "column": 12 + "column": 10 }, "end": { "line": 2, @@ -139,12 +139,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 35, + "start": 33, "end": 39, "loc": { "start": { "line": 2, - "column": 21 + "column": 19 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/interface/generic/expected.json b/test/fixtures/typescript/interface/generic/expected.json index 7abcc3fb45..d3f1878e05 100644 --- a/test/fixtures/typescript/interface/generic/expected.json +++ b/test/fixtures/typescript/interface/generic/expected.json @@ -153,12 +153,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 36, + "start": 34, "end": 42, "loc": { "start": { "line": 1, - "column": 36 + "column": 34 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/interface/index-signature/expected.json b/test/fixtures/typescript/interface/index-signature/expected.json index 574f4f8673..e3fe435449 100644 --- a/test/fixtures/typescript/interface/index-signature/expected.json +++ b/test/fixtures/typescript/interface/index-signature/expected.json @@ -139,12 +139,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 31, + "start": 29, "end": 37, "loc": { "start": { "line": 2, - "column": 17 + "column": 15 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/interface/method-computed/expected.json b/test/fixtures/typescript/interface/method-computed/expected.json index cfc2786e3b..d703d207c3 100644 --- a/test/fixtures/typescript/interface/method-computed/expected.json +++ b/test/fixtures/typescript/interface/method-computed/expected.json @@ -142,12 +142,12 @@ "parameters": [], "typeAnnotation": { "type": "TypeAnnotation", - "start": 39, + "start": 37, "end": 43, "loc": { "start": { "line": 2, - "column": 25 + "column": 23 }, "end": { "line": 2, @@ -240,12 +240,12 @@ "parameters": [], "typeAnnotation": { "type": "TypeAnnotation", - "start": 71, + "start": 69, "end": 77, "loc": { "start": { "line": 3, - "column": 26 + "column": 24 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/interface/method-generic/expected.json b/test/fixtures/typescript/interface/method-generic/expected.json index 14cc88e0d0..1293957e3f 100644 --- a/test/fixtures/typescript/interface/method-generic/expected.json +++ b/test/fixtures/typescript/interface/method-generic/expected.json @@ -200,12 +200,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 44, + "start": 42, "end": 50, "loc": { "start": { "line": 2, - "column": 30 + "column": 28 }, "end": { "line": 2, @@ -237,12 +237,12 @@ "parameters": [], "typeAnnotation": { "type": "TypeAnnotation", - "start": 57, + "start": 55, "end": 58, "loc": { "start": { "line": 2, - "column": 43 + "column": 41 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/interface/method-optional/expected.json b/test/fixtures/typescript/interface/method-optional/expected.json index 0964244246..3d52096664 100644 --- a/test/fixtures/typescript/interface/method-optional/expected.json +++ b/test/fixtures/typescript/interface/method-optional/expected.json @@ -110,12 +110,12 @@ "parameters": [], "typeAnnotation": { "type": "TypeAnnotation", - "start": 24, + "start": 22, "end": 28, "loc": { "start": { "line": 2, - "column": 10 + "column": 8 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/interface/method-plain/expected.json b/test/fixtures/typescript/interface/method-plain/expected.json index 8d6317dd5e..2ad1590a2a 100644 --- a/test/fixtures/typescript/interface/method-plain/expected.json +++ b/test/fixtures/typescript/interface/method-plain/expected.json @@ -160,12 +160,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 33, + "start": 31, "end": 39, "loc": { "start": { "line": 3, - "column": 10 + "column": 8 }, "end": { "line": 3, @@ -222,12 +222,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 47, + "start": 45, "end": 55, "loc": { "start": { "line": 3, - "column": 24 + "column": 22 }, "end": { "line": 3, @@ -269,12 +269,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 58, + "start": 56, "end": 62, "loc": { "start": { "line": 3, - "column": 35 + "column": 33 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/interface/modifiers/expected.json b/test/fixtures/typescript/interface/modifiers/expected.json index eedeb69ddc..64509cbce8 100644 --- a/test/fixtures/typescript/interface/modifiers/expected.json +++ b/test/fixtures/typescript/interface/modifiers/expected.json @@ -109,12 +109,12 @@ "readonly": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 30, + "start": 28, "end": 36, "loc": { "start": { "line": 2, - "column": 16 + "column": 14 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/interface/properties/expected.json b/test/fixtures/typescript/interface/properties/expected.json index 2ade19ecaf..71654d6806 100644 --- a/test/fixtures/typescript/interface/properties/expected.json +++ b/test/fixtures/typescript/interface/properties/expected.json @@ -141,12 +141,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 28, + "start": 26, "end": 34, "loc": { "start": { "line": 3, - "column": 7 + "column": 5 }, "end": { "line": 3, @@ -205,12 +205,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 44, + "start": 42, "end": 50, "loc": { "start": { "line": 4, - "column": 8 + "column": 6 }, "end": { "line": 4, diff --git a/test/fixtures/typescript/interface/property-computed/expected.json b/test/fixtures/typescript/interface/property-computed/expected.json index c4adcbaf6f..b859411d56 100644 --- a/test/fixtures/typescript/interface/property-computed/expected.json +++ b/test/fixtures/typescript/interface/property-computed/expected.json @@ -141,12 +141,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 37, + "start": 35, "end": 43, "loc": { "start": { "line": 2, - "column": 23 + "column": 21 }, "end": { "line": 2, @@ -238,12 +238,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 69, + "start": 67, "end": 75, "loc": { "start": { "line": 3, - "column": 24 + "column": 22 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/interface/property-named-public/expected.json b/test/fixtures/typescript/interface/property-named-public/expected.json index 92531b56c3..1e6b5d8e56 100644 --- a/test/fixtures/typescript/interface/property-named-public/expected.json +++ b/test/fixtures/typescript/interface/property-named-public/expected.json @@ -108,12 +108,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 23, + "start": 21, "end": 29, "loc": { "start": { "line": 2, - "column": 9 + "column": 7 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/interface/reserved-method-name/expected.json b/test/fixtures/typescript/interface/reserved-method-name/expected.json index 71cbbf880f..e63d75c7dc 100644 --- a/test/fixtures/typescript/interface/reserved-method-name/expected.json +++ b/test/fixtures/typescript/interface/reserved-method-name/expected.json @@ -109,12 +109,12 @@ "parameters": [], "typeAnnotation": { "type": "TypeAnnotation", - "start": 27, + "start": 25, "end": 31, "loc": { "start": { "line": 2, - "column": 13 + "column": 11 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/interface/separators/expected.json b/test/fixtures/typescript/interface/separators/expected.json index 6bee518fdf..6470be82e4 100644 --- a/test/fixtures/typescript/interface/separators/expected.json +++ b/test/fixtures/typescript/interface/separators/expected.json @@ -108,12 +108,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 21, + "start": 19, "end": 27, "loc": { "start": { "line": 1, - "column": 21 + "column": 19 }, "end": { "line": 1, @@ -171,12 +171,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 32, + "start": 30, "end": 38, "loc": { "start": { "line": 1, - "column": 32 + "column": 30 }, "end": { "line": 1, @@ -283,12 +283,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 61, + "start": 59, "end": 67, "loc": { "start": { "line": 2, - "column": 20 + "column": 18 }, "end": { "line": 2, @@ -346,12 +346,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 72, + "start": 70, "end": 78, "loc": { "start": { "line": 2, - "column": 31 + "column": 29 }, "end": { "line": 2, @@ -458,12 +458,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 108, + "start": 106, "end": 114, "loc": { "start": { "line": 4, - "column": 7 + "column": 5 }, "end": { "line": 4, @@ -521,12 +521,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 122, + "start": 120, "end": 128, "loc": { "start": { "line": 5, - "column": 7 + "column": 5 }, "end": { "line": 5, diff --git a/test/fixtures/typescript/module-namespace/body-declare/expected.json b/test/fixtures/typescript/module-namespace/body-declare/expected.json index 5376141777..31fb26a418 100644 --- a/test/fixtures/typescript/module-namespace/body-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/body-declare/expected.json @@ -121,12 +121,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 35, + "start": 33, "end": 41, "loc": { "start": { "line": 2, - "column": 13 + "column": 11 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json index 3172253e59..1a3d715c76 100644 --- a/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json +++ b/test/fixtures/typescript/module-namespace/body-nested-declare/expected.json @@ -167,12 +167,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 57, + "start": 55, "end": 63, "loc": { "start": { "line": 3, - "column": 17 + "column": 15 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/module-namespace/global-in-module/expected.json b/test/fixtures/typescript/module-namespace/global-in-module/expected.json index ffae13bcf8..1477ada618 100644 --- a/test/fixtures/typescript/module-namespace/global-in-module/expected.json +++ b/test/fixtures/typescript/module-namespace/global-in-module/expected.json @@ -171,12 +171,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 49, + "start": 47, "end": 55, "loc": { "start": { "line": 3, - "column": 15 + "column": 13 }, "end": { "line": 3, diff --git a/test/fixtures/typescript/type-alias/generic-complex/expected.json b/test/fixtures/typescript/type-alias/generic-complex/expected.json index f27221db96..f06680eaa4 100644 --- a/test/fixtures/typescript/type-alias/generic-complex/expected.json +++ b/test/fixtures/typescript/type-alias/generic-complex/expected.json @@ -153,12 +153,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 31, + "start": 29, "end": 37, "loc": { "start": { "line": 1, - "column": 31 + "column": 29 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/array/expected.json b/test/fixtures/typescript/types/array/expected.json index df9def1b23..544df535c4 100644 --- a/test/fixtures/typescript/types/array/expected.json +++ b/test/fixtures/typescript/types/array/expected.json @@ -75,12 +75,12 @@ "name": "arr", "typeAnnotation": { "type": "TypeAnnotation", - "start": 9, + "start": 7, "end": 19, "loc": { "start": { "line": 1, - "column": 9 + "column": 7 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/function-generic/expected.json b/test/fixtures/typescript/types/function-generic/expected.json index bc7d257bc2..11d92aed58 100644 --- a/test/fixtures/typescript/types/function-generic/expected.json +++ b/test/fixtures/typescript/types/function-generic/expected.json @@ -75,12 +75,12 @@ "name": "f", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 21, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, @@ -153,12 +153,12 @@ "name": "a", "typeAnnotation": { "type": "TypeAnnotation", - "start": 14, + "start": 12, "end": 15, "loc": { "start": { "line": 1, - "column": 14 + "column": 12 }, "end": { "line": 1, @@ -202,12 +202,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 20, + "start": 17, "end": 21, "loc": { "start": { "line": 1, - "column": 20 + "column": 17 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/function-in-generic/expected.json b/test/fixtures/typescript/types/function-in-generic/expected.json index 946b34e4de..89e8e68bad 100644 --- a/test/fixtures/typescript/types/function-in-generic/expected.json +++ b/test/fixtures/typescript/types/function-in-generic/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 24, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, @@ -150,12 +150,12 @@ "parameters": [], "typeAnnotation": { "type": "TypeAnnotation", - "start": 19, + "start": 16, "end": 23, "loc": { "start": { "line": 1, - "column": 19 + "column": 16 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/function-with-this/expected.json b/test/fixtures/typescript/types/function-with-this/expected.json index 8cfe6f197d..fc64db43de 100644 --- a/test/fixtures/typescript/types/function-with-this/expected.json +++ b/test/fixtures/typescript/types/function-with-this/expected.json @@ -75,12 +75,12 @@ "name": "f", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 29, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, @@ -120,12 +120,12 @@ "name": "this", "typeAnnotation": { "type": "TypeAnnotation", - "start": 14, + "start": 12, "end": 20, "loc": { "start": { "line": 1, - "column": 14 + "column": 12 }, "end": { "line": 1, @@ -152,12 +152,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 25, + "start": 22, "end": 29, "loc": { "start": { "line": 1, - "column": 25 + "column": 22 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/function/expected.json b/test/fixtures/typescript/types/function/expected.json index 32081abfbc..1a07ff6407 100644 --- a/test/fixtures/typescript/types/function/expected.json +++ b/test/fixtures/typescript/types/function/expected.json @@ -75,12 +75,12 @@ "name": "f", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 54, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, @@ -120,12 +120,12 @@ "name": "a", "typeAnnotation": { "type": "TypeAnnotation", - "start": 11, + "start": 9, "end": 17, "loc": { "start": { "line": 1, - "column": 11 + "column": 9 }, "end": { "line": 1, @@ -168,12 +168,12 @@ "optional": true, "typeAnnotation": { "type": "TypeAnnotation", - "start": 23, + "start": 21, "end": 29, "loc": { "start": { "line": 1, - "column": 23 + "column": 21 }, "end": { "line": 1, @@ -230,12 +230,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 37, + "start": 35, "end": 45, "loc": { "start": { "line": 1, - "column": 37 + "column": 35 }, "end": { "line": 1, @@ -277,12 +277,12 @@ ], "typeAnnotation": { "type": "TypeAnnotation", - "start": 50, + "start": 47, "end": 54, "loc": { "start": { "line": 1, - "column": 50 + "column": 47 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/indexed/expected.json b/test/fixtures/typescript/types/indexed/expected.json index 1be0b67ff4..0524607a50 100644 --- a/test/fixtures/typescript/types/indexed/expected.json +++ b/test/fixtures/typescript/types/indexed/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 11, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/keywords/expected.json b/test/fixtures/typescript/types/keywords/expected.json index 58298cde14..aef7589aae 100644 --- a/test/fixtures/typescript/types/keywords/expected.json +++ b/test/fixtures/typescript/types/keywords/expected.json @@ -75,12 +75,12 @@ "name": "a", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 10, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, @@ -156,12 +156,12 @@ "name": "b", "typeAnnotation": { "type": "TypeAnnotation", - "start": 19, + "start": 17, "end": 26, "loc": { "start": { "line": 2, - "column": 7 + "column": 5 }, "end": { "line": 2, @@ -237,12 +237,12 @@ "name": "ne", "typeAnnotation": { "type": "TypeAnnotation", - "start": 36, + "start": 34, "end": 41, "loc": { "start": { "line": 3, - "column": 8 + "column": 6 }, "end": { "line": 3, @@ -318,12 +318,12 @@ "name": "nul", "typeAnnotation": { "type": "TypeAnnotation", - "start": 52, + "start": 50, "end": 56, "loc": { "start": { "line": 4, - "column": 9 + "column": 7 }, "end": { "line": 4, @@ -399,12 +399,12 @@ "name": "num", "typeAnnotation": { "type": "TypeAnnotation", - "start": 67, + "start": 65, "end": 73, "loc": { "start": { "line": 5, - "column": 9 + "column": 7 }, "end": { "line": 5, @@ -480,12 +480,12 @@ "name": "o", "typeAnnotation": { "type": "TypeAnnotation", - "start": 82, + "start": 80, "end": 88, "loc": { "start": { "line": 6, - "column": 7 + "column": 5 }, "end": { "line": 6, @@ -561,12 +561,12 @@ "name": "st", "typeAnnotation": { "type": "TypeAnnotation", - "start": 98, + "start": 96, "end": 104, "loc": { "start": { "line": 7, - "column": 8 + "column": 6 }, "end": { "line": 7, @@ -642,12 +642,12 @@ "name": "sy", "typeAnnotation": { "type": "TypeAnnotation", - "start": 114, + "start": 112, "end": 120, "loc": { "start": { "line": 8, - "column": 8 + "column": 6 }, "end": { "line": 8, @@ -723,12 +723,12 @@ "name": "u", "typeAnnotation": { "type": "TypeAnnotation", - "start": 129, + "start": 127, "end": 138, "loc": { "start": { "line": 9, - "column": 7 + "column": 5 }, "end": { "line": 9, @@ -804,12 +804,12 @@ "name": "v", "typeAnnotation": { "type": "TypeAnnotation", - "start": 147, + "start": 145, "end": 151, "loc": { "start": { "line": 10, - "column": 7 + "column": 5 }, "end": { "line": 10, diff --git a/test/fixtures/typescript/types/literal-boolean/expected.json b/test/fixtures/typescript/types/literal-boolean/expected.json index 407b568dc7..6b0526b882 100644 --- a/test/fixtures/typescript/types/literal-boolean/expected.json +++ b/test/fixtures/typescript/types/literal-boolean/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 11, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, @@ -172,12 +172,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 20, + "start": 18, "end": 25, "loc": { "start": { "line": 2, - "column": 7 + "column": 5 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/types/literal-number-negative/expected.json b/test/fixtures/typescript/types/literal-number-negative/expected.json index 5f4f8ea30f..32eff1d795 100644 --- a/test/fixtures/typescript/types/literal-number-negative/expected.json +++ b/test/fixtures/typescript/types/literal-number-negative/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 9, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/literal-number/expected.json b/test/fixtures/typescript/types/literal-number/expected.json index bbe6431e6a..32e844e17f 100644 --- a/test/fixtures/typescript/types/literal-number/expected.json +++ b/test/fixtures/typescript/types/literal-number/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 8, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/literal-string/expected.json b/test/fixtures/typescript/types/literal-string/expected.json index d840cb8959..842cf493b2 100644 --- a/test/fixtures/typescript/types/literal-string/expected.json +++ b/test/fixtures/typescript/types/literal-string/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 12, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/mapped/expected.json b/test/fixtures/typescript/types/mapped/expected.json index c8320ac82c..441c47e824 100644 --- a/test/fixtures/typescript/types/mapped/expected.json +++ b/test/fixtures/typescript/types/mapped/expected.json @@ -75,12 +75,12 @@ "name": "map", "typeAnnotation": { "type": "TypeAnnotation", - "start": 9, + "start": 7, "end": 35, "loc": { "start": { "line": 1, - "column": 9 + "column": 7 }, "end": { "line": 1, @@ -202,12 +202,12 @@ "name": "map", "typeAnnotation": { "type": "TypeAnnotation", - "start": 46, + "start": 44, "end": 82, "loc": { "start": { "line": 2, - "column": 9 + "column": 7 }, "end": { "line": 2, diff --git a/test/fixtures/typescript/types/reference-generic-nested/expected.json b/test/fixtures/typescript/types/reference-generic-nested/expected.json index 32a5d1c7b2..a0e10cfcea 100644 --- a/test/fixtures/typescript/types/reference-generic-nested/expected.json +++ b/test/fixtures/typescript/types/reference-generic-nested/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 27, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/reference-generic/expected.json b/test/fixtures/typescript/types/reference-generic/expected.json index 9f720f041b..f9132c531e 100644 --- a/test/fixtures/typescript/types/reference-generic/expected.json +++ b/test/fixtures/typescript/types/reference-generic/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 20, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/reference/expected.json b/test/fixtures/typescript/types/reference/expected.json index 157b23cbba..6bec175d53 100644 --- a/test/fixtures/typescript/types/reference/expected.json +++ b/test/fixtures/typescript/types/reference/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 8, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/type-literal/expected.json b/test/fixtures/typescript/types/type-literal/expected.json index c6ded591bc..000ee5dad5 100644 --- a/test/fixtures/typescript/types/type-literal/expected.json +++ b/test/fixtures/typescript/types/type-literal/expected.json @@ -75,12 +75,12 @@ "name": "obj", "typeAnnotation": { "type": "TypeAnnotation", - "start": 9, + "start": 7, "end": 22, "loc": { "start": { "line": 1, - "column": 9 + "column": 7 }, "end": { "line": 1, @@ -136,12 +136,12 @@ }, "typeAnnotation": { "type": "TypeAnnotation", - "start": 14, + "start": 12, "end": 20, "loc": { "start": { "line": 1, - "column": 14 + "column": 12 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/type-operator/expected.json b/test/fixtures/typescript/types/type-operator/expected.json index 58080d57b3..87635dfffa 100644 --- a/test/fixtures/typescript/types/type-operator/expected.json +++ b/test/fixtures/typescript/types/type-operator/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 14, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/typeof/expected.json b/test/fixtures/typescript/types/typeof/expected.json index edd14acd6a..de41af47d4 100644 --- a/test/fixtures/typescript/types/typeof/expected.json +++ b/test/fixtures/typescript/types/typeof/expected.json @@ -75,12 +75,12 @@ "name": "x", "typeAnnotation": { "type": "TypeAnnotation", - "start": 7, + "start": 5, "end": 17, "loc": { "start": { "line": 1, - "column": 7 + "column": 5 }, "end": { "line": 1, diff --git a/test/fixtures/typescript/types/union-intersection/expected.json b/test/fixtures/typescript/types/union-intersection/expected.json index d54e2867dd..cc97fa9693 100644 --- a/test/fixtures/typescript/types/union-intersection/expected.json +++ b/test/fixtures/typescript/types/union-intersection/expected.json @@ -75,12 +75,12 @@ "name": "union", "typeAnnotation": { "type": "TypeAnnotation", - "start": 11, + "start": 9, "end": 36, "loc": { "start": { "line": 1, - "column": 11 + "column": 9 }, "end": { "line": 1, @@ -203,12 +203,12 @@ "name": "intersection", "typeAnnotation": { "type": "TypeAnnotation", - "start": 56, + "start": 54, "end": 71, "loc": { "start": { "line": 2, - "column": 18 + "column": 16 }, "end": { "line": 2, @@ -316,12 +316,12 @@ "name": "precedence1", "typeAnnotation": { "type": "TypeAnnotation", - "start": 90, + "start": 88, "end": 115, "loc": { "start": { "line": 3, - "column": 17 + "column": 15 }, "end": { "line": 3, @@ -461,12 +461,12 @@ "name": "precedence2", "typeAnnotation": { "type": "TypeAnnotation", - "start": 134, + "start": 132, "end": 159, "loc": { "start": { "line": 4, - "column": 17 + "column": 15 }, "end": { "line": 4, From f55e24f6b76cc0e840f1cc2bac51c98f27e9cc36 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 23 Jun 2017 10:32:12 -0700 Subject: [PATCH 52/55] Remove unused method --- src/parser/lval.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/parser/lval.js b/src/parser/lval.js index bedabd74cc..8b360455ea 100644 --- a/src/parser/lval.js +++ b/src/parser/lval.js @@ -1,7 +1,7 @@ // @flow import { types as tt, type TokenType } from "../tokenizer/types"; -import type { TSParameterProperty, Accessibility, Decorator, Expression, Identifier, Node, ObjectExpression, +import type { TSParameterProperty, Decorator, Expression, Identifier, Node, ObjectExpression, ObjectPattern, Pattern, RestElement, SpreadElement } from "../types"; import type { Pos, Position } from "../util/location"; import { NodeUtils } from "./node"; @@ -210,11 +210,6 @@ export default class LValParser extends NodeUtils { return elt; } - // Overridden in typescript.js - parseParameterModifiers(): { accessibility: ?Accessibility, readonly: boolean } { - return { accessibility: undefined, readonly: false }; - } - parseAssignableListItemTypes(param: Pattern): Pattern { return param; } From bb42b099eedfc4e7fb94e210cf0445d801d7c127 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Fri, 23 Jun 2017 10:32:21 -0700 Subject: [PATCH 53/55] Add test fow optional binding element in flow --- test/fixtures/flow/optional-type/6/actual.js | 1 + test/fixtures/flow/optional-type/6/options.json | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 test/fixtures/flow/optional-type/6/actual.js create mode 100644 test/fixtures/flow/optional-type/6/options.json diff --git a/test/fixtures/flow/optional-type/6/actual.js b/test/fixtures/flow/optional-type/6/actual.js new file mode 100644 index 0000000000..d7fa2d78dc --- /dev/null +++ b/test/fixtures/flow/optional-type/6/actual.js @@ -0,0 +1 @@ +function f([]?, {}) {} diff --git a/test/fixtures/flow/optional-type/6/options.json b/test/fixtures/flow/optional-type/6/options.json new file mode 100644 index 0000000000..fb2831d618 --- /dev/null +++ b/test/fixtures/flow/optional-type/6/options.json @@ -0,0 +1,3 @@ +{ + "throws": "A binding pattern parameter cannot be optional in an implementation signature. (1:11)" +} From 102be0ed29f70813880930c3fecf29b06d50d152 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 26 Jun 2017 14:00:39 -0700 Subject: [PATCH 54/55] Factor out overridable parseNewArguments to replace parseNewTypeArguments --- src/parser/expression.js | 22 +++++++++------------- src/plugins/typescript.js | 16 ++++++++-------- src/types.js | 1 + 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/parser/expression.js b/src/parser/expression.js index 07f5dde16a..29a75eae2c 100644 --- a/src/parser/expression.js +++ b/src/parser/expression.js @@ -793,26 +793,22 @@ export default class ExpressionParser extends LValParser { } node.callee = this.parseNoCallExpr(); - const optional = this.eat(tt.questionDot); - - this.parseNewTypeArguments(node); + if (this.eat(tt.questionDot)) node.optional = true; + this.parseNewArguments(node); + return this.finishNode(node, "NewExpression"); + } + parseNewArguments(node: N.NewExpression): void { if (this.eat(tt.parenL)) { - node.arguments = this.parseExprList(tt.parenR); - this.toReferencedList(node.arguments); + const args = this.parseExprList(tt.parenR); + this.toReferencedList(args); + // $FlowFixMe (parseExprList should be all non-null in this case) + node.arguments = args; } else { node.arguments = []; } - if (optional) { - node.optional = true; - } - - return this.finishNode(node, "NewExpression"); } - // eslint-disable-next-line no-unused-vars - parseNewTypeArguments(node: N.NewExpression) {} - // Parse template expression. parseTemplateElement(isTagged: boolean): N.TemplateElement { diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 16a6d45e03..100b1bbf4d 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1147,16 +1147,16 @@ export default (superClass: Class): Class => class extends super return super.parseSubscript(base, startPos, startLoc, noCalls, state); } - parseNewTypeArguments(node: N.NewExpression): void { - if (!this.isRelational("<")) { // tsTryParseAndCatch is expensive, so avoid if not necessary. - return; + parseNewArguments(node: N.NewExpression): void { + if (this.isRelational("<")) { // tsTryParseAndCatch is expensive, so avoid if not necessary. + // 99% certain this is `new C();`. But may be `new C < T;`, which is also legal. + const typeParameters = this.tsTryParseAndCatch(this.tsParseTypeArguments.bind(this)); + if (typeParameters) { + node.typeParameters = typeParameters; + } } - // 99% certain this is `new C();`. But may be `new C < T;`, which is also legal. - const typeParameters = this.tsTryParseAndCatch(this.tsParseTypeArguments.bind(this)); - if (typeParameters) { - node.typeParameters = typeParameters; - } + super.parseNewArguments(node); } parseExprOp( diff --git a/src/types.js b/src/types.js index b5e1a82a62..0b6492c8b5 100644 --- a/src/types.js +++ b/src/types.js @@ -487,6 +487,7 @@ export type CallExpression = CallOrNewBase & { export type NewExpression = CallOrNewBase & { type: "NewExpression"; + optional?: boolean; // TODO: Not in spec }; export type SequenceExpression = NodeBase & { From e272c449917d65e608511c45fa449eaffbf2bca8 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 27 Jun 2017 10:01:53 -0700 Subject: [PATCH 55/55] Fail on `new C;` with no parentheses --- src/plugins/typescript.js | 6 +++++- .../type-arguments/new-false-positive-2/actual.js | 1 + .../type-arguments/new-false-positive-2/options.json | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/typescript/type-arguments/new-false-positive-2/actual.js create mode 100644 test/fixtures/typescript/type-arguments/new-false-positive-2/options.json diff --git a/src/plugins/typescript.js b/src/plugins/typescript.js index 100b1bbf4d..b185e6059c 100644 --- a/src/plugins/typescript.js +++ b/src/plugins/typescript.js @@ -1150,7 +1150,11 @@ export default (superClass: Class): Class => class extends super parseNewArguments(node: N.NewExpression): void { if (this.isRelational("<")) { // tsTryParseAndCatch is expensive, so avoid if not necessary. // 99% certain this is `new C();`. But may be `new C < T;`, which is also legal. - const typeParameters = this.tsTryParseAndCatch(this.tsParseTypeArguments.bind(this)); + const typeParameters = this.tsTryParseAndCatch(() => { + const args = this.tsParseTypeArguments(); + if (!this.match(tt.parenL)) this.unexpected(); + return args; + }); if (typeParameters) { node.typeParameters = typeParameters; } diff --git a/test/fixtures/typescript/type-arguments/new-false-positive-2/actual.js b/test/fixtures/typescript/type-arguments/new-false-positive-2/actual.js new file mode 100644 index 0000000000..458a429660 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new-false-positive-2/actual.js @@ -0,0 +1 @@ +new A; diff --git a/test/fixtures/typescript/type-arguments/new-false-positive-2/options.json b/test/fixtures/typescript/type-arguments/new-false-positive-2/options.json new file mode 100644 index 0000000000..07d136e203 --- /dev/null +++ b/test/fixtures/typescript/type-arguments/new-false-positive-2/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:8)" +} \ No newline at end of file