indent-ts.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /**
  2. * @author Yosuke Ota
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. const {
  7. isClosingParenToken,
  8. isOpeningParenToken,
  9. isOpeningBraceToken,
  10. isNotClosingParenToken,
  11. isClosingBracketToken,
  12. isOpeningBracketToken
  13. } = require('eslint-utils')
  14. const { isTypeNode } = require('./ts-ast-utils')
  15. /**
  16. * @typedef {import('../../typings/eslint-plugin-vue/util-types/indent-helper').TSNodeListener} TSNodeListener
  17. * @typedef {import('../../typings/eslint-plugin-vue/util-types/node').HasLocation} HasLocation
  18. * @typedef { { type: string } & HasLocation } MaybeNode
  19. */
  20. /**
  21. * @typedef {import('@typescript-eslint/types').TSESTree.Node} TSESTreeNode
  22. * @typedef {import('@typescript-eslint/types').TSESTree.ClassExpression} ClassExpression
  23. * @typedef {import('@typescript-eslint/types').TSESTree.ClassDeclaration} ClassDeclaration
  24. * @typedef {import('@typescript-eslint/types').TSESTree.TSTypeAliasDeclaration} TSTypeAliasDeclaration
  25. * @typedef {import('@typescript-eslint/types').TSESTree.TSCallSignatureDeclaration} TSCallSignatureDeclaration
  26. * @typedef {import('@typescript-eslint/types').TSESTree.TSConstructSignatureDeclaration} TSConstructSignatureDeclaration
  27. * @typedef {import('@typescript-eslint/types').TSESTree.TSImportEqualsDeclaration} TSImportEqualsDeclaration
  28. * @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractMethodDefinition} TSAbstractMethodDefinition
  29. * @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractPropertyDefinition} TSAbstractPropertyDefinition
  30. * @typedef {import('@typescript-eslint/types').TSESTree.TSEnumMember} TSEnumMember
  31. * @typedef {import('@typescript-eslint/types').TSESTree.TSPropertySignature} TSPropertySignature
  32. * @typedef {import('@typescript-eslint/types').TSESTree.TSIndexSignature} TSIndexSignature
  33. * @typedef {import('@typescript-eslint/types').TSESTree.TSMethodSignature} TSMethodSignature
  34. * @typedef {import('@typescript-eslint/types').TSESTree.TSTypeParameterInstantiation} TSTypeParameterInstantiation
  35. * @typedef {import('@typescript-eslint/types').TSESTree.TSTypeParameterDeclaration} TSTypeParameterDeclaration
  36. * @typedef {import('@typescript-eslint/types').TSESTree.TSConstructorType} TSConstructorType
  37. * @typedef {import('@typescript-eslint/types').TSESTree.TSFunctionType} TSFunctionType
  38. * @typedef {import('@typescript-eslint/types').TSESTree.TSUnionType} TSUnionType
  39. * @typedef {import('@typescript-eslint/types').TSESTree.TSIntersectionType} TSIntersectionType
  40. * @typedef {import('@typescript-eslint/types').TSESTree.TSInterfaceHeritage} TSInterfaceHeritage
  41. * @typedef {import('@typescript-eslint/types').TSESTree.TSClassImplements} TSClassImplements
  42. * @typedef {import('@typescript-eslint/types').TSESTree.TSInterfaceBody} TSInterfaceBody
  43. * @typedef {import('@typescript-eslint/types').TSESTree.TSModuleBlock} TSModuleBlock
  44. * @typedef {import('@typescript-eslint/types').TSESTree.TSDeclareFunction} TSDeclareFunction
  45. * @typedef {import('@typescript-eslint/types').TSESTree.TSEmptyBodyFunctionExpression} TSEmptyBodyFunctionExpression
  46. * @typedef {import('@typescript-eslint/types').TSESTree.TSTypeOperator} TSTypeOperator
  47. * @typedef {import('@typescript-eslint/types').TSESTree.TSTypeQuery} TSTypeQuery
  48. * @typedef {import('@typescript-eslint/types').TSESTree.TSInferType} TSInferType
  49. * @typedef {import('@typescript-eslint/types').TSESTree.TSOptionalType} TSOptionalType
  50. * @typedef {import('@typescript-eslint/types').TSESTree.TSNonNullExpression} TSNonNullExpression
  51. * @typedef {import('@typescript-eslint/types').TSESTree.JSXChild} JSXChild
  52. * @typedef {import('@typescript-eslint/types').TSESTree.TypeNode} TypeNode
  53. *
  54. */
  55. /**
  56. * Deprecated in @typescript-eslint/parser v5
  57. * @typedef {import('@typescript-eslint/types').TSESTree.PropertyDefinition} ClassProperty
  58. * @typedef {import('@typescript-eslint/types').TSESTree.TSAbstractPropertyDefinition} TSAbstractClassProperty
  59. */
  60. module.exports = {
  61. defineVisitor
  62. }
  63. /**
  64. * Process the given node list.
  65. * The first node is offsetted from the given left token.
  66. * Rest nodes are adjusted to the first node.
  67. * @callback ProcessNodeList
  68. * @param {(MaybeNode|null)[]} nodeList The node to process.
  69. * @param {MaybeNode|Token|null} left The left parenthesis token.
  70. * @param {MaybeNode|Token|null} right The right parenthesis token.
  71. * @param {number} offset The offset to set.
  72. * @param {boolean} [alignVertically=true] The flag to align vertically. If `false`, this doesn't align vertically even if the first node is not at beginning of line.
  73. * @returns {void}
  74. */
  75. /**
  76. * Set offset to the given tokens.
  77. * @callback SetOffset
  78. * @param {Token|Token[]|null|(Token|null)[]} token The token to set.
  79. * @param {number} offset The offset of the tokens.
  80. * @param {Token} baseToken The token of the base offset.
  81. * @returns {void}
  82. */
  83. /**
  84. *
  85. * Copy offset to the given tokens from srcToken.
  86. * @callback CopyOffset
  87. * @param {Token} token The token to set.
  88. * @param {Token} srcToken The token of the source offset.
  89. * @returns {void}
  90. */
  91. /**
  92. * Process semicolons of the given statement node.
  93. * @callback ProcessSemicolons
  94. * @param {MaybeNode} node The statement node to process.
  95. * @returns {void}
  96. */
  97. /**
  98. * Get the first and last tokens of the given node.
  99. * If the node is parenthesized, this gets the outermost parentheses.
  100. * @callback GetFirstAndLastTokens
  101. * @param {MaybeNode} node The node to get.
  102. * @param {number} [borderOffset] The least offset of the first token. Defailt is 0. This value is used to prevent false positive in the following case: `(a) => {}` The parentheses are enclosing the whole parameter part rather than the first parameter, but this offset parameter is needed to distinguish.
  103. * @returns {{firstToken:Token,lastToken:Token}} The gotten tokens.
  104. */
  105. /**
  106. * @typedef {object} DefineVisitorParam
  107. * @property {ProcessNodeList} processNodeList
  108. * @property {ParserServices.TokenStore | SourceCode} tokenStore
  109. * @property {SetOffset} setOffset
  110. * @property {CopyOffset} copyOffset
  111. * @property {ProcessSemicolons} processSemicolons
  112. * @property {GetFirstAndLastTokens} getFirstAndLastTokens
  113. */
  114. /**
  115. * @param {DefineVisitorParam} param
  116. * @returns {TSNodeListener}
  117. */
  118. function defineVisitor({
  119. processNodeList,
  120. tokenStore,
  121. setOffset,
  122. copyOffset,
  123. processSemicolons,
  124. getFirstAndLastTokens
  125. }) {
  126. /**
  127. * Check whether a given token is the first token of:
  128. *
  129. * - A parameter of TSTypeParameterInstantiation
  130. * - An element of TSTupleType
  131. *
  132. * @param {Token} token The token to check.
  133. * @param {TSUnionType | TSIntersectionType} belongingNode The node that the token is belonging to.
  134. * @returns {boolean} `true` if the token is the first token of an element.
  135. */
  136. function isBeginningOfElement(token, belongingNode) {
  137. /** @type {TSESTreeNode | null} */
  138. let node = belongingNode
  139. while (node != null && node.parent != null) {
  140. /** @type {TSESTreeNode} */
  141. const parent = node.parent
  142. if (parent.type === 'TSTypeParameterInstantiation') {
  143. return (
  144. parent.params.length >= 2 &&
  145. parent.params.some(
  146. (param) =>
  147. getFirstAndLastTokens(param).firstToken.range[0] ===
  148. token.range[0]
  149. )
  150. )
  151. }
  152. if (parent.type === 'TSTupleType') {
  153. return parent.elementTypes.some(
  154. (element) =>
  155. element != null &&
  156. getFirstAndLastTokens(element).firstToken.range[0] ===
  157. token.range[0]
  158. )
  159. }
  160. node = parent
  161. }
  162. return false
  163. }
  164. return {
  165. // Support TypeScript
  166. /** @param {ClassDeclaration | ClassExpression} node */
  167. ['ClassDeclaration[implements], ClassDeclaration[typeParameters], ClassDeclaration[superTypeParameters],' +
  168. 'ClassExpression[implements], ClassExpression[typeParameters], ClassExpression[superTypeParameters]'](
  169. node
  170. ) {
  171. if (node.typeParameters != null) {
  172. setOffset(
  173. tokenStore.getFirstToken(node.typeParameters),
  174. 1,
  175. tokenStore.getFirstToken(node.id || node)
  176. )
  177. }
  178. if (node.superTypeParameters != null && node.superClass != null) {
  179. setOffset(
  180. tokenStore.getFirstToken(node.superTypeParameters),
  181. 1,
  182. tokenStore.getFirstToken(node.superClass)
  183. )
  184. }
  185. if (node.implements != null && node.implements.length) {
  186. const classToken = tokenStore.getFirstToken(node)
  187. const implementsToken = tokenStore.getTokenBefore(node.implements[0])
  188. setOffset(implementsToken, 1, classToken)
  189. processNodeList(node.implements, implementsToken, null, 1)
  190. }
  191. },
  192. // Process semicolons.
  193. /**
  194. * @param {TSTypeAliasDeclaration
  195. * | TSCallSignatureDeclaration
  196. * | TSConstructSignatureDeclaration
  197. * | TSImportEqualsDeclaration
  198. * | TSAbstractMethodDefinition
  199. * | TSAbstractPropertyDefinition
  200. * | TSAbstractClassProperty
  201. * | TSEnumMember
  202. * | ClassProperty
  203. * | TSPropertySignature
  204. * | TSIndexSignature
  205. * | TSMethodSignature} node
  206. */
  207. ['TSTypeAliasDeclaration, TSCallSignatureDeclaration, TSConstructSignatureDeclaration, TSImportEqualsDeclaration,' +
  208. 'TSAbstractMethodDefinition, TSAbstractPropertyDefinition, TSEnumMember,' +
  209. 'TSPropertySignature, TSIndexSignature, TSMethodSignature,' +
  210. // Deprecated in @typescript-eslint/parser v5
  211. 'ClassProperty, TSAbstractClassProperty'](node) {
  212. processSemicolons(node)
  213. },
  214. /**
  215. * @param {TSESTreeNode} node
  216. */
  217. // eslint-disable-next-line complexity -- ignore
  218. '*[type=/^TS/]'(node) {
  219. if (!isTypeNode(node)) {
  220. return
  221. }
  222. const typeNode = node
  223. if (/** @type {any} */ (typeNode.parent).type === 'TSParenthesizedType') {
  224. return
  225. }
  226. // Process parentheses.
  227. let leftToken = tokenStore.getTokenBefore(node)
  228. let rightToken = tokenStore.getTokenAfter(node)
  229. let firstToken = tokenStore.getFirstToken(node)
  230. while (
  231. leftToken &&
  232. rightToken &&
  233. isOpeningParenToken(leftToken) &&
  234. isClosingParenToken(rightToken)
  235. ) {
  236. setOffset(firstToken, 1, leftToken)
  237. setOffset(rightToken, 0, leftToken)
  238. firstToken = leftToken
  239. leftToken = tokenStore.getTokenBefore(leftToken)
  240. rightToken = tokenStore.getTokenAfter(rightToken)
  241. }
  242. },
  243. /**
  244. * Process type annotation
  245. *
  246. * e.g.
  247. * ```
  248. * const foo: Type
  249. * // ^^^^^^
  250. * type foo = () => string
  251. * // ^^^^^^^^^
  252. * ```
  253. */
  254. TSTypeAnnotation(node) {
  255. const [colonOrArrowToken, secondToken] = tokenStore.getFirstTokens(node, {
  256. count: 2,
  257. includeComments: false
  258. })
  259. const baseToken = tokenStore.getFirstToken(
  260. /** @type {HasLocation} */ (node.parent)
  261. )
  262. setOffset([colonOrArrowToken, secondToken], 1, baseToken)
  263. // a ?: T
  264. const before = tokenStore.getTokenBefore(colonOrArrowToken)
  265. if (before && before.value === '?') {
  266. setOffset(before, 1, baseToken)
  267. }
  268. },
  269. /**
  270. * Process as expression
  271. *
  272. * e.g.
  273. * ```
  274. * var foo = bar as boolean
  275. * // ^^^^^^^^^^^^^^
  276. * ```
  277. */
  278. TSAsExpression(node) {
  279. const expressionTokens = getFirstAndLastTokens(node.expression)
  280. const asToken = tokenStore.getTokenAfter(expressionTokens.lastToken)
  281. setOffset(
  282. [asToken, getFirstAndLastTokens(node.typeAnnotation).firstToken],
  283. 1,
  284. expressionTokens.firstToken
  285. )
  286. },
  287. /**
  288. * Process type reference
  289. *
  290. * e.g.
  291. * ```
  292. * const foo: Type<P>
  293. * // ^^^^^^^
  294. * ```
  295. */
  296. TSTypeReference(node) {
  297. if (node.typeParameters) {
  298. const typeNameTokens = getFirstAndLastTokens(node.typeName)
  299. setOffset(
  300. tokenStore.getFirstToken(node.typeParameters),
  301. 1,
  302. typeNameTokens.firstToken
  303. )
  304. }
  305. },
  306. /**
  307. * Process type parameter instantiation and type parameter declaration
  308. *
  309. * e.g.
  310. * ```
  311. * const foo: Type<P>
  312. * // ^^^
  313. * ```
  314. *
  315. * e.g.
  316. * ```
  317. * type Foo<T>
  318. * // ^^^
  319. * ```
  320. * @param {TSTypeParameterInstantiation | TSTypeParameterDeclaration} node
  321. */
  322. 'TSTypeParameterInstantiation, TSTypeParameterDeclaration'(node) {
  323. // <T>
  324. processNodeList(
  325. node.params,
  326. tokenStore.getFirstToken(node),
  327. tokenStore.getLastToken(node),
  328. 1
  329. )
  330. },
  331. /**
  332. * Process type alias declaration
  333. *
  334. * e.g.
  335. * ```
  336. * type Foo
  337. * ```
  338. */
  339. TSTypeAliasDeclaration(node) {
  340. // type T = {}
  341. const typeToken = tokenStore.getFirstToken(node)
  342. const idToken = tokenStore.getFirstToken(node.id)
  343. setOffset(idToken, 1, typeToken)
  344. let eqToken
  345. if (node.typeParameters) {
  346. setOffset(tokenStore.getFirstToken(node.typeParameters), 1, idToken)
  347. eqToken = tokenStore.getTokenAfter(node.typeParameters)
  348. } else {
  349. eqToken = tokenStore.getTokenAfter(node.id)
  350. }
  351. const initToken = tokenStore.getTokenAfter(eqToken)
  352. setOffset([eqToken, initToken], 1, idToken)
  353. },
  354. /**
  355. * Process constructor type or function type
  356. *
  357. * e.g.
  358. * ```
  359. * type Foo = new () => T
  360. * // ^^^^^^^^^^^
  361. * type Foo = () => void
  362. * // ^^^^^^^^^^
  363. * ```
  364. * @param {TSConstructorType | TSFunctionType} node
  365. */
  366. 'TSConstructorType, TSFunctionType'(node) {
  367. // ()=>void
  368. const firstToken = tokenStore.getFirstToken(node)
  369. // new or < or (
  370. let currToken = firstToken
  371. if (node.type === 'TSConstructorType') {
  372. // currToken is new token
  373. // < or (
  374. currToken = tokenStore.getTokenAfter(currToken)
  375. setOffset(currToken, 1, firstToken)
  376. }
  377. if (node.typeParameters) {
  378. // currToken is < token
  379. // (
  380. currToken = tokenStore.getTokenAfter(node.typeParameters)
  381. setOffset(currToken, 1, firstToken)
  382. }
  383. const leftParenToken = currToken
  384. const rightParenToken = /**@type {Token} */ (
  385. tokenStore.getTokenAfter(
  386. node.params[node.params.length - 1] || leftParenToken,
  387. isClosingParenToken
  388. )
  389. )
  390. processNodeList(node.params, leftParenToken, rightParenToken, 1)
  391. const arrowToken = tokenStore.getTokenAfter(rightParenToken)
  392. setOffset(arrowToken, 1, leftParenToken)
  393. },
  394. /**
  395. * Process type literal
  396. *
  397. * e.g.
  398. * ```
  399. * const foo: { bar: string }
  400. * // ^^^^^^^^^^^^^^^
  401. * ```
  402. */
  403. TSTypeLiteral(node) {
  404. processNodeList(
  405. node.members,
  406. tokenStore.getFirstToken(node),
  407. tokenStore.getLastToken(node),
  408. 1
  409. )
  410. },
  411. /**
  412. * Process property signature
  413. *
  414. * e.g.
  415. * ```
  416. * const foo: { bar: string }
  417. * // ^^^^^^^^^^^
  418. * ```
  419. */
  420. TSPropertySignature(node) {
  421. const firstToken = tokenStore.getFirstToken(node)
  422. const keyTokens = getFirstAndLastTokens(node.key)
  423. let keyLast
  424. if (node.computed) {
  425. const closeBracket = tokenStore.getTokenAfter(keyTokens.lastToken)
  426. processNodeList([node.key], firstToken, closeBracket, 1)
  427. keyLast = closeBracket
  428. } else {
  429. keyLast = keyTokens.lastToken
  430. }
  431. if (node.typeAnnotation) {
  432. const typeAnnotationToken = tokenStore.getFirstToken(
  433. node.typeAnnotation
  434. )
  435. setOffset(
  436. [
  437. ...tokenStore.getTokensBetween(keyLast, typeAnnotationToken),
  438. typeAnnotationToken
  439. ],
  440. 1,
  441. firstToken
  442. )
  443. } else if (node.optional) {
  444. const qToken = tokenStore.getLastToken(node)
  445. setOffset(qToken, 1, firstToken)
  446. }
  447. },
  448. /**
  449. * Process index signature
  450. *
  451. * e.g.
  452. * ```
  453. * const foo: { [bar: string]: string }
  454. * // ^^^^^^^^^^^^^^^^^^^^^
  455. * ```
  456. */
  457. TSIndexSignature(node) {
  458. const leftBracketToken = tokenStore.getFirstToken(node)
  459. const rightBracketToken = /**@type {Token} */ (
  460. tokenStore.getTokenAfter(
  461. node.parameters[node.parameters.length - 1] || leftBracketToken,
  462. isClosingBracketToken
  463. )
  464. )
  465. processNodeList(node.parameters, leftBracketToken, rightBracketToken, 1)
  466. const keyLast = rightBracketToken
  467. if (node.typeAnnotation) {
  468. const typeAnnotationToken = tokenStore.getFirstToken(
  469. node.typeAnnotation
  470. )
  471. setOffset(
  472. [
  473. ...tokenStore.getTokensBetween(keyLast, typeAnnotationToken),
  474. typeAnnotationToken
  475. ],
  476. 1,
  477. leftBracketToken
  478. )
  479. }
  480. },
  481. /**
  482. * Process array type
  483. *
  484. * e.g.
  485. * ```
  486. * const foo: Type[]
  487. * // ^^^^^^
  488. * ```
  489. */
  490. TSArrayType(node) {
  491. const firstToken = tokenStore.getFirstToken(node)
  492. setOffset(
  493. tokenStore.getLastTokens(node, { count: 2, includeComments: false }),
  494. 0,
  495. firstToken
  496. )
  497. },
  498. TSTupleType(node) {
  499. // [T, U]
  500. processNodeList(
  501. node.elementTypes,
  502. tokenStore.getFirstToken(node),
  503. tokenStore.getLastToken(node),
  504. 1
  505. )
  506. },
  507. TSQualifiedName(node) {
  508. // A.B
  509. const objectToken = tokenStore.getFirstToken(node)
  510. const dotToken = tokenStore.getTokenBefore(node.right)
  511. const propertyToken = tokenStore.getTokenAfter(dotToken)
  512. setOffset([dotToken, propertyToken], 1, objectToken)
  513. },
  514. TSIndexedAccessType(node) {
  515. // A[B]
  516. const objectToken = tokenStore.getFirstToken(node)
  517. const leftBracketToken = tokenStore.getTokenBefore(
  518. node.indexType,
  519. isOpeningBracketToken
  520. )
  521. const rightBracketToken = tokenStore.getTokenAfter(
  522. node.indexType,
  523. isClosingBracketToken
  524. )
  525. setOffset(leftBracketToken, 1, objectToken)
  526. processNodeList([node.indexType], leftBracketToken, rightBracketToken, 1)
  527. },
  528. /** @param {TSUnionType | TSIntersectionType} node */
  529. 'TSUnionType, TSIntersectionType'(node) {
  530. // A | B
  531. // A & B
  532. const firstToken = tokenStore.getFirstToken(node)
  533. const prevToken = tokenStore.getTokenBefore(firstToken)
  534. const shouldIndent =
  535. prevToken == null ||
  536. prevToken.loc.end.line === firstToken.loc.start.line ||
  537. isBeginningOfElement(firstToken, node)
  538. const offset = shouldIndent ? 1 : 0
  539. const typeTokensList = node.types.map(getFirstAndLastTokens)
  540. const typeTokens = typeTokensList.shift()
  541. if (!typeTokens) {
  542. return
  543. }
  544. let lastToken
  545. if (typeTokens.firstToken === firstToken) {
  546. lastToken = typeTokens.lastToken
  547. } else {
  548. typeTokensList.unshift(typeTokens)
  549. lastToken = firstToken
  550. }
  551. for (const typeTokens of typeTokensList) {
  552. setOffset(
  553. tokenStore.getTokensBetween(lastToken, typeTokens.firstToken),
  554. offset,
  555. firstToken
  556. )
  557. setOffset(typeTokens.firstToken, offset, firstToken)
  558. }
  559. },
  560. TSMappedType(node) {
  561. // {[key in foo]: bar}
  562. const leftBraceToken = tokenStore.getFirstToken(node)
  563. const leftBracketToken = tokenStore.getTokenBefore(node.typeParameter)
  564. const rightBracketToken = tokenStore.getTokenAfter(
  565. node.nameType || node.typeParameter
  566. )
  567. setOffset(
  568. [
  569. ...tokenStore.getTokensBetween(leftBraceToken, leftBracketToken),
  570. leftBracketToken
  571. ],
  572. 1,
  573. leftBraceToken
  574. )
  575. processNodeList(
  576. [node.typeParameter, node.nameType],
  577. leftBracketToken,
  578. rightBracketToken,
  579. 1
  580. )
  581. const rightBraceToken = tokenStore.getLastToken(node)
  582. if (node.typeAnnotation) {
  583. const typeAnnotationToken = tokenStore.getFirstToken(
  584. node.typeAnnotation
  585. )
  586. setOffset(
  587. [
  588. ...tokenStore.getTokensBetween(
  589. rightBracketToken,
  590. typeAnnotationToken
  591. ),
  592. typeAnnotationToken
  593. ],
  594. 1,
  595. leftBraceToken
  596. )
  597. } else {
  598. setOffset(
  599. [...tokenStore.getTokensBetween(rightBracketToken, rightBraceToken)],
  600. 1,
  601. leftBraceToken
  602. )
  603. }
  604. setOffset(rightBraceToken, 0, leftBraceToken)
  605. },
  606. /**
  607. * Process type parameter
  608. *
  609. * e.g.
  610. * ```
  611. * type Foo<T, U extends T, V = U>
  612. * // ^ ^^^^^^^^^^^ ^^^^^
  613. * type Foo = {[key in foo]: bar}
  614. * // ^^^^^^^^^^
  615. * ```
  616. */
  617. TSTypeParameter(node) {
  618. const [firstToken, ...afterTokens] = tokenStore.getTokens(node)
  619. for (const child of [node.constraint, node.default]) {
  620. if (!child) {
  621. continue
  622. }
  623. const [, ...removeTokens] = tokenStore.getTokens(child)
  624. for (const token of removeTokens) {
  625. const i = afterTokens.indexOf(token)
  626. if (i >= 0) {
  627. afterTokens.splice(i, 1)
  628. }
  629. }
  630. }
  631. const secondToken = afterTokens.shift()
  632. if (!secondToken) {
  633. return
  634. }
  635. setOffset(secondToken, 1, firstToken)
  636. if (secondToken.value === 'extends') {
  637. let prevToken = null
  638. let token = afterTokens.shift()
  639. while (token) {
  640. if (token.value === '=') {
  641. break
  642. }
  643. setOffset(token, 1, secondToken)
  644. prevToken = token
  645. token = afterTokens.shift()
  646. }
  647. while (token) {
  648. setOffset(token, 1, prevToken || secondToken)
  649. token = afterTokens.shift()
  650. }
  651. } else {
  652. setOffset(afterTokens, 1, firstToken)
  653. }
  654. },
  655. /**
  656. * Process conditional type
  657. *
  658. * e.g.
  659. * ```
  660. * type Foo = A extends B ? Bar : Baz
  661. * // ^^^^^^^^^^^^^^^^^^^^^^^
  662. * ```
  663. */
  664. TSConditionalType(node) {
  665. // T extends Foo ? T : U
  666. const checkTypeToken = tokenStore.getFirstToken(node)
  667. const extendsToken = tokenStore.getTokenAfter(node.checkType)
  668. const extendsTypeToken = tokenStore.getFirstToken(node.extendsType)
  669. setOffset(extendsToken, 1, checkTypeToken)
  670. setOffset(extendsTypeToken, 1, extendsToken)
  671. const questionToken = /**@type {Token} */ (
  672. tokenStore.getTokenAfter(node.extendsType, isNotClosingParenToken)
  673. )
  674. const consequentToken = tokenStore.getTokenAfter(questionToken)
  675. const colonToken = /**@type {Token} */ (
  676. tokenStore.getTokenAfter(node.trueType, isNotClosingParenToken)
  677. )
  678. const alternateToken = tokenStore.getTokenAfter(colonToken)
  679. let baseNode = node
  680. let parent = baseNode.parent
  681. while (
  682. parent &&
  683. parent.type === 'TSConditionalType' &&
  684. parent.falseType === baseNode
  685. ) {
  686. baseNode = parent
  687. parent = baseNode.parent
  688. }
  689. const baseToken = tokenStore.getFirstToken(baseNode)
  690. setOffset([questionToken, colonToken], 1, baseToken)
  691. setOffset(consequentToken, 1, questionToken)
  692. setOffset(alternateToken, 1, colonToken)
  693. },
  694. /**
  695. * Process interface declaration
  696. *
  697. * e.g.
  698. * ```
  699. * interface Foo { }
  700. * ```
  701. */
  702. TSInterfaceDeclaration(node) {
  703. const interfaceToken = tokenStore.getFirstToken(node)
  704. setOffset(tokenStore.getFirstToken(node.id), 1, interfaceToken)
  705. if (node.typeParameters != null) {
  706. setOffset(
  707. tokenStore.getFirstToken(node.typeParameters),
  708. 1,
  709. tokenStore.getFirstToken(node.id)
  710. )
  711. }
  712. if (node.extends != null && node.extends.length) {
  713. const extendsToken = tokenStore.getTokenBefore(node.extends[0])
  714. setOffset(extendsToken, 1, interfaceToken)
  715. processNodeList(node.extends, extendsToken, null, 1)
  716. }
  717. // It may not calculate the correct location because the visitor key is not provided.
  718. // if (node.implements != null && node.implements.length) {
  719. // const implementsToken = tokenStore.getTokenBefore(node.implements[0])
  720. // setOffset(implementsToken, 1, interfaceToken)
  721. // processNodeList(node.implements, implementsToken, null, 1)
  722. // }
  723. const bodyToken = tokenStore.getFirstToken(node.body)
  724. setOffset(bodyToken, 0, interfaceToken)
  725. },
  726. /**
  727. * Process interface body
  728. *
  729. * e.g.
  730. * ```
  731. * interface Foo { }
  732. * // ^^^
  733. * ```
  734. *
  735. * @param {TSInterfaceBody | TSModuleBlock} node
  736. */
  737. 'TSInterfaceBody, TSModuleBlock'(node) {
  738. processNodeList(
  739. node.body,
  740. tokenStore.getFirstToken(node),
  741. tokenStore.getLastToken(node),
  742. 1
  743. )
  744. },
  745. /**
  746. * Process interface heritage and class implements
  747. *
  748. * e.g.
  749. * ```
  750. * interface Foo<T> extends Bar<T> { }
  751. * // ^^^^^^
  752. * class Foo<T> implements Bar<T> { }
  753. * // ^^^^^^
  754. * ```
  755. * @param {TSInterfaceHeritage | TSClassImplements} node
  756. */
  757. 'TSClassImplements, TSInterfaceHeritage'(node) {
  758. if (node.typeParameters) {
  759. setOffset(
  760. tokenStore.getFirstToken(node.typeParameters),
  761. 1,
  762. tokenStore.getFirstToken(node)
  763. )
  764. }
  765. },
  766. /**
  767. * Process enum
  768. *
  769. * e.g.
  770. * ```
  771. * enum Foo { }
  772. * ```
  773. */
  774. TSEnumDeclaration(node) {
  775. const firstToken = tokenStore.getFirstToken(node)
  776. const idTokens = getFirstAndLastTokens(node.id)
  777. const prefixTokens = tokenStore.getTokensBetween(
  778. firstToken,
  779. idTokens.firstToken
  780. )
  781. setOffset(prefixTokens, 0, firstToken)
  782. setOffset(idTokens.firstToken, 1, firstToken)
  783. const leftBraceToken = tokenStore.getTokenAfter(idTokens.lastToken)
  784. const rightBraceToken = tokenStore.getLastToken(node)
  785. setOffset(leftBraceToken, 0, firstToken)
  786. processNodeList(node.members, leftBraceToken, rightBraceToken, 1)
  787. },
  788. TSModuleDeclaration(node) {
  789. const firstToken = tokenStore.getFirstToken(node)
  790. const idTokens = getFirstAndLastTokens(node.id)
  791. const prefixTokens = tokenStore.getTokensBetween(
  792. firstToken,
  793. idTokens.firstToken
  794. )
  795. setOffset(prefixTokens, 0, firstToken)
  796. setOffset(idTokens.firstToken, 1, firstToken)
  797. if (node.body) {
  798. const bodyFirstToken = tokenStore.getFirstToken(node.body)
  799. setOffset(
  800. bodyFirstToken,
  801. isOpeningBraceToken(bodyFirstToken) ? 0 : 1,
  802. firstToken
  803. )
  804. }
  805. },
  806. TSMethodSignature(node) {
  807. // fn(arg: A): R | null;
  808. const firstToken = tokenStore.getFirstToken(node)
  809. const keyTokens = getFirstAndLastTokens(node.key)
  810. let keyLast
  811. if (node.computed) {
  812. const closeBracket = tokenStore.getTokenAfter(keyTokens.lastToken)
  813. processNodeList([node.key], firstToken, closeBracket, 1)
  814. keyLast = closeBracket
  815. } else {
  816. keyLast = keyTokens.lastToken
  817. }
  818. const leftParenToken = /** @type {Token} */ (
  819. tokenStore.getTokenAfter(keyLast, isOpeningParenToken)
  820. )
  821. setOffset(
  822. [
  823. ...tokenStore.getTokensBetween(keyLast, leftParenToken),
  824. leftParenToken
  825. ],
  826. 1,
  827. firstToken
  828. )
  829. const rightParenToken = tokenStore.getTokenAfter(
  830. node.params[node.params.length - 1] || leftParenToken,
  831. isClosingParenToken
  832. )
  833. processNodeList(node.params, leftParenToken, rightParenToken, 1)
  834. if (node.returnType) {
  835. const typeAnnotationToken = tokenStore.getFirstToken(node.returnType)
  836. setOffset(
  837. [
  838. ...tokenStore.getTokensBetween(keyLast, typeAnnotationToken),
  839. typeAnnotationToken
  840. ],
  841. 1,
  842. firstToken
  843. )
  844. }
  845. },
  846. /**
  847. * Process call signature declaration and construct signature declaration
  848. *
  849. * e.g.
  850. * ```
  851. * interface Foo {
  852. * (): string;
  853. * //^^^^^^^^^^^
  854. * <T> (e: E): R
  855. * //^^^^^^^^^^^^^
  856. * }
  857. * ```
  858. *
  859. * e.g.
  860. * ```
  861. * interface Foo {
  862. * new ();
  863. * //^^^^^^^
  864. * }
  865. * interface A { new <T> (e: E): R }
  866. * // ^^^^^^^^^^^^^^^^^
  867. * ```
  868. * @param {TSCallSignatureDeclaration | TSConstructSignatureDeclaration} node
  869. */
  870. 'TSCallSignatureDeclaration, TSConstructSignatureDeclaration'(node) {
  871. const firstToken = tokenStore.getFirstToken(node)
  872. // new or < or (
  873. let currToken = firstToken
  874. if (node.type === 'TSConstructSignatureDeclaration') {
  875. // currToken is new token
  876. // < or (
  877. currToken = tokenStore.getTokenAfter(currToken)
  878. setOffset(currToken, 1, firstToken)
  879. }
  880. if (node.typeParameters) {
  881. // currToken is < token
  882. // (
  883. currToken = tokenStore.getTokenAfter(node.typeParameters)
  884. setOffset(currToken, 1, firstToken)
  885. }
  886. const leftParenToken = currToken
  887. const rightParenToken = /** @type {Token} */ (
  888. tokenStore.getTokenAfter(
  889. node.params[node.params.length - 1] || leftParenToken,
  890. isClosingParenToken
  891. )
  892. )
  893. processNodeList(node.params, leftParenToken, rightParenToken, 1)
  894. if (node.returnType) {
  895. const typeAnnotationToken = tokenStore.getFirstToken(node.returnType)
  896. setOffset(
  897. [
  898. ...tokenStore.getTokensBetween(
  899. rightParenToken,
  900. typeAnnotationToken
  901. ),
  902. typeAnnotationToken
  903. ],
  904. 1,
  905. firstToken
  906. )
  907. }
  908. },
  909. /**
  910. * Process declare function and empty body function
  911. *
  912. * e.g.
  913. * ```
  914. * declare function foo();
  915. * ```
  916. *
  917. * e.g.
  918. * ```
  919. * class Foo {
  920. * abstract fn();
  921. * // ^^^
  922. * }
  923. * ```
  924. * @param {TSDeclareFunction | TSEmptyBodyFunctionExpression} node
  925. */
  926. 'TSDeclareFunction, TSEmptyBodyFunctionExpression'(node) {
  927. const firstToken = tokenStore.getFirstToken(node)
  928. let leftParenToken, bodyBaseToken
  929. if (firstToken.type === 'Punctuator') {
  930. // method
  931. leftParenToken = firstToken
  932. bodyBaseToken = tokenStore.getFirstToken(
  933. /** @type {HasLocation} */ (node.parent)
  934. )
  935. } else {
  936. let nextToken = tokenStore.getTokenAfter(firstToken)
  937. let nextTokenOffset = 0
  938. while (
  939. nextToken &&
  940. !isOpeningParenToken(nextToken) &&
  941. nextToken.value !== '<'
  942. ) {
  943. if (
  944. nextToken.value === '*' ||
  945. (node.id && nextToken.range[0] === node.id.range[0])
  946. ) {
  947. nextTokenOffset = 1
  948. }
  949. setOffset(nextToken, nextTokenOffset, firstToken)
  950. nextToken = tokenStore.getTokenAfter(nextToken)
  951. }
  952. leftParenToken = nextToken
  953. bodyBaseToken = firstToken
  954. }
  955. if (!isOpeningParenToken(leftParenToken) && node.typeParameters) {
  956. leftParenToken = tokenStore.getTokenAfter(node.typeParameters)
  957. }
  958. const rightParenToken = tokenStore.getTokenAfter(
  959. node.params[node.params.length - 1] || leftParenToken,
  960. isClosingParenToken
  961. )
  962. setOffset(leftParenToken, 1, bodyBaseToken)
  963. processNodeList(node.params, leftParenToken, rightParenToken, 1)
  964. },
  965. /**
  966. * Process type operator, type query and infer type
  967. *
  968. * e.g.
  969. * ```
  970. * type Foo = keyof Bar
  971. * // ^^^^^^^^^
  972. * ```
  973. *
  974. * e.g.
  975. * ```
  976. * type T = typeof a
  977. * // ^^^^^^^^
  978. * ```
  979. *
  980. * e.g.
  981. * ```
  982. * type Foo<T> = T extends Bar<infer U> ? U : T;
  983. * // ^^^^^^^
  984. * ```
  985. *
  986. * @param {TSTypeOperator | TSTypeQuery | TSInferType} node
  987. */
  988. 'TSTypeOperator, TSTypeQuery, TSInferType'(node) {
  989. // keyof T
  990. // type T = typeof av
  991. // infer U
  992. const firstToken = tokenStore.getFirstToken(node)
  993. const nextToken = tokenStore.getTokenAfter(firstToken)
  994. setOffset(nextToken, 1, firstToken)
  995. },
  996. /**
  997. * Process type predicate
  998. *
  999. * e.g.
  1000. * ```
  1001. * function foo(value): value is string;
  1002. * // ^^^^^^^^^^^^^^^
  1003. * ```
  1004. */
  1005. TSTypePredicate(node) {
  1006. const firstToken = tokenStore.getFirstToken(node)
  1007. const opToken = tokenStore.getTokenAfter(
  1008. node.parameterName,
  1009. isNotClosingParenToken
  1010. )
  1011. const rightToken =
  1012. node.typeAnnotation &&
  1013. getFirstAndLastTokens(node.typeAnnotation).firstToken
  1014. setOffset(
  1015. [opToken, rightToken],
  1016. 1,
  1017. getFirstAndLastTokens(firstToken).firstToken
  1018. )
  1019. },
  1020. /**
  1021. * Process abstract method definition, abstract class property, enum member and class property
  1022. *
  1023. * e.g.
  1024. * ```
  1025. * class Foo {
  1026. * abstract fn()
  1027. * //^^^^^^^^^^^^^
  1028. * abstract x
  1029. * //^^^^^^^^^^
  1030. * x
  1031. * //^
  1032. * }
  1033. * ```
  1034. *
  1035. * e.g.
  1036. * ```
  1037. * enum Foo { Bar = x }
  1038. * // ^^^^^^^
  1039. * ```
  1040. *
  1041. * @param {TSAbstractMethodDefinition | TSAbstractPropertyDefinition | TSEnumMember | TSAbstractClassProperty | ClassProperty} node
  1042. *
  1043. */
  1044. ['TSAbstractMethodDefinition, TSAbstractPropertyDefinition, TSEnumMember,' +
  1045. // Deprecated in @typescript-eslint/parser v5
  1046. 'ClassProperty, TSAbstractClassProperty'](node) {
  1047. const { keyNode, valueNode } =
  1048. node.type === 'TSEnumMember'
  1049. ? { keyNode: node.id, valueNode: node.initializer }
  1050. : { keyNode: node.key, valueNode: node.value }
  1051. const firstToken = tokenStore.getFirstToken(node)
  1052. const keyTokens = getFirstAndLastTokens(keyNode)
  1053. const prefixTokens = tokenStore.getTokensBetween(
  1054. firstToken,
  1055. keyTokens.firstToken
  1056. )
  1057. if (node.computed) {
  1058. prefixTokens.pop() // pop [
  1059. }
  1060. setOffset(prefixTokens, 0, firstToken)
  1061. let lastKeyToken
  1062. if (node.computed) {
  1063. const leftBracketToken = tokenStore.getTokenBefore(keyTokens.firstToken)
  1064. const rightBracketToken = (lastKeyToken = tokenStore.getTokenAfter(
  1065. keyTokens.lastToken
  1066. ))
  1067. setOffset(leftBracketToken, 0, firstToken)
  1068. processNodeList([keyNode], leftBracketToken, rightBracketToken, 1)
  1069. } else {
  1070. setOffset(keyTokens.firstToken, 0, firstToken)
  1071. lastKeyToken = keyTokens.lastToken
  1072. }
  1073. if (valueNode != null) {
  1074. const initToken = tokenStore.getFirstToken(valueNode)
  1075. setOffset(
  1076. [...tokenStore.getTokensBetween(lastKeyToken, initToken), initToken],
  1077. 1,
  1078. lastKeyToken
  1079. )
  1080. }
  1081. },
  1082. /**
  1083. * Process optional type, non-null expression and JSDocNonNullableType
  1084. *
  1085. * e.g.
  1086. * ```
  1087. * type Foo = [number?]
  1088. * // ^^^^^^^
  1089. * const a = v!
  1090. * // ^
  1091. * type T = U!
  1092. * // ^^
  1093. * ```
  1094. *
  1095. * @param {TSOptionalType | TSNonNullExpression} node
  1096. */
  1097. 'TSOptionalType, TSNonNullExpression, TSJSDocNonNullableType'(node) {
  1098. setOffset(
  1099. tokenStore.getLastToken(node),
  1100. 1,
  1101. tokenStore.getFirstToken(node)
  1102. )
  1103. },
  1104. TSTypeAssertion(node) {
  1105. // <const>
  1106. const firstToken = tokenStore.getFirstToken(node)
  1107. const expressionToken = getFirstAndLastTokens(node.expression).firstToken
  1108. processNodeList(
  1109. [node.typeAnnotation],
  1110. firstToken,
  1111. tokenStore.getTokenBefore(expressionToken),
  1112. 1
  1113. )
  1114. setOffset(expressionToken, 1, firstToken)
  1115. },
  1116. /**
  1117. * Process import type
  1118. *
  1119. * e.g.
  1120. * ```
  1121. * const foo: import('foo').Bar<T>
  1122. * // ^^^^^^^^^^^^^^^^^^^^
  1123. * ```
  1124. */
  1125. TSImportType(node) {
  1126. const firstToken = tokenStore.getFirstToken(node)
  1127. const leftParenToken = tokenStore.getTokenAfter(
  1128. firstToken,
  1129. isOpeningParenToken
  1130. )
  1131. setOffset(leftParenToken, 1, firstToken)
  1132. const rightParenToken = tokenStore.getTokenAfter(
  1133. node.parameter,
  1134. isClosingParenToken
  1135. )
  1136. processNodeList([node.parameter], leftParenToken, rightParenToken, 1)
  1137. if (node.qualifier) {
  1138. const dotToken = tokenStore.getTokenBefore(node.qualifier)
  1139. const propertyToken = tokenStore.getTokenAfter(dotToken)
  1140. setOffset([dotToken, propertyToken], 1, firstToken)
  1141. }
  1142. if (node.typeParameters) {
  1143. setOffset(tokenStore.getFirstToken(node.typeParameters), 1, firstToken)
  1144. }
  1145. },
  1146. TSParameterProperty(node) {
  1147. // constructor(private a)
  1148. const firstToken = tokenStore.getFirstToken(node)
  1149. const parameterToken = tokenStore.getFirstToken(node.parameter)
  1150. setOffset(
  1151. [
  1152. ...tokenStore.getTokensBetween(firstToken, parameterToken),
  1153. parameterToken
  1154. ],
  1155. 1,
  1156. firstToken
  1157. )
  1158. },
  1159. /**
  1160. * Process import equal
  1161. *
  1162. * e.g.
  1163. * ```
  1164. * import foo = require('foo')
  1165. * ```
  1166. */
  1167. TSImportEqualsDeclaration(node) {
  1168. const importToken = tokenStore.getFirstToken(node)
  1169. const idTokens = getFirstAndLastTokens(node.id)
  1170. setOffset(idTokens.firstToken, 1, importToken)
  1171. const opToken = tokenStore.getTokenAfter(idTokens.lastToken)
  1172. setOffset(
  1173. [opToken, tokenStore.getFirstToken(node.moduleReference)],
  1174. 1,
  1175. idTokens.lastToken
  1176. )
  1177. },
  1178. /**
  1179. * Process external module reference
  1180. *
  1181. * e.g.
  1182. * ```
  1183. * import foo = require('foo')
  1184. * // ^^^^^^^^^^^^^^
  1185. * ```
  1186. */
  1187. TSExternalModuleReference(node) {
  1188. const requireToken = tokenStore.getFirstToken(node)
  1189. const leftParenToken = tokenStore.getTokenAfter(
  1190. requireToken,
  1191. isOpeningParenToken
  1192. )
  1193. const rightParenToken = tokenStore.getLastToken(node)
  1194. setOffset(leftParenToken, 1, requireToken)
  1195. processNodeList([node.expression], leftParenToken, rightParenToken, 1)
  1196. },
  1197. /**
  1198. * Process export assignment
  1199. *
  1200. * e.g.
  1201. * ```
  1202. * export = foo
  1203. * ```
  1204. */
  1205. TSExportAssignment(node) {
  1206. const exportNode = tokenStore.getFirstToken(node)
  1207. const exprTokens = getFirstAndLastTokens(node.expression)
  1208. const opToken = tokenStore.getTokenBefore(exprTokens.firstToken)
  1209. setOffset([opToken, exprTokens.firstToken], 1, exportNode)
  1210. },
  1211. TSNamedTupleMember(node) {
  1212. // [a: string, ...b: string[]]
  1213. // ^^^^^^^^^
  1214. const labelToken = tokenStore.getFirstToken(node)
  1215. const elementTokens = getFirstAndLastTokens(node.elementType)
  1216. setOffset(
  1217. [
  1218. ...tokenStore.getTokensBetween(labelToken, elementTokens.firstToken),
  1219. elementTokens.firstToken
  1220. ],
  1221. 1,
  1222. labelToken
  1223. )
  1224. },
  1225. TSRestType(node) {
  1226. // [a: string, ...b: string[]]
  1227. // ^^^^^^^^^^^^^^
  1228. const firstToken = tokenStore.getFirstToken(node)
  1229. const nextToken = tokenStore.getTokenAfter(firstToken)
  1230. setOffset(nextToken, 1, firstToken)
  1231. },
  1232. TSNamespaceExportDeclaration(node) {
  1233. const firstToken = tokenStore.getFirstToken(node)
  1234. const idToken = tokenStore.getFirstToken(node.id)
  1235. setOffset(
  1236. [...tokenStore.getTokensBetween(firstToken, idToken), idToken],
  1237. 1,
  1238. firstToken
  1239. )
  1240. },
  1241. TSTemplateLiteralType(node) {
  1242. const firstToken = tokenStore.getFirstToken(node)
  1243. const quasiTokens = node.quasis
  1244. .slice(1)
  1245. .map((n) => tokenStore.getFirstToken(n))
  1246. const expressionToken = node.quasis
  1247. .slice(0, -1)
  1248. .map((n) => tokenStore.getTokenAfter(n))
  1249. setOffset(quasiTokens, 0, firstToken)
  1250. setOffset(expressionToken, 1, firstToken)
  1251. },
  1252. // ----------------------------------------------------------------------
  1253. // NON-STANDARD NODES
  1254. // ----------------------------------------------------------------------
  1255. Decorator(node) {
  1256. // @Decorator
  1257. const [atToken, secondToken] = tokenStore.getFirstTokens(node, {
  1258. count: 2,
  1259. includeComments: false
  1260. })
  1261. setOffset(secondToken, 0, atToken)
  1262. const parent = /** @type {any} */ (node.parent)
  1263. const { decorators } = parent
  1264. if (!decorators || decorators.length === 0) {
  1265. return
  1266. }
  1267. if (decorators[0] === node) {
  1268. if (parent.range[0] === node.range[0]) {
  1269. const startParentToken = tokenStore.getTokenAfter(
  1270. decorators[decorators.length - 1]
  1271. )
  1272. setOffset(startParentToken, 0, atToken)
  1273. } else {
  1274. const startParentToken = tokenStore.getFirstToken(
  1275. parent.parent &&
  1276. (parent.parent.type === 'ExportDefaultDeclaration' ||
  1277. parent.parent.type === 'ExportNamedDeclaration') &&
  1278. node.range[0] < parent.parent.range[0]
  1279. ? parent.parent
  1280. : parent
  1281. )
  1282. copyOffset(atToken, startParentToken)
  1283. }
  1284. } else {
  1285. setOffset(atToken, 0, tokenStore.getFirstToken(decorators[0]))
  1286. }
  1287. },
  1288. ImportAttribute(node) {
  1289. const firstToken = tokenStore.getFirstToken(node)
  1290. const keyTokens = getFirstAndLastTokens(node.key)
  1291. const prefixTokens = tokenStore.getTokensBetween(
  1292. firstToken,
  1293. keyTokens.firstToken
  1294. )
  1295. setOffset(prefixTokens, 0, firstToken)
  1296. setOffset(keyTokens.firstToken, 0, firstToken)
  1297. const initToken = tokenStore.getFirstToken(node.value)
  1298. setOffset(
  1299. [
  1300. ...tokenStore.getTokensBetween(keyTokens.lastToken, initToken),
  1301. initToken
  1302. ],
  1303. 1,
  1304. keyTokens.lastToken
  1305. )
  1306. },
  1307. // ----------------------------------------------------------------------
  1308. // DEPRECATED NODES
  1309. // ----------------------------------------------------------------------
  1310. /** @param {any} node */
  1311. TSParenthesizedType(node) {
  1312. // Deprecated in @typescript-eslint/parser v5
  1313. // (T)
  1314. processNodeList(
  1315. [node.typeAnnotation],
  1316. tokenStore.getFirstToken(node),
  1317. tokenStore.getLastToken(node),
  1318. 1
  1319. )
  1320. },
  1321. // ----------------------------------------------------------------------
  1322. // SINGLE TOKEN NODES
  1323. // ----------------------------------------------------------------------
  1324. TSPrivateIdentifier() {
  1325. // Perhaps this node will be deprecated in the future.
  1326. // It was present in @typescript-eslint/parser@4.1.0.
  1327. },
  1328. // VALUES KEYWORD
  1329. TSAnyKeyword() {},
  1330. TSBigIntKeyword() {},
  1331. TSBooleanKeyword() {},
  1332. TSNeverKeyword() {},
  1333. TSNullKeyword() {},
  1334. TSNumberKeyword() {},
  1335. TSObjectKeyword() {},
  1336. TSStringKeyword() {},
  1337. TSSymbolKeyword() {},
  1338. TSUndefinedKeyword() {},
  1339. TSUnknownKeyword() {},
  1340. TSVoidKeyword() {},
  1341. // MODIFIERS KEYWORD
  1342. TSAbstractKeyword() {},
  1343. TSAsyncKeyword() {},
  1344. TSPrivateKeyword() {},
  1345. TSProtectedKeyword() {},
  1346. TSPublicKeyword() {},
  1347. TSReadonlyKeyword() {},
  1348. TSStaticKeyword() {},
  1349. // OTHERS KEYWORD
  1350. TSDeclareKeyword() {},
  1351. TSExportKeyword() {},
  1352. TSIntrinsicKeyword() {},
  1353. // OTHERS
  1354. TSThisType() {},
  1355. // ----------------------------------------------------------------------
  1356. // WRAPPER NODES
  1357. // ----------------------------------------------------------------------
  1358. TSLiteralType() {}
  1359. }
  1360. }