44import { parse , AST as VAST } from 'vue-eslint-parser'
55import type { AST as JSONAST } from 'jsonc-eslint-parser'
66import { parseJSON , getStaticJSONValue } from 'jsonc-eslint-parser'
7+ import type { StaticLiteral } from '../utils/index'
78import {
9+ getStaticLiteralValue ,
10+ isStaticLiteral ,
811 defineTemplateBodyVisitor ,
912 getLocaleMessages ,
1013 getStaticAttributes ,
@@ -28,11 +31,7 @@ import { createRule } from '../utils/rule'
2831import { toRegExp } from '../utils/regexp'
2932
3033type 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
3635type NodeScope = 'template' | 'template-option' | 'jsx'
3736type TargetAttrs = { name : RegExp ; attrs : Set < string > }
3837type Config = {
@@ -76,24 +75,8 @@ function getTargetAttrs(tagName: string, config: Config): Set<string> {
7675 return new Set ( result )
7776}
7877
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- }
8978function 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 ,
9780 base : TemplateOptionValueNode | null
9881) : Range {
9982 const range = node . range
@@ -104,12 +87,7 @@ function calculateRange(
10487 return [ offset + range [ 0 ] , offset + range [ 1 ] ]
10588}
10689function calculateLoc (
107- node :
108- | VAST . ESLintLiteral
109- | StaticTemplateLiteral
110- | VAST . VText
111- | JSXText
112- | VAST . VLiteral ,
90+ node : StaticLiteral | VAST . VText | JSXText | VAST . VLiteral ,
11391 base : TemplateOptionValueNode | null ,
11492 context : RuleContext
11593) {
@@ -209,16 +187,12 @@ function checkExpressionContainerText(
209187 baseNode : TemplateOptionValueNode | null ,
210188 scope : NodeScope
211189) {
212- if ( expression . type === 'Literal' ) {
213- checkLiteral ( context , expression , config , baseNode , scope )
214- } else if ( isStaticTemplateLiteral ( expression ) ) {
190+ if ( isStaticLiteral ( expression ) ) {
215191 checkLiteral ( context , expression , config , baseNode , scope )
216192 } else if ( expression . type === 'ConditionalExpression' ) {
217193 const targets = [ expression . consequent , expression . alternate ]
218194 targets . forEach ( target => {
219- if ( target . type === 'Literal' ) {
220- checkLiteral ( context , target , config , baseNode , scope )
221- } else if ( isStaticTemplateLiteral ( target ) ) {
195+ if ( isStaticLiteral ( target ) ) {
222196 checkLiteral ( context , target , config , baseNode , scope )
223197 }
224198 } )
@@ -227,15 +201,12 @@ function checkExpressionContainerText(
227201
228202function checkLiteral (
229203 context : RuleContext ,
230- literal : VAST . ESLintLiteral | StaticTemplateLiteral ,
204+ literal : StaticLiteral ,
231205 config : Config ,
232206 baseNode : TemplateOptionValueNode | null ,
233207 scope : NodeScope
234208) {
235- const value =
236- literal . type !== 'TemplateLiteral'
237- ? literal . value
238- : literal . quasis [ 0 ] . value . cooked
209+ const value = getStaticLiteralValue ( literal )
239210
240211 if ( testValue ( value , config ) ) {
241212 return
@@ -465,9 +436,7 @@ function getComponentTemplateValueNode(
465436 )
466437
467438 if ( templateNode ) {
468- if ( templateNode . value . type === 'Literal' ) {
469- return templateNode . value
470- } else if ( isStaticTemplateLiteral ( templateNode . value ) ) {
439+ if ( isStaticLiteral ( templateNode . value ) ) {
471440 return templateNode . value
472441 } else if ( templateNode . value . type === 'Identifier' ) {
473442 const templateVariable = findVariable (
@@ -478,9 +447,7 @@ function getComponentTemplateValueNode(
478447 const varDeclNode = templateVariable . defs [ 0 ]
479448 . node as VAST . ESLintVariableDeclarator
480449 if ( varDeclNode . init ) {
481- if ( varDeclNode . init . type === 'Literal' ) {
482- return varDeclNode . init
483- } else if ( isStaticTemplateLiteral ( varDeclNode . init ) ) {
450+ if ( isStaticLiteral ( varDeclNode . init ) ) {
484451 return varDeclNode . init
485452 }
486453 }
@@ -492,12 +459,8 @@ function getComponentTemplateValueNode(
492459}
493460
494461function 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 !
501464}
502465
503466function withoutEscape (
@@ -508,10 +471,7 @@ function withoutEscape(
508471 return false
509472 }
510473 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 ) } `
515475 return sourceText === templateText
516476}
517477
0 commit comments