Skip to content

Commit

Permalink
Apply rule against root-level strings (#9)
Browse files Browse the repository at this point in the history
* Apply rule against root-level strings

* Create moody-adults-camp.md

* Use `trim` instead of both `trimStart` an `trimEnd`

Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>

* Use `trim` instead of both `trimStart` an `trimEnd`

Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>

---------

Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
  • Loading branch information
ccdexcom and ota-meshi committed Jan 16, 2024
1 parent 916837b commit a311f0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-adults-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@intlify/eslint-plugin-svelte": minor
---

Apply rule against root-level strings
14 changes: 9 additions & 5 deletions lib/rules/no-raw-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function checkSvelteLiteralOrText(
const loc = literal.loc!
context.report({
loc,
message: `raw text '${literal.value}' is used`
message: `raw text '${literal.value.trim()}' is used`
})
}

Expand All @@ -134,7 +134,7 @@ function checkLiteral(
const loc = literal.loc!
context.report({
loc,
message: `raw text '${value}' is used`
message: `raw text '${String(value).trim()}' is used`
})
}
/**
Expand All @@ -157,6 +157,7 @@ function parseTargetAttrs(

function create(context: RuleContext): RuleListener {
const sourceCode = context.getSourceCode()

const config: Config = {
attributes: [],
ignorePattern: /^$/,
Expand All @@ -180,9 +181,12 @@ function create(context: RuleContext): RuleListener {
function isIgnore(node: SvAST.SvelteMustacheTag | SvAST.SvelteText) {
const element = getElement(node)

return (
!element ||
config.ignoreNodes.includes(sourceCode.text.slice(...element.name.range!))
if (!element) {
return false
}

return config.ignoreNodes.includes(
sourceCode.text.slice(...element.name.range!)
)
}
function getElement(node: SvAST.SvelteMustacheTag | SvAST.SvelteText) {
Expand Down
19 changes: 19 additions & 0 deletions tests/lib/rules/no-raw-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ tester.run('no-raw-text', rule as never, {
<p>world</p>
`,
options: [{ ignoreText: ['hello', 'world'] }]
},
{
code: `
{ $_('root level translation') }
`,
options: []
}
],

Expand Down Expand Up @@ -224,6 +230,19 @@ tester.run('no-raw-text', rule as never, {
column: 23
}
]
},
{
code: `
<script>
</script>
text at the root of the template
`,
errors: [
{
message: "raw text 'text at the root of the template' is used"
}
]
}
]
})

0 comments on commit a311f0e

Please sign in to comment.