Skip to content

Commit a311f0e

Browse files
Chadota-meshi
Chad
andauthored
Apply rule against root-level strings (#9)
* Apply rule against root-level strings * Create moody-adults-camp.md * Use `trim` instead of both `trimStart` an `trimEnd` Co-authored-by: Yosuke Ota <[email protected]> * Use `trim` instead of both `trimStart` an `trimEnd` Co-authored-by: Yosuke Ota <[email protected]> --------- Co-authored-by: Yosuke Ota <[email protected]>
1 parent 916837b commit a311f0e

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

.changeset/moody-adults-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@intlify/eslint-plugin-svelte": minor
3+
---
4+
5+
Apply rule against root-level strings

lib/rules/no-raw-text.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function checkSvelteLiteralOrText(
113113
const loc = literal.loc!
114114
context.report({
115115
loc,
116-
message: `raw text '${literal.value}' is used`
116+
message: `raw text '${literal.value.trim()}' is used`
117117
})
118118
}
119119

@@ -134,7 +134,7 @@ function checkLiteral(
134134
const loc = literal.loc!
135135
context.report({
136136
loc,
137-
message: `raw text '${value}' is used`
137+
message: `raw text '${String(value).trim()}' is used`
138138
})
139139
}
140140
/**
@@ -157,6 +157,7 @@ function parseTargetAttrs(
157157

158158
function create(context: RuleContext): RuleListener {
159159
const sourceCode = context.getSourceCode()
160+
160161
const config: Config = {
161162
attributes: [],
162163
ignorePattern: /^$/,
@@ -180,9 +181,12 @@ function create(context: RuleContext): RuleListener {
180181
function isIgnore(node: SvAST.SvelteMustacheTag | SvAST.SvelteText) {
181182
const element = getElement(node)
182183

183-
return (
184-
!element ||
185-
config.ignoreNodes.includes(sourceCode.text.slice(...element.name.range!))
184+
if (!element) {
185+
return false
186+
}
187+
188+
return config.ignoreNodes.includes(
189+
sourceCode.text.slice(...element.name.range!)
186190
)
187191
}
188192
function getElement(node: SvAST.SvelteMustacheTag | SvAST.SvelteText) {

tests/lib/rules/no-raw-text.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ tester.run('no-raw-text', rule as never, {
4747
<p>world</p>
4848
`,
4949
options: [{ ignoreText: ['hello', 'world'] }]
50+
},
51+
{
52+
code: `
53+
{ $_('root level translation') }
54+
`,
55+
options: []
5056
}
5157
],
5258

@@ -224,6 +230,19 @@ tester.run('no-raw-text', rule as never, {
224230
column: 23
225231
}
226232
]
233+
},
234+
{
235+
code: `
236+
<script>
237+
</script>
238+
239+
text at the root of the template
240+
`,
241+
errors: [
242+
{
243+
message: "raw text 'text at the root of the template' is used"
244+
}
245+
]
227246
}
228247
]
229248
})

0 commit comments

Comments
 (0)