Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ast-utils.js 45KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /**
  2. * @fileoverview Common utils for AST.
  3. * @author Gyandeep Singh
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const esutils = require("esutils");
  10. const espree = require("espree");
  11. //------------------------------------------------------------------------------
  12. // Helpers
  13. //------------------------------------------------------------------------------
  14. const anyFunctionPattern = /^(?:Function(?:Declaration|Expression)|ArrowFunctionExpression)$/;
  15. const anyLoopPattern = /^(?:DoWhile|For|ForIn|ForOf|While)Statement$/;
  16. const arrayOrTypedArrayPattern = /Array$/;
  17. const arrayMethodPattern = /^(?:every|filter|find|findIndex|forEach|map|some)$/;
  18. const bindOrCallOrApplyPattern = /^(?:bind|call|apply)$/;
  19. const breakableTypePattern = /^(?:(?:Do)?While|For(?:In|Of)?|Switch)Statement$/;
  20. const thisTagPattern = /^[\s*]*@this/m;
  21. const COMMENTS_IGNORE_PATTERN = /^\s*(?:eslint|jshint\s+|jslint\s+|istanbul\s+|globals?\s+|exported\s+|jscs)/;
  22. const LINEBREAKS = new Set(["\r\n", "\r", "\n", "\u2028", "\u2029"]);
  23. const LINEBREAK_MATCHER = /\r\n|[\r\n\u2028\u2029]/;
  24. const SHEBANG_MATCHER = /^#!([^\r\n]+)/;
  25. // A set of node types that can contain a list of statements
  26. const STATEMENT_LIST_PARENTS = new Set(["Program", "BlockStatement", "SwitchCase"]);
  27. /**
  28. * Checks reference if is non initializer and writable.
  29. * @param {Reference} reference - A reference to check.
  30. * @param {int} index - The index of the reference in the references.
  31. * @param {Reference[]} references - The array that the reference belongs to.
  32. * @returns {boolean} Success/Failure
  33. * @private
  34. */
  35. function isModifyingReference(reference, index, references) {
  36. const identifier = reference.identifier;
  37. /*
  38. * Destructuring assignments can have multiple default value, so
  39. * possibly there are multiple writeable references for the same
  40. * identifier.
  41. */
  42. const modifyingDifferentIdentifier = index === 0 ||
  43. references[index - 1].identifier !== identifier;
  44. return (identifier &&
  45. reference.init === false &&
  46. reference.isWrite() &&
  47. modifyingDifferentIdentifier
  48. );
  49. }
  50. /**
  51. * Checks whether the given string starts with uppercase or not.
  52. *
  53. * @param {string} s - The string to check.
  54. * @returns {boolean} `true` if the string starts with uppercase.
  55. */
  56. function startsWithUpperCase(s) {
  57. return s[0] !== s[0].toLocaleLowerCase();
  58. }
  59. /**
  60. * Checks whether or not a node is a constructor.
  61. * @param {ASTNode} node - A function node to check.
  62. * @returns {boolean} Wehether or not a node is a constructor.
  63. */
  64. function isES5Constructor(node) {
  65. return (node.id && startsWithUpperCase(node.id.name));
  66. }
  67. /**
  68. * Finds a function node from ancestors of a node.
  69. * @param {ASTNode} node - A start node to find.
  70. * @returns {Node|null} A found function node.
  71. */
  72. function getUpperFunction(node) {
  73. for (let currentNode = node; currentNode; currentNode = currentNode.parent) {
  74. if (anyFunctionPattern.test(currentNode.type)) {
  75. return currentNode;
  76. }
  77. }
  78. return null;
  79. }
  80. /**
  81. * Checks whether a given node is a function node or not.
  82. * The following types are function nodes:
  83. *
  84. * - ArrowFunctionExpression
  85. * - FunctionDeclaration
  86. * - FunctionExpression
  87. *
  88. * @param {ASTNode|null} node - A node to check.
  89. * @returns {boolean} `true` if the node is a function node.
  90. */
  91. function isFunction(node) {
  92. return Boolean(node && anyFunctionPattern.test(node.type));
  93. }
  94. /**
  95. * Checks whether a given node is a loop node or not.
  96. * The following types are loop nodes:
  97. *
  98. * - DoWhileStatement
  99. * - ForInStatement
  100. * - ForOfStatement
  101. * - ForStatement
  102. * - WhileStatement
  103. *
  104. * @param {ASTNode|null} node - A node to check.
  105. * @returns {boolean} `true` if the node is a loop node.
  106. */
  107. function isLoop(node) {
  108. return Boolean(node && anyLoopPattern.test(node.type));
  109. }
  110. /**
  111. * Checks whether the given node is in a loop or not.
  112. *
  113. * @param {ASTNode} node - The node to check.
  114. * @returns {boolean} `true` if the node is in a loop.
  115. */
  116. function isInLoop(node) {
  117. for (let currentNode = node; currentNode && !isFunction(currentNode); currentNode = currentNode.parent) {
  118. if (isLoop(currentNode)) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. /**
  125. * Checks whether or not a node is `null` or `undefined`.
  126. * @param {ASTNode} node - A node to check.
  127. * @returns {boolean} Whether or not the node is a `null` or `undefined`.
  128. * @public
  129. */
  130. function isNullOrUndefined(node) {
  131. return (
  132. module.exports.isNullLiteral(node) ||
  133. (node.type === "Identifier" && node.name === "undefined") ||
  134. (node.type === "UnaryExpression" && node.operator === "void")
  135. );
  136. }
  137. /**
  138. * Checks whether or not a node is callee.
  139. * @param {ASTNode} node - A node to check.
  140. * @returns {boolean} Whether or not the node is callee.
  141. */
  142. function isCallee(node) {
  143. return node.parent.type === "CallExpression" && node.parent.callee === node;
  144. }
  145. /**
  146. * Checks whether or not a node is `Reflect.apply`.
  147. * @param {ASTNode} node - A node to check.
  148. * @returns {boolean} Whether or not the node is a `Reflect.apply`.
  149. */
  150. function isReflectApply(node) {
  151. return (
  152. node.type === "MemberExpression" &&
  153. node.object.type === "Identifier" &&
  154. node.object.name === "Reflect" &&
  155. node.property.type === "Identifier" &&
  156. node.property.name === "apply" &&
  157. node.computed === false
  158. );
  159. }
  160. /**
  161. * Checks whether or not a node is `Array.from`.
  162. * @param {ASTNode} node - A node to check.
  163. * @returns {boolean} Whether or not the node is a `Array.from`.
  164. */
  165. function isArrayFromMethod(node) {
  166. return (
  167. node.type === "MemberExpression" &&
  168. node.object.type === "Identifier" &&
  169. arrayOrTypedArrayPattern.test(node.object.name) &&
  170. node.property.type === "Identifier" &&
  171. node.property.name === "from" &&
  172. node.computed === false
  173. );
  174. }
  175. /**
  176. * Checks whether or not a node is a method which has `thisArg`.
  177. * @param {ASTNode} node - A node to check.
  178. * @returns {boolean} Whether or not the node is a method which has `thisArg`.
  179. */
  180. function isMethodWhichHasThisArg(node) {
  181. for (
  182. let currentNode = node;
  183. currentNode.type === "MemberExpression" && !currentNode.computed;
  184. currentNode = currentNode.property
  185. ) {
  186. if (currentNode.property.type === "Identifier") {
  187. return arrayMethodPattern.test(currentNode.property.name);
  188. }
  189. }
  190. return false;
  191. }
  192. /**
  193. * Creates the negate function of the given function.
  194. * @param {Function} f - The function to negate.
  195. * @returns {Function} Negated function.
  196. */
  197. function negate(f) {
  198. return token => !f(token);
  199. }
  200. /**
  201. * Checks whether or not a node has a `@this` tag in its comments.
  202. * @param {ASTNode} node - A node to check.
  203. * @param {SourceCode} sourceCode - A SourceCode instance to get comments.
  204. * @returns {boolean} Whether or not the node has a `@this` tag in its comments.
  205. */
  206. function hasJSDocThisTag(node, sourceCode) {
  207. const jsdocComment = sourceCode.getJSDocComment(node);
  208. if (jsdocComment && thisTagPattern.test(jsdocComment.value)) {
  209. return true;
  210. }
  211. // Checks `@this` in its leading comments for callbacks,
  212. // because callbacks don't have its JSDoc comment.
  213. // e.g.
  214. // sinon.test(/* @this sinon.Sandbox */function() { this.spy(); });
  215. return sourceCode.getCommentsBefore(node).some(comment => thisTagPattern.test(comment.value));
  216. }
  217. /**
  218. * Determines if a node is surrounded by parentheses.
  219. * @param {SourceCode} sourceCode The ESLint source code object
  220. * @param {ASTNode} node The node to be checked.
  221. * @returns {boolean} True if the node is parenthesised.
  222. * @private
  223. */
  224. function isParenthesised(sourceCode, node) {
  225. const previousToken = sourceCode.getTokenBefore(node),
  226. nextToken = sourceCode.getTokenAfter(node);
  227. return Boolean(previousToken && nextToken) &&
  228. previousToken.value === "(" && previousToken.range[1] <= node.range[0] &&
  229. nextToken.value === ")" && nextToken.range[0] >= node.range[1];
  230. }
  231. /**
  232. * Checks if the given token is an arrow token or not.
  233. *
  234. * @param {Token} token - The token to check.
  235. * @returns {boolean} `true` if the token is an arrow token.
  236. */
  237. function isArrowToken(token) {
  238. return token.value === "=>" && token.type === "Punctuator";
  239. }
  240. /**
  241. * Checks if the given token is a comma token or not.
  242. *
  243. * @param {Token} token - The token to check.
  244. * @returns {boolean} `true` if the token is a comma token.
  245. */
  246. function isCommaToken(token) {
  247. return token.value === "," && token.type === "Punctuator";
  248. }
  249. /**
  250. * Checks if the given token is a semicolon token or not.
  251. *
  252. * @param {Token} token - The token to check.
  253. * @returns {boolean} `true` if the token is a semicolon token.
  254. */
  255. function isSemicolonToken(token) {
  256. return token.value === ";" && token.type === "Punctuator";
  257. }
  258. /**
  259. * Checks if the given token is a colon token or not.
  260. *
  261. * @param {Token} token - The token to check.
  262. * @returns {boolean} `true` if the token is a colon token.
  263. */
  264. function isColonToken(token) {
  265. return token.value === ":" && token.type === "Punctuator";
  266. }
  267. /**
  268. * Checks if the given token is an opening parenthesis token or not.
  269. *
  270. * @param {Token} token - The token to check.
  271. * @returns {boolean} `true` if the token is an opening parenthesis token.
  272. */
  273. function isOpeningParenToken(token) {
  274. return token.value === "(" && token.type === "Punctuator";
  275. }
  276. /**
  277. * Checks if the given token is a closing parenthesis token or not.
  278. *
  279. * @param {Token} token - The token to check.
  280. * @returns {boolean} `true` if the token is a closing parenthesis token.
  281. */
  282. function isClosingParenToken(token) {
  283. return token.value === ")" && token.type === "Punctuator";
  284. }
  285. /**
  286. * Checks if the given token is an opening square bracket token or not.
  287. *
  288. * @param {Token} token - The token to check.
  289. * @returns {boolean} `true` if the token is an opening square bracket token.
  290. */
  291. function isOpeningBracketToken(token) {
  292. return token.value === "[" && token.type === "Punctuator";
  293. }
  294. /**
  295. * Checks if the given token is a closing square bracket token or not.
  296. *
  297. * @param {Token} token - The token to check.
  298. * @returns {boolean} `true` if the token is a closing square bracket token.
  299. */
  300. function isClosingBracketToken(token) {
  301. return token.value === "]" && token.type === "Punctuator";
  302. }
  303. /**
  304. * Checks if the given token is an opening brace token or not.
  305. *
  306. * @param {Token} token - The token to check.
  307. * @returns {boolean} `true` if the token is an opening brace token.
  308. */
  309. function isOpeningBraceToken(token) {
  310. return token.value === "{" && token.type === "Punctuator";
  311. }
  312. /**
  313. * Checks if the given token is a closing brace token or not.
  314. *
  315. * @param {Token} token - The token to check.
  316. * @returns {boolean} `true` if the token is a closing brace token.
  317. */
  318. function isClosingBraceToken(token) {
  319. return token.value === "}" && token.type === "Punctuator";
  320. }
  321. /**
  322. * Checks if the given token is a comment token or not.
  323. *
  324. * @param {Token} token - The token to check.
  325. * @returns {boolean} `true` if the token is a comment token.
  326. */
  327. function isCommentToken(token) {
  328. return token.type === "Line" || token.type === "Block" || token.type === "Shebang";
  329. }
  330. /**
  331. * Checks if the given token is a keyword token or not.
  332. *
  333. * @param {Token} token - The token to check.
  334. * @returns {boolean} `true` if the token is a keyword token.
  335. */
  336. function isKeywordToken(token) {
  337. return token.type === "Keyword";
  338. }
  339. /**
  340. * Gets the `(` token of the given function node.
  341. *
  342. * @param {ASTNode} node - The function node to get.
  343. * @param {SourceCode} sourceCode - The source code object to get tokens.
  344. * @returns {Token} `(` token.
  345. */
  346. function getOpeningParenOfParams(node, sourceCode) {
  347. return node.id
  348. ? sourceCode.getTokenAfter(node.id, isOpeningParenToken)
  349. : sourceCode.getFirstToken(node, isOpeningParenToken);
  350. }
  351. /**
  352. * Creates a version of the LINEBREAK_MATCHER regex with the global flag.
  353. * Global regexes are mutable, so this needs to be a function instead of a constant.
  354. * @returns {RegExp} A global regular expression that matches line terminators
  355. */
  356. function createGlobalLinebreakMatcher() {
  357. return new RegExp(LINEBREAK_MATCHER.source, "g");
  358. }
  359. /**
  360. * Checks whether or not the tokens of two given nodes are same.
  361. * @param {ASTNode} left - A node 1 to compare.
  362. * @param {ASTNode} right - A node 2 to compare.
  363. * @param {SourceCode} sourceCode - The ESLint source code object.
  364. * @returns {boolean} the source code for the given node.
  365. */
  366. function equalTokens(left, right, sourceCode) {
  367. const tokensL = sourceCode.getTokens(left);
  368. const tokensR = sourceCode.getTokens(right);
  369. if (tokensL.length !== tokensR.length) {
  370. return false;
  371. }
  372. for (let i = 0; i < tokensL.length; ++i) {
  373. if (tokensL[i].type !== tokensR[i].type ||
  374. tokensL[i].value !== tokensR[i].value
  375. ) {
  376. return false;
  377. }
  378. }
  379. return true;
  380. }
  381. //------------------------------------------------------------------------------
  382. // Public Interface
  383. //------------------------------------------------------------------------------
  384. module.exports = {
  385. COMMENTS_IGNORE_PATTERN,
  386. LINEBREAKS,
  387. LINEBREAK_MATCHER,
  388. SHEBANG_MATCHER,
  389. STATEMENT_LIST_PARENTS,
  390. /**
  391. * Determines whether two adjacent tokens are on the same line.
  392. * @param {Object} left - The left token object.
  393. * @param {Object} right - The right token object.
  394. * @returns {boolean} Whether or not the tokens are on the same line.
  395. * @public
  396. */
  397. isTokenOnSameLine(left, right) {
  398. return left.loc.end.line === right.loc.start.line;
  399. },
  400. isNullOrUndefined,
  401. isCallee,
  402. isES5Constructor,
  403. getUpperFunction,
  404. isFunction,
  405. isLoop,
  406. isInLoop,
  407. isArrayFromMethod,
  408. isParenthesised,
  409. createGlobalLinebreakMatcher,
  410. equalTokens,
  411. isArrowToken,
  412. isClosingBraceToken,
  413. isClosingBracketToken,
  414. isClosingParenToken,
  415. isColonToken,
  416. isCommaToken,
  417. isCommentToken,
  418. isKeywordToken,
  419. isNotClosingBraceToken: negate(isClosingBraceToken),
  420. isNotClosingBracketToken: negate(isClosingBracketToken),
  421. isNotClosingParenToken: negate(isClosingParenToken),
  422. isNotColonToken: negate(isColonToken),
  423. isNotCommaToken: negate(isCommaToken),
  424. isNotOpeningBraceToken: negate(isOpeningBraceToken),
  425. isNotOpeningBracketToken: negate(isOpeningBracketToken),
  426. isNotOpeningParenToken: negate(isOpeningParenToken),
  427. isNotSemicolonToken: negate(isSemicolonToken),
  428. isOpeningBraceToken,
  429. isOpeningBracketToken,
  430. isOpeningParenToken,
  431. isSemicolonToken,
  432. /**
  433. * Checks whether or not a given node is a string literal.
  434. * @param {ASTNode} node - A node to check.
  435. * @returns {boolean} `true` if the node is a string literal.
  436. */
  437. isStringLiteral(node) {
  438. return (
  439. (node.type === "Literal" && typeof node.value === "string") ||
  440. node.type === "TemplateLiteral"
  441. );
  442. },
  443. /**
  444. * Checks whether a given node is a breakable statement or not.
  445. * The node is breakable if the node is one of the following type:
  446. *
  447. * - DoWhileStatement
  448. * - ForInStatement
  449. * - ForOfStatement
  450. * - ForStatement
  451. * - SwitchStatement
  452. * - WhileStatement
  453. *
  454. * @param {ASTNode} node - A node to check.
  455. * @returns {boolean} `true` if the node is breakable.
  456. */
  457. isBreakableStatement(node) {
  458. return breakableTypePattern.test(node.type);
  459. },
  460. /**
  461. * Gets the label if the parent node of a given node is a LabeledStatement.
  462. *
  463. * @param {ASTNode} node - A node to get.
  464. * @returns {string|null} The label or `null`.
  465. */
  466. getLabel(node) {
  467. if (node.parent.type === "LabeledStatement") {
  468. return node.parent.label.name;
  469. }
  470. return null;
  471. },
  472. /**
  473. * Gets references which are non initializer and writable.
  474. * @param {Reference[]} references - An array of references.
  475. * @returns {Reference[]} An array of only references which are non initializer and writable.
  476. * @public
  477. */
  478. getModifyingReferences(references) {
  479. return references.filter(isModifyingReference);
  480. },
  481. /**
  482. * Validate that a string passed in is surrounded by the specified character
  483. * @param {string} val The text to check.
  484. * @param {string} character The character to see if it's surrounded by.
  485. * @returns {boolean} True if the text is surrounded by the character, false if not.
  486. * @private
  487. */
  488. isSurroundedBy(val, character) {
  489. return val[0] === character && val[val.length - 1] === character;
  490. },
  491. /**
  492. * Returns whether the provided node is an ESLint directive comment or not
  493. * @param {Line|Block} node The comment token to be checked
  494. * @returns {boolean} `true` if the node is an ESLint directive comment
  495. */
  496. isDirectiveComment(node) {
  497. const comment = node.value.trim();
  498. return (
  499. node.type === "Line" && comment.indexOf("eslint-") === 0 ||
  500. node.type === "Block" && (
  501. comment.indexOf("global ") === 0 ||
  502. comment.indexOf("eslint ") === 0 ||
  503. comment.indexOf("eslint-") === 0
  504. )
  505. );
  506. },
  507. /**
  508. * Gets the trailing statement of a given node.
  509. *
  510. * if (code)
  511. * consequent;
  512. *
  513. * When taking this `IfStatement`, returns `consequent;` statement.
  514. *
  515. * @param {ASTNode} A node to get.
  516. * @returns {ASTNode|null} The trailing statement's node.
  517. */
  518. getTrailingStatement: esutils.ast.trailingStatement,
  519. /**
  520. * Finds the variable by a given name in a given scope and its upper scopes.
  521. *
  522. * @param {eslint-scope.Scope} initScope - A scope to start find.
  523. * @param {string} name - A variable name to find.
  524. * @returns {eslint-scope.Variable|null} A found variable or `null`.
  525. */
  526. getVariableByName(initScope, name) {
  527. let scope = initScope;
  528. while (scope) {
  529. const variable = scope.set.get(name);
  530. if (variable) {
  531. return variable;
  532. }
  533. scope = scope.upper;
  534. }
  535. return null;
  536. },
  537. /**
  538. * Checks whether or not a given function node is the default `this` binding.
  539. *
  540. * First, this checks the node:
  541. *
  542. * - The function name does not start with uppercase (it's a constructor).
  543. * - The function does not have a JSDoc comment that has a @this tag.
  544. *
  545. * Next, this checks the location of the node.
  546. * If the location is below, this judges `this` is valid.
  547. *
  548. * - The location is not on an object literal.
  549. * - The location is not assigned to a variable which starts with an uppercase letter.
  550. * - The location is not on an ES2015 class.
  551. * - Its `bind`/`call`/`apply` method is not called directly.
  552. * - The function is not a callback of array methods (such as `.forEach()`) if `thisArg` is given.
  553. *
  554. * @param {ASTNode} node - A function node to check.
  555. * @param {SourceCode} sourceCode - A SourceCode instance to get comments.
  556. * @returns {boolean} The function node is the default `this` binding.
  557. */
  558. isDefaultThisBinding(node, sourceCode) {
  559. if (isES5Constructor(node) || hasJSDocThisTag(node, sourceCode)) {
  560. return false;
  561. }
  562. const isAnonymous = node.id === null;
  563. let currentNode = node;
  564. while (currentNode) {
  565. const parent = currentNode.parent;
  566. switch (parent.type) {
  567. /*
  568. * Looks up the destination.
  569. * e.g., obj.foo = nativeFoo || function foo() { ... };
  570. */
  571. case "LogicalExpression":
  572. case "ConditionalExpression":
  573. currentNode = parent;
  574. break;
  575. /*
  576. * If the upper function is IIFE, checks the destination of the return value.
  577. * e.g.
  578. * obj.foo = (function() {
  579. * // setup...
  580. * return function foo() { ... };
  581. * })();
  582. * obj.foo = (() =>
  583. * function foo() { ... }
  584. * )();
  585. */
  586. case "ReturnStatement": {
  587. const func = getUpperFunction(parent);
  588. if (func === null || !isCallee(func)) {
  589. return true;
  590. }
  591. currentNode = func.parent;
  592. break;
  593. }
  594. case "ArrowFunctionExpression":
  595. if (currentNode !== parent.body || !isCallee(parent)) {
  596. return true;
  597. }
  598. currentNode = parent.parent;
  599. break;
  600. /*
  601. * e.g.
  602. * var obj = { foo() { ... } };
  603. * var obj = { foo: function() { ... } };
  604. * class A { constructor() { ... } }
  605. * class A { foo() { ... } }
  606. * class A { get foo() { ... } }
  607. * class A { set foo() { ... } }
  608. * class A { static foo() { ... } }
  609. */
  610. case "Property":
  611. case "MethodDefinition":
  612. return parent.value !== currentNode;
  613. /*
  614. * e.g.
  615. * obj.foo = function foo() { ... };
  616. * Foo = function() { ... };
  617. * [obj.foo = function foo() { ... }] = a;
  618. * [Foo = function() { ... }] = a;
  619. */
  620. case "AssignmentExpression":
  621. case "AssignmentPattern":
  622. if (parent.left.type === "MemberExpression") {
  623. return false;
  624. }
  625. if (
  626. isAnonymous &&
  627. parent.left.type === "Identifier" &&
  628. startsWithUpperCase(parent.left.name)
  629. ) {
  630. return false;
  631. }
  632. return true;
  633. /*
  634. * e.g.
  635. * var Foo = function() { ... };
  636. */
  637. case "VariableDeclarator":
  638. return !(
  639. isAnonymous &&
  640. parent.init === currentNode &&
  641. parent.id.type === "Identifier" &&
  642. startsWithUpperCase(parent.id.name)
  643. );
  644. /*
  645. * e.g.
  646. * var foo = function foo() { ... }.bind(obj);
  647. * (function foo() { ... }).call(obj);
  648. * (function foo() { ... }).apply(obj, []);
  649. */
  650. case "MemberExpression":
  651. return (
  652. parent.object !== currentNode ||
  653. parent.property.type !== "Identifier" ||
  654. !bindOrCallOrApplyPattern.test(parent.property.name) ||
  655. !isCallee(parent) ||
  656. parent.parent.arguments.length === 0 ||
  657. isNullOrUndefined(parent.parent.arguments[0])
  658. );
  659. /*
  660. * e.g.
  661. * Reflect.apply(function() {}, obj, []);
  662. * Array.from([], function() {}, obj);
  663. * list.forEach(function() {}, obj);
  664. */
  665. case "CallExpression":
  666. if (isReflectApply(parent.callee)) {
  667. return (
  668. parent.arguments.length !== 3 ||
  669. parent.arguments[0] !== currentNode ||
  670. isNullOrUndefined(parent.arguments[1])
  671. );
  672. }
  673. if (isArrayFromMethod(parent.callee)) {
  674. return (
  675. parent.arguments.length !== 3 ||
  676. parent.arguments[1] !== currentNode ||
  677. isNullOrUndefined(parent.arguments[2])
  678. );
  679. }
  680. if (isMethodWhichHasThisArg(parent.callee)) {
  681. return (
  682. parent.arguments.length !== 2 ||
  683. parent.arguments[0] !== currentNode ||
  684. isNullOrUndefined(parent.arguments[1])
  685. );
  686. }
  687. return true;
  688. // Otherwise `this` is default.
  689. default:
  690. return true;
  691. }
  692. }
  693. /* istanbul ignore next */
  694. return true;
  695. },
  696. /**
  697. * Get the precedence level based on the node type
  698. * @param {ASTNode} node node to evaluate
  699. * @returns {int} precedence level
  700. * @private
  701. */
  702. getPrecedence(node) {
  703. switch (node.type) {
  704. case "SequenceExpression":
  705. return 0;
  706. case "AssignmentExpression":
  707. case "ArrowFunctionExpression":
  708. case "YieldExpression":
  709. return 1;
  710. case "ConditionalExpression":
  711. return 3;
  712. case "LogicalExpression":
  713. switch (node.operator) {
  714. case "||":
  715. return 4;
  716. case "&&":
  717. return 5;
  718. // no default
  719. }
  720. /* falls through */
  721. case "BinaryExpression":
  722. switch (node.operator) {
  723. case "|":
  724. return 6;
  725. case "^":
  726. return 7;
  727. case "&":
  728. return 8;
  729. case "==":
  730. case "!=":
  731. case "===":
  732. case "!==":
  733. return 9;
  734. case "<":
  735. case "<=":
  736. case ">":
  737. case ">=":
  738. case "in":
  739. case "instanceof":
  740. return 10;
  741. case "<<":
  742. case ">>":
  743. case ">>>":
  744. return 11;
  745. case "+":
  746. case "-":
  747. return 12;
  748. case "*":
  749. case "/":
  750. case "%":
  751. return 13;
  752. case "**":
  753. return 15;
  754. // no default
  755. }
  756. /* falls through */
  757. case "UnaryExpression":
  758. case "AwaitExpression":
  759. return 16;
  760. case "UpdateExpression":
  761. return 17;
  762. case "CallExpression":
  763. return 18;
  764. case "NewExpression":
  765. return 19;
  766. default:
  767. return 20;
  768. }
  769. },
  770. /**
  771. * Checks whether the given node is an empty block node or not.
  772. *
  773. * @param {ASTNode|null} node - The node to check.
  774. * @returns {boolean} `true` if the node is an empty block.
  775. */
  776. isEmptyBlock(node) {
  777. return Boolean(node && node.type === "BlockStatement" && node.body.length === 0);
  778. },
  779. /**
  780. * Checks whether the given node is an empty function node or not.
  781. *
  782. * @param {ASTNode|null} node - The node to check.
  783. * @returns {boolean} `true` if the node is an empty function.
  784. */
  785. isEmptyFunction(node) {
  786. return isFunction(node) && module.exports.isEmptyBlock(node.body);
  787. },
  788. /**
  789. * Gets the property name of a given node.
  790. * The node can be a MemberExpression, a Property, or a MethodDefinition.
  791. *
  792. * If the name is dynamic, this returns `null`.
  793. *
  794. * For examples:
  795. *
  796. * a.b // => "b"
  797. * a["b"] // => "b"
  798. * a['b'] // => "b"
  799. * a[`b`] // => "b"
  800. * a[100] // => "100"
  801. * a[b] // => null
  802. * a["a" + "b"] // => null
  803. * a[tag`b`] // => null
  804. * a[`${b}`] // => null
  805. *
  806. * let a = {b: 1} // => "b"
  807. * let a = {["b"]: 1} // => "b"
  808. * let a = {['b']: 1} // => "b"
  809. * let a = {[`b`]: 1} // => "b"
  810. * let a = {[100]: 1} // => "100"
  811. * let a = {[b]: 1} // => null
  812. * let a = {["a" + "b"]: 1} // => null
  813. * let a = {[tag`b`]: 1} // => null
  814. * let a = {[`${b}`]: 1} // => null
  815. *
  816. * @param {ASTNode} node - The node to get.
  817. * @returns {string|null} The property name if static. Otherwise, null.
  818. */
  819. getStaticPropertyName(node) {
  820. let prop;
  821. switch (node && node.type) {
  822. case "Property":
  823. case "MethodDefinition":
  824. prop = node.key;
  825. break;
  826. case "MemberExpression":
  827. prop = node.property;
  828. break;
  829. // no default
  830. }
  831. switch (prop && prop.type) {
  832. case "Literal":
  833. return String(prop.value);
  834. case "TemplateLiteral":
  835. if (prop.expressions.length === 0 && prop.quasis.length === 1) {
  836. return prop.quasis[0].value.cooked;
  837. }
  838. break;
  839. case "Identifier":
  840. if (!node.computed) {
  841. return prop.name;
  842. }
  843. break;
  844. // no default
  845. }
  846. return null;
  847. },
  848. /**
  849. * Get directives from directive prologue of a Program or Function node.
  850. * @param {ASTNode} node - The node to check.
  851. * @returns {ASTNode[]} The directives found in the directive prologue.
  852. */
  853. getDirectivePrologue(node) {
  854. const directives = [];
  855. // Directive prologues only occur at the top of files or functions.
  856. if (
  857. node.type === "Program" ||
  858. node.type === "FunctionDeclaration" ||
  859. node.type === "FunctionExpression" ||
  860. /*
  861. * Do not check arrow functions with implicit return.
  862. * `() => "use strict";` returns the string `"use strict"`.
  863. */
  864. (node.type === "ArrowFunctionExpression" && node.body.type === "BlockStatement")
  865. ) {
  866. const statements = node.type === "Program" ? node.body : node.body.body;
  867. for (const statement of statements) {
  868. if (
  869. statement.type === "ExpressionStatement" &&
  870. statement.expression.type === "Literal"
  871. ) {
  872. directives.push(statement);
  873. } else {
  874. break;
  875. }
  876. }
  877. }
  878. return directives;
  879. },
  880. /**
  881. * Determines whether this node is a decimal integer literal. If a node is a decimal integer literal, a dot added
  882. * after the node will be parsed as a decimal point, rather than a property-access dot.
  883. * @param {ASTNode} node - The node to check.
  884. * @returns {boolean} `true` if this node is a decimal integer.
  885. * @example
  886. *
  887. * 5 // true
  888. * 5. // false
  889. * 5.0 // false
  890. * 05 // false
  891. * 0x5 // false
  892. * 0b101 // false
  893. * 0o5 // false
  894. * 5e0 // false
  895. * '5' // false
  896. */
  897. isDecimalInteger(node) {
  898. return node.type === "Literal" && typeof node.value === "number" && /^(0|[1-9]\d*)$/.test(node.raw);
  899. },
  900. /**
  901. * Gets the name and kind of the given function node.
  902. *
  903. * - `function foo() {}` .................... `function 'foo'`
  904. * - `(function foo() {})` .................. `function 'foo'`
  905. * - `(function() {})` ...................... `function`
  906. * - `function* foo() {}` ................... `generator function 'foo'`
  907. * - `(function* foo() {})` ................. `generator function 'foo'`
  908. * - `(function*() {})` ..................... `generator function`
  909. * - `() => {}` ............................. `arrow function`
  910. * - `async () => {}` ....................... `async arrow function`
  911. * - `({ foo: function foo() {} })` ......... `method 'foo'`
  912. * - `({ foo: function() {} })` ............. `method 'foo'`
  913. * - `({ ['foo']: function() {} })` ......... `method 'foo'`
  914. * - `({ [foo]: function() {} })` ........... `method`
  915. * - `({ foo() {} })` ....................... `method 'foo'`
  916. * - `({ foo: function* foo() {} })` ........ `generator method 'foo'`
  917. * - `({ foo: function*() {} })` ............ `generator method 'foo'`
  918. * - `({ ['foo']: function*() {} })` ........ `generator method 'foo'`
  919. * - `({ [foo]: function*() {} })` .......... `generator method`
  920. * - `({ *foo() {} })` ...................... `generator method 'foo'`
  921. * - `({ foo: async function foo() {} })` ... `async method 'foo'`
  922. * - `({ foo: async function() {} })` ....... `async method 'foo'`
  923. * - `({ ['foo']: async function() {} })` ... `async method 'foo'`
  924. * - `({ [foo]: async function() {} })` ..... `async method`
  925. * - `({ async foo() {} })` ................. `async method 'foo'`
  926. * - `({ get foo() {} })` ................... `getter 'foo'`
  927. * - `({ set foo(a) {} })` .................. `setter 'foo'`
  928. * - `class A { constructor() {} }` ......... `constructor`
  929. * - `class A { foo() {} }` ................. `method 'foo'`
  930. * - `class A { *foo() {} }` ................ `generator method 'foo'`
  931. * - `class A { async foo() {} }` ........... `async method 'foo'`
  932. * - `class A { ['foo']() {} }` ............. `method 'foo'`
  933. * - `class A { *['foo']() {} }` ............ `generator method 'foo'`
  934. * - `class A { async ['foo']() {} }` ....... `async method 'foo'`
  935. * - `class A { [foo]() {} }` ............... `method`
  936. * - `class A { *[foo]() {} }` .............. `generator method`
  937. * - `class A { async [foo]() {} }` ......... `async method`
  938. * - `class A { get foo() {} }` ............. `getter 'foo'`
  939. * - `class A { set foo(a) {} }` ............ `setter 'foo'`
  940. * - `class A { static foo() {} }` .......... `static method 'foo'`
  941. * - `class A { static *foo() {} }` ......... `static generator method 'foo'`
  942. * - `class A { static async foo() {} }` .... `static async method 'foo'`
  943. * - `class A { static get foo() {} }` ...... `static getter 'foo'`
  944. * - `class A { static set foo(a) {} }` ..... `static setter 'foo'`
  945. *
  946. * @param {ASTNode} node - The function node to get.
  947. * @returns {string} The name and kind of the function node.
  948. */
  949. getFunctionNameWithKind(node) {
  950. const parent = node.parent;
  951. const tokens = [];
  952. if (parent.type === "MethodDefinition" && parent.static) {
  953. tokens.push("static");
  954. }
  955. if (node.async) {
  956. tokens.push("async");
  957. }
  958. if (node.generator) {
  959. tokens.push("generator");
  960. }
  961. if (node.type === "ArrowFunctionExpression") {
  962. tokens.push("arrow", "function");
  963. } else if (parent.type === "Property" || parent.type === "MethodDefinition") {
  964. if (parent.kind === "constructor") {
  965. return "constructor";
  966. }
  967. if (parent.kind === "get") {
  968. tokens.push("getter");
  969. } else if (parent.kind === "set") {
  970. tokens.push("setter");
  971. } else {
  972. tokens.push("method");
  973. }
  974. } else {
  975. tokens.push("function");
  976. }
  977. if (node.id) {
  978. tokens.push(`'${node.id.name}'`);
  979. } else {
  980. const name = module.exports.getStaticPropertyName(parent);
  981. if (name) {
  982. tokens.push(`'${name}'`);
  983. }
  984. }
  985. return tokens.join(" ");
  986. },
  987. /**
  988. * Gets the location of the given function node for reporting.
  989. *
  990. * - `function foo() {}`
  991. * ^^^^^^^^^^^^
  992. * - `(function foo() {})`
  993. * ^^^^^^^^^^^^
  994. * - `(function() {})`
  995. * ^^^^^^^^
  996. * - `function* foo() {}`
  997. * ^^^^^^^^^^^^^
  998. * - `(function* foo() {})`
  999. * ^^^^^^^^^^^^^
  1000. * - `(function*() {})`
  1001. * ^^^^^^^^^
  1002. * - `() => {}`
  1003. * ^^
  1004. * - `async () => {}`
  1005. * ^^
  1006. * - `({ foo: function foo() {} })`
  1007. * ^^^^^^^^^^^^^^^^^
  1008. * - `({ foo: function() {} })`
  1009. * ^^^^^^^^^^^^^
  1010. * - `({ ['foo']: function() {} })`
  1011. * ^^^^^^^^^^^^^^^^^
  1012. * - `({ [foo]: function() {} })`
  1013. * ^^^^^^^^^^^^^^^
  1014. * - `({ foo() {} })`
  1015. * ^^^
  1016. * - `({ foo: function* foo() {} })`
  1017. * ^^^^^^^^^^^^^^^^^^
  1018. * - `({ foo: function*() {} })`
  1019. * ^^^^^^^^^^^^^^
  1020. * - `({ ['foo']: function*() {} })`
  1021. * ^^^^^^^^^^^^^^^^^^
  1022. * - `({ [foo]: function*() {} })`
  1023. * ^^^^^^^^^^^^^^^^
  1024. * - `({ *foo() {} })`
  1025. * ^^^^
  1026. * - `({ foo: async function foo() {} })`
  1027. * ^^^^^^^^^^^^^^^^^^^^^^^
  1028. * - `({ foo: async function() {} })`
  1029. * ^^^^^^^^^^^^^^^^^^^
  1030. * - `({ ['foo']: async function() {} })`
  1031. * ^^^^^^^^^^^^^^^^^^^^^^^
  1032. * - `({ [foo]: async function() {} })`
  1033. * ^^^^^^^^^^^^^^^^^^^^^
  1034. * - `({ async foo() {} })`
  1035. * ^^^^^^^^^
  1036. * - `({ get foo() {} })`
  1037. * ^^^^^^^
  1038. * - `({ set foo(a) {} })`
  1039. * ^^^^^^^
  1040. * - `class A { constructor() {} }`
  1041. * ^^^^^^^^^^^
  1042. * - `class A { foo() {} }`
  1043. * ^^^
  1044. * - `class A { *foo() {} }`
  1045. * ^^^^
  1046. * - `class A { async foo() {} }`
  1047. * ^^^^^^^^^
  1048. * - `class A { ['foo']() {} }`
  1049. * ^^^^^^^
  1050. * - `class A { *['foo']() {} }`
  1051. * ^^^^^^^^
  1052. * - `class A { async ['foo']() {} }`
  1053. * ^^^^^^^^^^^^^
  1054. * - `class A { [foo]() {} }`
  1055. * ^^^^^
  1056. * - `class A { *[foo]() {} }`
  1057. * ^^^^^^
  1058. * - `class A { async [foo]() {} }`
  1059. * ^^^^^^^^^^^
  1060. * - `class A { get foo() {} }`
  1061. * ^^^^^^^
  1062. * - `class A { set foo(a) {} }`
  1063. * ^^^^^^^
  1064. * - `class A { static foo() {} }`
  1065. * ^^^^^^^^^^
  1066. * - `class A { static *foo() {} }`
  1067. * ^^^^^^^^^^^
  1068. * - `class A { static async foo() {} }`
  1069. * ^^^^^^^^^^^^^^^^
  1070. * - `class A { static get foo() {} }`
  1071. * ^^^^^^^^^^^^^^
  1072. * - `class A { static set foo(a) {} }`
  1073. * ^^^^^^^^^^^^^^
  1074. *
  1075. * @param {ASTNode} node - The function node to get.
  1076. * @param {SourceCode} sourceCode - The source code object to get tokens.
  1077. * @returns {string} The location of the function node for reporting.
  1078. */
  1079. getFunctionHeadLoc(node, sourceCode) {
  1080. const parent = node.parent;
  1081. let start = null;
  1082. let end = null;
  1083. if (node.type === "ArrowFunctionExpression") {
  1084. const arrowToken = sourceCode.getTokenBefore(node.body, isArrowToken);
  1085. start = arrowToken.loc.start;
  1086. end = arrowToken.loc.end;
  1087. } else if (parent.type === "Property" || parent.type === "MethodDefinition") {
  1088. start = parent.loc.start;
  1089. end = getOpeningParenOfParams(node, sourceCode).loc.start;
  1090. } else {
  1091. start = node.loc.start;
  1092. end = getOpeningParenOfParams(node, sourceCode).loc.start;
  1093. }
  1094. return {
  1095. start: Object.assign({}, start),
  1096. end: Object.assign({}, end)
  1097. };
  1098. },
  1099. /**
  1100. * Gets the parenthesized text of a node. This is similar to sourceCode.getText(node), but it also includes any parentheses
  1101. * surrounding the node.
  1102. * @param {SourceCode} sourceCode The source code object
  1103. * @param {ASTNode} node An expression node
  1104. * @returns {string} The text representing the node, with all surrounding parentheses included
  1105. */
  1106. getParenthesisedText(sourceCode, node) {
  1107. let leftToken = sourceCode.getFirstToken(node);
  1108. let rightToken = sourceCode.getLastToken(node);
  1109. while (
  1110. sourceCode.getTokenBefore(leftToken) &&
  1111. sourceCode.getTokenBefore(leftToken).type === "Punctuator" &&
  1112. sourceCode.getTokenBefore(leftToken).value === "(" &&
  1113. sourceCode.getTokenAfter(rightToken) &&
  1114. sourceCode.getTokenAfter(rightToken).type === "Punctuator" &&
  1115. sourceCode.getTokenAfter(rightToken).value === ")"
  1116. ) {
  1117. leftToken = sourceCode.getTokenBefore(leftToken);
  1118. rightToken = sourceCode.getTokenAfter(rightToken);
  1119. }
  1120. return sourceCode.getText().slice(leftToken.range[0], rightToken.range[1]);
  1121. },
  1122. /*
  1123. * Determine if a node has a possiblity to be an Error object
  1124. * @param {ASTNode} node ASTNode to check
  1125. * @returns {boolean} True if there is a chance it contains an Error obj
  1126. */
  1127. couldBeError(node) {
  1128. switch (node.type) {
  1129. case "Identifier":
  1130. case "CallExpression":
  1131. case "NewExpression":
  1132. case "MemberExpression":
  1133. case "TaggedTemplateExpression":
  1134. case "YieldExpression":
  1135. case "AwaitExpression":
  1136. return true; // possibly an error object.
  1137. case "AssignmentExpression":
  1138. return module.exports.couldBeError(node.right);
  1139. case "SequenceExpression": {
  1140. const exprs = node.expressions;
  1141. return exprs.length !== 0 && module.exports.couldBeError(exprs[exprs.length - 1]);
  1142. }
  1143. case "LogicalExpression":
  1144. return module.exports.couldBeError(node.left) || module.exports.couldBeError(node.right);
  1145. case "ConditionalExpression":
  1146. return module.exports.couldBeError(node.consequent) || module.exports.couldBeError(node.alternate);
  1147. default:
  1148. return false;
  1149. }
  1150. },
  1151. /**
  1152. * Determines whether the given node is a `null` literal.
  1153. * @param {ASTNode} node The node to check
  1154. * @returns {boolean} `true` if the node is a `null` literal
  1155. */
  1156. isNullLiteral(node) {
  1157. /*
  1158. * Checking `node.value === null` does not guarantee that a literal is a null literal.
  1159. * When parsing values that cannot be represented in the current environment (e.g. unicode
  1160. * regexes in Node 4), `node.value` is set to `null` because it wouldn't be possible to
  1161. * set `node.value` to a unicode regex. To make sure a literal is actually `null`, check
  1162. * `node.regex` instead. Also see: https://github.com/eslint/eslint/issues/8020
  1163. */
  1164. return node.type === "Literal" && node.value === null && !node.regex;
  1165. },
  1166. /**
  1167. * Determines whether two tokens can safely be placed next to each other without merging into a single token
  1168. * @param {Token|string} leftValue The left token. If this is a string, it will be tokenized and the last token will be used.
  1169. * @param {Token|string} rightValue The right token. If this is a string, it will be tokenized and the first token will be used.
  1170. * @returns {boolean} If the tokens cannot be safely placed next to each other, returns `false`. If the tokens can be placed
  1171. * next to each other, behavior is undefined (although it should return `true` in most cases).
  1172. */
  1173. canTokensBeAdjacent(leftValue, rightValue) {
  1174. let leftToken;
  1175. if (typeof leftValue === "string") {
  1176. const leftTokens = espree.tokenize(leftValue, { ecmaVersion: 2015 });
  1177. leftToken = leftTokens[leftTokens.length - 1];
  1178. } else {
  1179. leftToken = leftValue;
  1180. }
  1181. const rightToken = typeof rightValue === "string" ? espree.tokenize(rightValue, { ecmaVersion: 2015 })[0] : rightValue;
  1182. if (leftToken.type === "Punctuator" || rightToken.type === "Punctuator") {
  1183. if (leftToken.type === "Punctuator" && rightToken.type === "Punctuator") {
  1184. const PLUS_TOKENS = new Set(["+", "++"]);
  1185. const MINUS_TOKENS = new Set(["-", "--"]);
  1186. return !(
  1187. PLUS_TOKENS.has(leftToken.value) && PLUS_TOKENS.has(rightToken.value) ||
  1188. MINUS_TOKENS.has(leftToken.value) && MINUS_TOKENS.has(rightToken.value)
  1189. );
  1190. }
  1191. return true;
  1192. }
  1193. if (
  1194. leftToken.type === "String" || rightToken.type === "String" ||
  1195. leftToken.type === "Template" || rightToken.type === "Template"
  1196. ) {
  1197. return true;
  1198. }
  1199. if (leftToken.type !== "Numeric" && rightToken.type === "Numeric" && rightToken.value.startsWith(".")) {
  1200. return true;
  1201. }
  1202. return false;
  1203. }
  1204. };