Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c68cb1a

Browse files
waynzhFloEdelmann
andauthored
feat(no-bare-strings-in-template): allowlist support regex (#2734)
Co-authored-by: Flo Edelmann <git@flo-edelmann.de>
1 parent 654c3cb commit c68cb1a

File tree

3 files changed

+102
-12
lines changed

3 files changed

+102
-12
lines changed

‎docs/rules/no-bare-strings-in-template.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ since: v7.0.0
1212
1313
## :book: Rule Details
1414

15-
This rule disallows the use of bare strings in `<template>`.
15+
This rule disallows the use of bare strings in `<template>`.
1616
In order to be able to internationalize your application, you will need to avoid using plain strings in your templates. Instead, you would need to use a template helper specializing in translation.
1717

1818
This rule was inspired by [no-bare-strings rule in ember-template-lint](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/no-bare-strings.md).
@@ -50,7 +50,7 @@ This rule was inspired by [no-bare-strings rule in ember-template-lint](https://
5050
</eslint-code-block>
5151

5252
:::tip
53-
This rule does not check for string literals, in bindings and mustaches interpolation. This is because it looks like a conscious decision.
53+
This rule does not check for string literals, in bindings and mustaches interpolation. This is because it looks like a conscious decision.
5454
If you want to report these string literals, enable the [vue/no-useless-v-bind] and [vue/no-useless-mustaches] rules and fix the useless string literals.
5555
:::
5656

@@ -72,7 +72,7 @@ If you want to report these string literals, enable the [vue/no-useless-v-bind]
7272
}
7373
```
7474

75-
- `allowlist` ... An array of allowed strings.
75+
- `allowlist` ... An array of allowed strings or regular expression patterns (e.g. `/\d+/` to allow numbers).
7676
- `attributes` ... An object whose keys are tag name or patterns and value is an array of attributes to check for that tag name.
7777
- `directives` ... An array of directive names to check literal value.
7878

‎lib/rules/no-bare-strings-in-template.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,33 @@ module.exports = {
149149
*/
150150
const opts = context.options[0] || {}
151151
/** @type {string[]} */
152-
const allowlist = opts.allowlist || DEFAULT_ALLOWLIST
152+
const rawAllowlist = opts.allowlist || DEFAULT_ALLOWLIST
153153
const attributes = parseTargetAttrs(opts.attributes || DEFAULT_ATTRIBUTES)
154154
const directives = opts.directives || DEFAULT_DIRECTIVES
155155

156-
const allowlistRe = new RegExp(
157-
allowlist
158-
.map((w) => regexp.escape(w))
159-
.sort((a, b) => b.length - a.length)
160-
.join('|'),
161-
'gu'
162-
)
156+
/** @type {string[]} */
157+
const stringAllowlist = []
158+
/** @type {RegExp[]} */
159+
const regexAllowlist = []
160+
161+
for (const item of rawAllowlist) {
162+
if (regexp.isRegExp(item)) {
163+
regexAllowlist.push(regexp.toRegExp(item))
164+
} else {
165+
stringAllowlist.push(item)
166+
}
167+
}
168+
169+
const allowlistRe =
170+
stringAllowlist.length > 0
171+
? new RegExp(
172+
stringAllowlist
173+
.map((w) => regexp.escape(w))
174+
.sort((a, b) => b.length - a.length)
175+
.join('|'),
176+
'gu'
177+
)
178+
: null
163179

164180
/** @type {ElementStack | null} */
165181
let elementStack = null
@@ -168,7 +184,21 @@ module.exports = {
168184
* @param {string} str
169185
*/
170186
function getBareString(str) {
171-
return str.trim().replace(allowlistRe, '').trim()
187+
let result = str.trim()
188+
189+
if (allowlistRe) {
190+
result = result.replace(allowlistRe, '')
191+
}
192+
193+
for (const regex of regexAllowlist) {
194+
const flags = regex.flags.includes('g')
195+
? regex.flags
196+
: `${regex.flags}g`
197+
const globalRegex = new RegExp(regex.source, flags)
198+
result = result.replace(globalRegex, '')
199+
}
200+
201+
return result.trim()
172202
}
173203

174204
/**

‎tests/lib/rules/no-bare-strings-in-template.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,32 @@ tester.run('no-bare-strings-in-template', rule, {
132132
</template>
133133
`,
134134
options: [{ allowlist: ['@@'] }]
135+
},
136+
// regex
137+
{
138+
code: `
139+
<template>
140+
<h1>123 321</h1>
141+
</template>
142+
`,
143+
options: [{ allowlist: [String.raw`/\d+/g`] }]
144+
},
145+
{
146+
code: `
147+
<template>
148+
<h1>$foo</h1>
149+
<h1>$bar</h1>
150+
</template>
151+
`,
152+
options: [{ allowlist: [String.raw`/\$\w+/`] }]
153+
},
154+
{
155+
code: `
156+
<template>
157+
<h1>foo123foo</h1>
158+
</template>
159+
`,
160+
options: [{ allowlist: [String.raw`/\d+/`, 'foo'] }]
135161
}
136162
],
137163
invalid: [
@@ -316,6 +342,40 @@ tester.run('no-bare-strings-in-template', rule, {
316342
endColumn: 34
317343
}
318344
]
345+
},
346+
{
347+
code: `
348+
<template>
349+
<h1>123, foo is invalid, 321</h1>
350+
</template>
351+
`,
352+
options: [{ allowlist: [String.raw`/^\d+$/g`] }],
353+
errors: [
354+
{
355+
messageId: 'unexpected',
356+
line: 3,
357+
column: 13,
358+
endLine: 3,
359+
endColumn: 37
360+
}
361+
]
362+
},
363+
{
364+
code: `
365+
<template>
366+
<h1>foo123bar</h1>
367+
</template>
368+
`,
369+
options: [{ allowlist: [String.raw`/\d+/`, 'foo'] }],
370+
errors: [
371+
{
372+
messageId: 'unexpected',
373+
line: 3,
374+
column: 13,
375+
endLine: 3,
376+
endColumn: 22
377+
}
378+
]
319379
}
320380
]
321381
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /