4
4
import { parse , AST as VAST } from 'vue-eslint-parser'
5
5
import type { AST as JSONAST } from 'jsonc-eslint-parser'
6
6
import { parseJSON , getStaticJSONValue } from 'jsonc-eslint-parser'
7
+ import type { StaticLiteral } from '../utils/index'
7
8
import {
9
+ getStaticLiteralValue ,
10
+ isStaticLiteral ,
8
11
defineTemplateBodyVisitor ,
9
12
getLocaleMessages ,
10
13
getStaticAttributes ,
@@ -28,11 +31,7 @@ import { createRule } from '../utils/rule'
28
31
import { toRegExp } from '../utils/regexp'
29
32
30
33
type LiteralValue = VAST . ESLintLiteral [ 'value' ]
31
- type StaticTemplateLiteral = VAST . ESLintTemplateLiteral & {
32
- quasis : [ VAST . ESLintTemplateElement ]
33
- expressions : [ /* empty */ ]
34
- }
35
- type TemplateOptionValueNode = VAST . ESLintLiteral | StaticTemplateLiteral
34
+ type TemplateOptionValueNode = StaticLiteral
36
35
type NodeScope = 'template' | 'template-option' | 'jsx'
37
36
type TargetAttrs = { name : RegExp ; attrs : Set < string > }
38
37
type Config = {
@@ -76,24 +75,8 @@ function getTargetAttrs(tagName: string, config: Config): Set<string> {
76
75
return new Set ( result )
77
76
}
78
77
79
- function isStaticTemplateLiteral (
80
- node :
81
- | VAST . ESLintExpression
82
- | VAST . VExpressionContainer [ 'expression' ]
83
- | VAST . ESLintPattern
84
- ) : node is StaticTemplateLiteral {
85
- return Boolean (
86
- node && node . type === 'TemplateLiteral' && node . expressions . length === 0
87
- )
88
- }
89
78
function calculateRange (
90
- node :
91
- | VAST . ESLintLiteral
92
- | StaticTemplateLiteral
93
- | VAST . VText
94
- | JSXText
95
- | VAST . VLiteral
96
- | VAST . VIdentifier ,
79
+ node : StaticLiteral | VAST . VText | JSXText | VAST . VLiteral | VAST . VIdentifier ,
97
80
base : TemplateOptionValueNode | null
98
81
) : Range {
99
82
const range = node . range
@@ -104,12 +87,7 @@ function calculateRange(
104
87
return [ offset + range [ 0 ] , offset + range [ 1 ] ]
105
88
}
106
89
function calculateLoc (
107
- node :
108
- | VAST . ESLintLiteral
109
- | StaticTemplateLiteral
110
- | VAST . VText
111
- | JSXText
112
- | VAST . VLiteral ,
90
+ node : StaticLiteral | VAST . VText | JSXText | VAST . VLiteral ,
113
91
base : TemplateOptionValueNode | null ,
114
92
context : RuleContext
115
93
) {
@@ -209,16 +187,12 @@ function checkExpressionContainerText(
209
187
baseNode : TemplateOptionValueNode | null ,
210
188
scope : NodeScope
211
189
) {
212
- if ( expression . type === 'Literal' ) {
213
- checkLiteral ( context , expression , config , baseNode , scope )
214
- } else if ( isStaticTemplateLiteral ( expression ) ) {
190
+ if ( isStaticLiteral ( expression ) ) {
215
191
checkLiteral ( context , expression , config , baseNode , scope )
216
192
} else if ( expression . type === 'ConditionalExpression' ) {
217
193
const targets = [ expression . consequent , expression . alternate ]
218
194
targets . forEach ( target => {
219
- if ( target . type === 'Literal' ) {
220
- checkLiteral ( context , target , config , baseNode , scope )
221
- } else if ( isStaticTemplateLiteral ( target ) ) {
195
+ if ( isStaticLiteral ( target ) ) {
222
196
checkLiteral ( context , target , config , baseNode , scope )
223
197
}
224
198
} )
@@ -227,15 +201,12 @@ function checkExpressionContainerText(
227
201
228
202
function checkLiteral (
229
203
context : RuleContext ,
230
- literal : VAST . ESLintLiteral | StaticTemplateLiteral ,
204
+ literal : StaticLiteral ,
231
205
config : Config ,
232
206
baseNode : TemplateOptionValueNode | null ,
233
207
scope : NodeScope
234
208
) {
235
- const value =
236
- literal . type !== 'TemplateLiteral'
237
- ? literal . value
238
- : literal . quasis [ 0 ] . value . cooked
209
+ const value = getStaticLiteralValue ( literal )
239
210
240
211
if ( testValue ( value , config ) ) {
241
212
return
@@ -465,9 +436,7 @@ function getComponentTemplateValueNode(
465
436
)
466
437
467
438
if ( templateNode ) {
468
- if ( templateNode . value . type === 'Literal' ) {
469
- return templateNode . value
470
- } else if ( isStaticTemplateLiteral ( templateNode . value ) ) {
439
+ if ( isStaticLiteral ( templateNode . value ) ) {
471
440
return templateNode . value
472
441
} else if ( templateNode . value . type === 'Identifier' ) {
473
442
const templateVariable = findVariable (
@@ -478,9 +447,7 @@ function getComponentTemplateValueNode(
478
447
const varDeclNode = templateVariable . defs [ 0 ]
479
448
. node as VAST . ESLintVariableDeclarator
480
449
if ( varDeclNode . init ) {
481
- if ( varDeclNode . init . type === 'Literal' ) {
482
- return varDeclNode . init
483
- } else if ( isStaticTemplateLiteral ( varDeclNode . init ) ) {
450
+ if ( isStaticLiteral ( varDeclNode . init ) ) {
484
451
return varDeclNode . init
485
452
}
486
453
}
@@ -492,12 +459,8 @@ function getComponentTemplateValueNode(
492
459
}
493
460
494
461
function getComponentTemplateNode ( node : TemplateOptionValueNode ) {
495
- return parse (
496
- `<template>${
497
- node . type === 'TemplateLiteral' ? node . quasis [ 0 ] . value . cooked : node . value
498
- } </template>`,
499
- { }
500
- ) . templateBody !
462
+ return parse ( `<template>${ getStaticLiteralValue ( node ) } </template>` , { } )
463
+ . templateBody !
501
464
}
502
465
503
466
function withoutEscape (
@@ -508,10 +471,7 @@ function withoutEscape(
508
471
return false
509
472
}
510
473
const sourceText = context . getSourceCode ( ) . getText ( baseNode ) . slice ( 1 , - 1 )
511
- const templateText =
512
- baseNode . type === 'TemplateLiteral'
513
- ? baseNode . quasis [ 0 ] . value . cooked
514
- : `${ baseNode . value } `
474
+ const templateText = `${ getStaticLiteralValue ( baseNode ) } `
515
475
return sourceText === templateText
516
476
}
517
477
0 commit comments