indent-ts.js 45 KB

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