Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply rule against root-level strings #9

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/rules/no-raw-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function checkSvelteLiteralOrText(
const loc = literal.loc!
context.report({
loc,
message: `raw text '${literal.value}' is used`
message: `raw text '${literal.value.trimStart().trimEnd()}' is used`
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this would help with cleaning up the whitespace so the error message was relevant.

This conversation was marked as resolved.
Show resolved Hide resolved
})
}

Expand All @@ -132,7 +132,7 @@ function checkLiteral(
const loc = literal.loc!
context.report({
loc,
message: `raw text '${value}' is used`
message: `raw text '${String(value).trimStart().trimEnd()}' is used`
This conversation was marked as resolved.
Show resolved Hide resolved
})
}
/**
Expand All @@ -155,6 +155,7 @@ function parseTargetAttrs(

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

const config: Config = {
attributes: [],
ignorePattern: /^$/,
Expand All @@ -178,9 +179,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"
}
]
}
]
})
Loading