-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rule
@git-validator/no-unnecessary-template-string
- Loading branch information
1 parent
89ab89a
commit 18617b4
Showing
5 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@git-validator/eslint-config": patch | ||
"@git-validator/eslint-plugin": patch | ||
--- | ||
|
||
feat: add rule `@git-validator/no-unnecessary-template-string` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/eslint-plugin/src/rules/no-unnecessary-template-string.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import rule from "./no-unnecessary-template-string.js"; | ||
import { test } from "./utils.spec.js"; | ||
|
||
const valid = [ | ||
"'abc'", | ||
'"def"', | ||
"`ab${cd}ef`", // eslint-disable-line no-template-curly-in-string | ||
"`\n`", | ||
"`abc\n`", | ||
"`\nabc`", | ||
"`a\nbc`", | ||
]; | ||
|
||
const invalid = ["``", "`abc`", "`abc\\n`", "`\\nabc`", "`a\\nbc`"]; | ||
|
||
test({ valid, invalid, ...rule }); |
18 changes: 18 additions & 0 deletions
18
packages/eslint-plugin/src/rules/no-unnecessary-template-string.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createSimpleRule, getRuleName } from "../utils.js"; | ||
|
||
export default createSimpleRule({ | ||
name: getRuleName(import.meta.url), | ||
message: | ||
"Disallow using template string when it's unnecessary. Use normal literal string expression instead.", | ||
create: (context) => ({ | ||
TemplateLiteral: (node) => { | ||
if ( | ||
node.quasis.length === 1 && | ||
node.expressions.length === 0 && | ||
node.loc.start.line === node.loc.end.line | ||
) { | ||
context.reportNode(node); | ||
} | ||
}, | ||
}), | ||
}); |