indent-common.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. /**
  2. * @author Toru Nagashima <https://github.com/mysticatea>
  3. * See LICENSE file in root directory for full license.
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const assert = require('assert')
  10. // ------------------------------------------------------------------------------
  11. // Helpers
  12. // ------------------------------------------------------------------------------
  13. const KNOWN_NODES = new Set(['ArrayExpression', 'ArrayPattern', 'ArrowFunctionExpression', 'AssignmentExpression', 'AssignmentPattern', 'AwaitExpression', 'BinaryExpression', 'BlockStatement', 'BreakStatement', 'CallExpression', 'CatchClause', 'ClassBody', 'ClassDeclaration', 'ClassExpression', 'ConditionalExpression', 'ContinueStatement', 'DebuggerStatement', 'DoWhileStatement', 'EmptyStatement', 'ExperimentalRestProperty', 'ExperimentalSpreadProperty', 'ExportAllDeclaration', 'ExportDefaultDeclaration', 'ExportNamedDeclaration', 'ExportSpecifier', 'ExpressionStatement', 'ForInStatement', 'ForOfStatement', 'ForStatement', 'FunctionDeclaration', 'FunctionExpression', 'Identifier', 'IfStatement', 'ImportDeclaration', 'ImportDefaultSpecifier', 'ImportNamespaceSpecifier', 'ImportSpecifier', 'LabeledStatement', 'Literal', 'LogicalExpression', 'MemberExpression', 'MetaProperty', 'MethodDefinition', 'NewExpression', 'ObjectExpression', 'ObjectPattern', 'Program', 'Property', 'RestElement', 'ReturnStatement', 'SequenceExpression', 'SpreadElement', 'Super', 'SwitchCase', 'SwitchStatement', 'TaggedTemplateExpression', 'TemplateElement', 'TemplateLiteral', 'ThisExpression', 'ThrowStatement', 'TryStatement', 'UnaryExpression', 'UpdateExpression', 'VariableDeclaration', 'VariableDeclarator', 'WhileStatement', 'WithStatement', 'YieldExpression', 'VAttribute', 'VDirectiveKey', 'VDocumentFragment', 'VElement', 'VEndTag', 'VExpressionContainer', 'VFilter', 'VFilterSequenceExpression', 'VForExpression', 'VIdentifier', 'VLiteral', 'VOnExpression', 'VSlotScopeExpression', 'VStartTag', 'VText'])
  14. const LT_CHAR = /[\r\n\u2028\u2029]/
  15. const LINES = /[^\r\n\u2028\u2029]+(?:$|\r\n|[\r\n\u2028\u2029])/g
  16. const BLOCK_COMMENT_PREFIX = /^\s*\*/
  17. const ITERATION_OPTS = Object.freeze({ includeComments: true, filter: isNotWhitespace })
  18. const PREFORMATTED_ELEMENT_NAMES = ['pre', 'textarea']
  19. /**
  20. * Normalize options.
  21. * @param {number|"tab"|undefined} type The type of indentation.
  22. * @param {Object} options Other options.
  23. * @param {Object} defaultOptions The default value of options.
  24. * @returns {{indentChar:" "|"\t",indentSize:number,baseIndent:number,attribute:number,closeBracket:number,switchCase:number,alignAttributesVertically:boolean,ignores:string[]}} Normalized options.
  25. */
  26. function parseOptions (type, options, defaultOptions) {
  27. const ret = Object.assign({
  28. indentChar: ' ',
  29. indentSize: 2,
  30. baseIndent: 0,
  31. attribute: 1,
  32. closeBracket: 0,
  33. switchCase: 0,
  34. alignAttributesVertically: true,
  35. ignores: []
  36. }, defaultOptions)
  37. if (Number.isSafeInteger(type)) {
  38. ret.indentSize = type
  39. } else if (type === 'tab') {
  40. ret.indentChar = '\t'
  41. ret.indentSize = 1
  42. }
  43. if (Number.isSafeInteger(options.baseIndent)) {
  44. ret.baseIndent = options.baseIndent
  45. }
  46. if (Number.isSafeInteger(options.attribute)) {
  47. ret.attribute = options.attribute
  48. }
  49. if (Number.isSafeInteger(options.closeBracket)) {
  50. ret.closeBracket = options.closeBracket
  51. }
  52. if (Number.isSafeInteger(options.switchCase)) {
  53. ret.switchCase = options.switchCase
  54. }
  55. if (options.alignAttributesVertically != null) {
  56. ret.alignAttributesVertically = options.alignAttributesVertically
  57. }
  58. if (options.ignores != null) {
  59. ret.ignores = options.ignores
  60. }
  61. return ret
  62. }
  63. /**
  64. * Check whether the given token is an arrow.
  65. * @param {Token} token The token to check.
  66. * @returns {boolean} `true` if the token is an arrow.
  67. */
  68. function isArrow (token) {
  69. return token != null && token.type === 'Punctuator' && token.value === '=>'
  70. }
  71. /**
  72. * Check whether the given token is a left parenthesis.
  73. * @param {Token} token The token to check.
  74. * @returns {boolean} `true` if the token is a left parenthesis.
  75. */
  76. function isLeftParen (token) {
  77. return token != null && token.type === 'Punctuator' && token.value === '('
  78. }
  79. /**
  80. * Check whether the given token is a left parenthesis.
  81. * @param {Token} token The token to check.
  82. * @returns {boolean} `false` if the token is a left parenthesis.
  83. */
  84. function isNotLeftParen (token) {
  85. return token != null && (token.type !== 'Punctuator' || token.value !== '(')
  86. }
  87. /**
  88. * Check whether the given token is a right parenthesis.
  89. * @param {Token} token The token to check.
  90. * @returns {boolean} `true` if the token is a right parenthesis.
  91. */
  92. function isRightParen (token) {
  93. return token != null && token.type === 'Punctuator' && token.value === ')'
  94. }
  95. /**
  96. * Check whether the given token is a right parenthesis.
  97. * @param {Token} token The token to check.
  98. * @returns {boolean} `false` if the token is a right parenthesis.
  99. */
  100. function isNotRightParen (token) {
  101. return token != null && (token.type !== 'Punctuator' || token.value !== ')')
  102. }
  103. /**
  104. * Check whether the given token is a left brace.
  105. * @param {Token} token The token to check.
  106. * @returns {boolean} `true` if the token is a left brace.
  107. */
  108. function isLeftBrace (token) {
  109. return token != null && token.type === 'Punctuator' && token.value === '{'
  110. }
  111. /**
  112. * Check whether the given token is a right brace.
  113. * @param {Token} token The token to check.
  114. * @returns {boolean} `true` if the token is a right brace.
  115. */
  116. function isRightBrace (token) {
  117. return token != null && token.type === 'Punctuator' && token.value === '}'
  118. }
  119. /**
  120. * Check whether the given token is a left bracket.
  121. * @param {Token} token The token to check.
  122. * @returns {boolean} `true` if the token is a left bracket.
  123. */
  124. function isLeftBracket (token) {
  125. return token != null && token.type === 'Punctuator' && token.value === '['
  126. }
  127. /**
  128. * Check whether the given token is a right bracket.
  129. * @param {Token} token The token to check.
  130. * @returns {boolean} `true` if the token is a right bracket.
  131. */
  132. function isRightBracket (token) {
  133. return token != null && token.type === 'Punctuator' && token.value === ']'
  134. }
  135. /**
  136. * Check whether the given token is a semicolon.
  137. * @param {Token} token The token to check.
  138. * @returns {boolean} `true` if the token is a semicolon.
  139. */
  140. function isSemicolon (token) {
  141. return token != null && token.type === 'Punctuator' && token.value === ';'
  142. }
  143. /**
  144. * Check whether the given token is a comma.
  145. * @param {Token} token The token to check.
  146. * @returns {boolean} `true` if the token is a comma.
  147. */
  148. function isComma (token) {
  149. return token != null && token.type === 'Punctuator' && token.value === ','
  150. }
  151. /**
  152. * Check whether the given token is a whitespace.
  153. * @param {Token} token The token to check.
  154. * @returns {boolean} `true` if the token is a whitespace.
  155. */
  156. function isNotWhitespace (token) {
  157. return token != null && token.type !== 'HTMLWhitespace'
  158. }
  159. /**
  160. * Check whether the given token is a comment.
  161. * @param {Token} token The token to check.
  162. * @returns {boolean} `true` if the token is a comment.
  163. */
  164. function isComment (token) {
  165. return token != null && (token.type === 'Block' || token.type === 'Line' || token.type === 'Shebang' || token.type.endsWith('Comment'))
  166. }
  167. /**
  168. * Check whether the given token is a comment.
  169. * @param {Token} token The token to check.
  170. * @returns {boolean} `false` if the token is a comment.
  171. */
  172. function isNotComment (token) {
  173. return token != null && token.type !== 'Block' && token.type !== 'Line' && token.type !== 'Shebang' && !token.type.endsWith('Comment')
  174. }
  175. /**
  176. * Check whether the given node is not an empty text node.
  177. * @param {Node} node The node to check.
  178. * @returns {boolean} `false` if the token is empty text node.
  179. */
  180. function isNotEmptyTextNode (node) {
  181. return !(node.type === 'VText' && node.value.trim() === '')
  182. }
  183. /**
  184. * Check whether the given token is a pipe operator.
  185. * @param {Token} token The token to check.
  186. * @returns {boolean} `true` if the token is a pipe operator.
  187. */
  188. function isPipeOperator (token) {
  189. return token != null && token.type === 'Punctuator' && token.value === '|'
  190. }
  191. /**
  192. * Get the last element.
  193. * @param {Array} xs The array to get the last element.
  194. * @returns {any|undefined} The last element or undefined.
  195. */
  196. function last (xs) {
  197. return xs.length === 0 ? undefined : xs[xs.length - 1]
  198. }
  199. /**
  200. * Check whether the node is at the beginning of line.
  201. * @param {Node} node The node to check.
  202. * @param {number} index The index of the node in the nodes.
  203. * @param {Node[]} nodes The array of nodes.
  204. * @returns {boolean} `true` if the node is at the beginning of line.
  205. */
  206. function isBeginningOfLine (node, index, nodes) {
  207. if (node != null) {
  208. for (let i = index - 1; i >= 0; --i) {
  209. const prevNode = nodes[i]
  210. if (prevNode == null) {
  211. continue
  212. }
  213. return node.loc.start.line !== prevNode.loc.end.line
  214. }
  215. }
  216. return false
  217. }
  218. /**
  219. * Check whether a given token is a closing token which triggers unindent.
  220. * @param {Token} token The token to check.
  221. * @returns {boolean} `true` if the token is a closing token.
  222. */
  223. function isClosingToken (token) {
  224. return token != null && (
  225. token.type === 'HTMLEndTagOpen' ||
  226. token.type === 'VExpressionEnd' ||
  227. (
  228. token.type === 'Punctuator' &&
  229. (
  230. token.value === ')' ||
  231. token.value === '}' ||
  232. token.value === ']'
  233. )
  234. )
  235. )
  236. }
  237. /**
  238. * Creates AST event handlers for html-indent.
  239. *
  240. * @param {RuleContext} context The rule context.
  241. * @param {TokenStore} tokenStore The token store object to get tokens.
  242. * @param {Object} defaultOptions The default value of options.
  243. * @returns {object} AST event handlers.
  244. */
  245. module.exports.defineVisitor = function create (context, tokenStore, defaultOptions) {
  246. if (!context.getFilename().endsWith('.vue')) return {}
  247. const options = parseOptions(context.options[0], context.options[1] || {}, defaultOptions)
  248. const sourceCode = context.getSourceCode()
  249. const offsets = new Map()
  250. const ignoreTokens = new Set()
  251. /**
  252. * Set offset to the given tokens.
  253. * @param {Token|Token[]} token The token to set.
  254. * @param {number} offset The offset of the tokens.
  255. * @param {Token} baseToken The token of the base offset.
  256. * @param {boolean} [trivial=false] The flag for trivial tokens.
  257. * @returns {void}
  258. */
  259. function setOffset (token, offset, baseToken) {
  260. assert(baseToken != null, "'baseToken' should not be null or undefined.")
  261. if (Array.isArray(token)) {
  262. for (const t of token) {
  263. offsets.set(t, {
  264. baseToken,
  265. offset,
  266. baseline: false,
  267. expectedIndent: undefined
  268. })
  269. }
  270. } else {
  271. offsets.set(token, {
  272. baseToken,
  273. offset,
  274. baseline: false,
  275. expectedIndent: undefined
  276. })
  277. }
  278. }
  279. /**
  280. * Set baseline flag to the given token.
  281. * @param {Token} token The token to set.
  282. * @returns {void}
  283. */
  284. function setBaseline (token) {
  285. const offsetInfo = offsets.get(token)
  286. if (offsetInfo != null) {
  287. offsetInfo.baseline = true
  288. }
  289. }
  290. /**
  291. * Sets preformatted tokens to the given element node.
  292. * @param {Node} node The node to set.
  293. * @returns {void}
  294. */
  295. function setPreformattedTokens (node) {
  296. const endToken = (node.endTag && tokenStore.getFirstToken(node.endTag)) || tokenStore.getTokenAfter(node)
  297. const option = {
  298. includeComments: true,
  299. filter: token => token != null && (
  300. token.type === 'HTMLText' ||
  301. token.type === 'HTMLRCDataText' ||
  302. token.type === 'HTMLTagOpen' ||
  303. token.type === 'HTMLEndTagOpen' ||
  304. token.type === 'HTMLComment'
  305. )
  306. }
  307. for (const token of tokenStore.getTokensBetween(node.startTag, endToken, option)) {
  308. ignoreTokens.add(token)
  309. }
  310. ignoreTokens.add(endToken)
  311. }
  312. /**
  313. * Get the first and last tokens of the given node.
  314. * If the node is parenthesized, this gets the outermost parentheses.
  315. * @param {Node} node The node to get.
  316. * @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.
  317. * @returns {{firstToken:Token,lastToken:Token}} The gotten tokens.
  318. */
  319. function getFirstAndLastTokens (node, borderOffset) {
  320. borderOffset |= 0
  321. let firstToken = tokenStore.getFirstToken(node)
  322. let lastToken = tokenStore.getLastToken(node)
  323. // Get the outermost left parenthesis if it's parenthesized.
  324. let t, u
  325. while ((t = tokenStore.getTokenBefore(firstToken)) != null && (u = tokenStore.getTokenAfter(lastToken)) != null && isLeftParen(t) && isRightParen(u) && t.range[0] >= borderOffset) {
  326. firstToken = t
  327. lastToken = u
  328. }
  329. return { firstToken, lastToken }
  330. }
  331. /**
  332. * Process the given node list.
  333. * The first node is offsetted from the given left token.
  334. * Rest nodes are adjusted to the first node.
  335. * @param {Node[]} nodeList The node to process.
  336. * @param {Node|Token|null} left The left parenthesis token.
  337. * @param {Node|Token|null} right The right parenthesis token.
  338. * @param {number} offset The offset to set.
  339. * @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.
  340. * @returns {void}
  341. */
  342. function processNodeList (nodeList, left, right, offset, alignVertically) {
  343. let t
  344. const leftToken = (left && tokenStore.getFirstToken(left)) || left
  345. const rightToken = (right && tokenStore.getFirstToken(right)) || right
  346. if (nodeList.length >= 1) {
  347. let baseToken = null
  348. let lastToken = left
  349. const alignTokensBeforeBaseToken = []
  350. const alignTokens = []
  351. for (let i = 0; i < nodeList.length; ++i) {
  352. const node = nodeList[i]
  353. if (node == null) {
  354. // Holes of an array.
  355. continue
  356. }
  357. const elementTokens = getFirstAndLastTokens(node, lastToken != null ? lastToken.range[1] : 0)
  358. // Collect comma/comment tokens between the last token of the previous node and the first token of this node.
  359. if (lastToken != null) {
  360. t = lastToken
  361. while (
  362. (t = tokenStore.getTokenAfter(t, ITERATION_OPTS)) != null &&
  363. t.range[1] <= elementTokens.firstToken.range[0]
  364. ) {
  365. if (baseToken == null) {
  366. alignTokensBeforeBaseToken.push(t)
  367. } else {
  368. alignTokens.push(t)
  369. }
  370. }
  371. }
  372. if (baseToken == null) {
  373. baseToken = elementTokens.firstToken
  374. } else {
  375. alignTokens.push(elementTokens.firstToken)
  376. }
  377. // Save the last token to find tokens between this node and the next node.
  378. lastToken = elementTokens.lastToken
  379. }
  380. // Check trailing commas and comments.
  381. if (rightToken != null && lastToken != null) {
  382. t = lastToken
  383. while (
  384. (t = tokenStore.getTokenAfter(t, ITERATION_OPTS)) != null &&
  385. t.range[1] <= rightToken.range[0]
  386. ) {
  387. if (baseToken == null) {
  388. alignTokensBeforeBaseToken.push(t)
  389. } else {
  390. alignTokens.push(t)
  391. }
  392. }
  393. }
  394. // Set offsets.
  395. if (leftToken != null) {
  396. setOffset(alignTokensBeforeBaseToken, offset, leftToken)
  397. }
  398. if (baseToken != null) {
  399. // Set offset to the first token.
  400. if (leftToken != null) {
  401. setOffset(baseToken, offset, leftToken)
  402. }
  403. // Set baseline.
  404. if (nodeList.some(isBeginningOfLine)) {
  405. setBaseline(baseToken)
  406. }
  407. if (alignVertically === false && leftToken != null) {
  408. // Align tokens relatively to the left token.
  409. setOffset(alignTokens, offset, leftToken)
  410. } else {
  411. // Align the rest tokens to the first token.
  412. setOffset(alignTokens, 0, baseToken)
  413. }
  414. }
  415. }
  416. if (rightToken != null) {
  417. setOffset(rightToken, 0, leftToken)
  418. }
  419. }
  420. /**
  421. * Process the given node as body.
  422. * The body node maybe a block statement or an expression node.
  423. * @param {Node} node The body node to process.
  424. * @param {Token} baseToken The base token.
  425. * @returns {void}
  426. */
  427. function processMaybeBlock (node, baseToken) {
  428. const firstToken = getFirstAndLastTokens(node).firstToken
  429. setOffset(firstToken, isLeftBrace(firstToken) ? 0 : 1, baseToken)
  430. }
  431. /**
  432. * Collect prefix tokens of the given property.
  433. * The prefix includes `async`, `get`, `set`, `static`, and `*`.
  434. * @param {Property|MethodDefinition} node The property node to collect prefix tokens.
  435. */
  436. function getPrefixTokens (node) {
  437. const prefixes = []
  438. let token = tokenStore.getFirstToken(node)
  439. while (token != null && token.range[1] <= node.key.range[0]) {
  440. prefixes.push(token)
  441. token = tokenStore.getTokenAfter(token)
  442. }
  443. while (isLeftParen(last(prefixes)) || isLeftBracket(last(prefixes))) {
  444. prefixes.pop()
  445. }
  446. return prefixes
  447. }
  448. /**
  449. * Find the head of chaining nodes.
  450. * @param {Node} node The start node to find the head.
  451. * @returns {Token} The head token of the chain.
  452. */
  453. function getChainHeadToken (node) {
  454. const type = node.type
  455. while (node.parent.type === type) {
  456. const prevToken = tokenStore.getTokenBefore(node)
  457. if (isLeftParen(prevToken)) {
  458. // The chaining is broken by parentheses.
  459. break
  460. }
  461. node = node.parent
  462. }
  463. return tokenStore.getFirstToken(node)
  464. }
  465. /**
  466. * Check whether a given token is the first token of:
  467. *
  468. * - ExpressionStatement
  469. * - VExpressionContainer
  470. * - A parameter of CallExpression/NewExpression
  471. * - An element of ArrayExpression
  472. * - An expression of SequenceExpression
  473. *
  474. * @param {Token} token The token to check.
  475. * @param {Node} belongingNode The node that the token is belonging to.
  476. * @returns {boolean} `true` if the token is the first token of an element.
  477. */
  478. function isBeginningOfElement (token, belongingNode) {
  479. let node = belongingNode
  480. while (node != null) {
  481. const parent = node.parent
  482. const t = parent && parent.type
  483. if (t != null && (t.endsWith('Statement') || t.endsWith('Declaration'))) {
  484. return parent.range[0] === token.range[0]
  485. }
  486. if (t === 'VExpressionContainer') {
  487. if (node.range[0] !== token.range[0]) {
  488. return false
  489. }
  490. const prevToken = tokenStore.getTokenBefore(belongingNode)
  491. if (isLeftParen(prevToken)) {
  492. // It is not the first token because it is enclosed in parentheses.
  493. return false
  494. }
  495. return true
  496. }
  497. if (t === 'CallExpression' || t === 'NewExpression') {
  498. const openParen = tokenStore.getTokenAfter(parent.callee, isNotRightParen)
  499. return parent.arguments.some(param =>
  500. getFirstAndLastTokens(param, openParen.range[1]).firstToken.range[0] === token.range[0]
  501. )
  502. }
  503. if (t === 'ArrayExpression') {
  504. return parent.elements.some(element =>
  505. element != null &&
  506. getFirstAndLastTokens(element).firstToken.range[0] === token.range[0]
  507. )
  508. }
  509. if (t === 'SequenceExpression') {
  510. return parent.expressions.some(expr =>
  511. getFirstAndLastTokens(expr).firstToken.range[0] === token.range[0]
  512. )
  513. }
  514. node = parent
  515. }
  516. return false
  517. }
  518. /**
  519. * Set the base indentation to a given top-level AST node.
  520. * @param {Node} node The node to set.
  521. * @param {number} expectedIndent The number of expected indent.
  522. * @returns {void}
  523. */
  524. function processTopLevelNode (node, expectedIndent) {
  525. const token = tokenStore.getFirstToken(node)
  526. const offsetInfo = offsets.get(token)
  527. if (offsetInfo != null) {
  528. offsetInfo.expectedIndent = expectedIndent
  529. } else {
  530. offsets.set(token, { baseToken: null, offset: 0, baseline: false, expectedIndent })
  531. }
  532. }
  533. /**
  534. * Ignore all tokens of the given node.
  535. * @param {Node} node The node to ignore.
  536. * @returns {void}
  537. */
  538. function ignore (node) {
  539. for (const token of tokenStore.getTokens(node)) {
  540. offsets.delete(token)
  541. ignoreTokens.add(token)
  542. }
  543. }
  544. /**
  545. * Define functions to ignore nodes into the given visitor.
  546. * @param {Object} visitor The visitor to define functions to ignore nodes.
  547. * @returns {Object} The visitor.
  548. */
  549. function processIgnores (visitor) {
  550. for (const ignorePattern of options.ignores) {
  551. const key = `${ignorePattern}:exit`
  552. if (visitor.hasOwnProperty(key)) {
  553. const handler = visitor[key]
  554. visitor[key] = function (node) {
  555. const ret = handler.apply(this, arguments)
  556. ignore(node)
  557. return ret
  558. }
  559. } else {
  560. visitor[key] = ignore
  561. }
  562. }
  563. return visitor
  564. }
  565. /**
  566. * Calculate correct indentation of the line of the given tokens.
  567. * @param {Token[]} tokens Tokens which are on the same line.
  568. * @returns {object|null} Correct indentation. If it failed to calculate then `null`.
  569. */
  570. function getExpectedIndents (tokens) {
  571. const expectedIndents = []
  572. for (let i = 0; i < tokens.length; ++i) {
  573. const token = tokens[i]
  574. const offsetInfo = offsets.get(token)
  575. if (offsetInfo != null) {
  576. if (offsetInfo.expectedIndent != null) {
  577. expectedIndents.push(offsetInfo.expectedIndent)
  578. } else {
  579. const baseOffsetInfo = offsets.get(offsetInfo.baseToken)
  580. if (baseOffsetInfo != null && baseOffsetInfo.expectedIndent != null && (i === 0 || !baseOffsetInfo.baseline)) {
  581. expectedIndents.push(baseOffsetInfo.expectedIndent + (offsetInfo.offset * options.indentSize))
  582. if (baseOffsetInfo.baseline) {
  583. break
  584. }
  585. }
  586. }
  587. }
  588. }
  589. if (!expectedIndents.length) {
  590. return null
  591. }
  592. return {
  593. expectedIndent: expectedIndents[0],
  594. expectedBaseIndent: expectedIndents.reduce((a, b) => Math.min(a, b))
  595. }
  596. }
  597. /**
  598. * Get the text of the indentation part of the line which the given token is on.
  599. * @param {Token} firstToken The first token on a line.
  600. * @returns {string} The text of indentation part.
  601. */
  602. function getIndentText (firstToken) {
  603. const text = sourceCode.text
  604. let i = firstToken.range[0] - 1
  605. while (i >= 0 && !LT_CHAR.test(text[i])) {
  606. i -= 1
  607. }
  608. return text.slice(i + 1, firstToken.range[0])
  609. }
  610. /**
  611. * Define the function which fixes the problem.
  612. * @param {Token} token The token to fix.
  613. * @param {number} actualIndent The number of actual indentaion.
  614. * @param {number} expectedIndent The number of expected indentation.
  615. * @returns {Function} The defined function.
  616. */
  617. function defineFix (token, actualIndent, expectedIndent) {
  618. if (token.type === 'Block' && token.loc.start.line !== token.loc.end.line) {
  619. // Fix indentation in multiline block comments.
  620. const lines = sourceCode.getText(token).match(LINES)
  621. const firstLine = lines.shift()
  622. if (lines.every(l => BLOCK_COMMENT_PREFIX.test(l))) {
  623. return fixer => {
  624. const range = [token.range[0] - actualIndent, token.range[1]]
  625. const indent = options.indentChar.repeat(expectedIndent)
  626. return fixer.replaceTextRange(
  627. range,
  628. `${indent}${firstLine}${lines.map(l => l.replace(BLOCK_COMMENT_PREFIX, `${indent} *`)).join('')}`
  629. )
  630. }
  631. }
  632. }
  633. return fixer => {
  634. const range = [token.range[0] - actualIndent, token.range[0]]
  635. const indent = options.indentChar.repeat(expectedIndent)
  636. return fixer.replaceTextRange(range, indent)
  637. }
  638. }
  639. /**
  640. * Validate the given token with the pre-calculated expected indentation.
  641. * @param {Token} token The token to validate.
  642. * @param {number} expectedIndent The expected indentation.
  643. * @param {number[]|undefined} optionalExpectedIndents The optional expected indentation.
  644. * @returns {void}
  645. */
  646. function validateCore (token, expectedIndent, optionalExpectedIndents) {
  647. const line = token.loc.start.line
  648. const indentText = getIndentText(token)
  649. // If there is no line terminator after the `<script>` start tag,
  650. // `indentText` contains non-whitespace characters.
  651. // In that case, do nothing in order to prevent removing the `<script>` tag.
  652. if (indentText.trim() !== '') {
  653. return
  654. }
  655. const actualIndent = token.loc.start.column
  656. const unit = (options.indentChar === '\t' ? 'tab' : 'space')
  657. for (let i = 0; i < indentText.length; ++i) {
  658. if (indentText[i] !== options.indentChar) {
  659. context.report({
  660. loc: {
  661. start: { line, column: i },
  662. end: { line, column: i + 1 }
  663. },
  664. message: 'Expected {{expected}} character, but found {{actual}} character.',
  665. data: {
  666. expected: JSON.stringify(options.indentChar),
  667. actual: JSON.stringify(indentText[i])
  668. },
  669. fix: defineFix(token, actualIndent, expectedIndent)
  670. })
  671. return
  672. }
  673. }
  674. if (actualIndent !== expectedIndent && (optionalExpectedIndents == null || !optionalExpectedIndents.includes(actualIndent))) {
  675. context.report({
  676. loc: {
  677. start: { line, column: 0 },
  678. end: { line, column: actualIndent }
  679. },
  680. message: 'Expected indentation of {{expectedIndent}} {{unit}}{{expectedIndentPlural}} but found {{actualIndent}} {{unit}}{{actualIndentPlural}}.',
  681. data: {
  682. expectedIndent,
  683. actualIndent,
  684. unit,
  685. expectedIndentPlural: (expectedIndent === 1) ? '' : 's',
  686. actualIndentPlural: (actualIndent === 1) ? '' : 's'
  687. },
  688. fix: defineFix(token, actualIndent, expectedIndent)
  689. })
  690. }
  691. }
  692. /**
  693. * Get the expected indent of comments.
  694. * @param {Token|null} nextToken The next token of comments.
  695. * @param {number|undefined} nextExpectedIndent The expected indent of the next token.
  696. * @param {number|undefined} lastExpectedIndent The expected indent of the last token.
  697. * @returns {number[]}
  698. */
  699. function getCommentExpectedIndents (nextToken, nextExpectedIndent, lastExpectedIndent) {
  700. if (typeof lastExpectedIndent === 'number' && isClosingToken(nextToken)) {
  701. if (nextExpectedIndent === lastExpectedIndent) {
  702. // For solo comment. E.g.,
  703. // <div>
  704. // <!-- comment -->
  705. // </div>
  706. return [nextExpectedIndent + options.indentSize, nextExpectedIndent]
  707. }
  708. // For last comment. E.g.,
  709. // <div>
  710. // <div></div>
  711. // <!-- comment -->
  712. // </div>
  713. return [lastExpectedIndent, nextExpectedIndent]
  714. }
  715. // Adjust to next normally. E.g.,
  716. // <div>
  717. // <!-- comment -->
  718. // <div></div>
  719. // </div>
  720. return [nextExpectedIndent]
  721. }
  722. /**
  723. * Validate indentation of the line that the given tokens are on.
  724. * @param {Token[]} tokens The tokens on the same line to validate.
  725. * @param {Token[]} comments The comments which are on the immediately previous lines of the tokens.
  726. * @param {Token|null} lastToken The last validated token. Comments can adjust to the token.
  727. * @returns {void}
  728. */
  729. function validate (tokens, comments, lastToken) {
  730. // Calculate and save expected indentation.
  731. const firstToken = tokens[0]
  732. const actualIndent = firstToken.loc.start.column
  733. const expectedIndents = getExpectedIndents(tokens)
  734. if (!expectedIndents) {
  735. return
  736. }
  737. const expectedBaseIndent = expectedIndents.expectedBaseIndent
  738. const expectedIndent = expectedIndents.expectedIndent
  739. // Debug log
  740. // console.log('line', firstToken.loc.start.line, '=', { actualIndent, expectedIndent }, 'from:')
  741. // for (const token of tokens) {
  742. // const offsetInfo = offsets.get(token)
  743. // if (offsetInfo == null) {
  744. // console.log(' ', JSON.stringify(sourceCode.getText(token)), 'is unknown.')
  745. // } else if (offsetInfo.expectedIndent != null) {
  746. // console.log(' ', JSON.stringify(sourceCode.getText(token)), 'is fixed at', offsetInfo.expectedIndent, '.')
  747. // } else {
  748. // const baseOffsetInfo = offsets.get(offsetInfo.baseToken)
  749. // console.log(' ', JSON.stringify(sourceCode.getText(token)), 'is', offsetInfo.offset, 'offset from ', JSON.stringify(sourceCode.getText(offsetInfo.baseToken)), '( line:', offsetInfo.baseToken && offsetInfo.baseToken.loc.start.line, ', indent:', baseOffsetInfo && baseOffsetInfo.expectedIndent, ', baseline:', baseOffsetInfo && baseOffsetInfo.baseline, ')')
  750. // }
  751. // }
  752. // Save.
  753. const baseline = new Set()
  754. for (const token of tokens) {
  755. const offsetInfo = offsets.get(token)
  756. if (offsetInfo != null) {
  757. if (offsetInfo.baseline) {
  758. // This is a baseline token, so the expected indent is the column of this token.
  759. if (options.indentChar === ' ') {
  760. offsetInfo.expectedIndent = Math.max(0, token.loc.start.column + expectedBaseIndent - actualIndent)
  761. } else {
  762. // In hard-tabs mode, it cannot align tokens strictly, so use one additional offset.
  763. // But the additional offset isn't needed if it's at the beginning of the line.
  764. offsetInfo.expectedIndent = expectedBaseIndent + (token === tokens[0] ? 0 : 1)
  765. }
  766. baseline.add(token)
  767. } else if (baseline.has(offsetInfo.baseToken)) {
  768. // The base token is a baseline token on this line, so inherit it.
  769. offsetInfo.expectedIndent = offsets.get(offsetInfo.baseToken).expectedIndent
  770. baseline.add(token)
  771. } else {
  772. // Otherwise, set the expected indent of this line.
  773. offsetInfo.expectedIndent = expectedBaseIndent
  774. }
  775. }
  776. }
  777. // It does not validate ignore tokens.
  778. if (ignoreTokens.has(firstToken)) {
  779. return
  780. }
  781. // Calculate the expected indents for comments.
  782. // It allows the same indent level with the previous line.
  783. const lastOffsetInfo = offsets.get(lastToken)
  784. const lastExpectedIndent = lastOffsetInfo && lastOffsetInfo.expectedIndent
  785. const commentOptionalExpectedIndents = getCommentExpectedIndents(firstToken, expectedIndent, lastExpectedIndent)
  786. // Validate.
  787. for (const comment of comments) {
  788. const commentExpectedIndents = getExpectedIndents([comment])
  789. const commentExpectedIndent =
  790. commentExpectedIndents
  791. ? commentExpectedIndents.expectedIndent
  792. : commentOptionalExpectedIndents[0]
  793. validateCore(comment, commentExpectedIndent, commentOptionalExpectedIndents)
  794. }
  795. validateCore(firstToken, expectedIndent)
  796. }
  797. // ------------------------------------------------------------------------------
  798. // Main
  799. // ------------------------------------------------------------------------------
  800. return processIgnores({
  801. VAttribute (node) {
  802. const keyToken = tokenStore.getFirstToken(node)
  803. const eqToken = tokenStore.getTokenAfter(node.key)
  804. if (eqToken != null && eqToken.range[1] <= node.range[1]) {
  805. setOffset(eqToken, 1, keyToken)
  806. const valueToken = tokenStore.getTokenAfter(eqToken)
  807. if (valueToken != null && valueToken.range[1] <= node.range[1]) {
  808. setOffset(valueToken, 1, keyToken)
  809. }
  810. }
  811. },
  812. VElement (node) {
  813. if (!PREFORMATTED_ELEMENT_NAMES.includes(node.name)) {
  814. const isTopLevel = node.parent.type !== 'VElement'
  815. const offset = isTopLevel ? options.baseIndent : 1
  816. processNodeList(node.children.filter(isNotEmptyTextNode), node.startTag, node.endTag, offset, false)
  817. } else {
  818. const startTagToken = tokenStore.getFirstToken(node)
  819. const endTagToken = node.endTag && tokenStore.getFirstToken(node.endTag)
  820. setOffset(endTagToken, 0, startTagToken)
  821. setPreformattedTokens(node)
  822. }
  823. },
  824. VEndTag (node) {
  825. const openToken = tokenStore.getFirstToken(node)
  826. const closeToken = tokenStore.getLastToken(node)
  827. if (closeToken.type.endsWith('TagClose')) {
  828. setOffset(closeToken, options.closeBracket, openToken)
  829. }
  830. },
  831. VExpressionContainer (node) {
  832. if (node.expression != null && node.range[0] !== node.expression.range[0]) {
  833. const startQuoteToken = tokenStore.getFirstToken(node)
  834. const endQuoteToken = tokenStore.getLastToken(node)
  835. const childToken = tokenStore.getTokenAfter(startQuoteToken)
  836. setOffset(childToken, 1, startQuoteToken)
  837. setOffset(endQuoteToken, 0, startQuoteToken)
  838. }
  839. },
  840. VFilter (node) {
  841. const idToken = tokenStore.getFirstToken(node)
  842. const lastToken = tokenStore.getLastToken(node)
  843. if (isRightParen(lastToken)) {
  844. const leftParenToken = tokenStore.getTokenAfter(node.callee)
  845. setOffset(leftParenToken, 1, idToken)
  846. processNodeList(node.arguments, leftParenToken, lastToken, 1)
  847. }
  848. },
  849. VFilterSequenceExpression (node) {
  850. if (node.filters.length === 0) {
  851. return
  852. }
  853. const firstToken = tokenStore.getFirstToken(node)
  854. const tokens = []
  855. for (const filter of node.filters) {
  856. tokens.push(
  857. tokenStore.getTokenBefore(filter, isPipeOperator),
  858. tokenStore.getFirstToken(filter)
  859. )
  860. }
  861. setOffset(tokens, 1, firstToken)
  862. },
  863. VForExpression (node) {
  864. const firstToken = tokenStore.getFirstToken(node)
  865. const lastOfLeft = last(node.left) || firstToken
  866. const inToken = tokenStore.getTokenAfter(lastOfLeft, isNotRightParen)
  867. const rightToken = tokenStore.getFirstToken(node.right)
  868. if (isLeftParen(firstToken)) {
  869. const rightToken = tokenStore.getTokenAfter(lastOfLeft, isRightParen)
  870. processNodeList(node.left, firstToken, rightToken, 1)
  871. }
  872. setOffset(inToken, 1, firstToken)
  873. setOffset(rightToken, 1, inToken)
  874. },
  875. VOnExpression (node) {
  876. processNodeList(node.body, null, null, 0)
  877. },
  878. VStartTag (node) {
  879. const openToken = tokenStore.getFirstToken(node)
  880. const closeToken = tokenStore.getLastToken(node)
  881. processNodeList(
  882. node.attributes,
  883. openToken,
  884. null,
  885. options.attribute,
  886. options.alignAttributesVertically
  887. )
  888. if (closeToken != null && closeToken.type.endsWith('TagClose')) {
  889. setOffset(closeToken, options.closeBracket, openToken)
  890. }
  891. },
  892. VText (node) {
  893. const tokens = tokenStore.getTokens(node, isNotWhitespace)
  894. const firstTokenInfo = offsets.get(tokenStore.getFirstToken(node))
  895. for (const token of tokens) {
  896. offsets.set(token, Object.assign({}, firstTokenInfo))
  897. }
  898. },
  899. 'ArrayExpression, ArrayPattern' (node) {
  900. processNodeList(node.elements, tokenStore.getFirstToken(node), tokenStore.getLastToken(node), 1)
  901. },
  902. ArrowFunctionExpression (node) {
  903. const firstToken = tokenStore.getFirstToken(node)
  904. const secondToken = tokenStore.getTokenAfter(firstToken)
  905. const leftToken = node.async ? secondToken : firstToken
  906. const arrowToken = tokenStore.getTokenBefore(node.body, isArrow)
  907. if (node.async) {
  908. setOffset(secondToken, 1, firstToken)
  909. }
  910. if (isLeftParen(leftToken)) {
  911. const rightToken = tokenStore.getTokenAfter(last(node.params) || leftToken, isRightParen)
  912. processNodeList(node.params, leftToken, rightToken, 1)
  913. }
  914. setOffset(arrowToken, 1, firstToken)
  915. processMaybeBlock(node.body, firstToken)
  916. },
  917. 'AssignmentExpression, AssignmentPattern, BinaryExpression, LogicalExpression' (node) {
  918. const leftToken = getChainHeadToken(node)
  919. const opToken = tokenStore.getTokenAfter(node.left, isNotRightParen)
  920. const rightToken = tokenStore.getTokenAfter(opToken)
  921. const prevToken = tokenStore.getTokenBefore(leftToken)
  922. const shouldIndent = (
  923. prevToken == null ||
  924. prevToken.loc.end.line === leftToken.loc.start.line ||
  925. isBeginningOfElement(leftToken, node)
  926. )
  927. setOffset([opToken, rightToken], shouldIndent ? 1 : 0, leftToken)
  928. },
  929. 'AwaitExpression, RestElement, SpreadElement, UnaryExpression' (node) {
  930. const firstToken = tokenStore.getFirstToken(node)
  931. const nextToken = tokenStore.getTokenAfter(firstToken)
  932. setOffset(nextToken, 1, firstToken)
  933. },
  934. 'BlockStatement, ClassBody' (node) {
  935. processNodeList(node.body, tokenStore.getFirstToken(node), tokenStore.getLastToken(node), 1)
  936. },
  937. 'BreakStatement, ContinueStatement, ReturnStatement, ThrowStatement' (node) {
  938. if (node.argument != null || node.label != null) {
  939. const firstToken = tokenStore.getFirstToken(node)
  940. const nextToken = tokenStore.getTokenAfter(firstToken)
  941. setOffset(nextToken, 1, firstToken)
  942. }
  943. },
  944. CallExpression (node) {
  945. const firstToken = tokenStore.getFirstToken(node)
  946. const rightToken = tokenStore.getLastToken(node)
  947. const leftToken = tokenStore.getTokenAfter(node.callee, isLeftParen)
  948. setOffset(leftToken, 1, firstToken)
  949. processNodeList(node.arguments, leftToken, rightToken, 1)
  950. },
  951. CatchClause (node) {
  952. const firstToken = tokenStore.getFirstToken(node)
  953. const bodyToken = tokenStore.getFirstToken(node.body)
  954. if (node.param != null) {
  955. const leftToken = tokenStore.getTokenAfter(firstToken)
  956. const rightToken = tokenStore.getTokenAfter(node.param)
  957. setOffset(leftToken, 1, firstToken)
  958. processNodeList([node.param], leftToken, rightToken, 1)
  959. }
  960. setOffset(bodyToken, 0, firstToken)
  961. },
  962. 'ClassDeclaration, ClassExpression' (node) {
  963. const firstToken = tokenStore.getFirstToken(node)
  964. const bodyToken = tokenStore.getFirstToken(node.body)
  965. if (node.id != null) {
  966. setOffset(tokenStore.getFirstToken(node.id), 1, firstToken)
  967. }
  968. if (node.superClass != null) {
  969. const extendsToken = tokenStore.getTokenAfter(node.id || firstToken)
  970. const superClassToken = tokenStore.getTokenAfter(extendsToken)
  971. setOffset(extendsToken, 1, firstToken)
  972. setOffset(superClassToken, 1, extendsToken)
  973. }
  974. setOffset(bodyToken, 0, firstToken)
  975. },
  976. ConditionalExpression (node) {
  977. const prevToken = tokenStore.getTokenBefore(node)
  978. const firstToken = tokenStore.getFirstToken(node)
  979. const questionToken = tokenStore.getTokenAfter(node.test, isNotRightParen)
  980. const consequentToken = tokenStore.getTokenAfter(questionToken)
  981. const colonToken = tokenStore.getTokenAfter(node.consequent, isNotRightParen)
  982. const alternateToken = tokenStore.getTokenAfter(colonToken)
  983. const isFlat =
  984. prevToken &&
  985. prevToken.loc.end.line !== node.loc.start.line &&
  986. node.test.loc.end.line === node.consequent.loc.start.line
  987. if (isFlat) {
  988. setOffset([questionToken, consequentToken, colonToken, alternateToken], 0, firstToken)
  989. } else {
  990. setOffset([questionToken, colonToken], 1, firstToken)
  991. setOffset([consequentToken, alternateToken], 1, questionToken)
  992. }
  993. },
  994. DoWhileStatement (node) {
  995. const doToken = tokenStore.getFirstToken(node)
  996. const whileToken = tokenStore.getTokenAfter(node.body, isNotRightParen)
  997. const leftToken = tokenStore.getTokenAfter(whileToken)
  998. const testToken = tokenStore.getTokenAfter(leftToken)
  999. const lastToken = tokenStore.getLastToken(node)
  1000. const rightToken = isSemicolon(lastToken) ? tokenStore.getTokenBefore(lastToken) : lastToken
  1001. processMaybeBlock(node.body, doToken)
  1002. setOffset(whileToken, 0, doToken)
  1003. setOffset(leftToken, 1, whileToken)
  1004. setOffset(testToken, 1, leftToken)
  1005. setOffset(rightToken, 0, leftToken)
  1006. },
  1007. ExportAllDeclaration (node) {
  1008. const tokens = tokenStore.getTokens(node)
  1009. const firstToken = tokens.shift()
  1010. if (isSemicolon(last(tokens))) {
  1011. tokens.pop()
  1012. }
  1013. setOffset(tokens, 1, firstToken)
  1014. },
  1015. ExportDefaultDeclaration (node) {
  1016. const exportToken = tokenStore.getFirstToken(node)
  1017. const defaultToken = tokenStore.getFirstToken(node, 1)
  1018. const declarationToken = getFirstAndLastTokens(node.declaration).firstToken
  1019. setOffset([defaultToken, declarationToken], 1, exportToken)
  1020. },
  1021. ExportNamedDeclaration (node) {
  1022. const exportToken = tokenStore.getFirstToken(node)
  1023. if (node.declaration) {
  1024. // export var foo = 1;
  1025. const declarationToken = tokenStore.getFirstToken(node, 1)
  1026. setOffset(declarationToken, 1, exportToken)
  1027. } else {
  1028. // export {foo, bar}; or export {foo, bar} from "mod";
  1029. const leftParenToken = tokenStore.getFirstToken(node, 1)
  1030. const rightParenToken = tokenStore.getLastToken(node, isRightBrace)
  1031. setOffset(leftParenToken, 0, exportToken)
  1032. processNodeList(node.specifiers, leftParenToken, rightParenToken, 1)
  1033. const maybeFromToken = tokenStore.getTokenAfter(rightParenToken)
  1034. if (maybeFromToken != null && sourceCode.getText(maybeFromToken) === 'from') {
  1035. const fromToken = maybeFromToken
  1036. const nameToken = tokenStore.getTokenAfter(fromToken)
  1037. setOffset([fromToken, nameToken], 1, exportToken)
  1038. }
  1039. }
  1040. },
  1041. ExportSpecifier (node) {
  1042. const tokens = tokenStore.getTokens(node)
  1043. const firstToken = tokens.shift()
  1044. setOffset(tokens, 1, firstToken)
  1045. },
  1046. 'ForInStatement, ForOfStatement' (node) {
  1047. const forToken = tokenStore.getFirstToken(node)
  1048. const leftParenToken = tokenStore.getTokenAfter(forToken)
  1049. const leftToken = tokenStore.getTokenAfter(leftParenToken)
  1050. const inToken = tokenStore.getTokenAfter(leftToken, isNotRightParen)
  1051. const rightToken = tokenStore.getTokenAfter(inToken)
  1052. const rightParenToken = tokenStore.getTokenBefore(node.body, isNotLeftParen)
  1053. setOffset(leftParenToken, 1, forToken)
  1054. setOffset(leftToken, 1, leftParenToken)
  1055. setOffset(inToken, 1, leftToken)
  1056. setOffset(rightToken, 1, leftToken)
  1057. setOffset(rightParenToken, 0, leftParenToken)
  1058. processMaybeBlock(node.body, forToken)
  1059. },
  1060. ForStatement (node) {
  1061. const forToken = tokenStore.getFirstToken(node)
  1062. const leftParenToken = tokenStore.getTokenAfter(forToken)
  1063. const rightParenToken = tokenStore.getTokenBefore(node.body, isNotLeftParen)
  1064. setOffset(leftParenToken, 1, forToken)
  1065. processNodeList([node.init, node.test, node.update], leftParenToken, rightParenToken, 1)
  1066. processMaybeBlock(node.body, forToken)
  1067. },
  1068. 'FunctionDeclaration, FunctionExpression' (node) {
  1069. const firstToken = tokenStore.getFirstToken(node)
  1070. if (isLeftParen(firstToken)) {
  1071. // Methods.
  1072. const leftToken = firstToken
  1073. const rightToken = tokenStore.getTokenAfter(last(node.params) || leftToken, isRightParen)
  1074. const bodyToken = tokenStore.getFirstToken(node.body)
  1075. processNodeList(node.params, leftToken, rightToken, 1)
  1076. setOffset(bodyToken, 0, tokenStore.getFirstToken(node.parent))
  1077. } else {
  1078. // Normal functions.
  1079. const functionToken = node.async ? tokenStore.getTokenAfter(firstToken) : firstToken
  1080. const starToken = node.generator ? tokenStore.getTokenAfter(functionToken) : null
  1081. const idToken = node.id && tokenStore.getFirstToken(node.id)
  1082. const leftToken = tokenStore.getTokenAfter(idToken || starToken || functionToken)
  1083. const rightToken = tokenStore.getTokenAfter(last(node.params) || leftToken, isRightParen)
  1084. const bodyToken = tokenStore.getFirstToken(node.body)
  1085. if (node.async) {
  1086. setOffset(functionToken, 0, firstToken)
  1087. }
  1088. if (node.generator) {
  1089. setOffset(starToken, 1, firstToken)
  1090. }
  1091. if (node.id != null) {
  1092. setOffset(idToken, 1, firstToken)
  1093. }
  1094. setOffset(leftToken, 1, firstToken)
  1095. processNodeList(node.params, leftToken, rightToken, 1)
  1096. setOffset(bodyToken, 0, firstToken)
  1097. }
  1098. },
  1099. IfStatement (node) {
  1100. const ifToken = tokenStore.getFirstToken(node)
  1101. const ifLeftParenToken = tokenStore.getTokenAfter(ifToken)
  1102. const ifRightParenToken = tokenStore.getTokenBefore(node.consequent, isRightParen)
  1103. setOffset(ifLeftParenToken, 1, ifToken)
  1104. setOffset(ifRightParenToken, 0, ifLeftParenToken)
  1105. processMaybeBlock(node.consequent, ifToken)
  1106. if (node.alternate != null) {
  1107. const elseToken = tokenStore.getTokenAfter(node.consequent, isNotRightParen)
  1108. setOffset(elseToken, 0, ifToken)
  1109. processMaybeBlock(node.alternate, elseToken)
  1110. }
  1111. },
  1112. ImportDeclaration (node) {
  1113. const firstSpecifier = node.specifiers[0]
  1114. const secondSpecifier = node.specifiers[1]
  1115. const importToken = tokenStore.getFirstToken(node)
  1116. const hasSemi = tokenStore.getLastToken(node).value === ';'
  1117. const tokens = [] // tokens to one indent
  1118. if (!firstSpecifier) {
  1119. // There are 2 patterns:
  1120. // import "foo"
  1121. // import {} from "foo"
  1122. const secondToken = tokenStore.getFirstToken(node, 1)
  1123. if (isLeftBrace(secondToken)) {
  1124. setOffset(
  1125. [secondToken, tokenStore.getTokenAfter(secondToken)],
  1126. 0,
  1127. importToken
  1128. )
  1129. tokens.push(
  1130. tokenStore.getLastToken(node, hasSemi ? 2 : 1), // from
  1131. tokenStore.getLastToken(node, hasSemi ? 1 : 0) // "foo"
  1132. )
  1133. } else {
  1134. tokens.push(tokenStore.getLastToken(node, hasSemi ? 1 : 0))
  1135. }
  1136. } else if (firstSpecifier.type === 'ImportDefaultSpecifier') {
  1137. if (secondSpecifier && secondSpecifier.type === 'ImportNamespaceSpecifier') {
  1138. // There is a pattern:
  1139. // import Foo, * as foo from "foo"
  1140. tokens.push(
  1141. tokenStore.getFirstToken(firstSpecifier), // Foo
  1142. tokenStore.getTokenAfter(firstSpecifier), // comma
  1143. tokenStore.getFirstToken(secondSpecifier), // *
  1144. tokenStore.getLastToken(node, hasSemi ? 2 : 1), // from
  1145. tokenStore.getLastToken(node, hasSemi ? 1 : 0) // "foo"
  1146. )
  1147. } else {
  1148. // There are 3 patterns:
  1149. // import Foo from "foo"
  1150. // import Foo, {} from "foo"
  1151. // import Foo, {a} from "foo"
  1152. const idToken = tokenStore.getFirstToken(firstSpecifier)
  1153. const nextToken = tokenStore.getTokenAfter(firstSpecifier)
  1154. if (isComma(nextToken)) {
  1155. const leftBrace = tokenStore.getTokenAfter(nextToken)
  1156. const rightBrace = tokenStore.getLastToken(node, hasSemi ? 3 : 2)
  1157. setOffset([idToken, nextToken], 1, importToken)
  1158. setOffset(leftBrace, 0, idToken)
  1159. processNodeList(node.specifiers.slice(1), leftBrace, rightBrace, 1)
  1160. tokens.push(
  1161. tokenStore.getLastToken(node, hasSemi ? 2 : 1), // from
  1162. tokenStore.getLastToken(node, hasSemi ? 1 : 0) // "foo"
  1163. )
  1164. } else {
  1165. tokens.push(
  1166. idToken,
  1167. nextToken, // from
  1168. tokenStore.getTokenAfter(nextToken) // "foo"
  1169. )
  1170. }
  1171. }
  1172. } else if (firstSpecifier.type === 'ImportNamespaceSpecifier') {
  1173. // There is a pattern:
  1174. // import * as foo from "foo"
  1175. tokens.push(
  1176. tokenStore.getFirstToken(firstSpecifier), // *
  1177. tokenStore.getLastToken(node, hasSemi ? 2 : 1), // from
  1178. tokenStore.getLastToken(node, hasSemi ? 1 : 0) // "foo"
  1179. )
  1180. } else {
  1181. // There is a pattern:
  1182. // import {a} from "foo"
  1183. const leftBrace = tokenStore.getFirstToken(node, 1)
  1184. const rightBrace = tokenStore.getLastToken(node, hasSemi ? 3 : 2)
  1185. setOffset(leftBrace, 0, importToken)
  1186. processNodeList(node.specifiers, leftBrace, rightBrace, 1)
  1187. tokens.push(
  1188. tokenStore.getLastToken(node, hasSemi ? 2 : 1), // from
  1189. tokenStore.getLastToken(node, hasSemi ? 1 : 0) // "foo"
  1190. )
  1191. }
  1192. setOffset(tokens, 1, importToken)
  1193. },
  1194. ImportSpecifier (node) {
  1195. if (node.local.range[0] !== node.imported.range[0]) {
  1196. const tokens = tokenStore.getTokens(node)
  1197. const firstToken = tokens.shift()
  1198. setOffset(tokens, 1, firstToken)
  1199. }
  1200. },
  1201. ImportNamespaceSpecifier (node) {
  1202. const tokens = tokenStore.getTokens(node)
  1203. const firstToken = tokens.shift()
  1204. setOffset(tokens, 1, firstToken)
  1205. },
  1206. LabeledStatement (node) {
  1207. const labelToken = tokenStore.getFirstToken(node)
  1208. const colonToken = tokenStore.getTokenAfter(labelToken)
  1209. const bodyToken = tokenStore.getTokenAfter(colonToken)
  1210. setOffset([colonToken, bodyToken], 1, labelToken)
  1211. },
  1212. 'MemberExpression, MetaProperty' (node) {
  1213. const objectToken = tokenStore.getFirstToken(node)
  1214. if (node.computed) {
  1215. const leftBracketToken = tokenStore.getTokenBefore(node.property, isLeftBracket)
  1216. const propertyToken = tokenStore.getTokenAfter(leftBracketToken)
  1217. const rightBracketToken = tokenStore.getTokenAfter(node.property, isRightBracket)
  1218. setOffset(leftBracketToken, 1, objectToken)
  1219. setOffset(propertyToken, 1, leftBracketToken)
  1220. setOffset(rightBracketToken, 0, leftBracketToken)
  1221. } else {
  1222. const dotToken = tokenStore.getTokenBefore(node.property)
  1223. const propertyToken = tokenStore.getTokenAfter(dotToken)
  1224. setOffset([dotToken, propertyToken], 1, objectToken)
  1225. }
  1226. },
  1227. 'MethodDefinition, Property' (node) {
  1228. const isMethod = (node.type === 'MethodDefinition' || node.method === true)
  1229. const prefixTokens = getPrefixTokens(node)
  1230. const hasPrefix = prefixTokens.length >= 1
  1231. for (let i = 1; i < prefixTokens.length; ++i) {
  1232. setOffset(prefixTokens[i], 0, prefixTokens[i - 1])
  1233. }
  1234. let lastKeyToken = null
  1235. if (node.computed) {
  1236. const keyLeftToken = tokenStore.getFirstToken(node, isLeftBracket)
  1237. const keyToken = tokenStore.getTokenAfter(keyLeftToken)
  1238. const keyRightToken = lastKeyToken = tokenStore.getTokenAfter(node.key, isRightBracket)
  1239. if (hasPrefix) {
  1240. setOffset(keyLeftToken, 0, last(prefixTokens))
  1241. }
  1242. setOffset(keyToken, 1, keyLeftToken)
  1243. setOffset(keyRightToken, 0, keyLeftToken)
  1244. } else {
  1245. const idToken = lastKeyToken = tokenStore.getFirstToken(node.key)
  1246. if (hasPrefix) {
  1247. setOffset(idToken, 0, last(prefixTokens))
  1248. }
  1249. }
  1250. if (isMethod) {
  1251. const leftParenToken = tokenStore.getTokenAfter(lastKeyToken)
  1252. setOffset(leftParenToken, 1, lastKeyToken)
  1253. } else if (!node.shorthand) {
  1254. const colonToken = tokenStore.getTokenAfter(lastKeyToken)
  1255. const valueToken = tokenStore.getTokenAfter(colonToken)
  1256. setOffset([colonToken, valueToken], 1, lastKeyToken)
  1257. }
  1258. },
  1259. NewExpression (node) {
  1260. const newToken = tokenStore.getFirstToken(node)
  1261. const calleeToken = tokenStore.getTokenAfter(newToken)
  1262. const rightToken = tokenStore.getLastToken(node)
  1263. const leftToken = isRightParen(rightToken)
  1264. ? tokenStore.getFirstTokenBetween(node.callee, rightToken, isLeftParen)
  1265. : null
  1266. setOffset(calleeToken, 1, newToken)
  1267. if (leftToken != null) {
  1268. setOffset(leftToken, 1, calleeToken)
  1269. processNodeList(node.arguments, leftToken, rightToken, 1)
  1270. }
  1271. },
  1272. 'ObjectExpression, ObjectPattern' (node) {
  1273. processNodeList(node.properties, tokenStore.getFirstToken(node), tokenStore.getLastToken(node), 1)
  1274. },
  1275. SequenceExpression (node) {
  1276. processNodeList(node.expressions, null, null, 0)
  1277. },
  1278. SwitchCase (node) {
  1279. const caseToken = tokenStore.getFirstToken(node)
  1280. if (node.test != null) {
  1281. const testToken = tokenStore.getTokenAfter(caseToken)
  1282. const colonToken = tokenStore.getTokenAfter(node.test, isNotRightParen)
  1283. setOffset([testToken, colonToken], 1, caseToken)
  1284. } else {
  1285. const colonToken = tokenStore.getTokenAfter(caseToken)
  1286. setOffset(colonToken, 1, caseToken)
  1287. }
  1288. if (node.consequent.length === 1 && node.consequent[0].type === 'BlockStatement') {
  1289. setOffset(tokenStore.getFirstToken(node.consequent[0]), 0, caseToken)
  1290. } else if (node.consequent.length >= 1) {
  1291. setOffset(tokenStore.getFirstToken(node.consequent[0]), 1, caseToken)
  1292. processNodeList(node.consequent, null, null, 0)
  1293. }
  1294. },
  1295. SwitchStatement (node) {
  1296. const switchToken = tokenStore.getFirstToken(node)
  1297. const leftParenToken = tokenStore.getTokenAfter(switchToken)
  1298. const discriminantToken = tokenStore.getTokenAfter(leftParenToken)
  1299. const leftBraceToken = tokenStore.getTokenAfter(node.discriminant, isLeftBrace)
  1300. const rightParenToken = tokenStore.getTokenBefore(leftBraceToken)
  1301. const rightBraceToken = tokenStore.getLastToken(node)
  1302. setOffset(leftParenToken, 1, switchToken)
  1303. setOffset(discriminantToken, 1, leftParenToken)
  1304. setOffset(rightParenToken, 0, leftParenToken)
  1305. setOffset(leftBraceToken, 0, switchToken)
  1306. processNodeList(node.cases, leftBraceToken, rightBraceToken, options.switchCase)
  1307. },
  1308. TaggedTemplateExpression (node) {
  1309. const tagTokens = getFirstAndLastTokens(node.tag, node.range[0])
  1310. const quasiToken = tokenStore.getTokenAfter(tagTokens.lastToken)
  1311. setOffset(quasiToken, 1, tagTokens.firstToken)
  1312. },
  1313. TemplateLiteral (node) {
  1314. const firstToken = tokenStore.getFirstToken(node)
  1315. const quasiTokens = node.quasis.slice(1).map(n => tokenStore.getFirstToken(n))
  1316. const expressionToken = node.quasis.slice(0, -1).map(n => tokenStore.getTokenAfter(n))
  1317. setOffset(quasiTokens, 0, firstToken)
  1318. setOffset(expressionToken, 1, firstToken)
  1319. },
  1320. TryStatement (node) {
  1321. const tryToken = tokenStore.getFirstToken(node)
  1322. const tryBlockToken = tokenStore.getFirstToken(node.block)
  1323. setOffset(tryBlockToken, 0, tryToken)
  1324. if (node.handler != null) {
  1325. const catchToken = tokenStore.getFirstToken(node.handler)
  1326. setOffset(catchToken, 0, tryToken)
  1327. }
  1328. if (node.finalizer != null) {
  1329. const finallyToken = tokenStore.getTokenBefore(node.finalizer)
  1330. const finallyBlockToken = tokenStore.getFirstToken(node.finalizer)
  1331. setOffset([finallyToken, finallyBlockToken], 0, tryToken)
  1332. }
  1333. },
  1334. UpdateExpression (node) {
  1335. const firstToken = tokenStore.getFirstToken(node)
  1336. const nextToken = tokenStore.getTokenAfter(firstToken)
  1337. setOffset(nextToken, 1, firstToken)
  1338. },
  1339. VariableDeclaration (node) {
  1340. processNodeList(node.declarations, tokenStore.getFirstToken(node), null, 1)
  1341. },
  1342. VariableDeclarator (node) {
  1343. if (node.init != null) {
  1344. const idToken = tokenStore.getFirstToken(node)
  1345. const eqToken = tokenStore.getTokenAfter(node.id)
  1346. const initToken = tokenStore.getTokenAfter(eqToken)
  1347. setOffset([eqToken, initToken], 1, idToken)
  1348. }
  1349. },
  1350. 'WhileStatement, WithStatement' (node) {
  1351. const firstToken = tokenStore.getFirstToken(node)
  1352. const leftParenToken = tokenStore.getTokenAfter(firstToken)
  1353. const rightParenToken = tokenStore.getTokenBefore(node.body, isRightParen)
  1354. setOffset(leftParenToken, 1, firstToken)
  1355. setOffset(rightParenToken, 0, leftParenToken)
  1356. processMaybeBlock(node.body, firstToken)
  1357. },
  1358. YieldExpression (node) {
  1359. if (node.argument != null) {
  1360. const yieldToken = tokenStore.getFirstToken(node)
  1361. setOffset(tokenStore.getTokenAfter(yieldToken), 1, yieldToken)
  1362. if (node.delegate) {
  1363. setOffset(tokenStore.getTokenAfter(yieldToken, 1), 1, yieldToken)
  1364. }
  1365. }
  1366. },
  1367. // Process semicolons.
  1368. ':statement' (node) {
  1369. const firstToken = tokenStore.getFirstToken(node)
  1370. const lastToken = tokenStore.getLastToken(node)
  1371. if (isSemicolon(lastToken) && firstToken !== lastToken) {
  1372. setOffset(lastToken, 0, firstToken)
  1373. }
  1374. // Set to the semicolon of the previous token for semicolon-free style.
  1375. // E.g.,
  1376. // foo
  1377. // ;[1,2,3].forEach(f)
  1378. const info = offsets.get(firstToken)
  1379. const prevToken = tokenStore.getTokenBefore(firstToken)
  1380. if (info != null && isSemicolon(prevToken) && prevToken.loc.end.line === firstToken.loc.start.line) {
  1381. offsets.set(prevToken, info)
  1382. }
  1383. },
  1384. // Process parentheses.
  1385. // `:expression` does not match with MetaProperty and TemplateLiteral as a bug: https://github.com/estools/esquery/pull/59
  1386. ':expression, MetaProperty, TemplateLiteral' (node) {
  1387. let leftToken = tokenStore.getTokenBefore(node)
  1388. let rightToken = tokenStore.getTokenAfter(node)
  1389. let firstToken = tokenStore.getFirstToken(node)
  1390. while (isLeftParen(leftToken) && isRightParen(rightToken)) {
  1391. setOffset(firstToken, 1, leftToken)
  1392. setOffset(rightToken, 0, leftToken)
  1393. firstToken = leftToken
  1394. leftToken = tokenStore.getTokenBefore(leftToken)
  1395. rightToken = tokenStore.getTokenAfter(rightToken)
  1396. }
  1397. },
  1398. // Ignore tokens of unknown nodes.
  1399. '*:exit' (node) {
  1400. if (!KNOWN_NODES.has(node.type)) {
  1401. ignore(node)
  1402. }
  1403. },
  1404. // Top-level process.
  1405. Program (node) {
  1406. const firstToken = node.tokens[0]
  1407. const isScriptTag = (
  1408. firstToken != null &&
  1409. firstToken.type === 'Punctuator' &&
  1410. firstToken.value === '<script>'
  1411. )
  1412. const baseIndent =
  1413. isScriptTag ? (options.indentSize * options.baseIndent) : 0
  1414. for (const statement of node.body) {
  1415. processTopLevelNode(statement, baseIndent)
  1416. }
  1417. },
  1418. "VElement[parent.type!='VElement']" (node) {
  1419. processTopLevelNode(node, 0)
  1420. },
  1421. // Do validation.
  1422. ":matches(Program, VElement[parent.type!='VElement']):exit" (node) {
  1423. let comments = []
  1424. let tokensOnSameLine = []
  1425. let isBesideMultilineToken = false
  1426. let lastValidatedToken = null
  1427. // Validate indentation of tokens.
  1428. for (const token of tokenStore.getTokens(node, ITERATION_OPTS)) {
  1429. if (tokensOnSameLine.length === 0 || tokensOnSameLine[0].loc.start.line === token.loc.start.line) {
  1430. // This is on the same line (or the first token).
  1431. tokensOnSameLine.push(token)
  1432. } else if (tokensOnSameLine.every(isComment)) {
  1433. // New line is detected, but the all tokens of the previous line are comment.
  1434. // Comment lines are adjusted to the next code line.
  1435. comments.push(tokensOnSameLine[0])
  1436. isBesideMultilineToken = last(tokensOnSameLine).loc.end.line === token.loc.start.line
  1437. tokensOnSameLine = [token]
  1438. } else {
  1439. // New line is detected, so validate the tokens.
  1440. if (!isBesideMultilineToken) {
  1441. validate(tokensOnSameLine, comments, lastValidatedToken)
  1442. lastValidatedToken = tokensOnSameLine[0]
  1443. }
  1444. isBesideMultilineToken = last(tokensOnSameLine).loc.end.line === token.loc.start.line
  1445. tokensOnSameLine = [token]
  1446. comments = []
  1447. }
  1448. }
  1449. if (tokensOnSameLine.length >= 1 && tokensOnSameLine.some(isNotComment)) {
  1450. validate(tokensOnSameLine, comments, lastValidatedToken)
  1451. }
  1452. }
  1453. })
  1454. }