@@ -4,6 +4,7 @@ import type { Scope, Variable } from '@typescript-eslint/scope-manager';
44import type { AST as SvAST } from 'svelte-eslint-parser' ;
55import * as eslintUtils from '@eslint-community/eslint-utils' ;
66import { voidElements , svgElements , mathmlElements } from './element-types.js' ;
7+ import { ASTSearchHelper } from './ast-search-helper.js' ;
78
89/**
910 * Checks whether or not the tokens of two given nodes are same.
@@ -34,39 +35,37 @@ export function equalTokens(left: ASTNode, right: ASTNode, sourceCode: SourceCod
3435export function getStringIfConstant (
3536 node : TSESTree . Expression | TSESTree . PrivateIdentifier
3637) : string | null {
37- if ( node . type === 'Literal' ) {
38- if ( typeof node . value === 'string' ) return node . value ;
39- } else if ( node . type === 'TemplateLiteral' ) {
40- let str = '' ;
41- const quasis = [ ...node . quasis ] ;
42- const expressions = [ ...node . expressions ] ;
43- let quasi : TSESTree . TemplateElement | undefined , expr : TSESTree . Expression | undefined ;
44- while ( ( quasi = quasis . shift ( ) ) ) {
45- str += quasi . value . cooked ;
46- expr = expressions . shift ( ) ;
47- if ( expr ) {
48- const exprStr = getStringIfConstant ( expr ) ;
49- if ( exprStr == null ) {
50- return null ;
51- }
52- str += exprStr ;
38+ return ASTSearchHelper ( node , {
39+ BinaryExpression : ( node , searchAnotherNode ) => {
40+ if ( node . operator !== '+' ) {
41+ return null ;
5342 }
54- }
55- return str ;
56- } else if ( node . type === 'BinaryExpression' ) {
57- if ( node . operator === '+' ) {
58- const left = getStringIfConstant ( node . left ) ;
59- if ( left == null ) {
43+ const left = searchAnotherNode ( node . left ) ;
44+ if ( left === null ) {
6045 return null ;
6146 }
62- const right = getStringIfConstant ( node . right ) ;
63- if ( right == null ) {
47+ const right = searchAnotherNode ( node . right ) ;
48+ if ( right === null ) {
6449 return null ;
6550 }
6651 return left + right ;
52+ } ,
53+ Literal : ( node ) => ( typeof node . value === 'string' ? node . value : null ) ,
54+ TemplateLiteral : ( node , searchAnotherNode ) => {
55+ let str = '' ;
56+ for ( const quasi of node . quasis ) {
57+ str += quasi . value . cooked ;
58+ if ( node . expressions [ 0 ] ) {
59+ const exprStr = searchAnotherNode ( node . expressions [ 0 ] ) ;
60+ if ( exprStr === null ) {
61+ return null ;
62+ }
63+ str += exprStr ;
64+ }
65+ }
66+ return str ;
6767 }
68- }
69- return null ;
68+ } ) ;
7069}
7170
7271/**
0 commit comments