From cce7f99253c5c54e4c060af51bd8812ce676cba5 Mon Sep 17 00:00:00 2001 From: Chad Carbert Date: Fri, 12 Jan 2024 17:28:50 -0500 Subject: [PATCH 1/4] Apply rule against root-level strings --- lib/rules/no-raw-text.ts | 14 +++++++++----- tests/lib/rules/no-raw-text.ts | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/rules/no-raw-text.ts b/lib/rules/no-raw-text.ts index f2b6720..12cfbd4 100644 --- a/lib/rules/no-raw-text.ts +++ b/lib/rules/no-raw-text.ts @@ -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` }) } @@ -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` }) } /** @@ -155,6 +155,7 @@ function parseTargetAttrs( function create(context: RuleContext): RuleListener { const sourceCode = context.getSourceCode() + const config: Config = { attributes: [], ignorePattern: /^$/, @@ -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) { diff --git a/tests/lib/rules/no-raw-text.ts b/tests/lib/rules/no-raw-text.ts index 980275f..30e592d 100644 --- a/tests/lib/rules/no-raw-text.ts +++ b/tests/lib/rules/no-raw-text.ts @@ -47,6 +47,12 @@ tester.run('no-raw-text', rule as never, {

world

`, options: [{ ignoreText: ['hello', 'world'] }] + }, + { + code: ` + { $_('root level translation') } + `, + options: [] } ], @@ -224,6 +230,19 @@ tester.run('no-raw-text', rule as never, { column: 23 } ] + }, + { + code: ` + + + text at the root of the template + `, + errors: [ + { + message: "raw text 'text at the root of the template' is used" + } + ] } ] }) From 6d81e779291807614afe27615b33fce856797e2e Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Sat, 13 Jan 2024 15:52:07 +0900 Subject: [PATCH 2/4] Create moody-adults-camp.md --- .changeset/moody-adults-camp.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/moody-adults-camp.md diff --git a/.changeset/moody-adults-camp.md b/.changeset/moody-adults-camp.md new file mode 100644 index 0000000..43662eb --- /dev/null +++ b/.changeset/moody-adults-camp.md @@ -0,0 +1,5 @@ +--- +"@intlify/eslint-plugin-svelte": minor +--- + +Apply rule against root-level strings From 086ec306d9a7472a15c1675266fb6966d903dc4c Mon Sep 17 00:00:00 2001 From: Chad <136512293+ccdexcom@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:14:18 -0500 Subject: [PATCH 3/4] Use `trim` instead of both `trimStart` an `trimEnd` Co-authored-by: Yosuke Ota --- lib/rules/no-raw-text.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/no-raw-text.ts b/lib/rules/no-raw-text.ts index 12cfbd4..2279759 100644 --- a/lib/rules/no-raw-text.ts +++ b/lib/rules/no-raw-text.ts @@ -132,7 +132,7 @@ function checkLiteral( const loc = literal.loc! context.report({ loc, - message: `raw text '${String(value).trimStart().trimEnd()}' is used` + message: `raw text '${String(value).trim()}' is used` }) } /** From 7ee3f3a598e304d079652ecd0fa2c1c4f45eee31 Mon Sep 17 00:00:00 2001 From: Chad <136512293+ccdexcom@users.noreply.github.com> Date: Tue, 16 Jan 2024 10:14:25 -0500 Subject: [PATCH 4/4] Use `trim` instead of both `trimStart` an `trimEnd` Co-authored-by: Yosuke Ota --- lib/rules/no-raw-text.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/no-raw-text.ts b/lib/rules/no-raw-text.ts index 2279759..7b31544 100644 --- a/lib/rules/no-raw-text.ts +++ b/lib/rules/no-raw-text.ts @@ -111,7 +111,7 @@ function checkSvelteLiteralOrText( const loc = literal.loc! context.report({ loc, - message: `raw text '${literal.value.trimStart().trimEnd()}' is used` + message: `raw text '${literal.value.trim()}' is used` }) }