Skip to content

Commit cce7f99

Browse files
author
Chad Carbert
committed
Apply rule against root-level strings
1 parent eb92175 commit cce7f99

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/rules/no-raw-text.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function checkSvelteLiteralOrText(
111111
const loc = literal.loc!
112112
context.report({
113113
loc,
114-
message: `raw text '${literal.value}' is used`
114+
message: `raw text '${literal.value.trimStart().trimEnd()}' is used`
115115
})
116116
}
117117

@@ -132,7 +132,7 @@ function checkLiteral(
132132
const loc = literal.loc!
133133
context.report({
134134
loc,
135-
message: `raw text '${value}' is used`
135+
message: `raw text '${String(value).trimStart().trimEnd()}' is used`
136136
})
137137
}
138138
/**
@@ -155,6 +155,7 @@ function parseTargetAttrs(
155155

156156
function create(context: RuleContext): RuleListener {
157157
const sourceCode = context.getSourceCode()
158+
158159
const config: Config = {
159160
attributes: [],
160161
ignorePattern: /^$/,
@@ -178,9 +179,12 @@ function create(context: RuleContext): RuleListener {
178179
function isIgnore(node: SvAST.SvelteMustacheTag | SvAST.SvelteText) {
179180
const element = getElement(node)
180181

181-
return (
182-
!element ||
183-
config.ignoreNodes.includes(sourceCode.text.slice(...element.name.range!))
182+
if (!element) {
183+
return false
184+
}
185+
186+
return config.ignoreNodes.includes(
187+
sourceCode.text.slice(...element.name.range!)
184188
)
185189
}
186190
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)