Skip to content

Commit

Permalink
feat: add rule @git-validator/no-unnecessary-template-string
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Jul 7, 2024
1 parent 89ab89a commit 18617b4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/healthy-needles-boil.md
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`
3 changes: 2 additions & 1 deletion packages/eslint-config/src/javascript-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export default {
{
patterns: [
{
group: [`*.d.js`, `*.d.cjs`, `*.d.mjs`, `*.d.jsx`],
group: ["*.d.js", "*.d.cjs", "*.d.mjs", "*.d.jsx"],
message: "Do not import from a declaration style file",
},
{
Expand Down Expand Up @@ -411,6 +411,7 @@ export default {
"@git-validator/no-instanceof-builtin": "error",
"@git-validator/no-legacy-getter-setter": "error",
"@git-validator/no-relative-parent-imports": "error",
"@git-validator/no-unnecessary-template-string": "error",
"@git-validator/prefer-global-this": "error",
"@git-validator/prefer-shortest-relative-path": "error",
"@git-validator/require-reduce-initial-value": "error",
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import noInstanceofBuiltin from "./rules/no-instanceof-builtin.js";
import noLegacyGetterSetter from "./rules/no-legacy-getter-setter.js";
import noPropertyDecorator from "./rules/no-property-decorator.js";
import noRelativeParentImports from "./rules/no-relative-parent-imports.js";
import noUnnecessaryTemplateString from "./rules/no-unnecessary-template-string.js";
import noUntypedEmptyArray from "./rules/no-untyped-empty-array.js";
import preferGlobalThis from "./rules/prefer-global-this.js";
import preferShortestRelativePath from "./rules/prefer-shortest-relative-path.js";
Expand All @@ -31,6 +32,7 @@ export default {
[noLegacyGetterSetter.name]: noLegacyGetterSetter.rule,
[noPropertyDecorator.name]: noPropertyDecorator.rule,
[noRelativeParentImports.name]: noRelativeParentImports.rule,
[noUnnecessaryTemplateString.name]: noUnnecessaryTemplateString.rule,
[noUntypedEmptyArray.name]: noUntypedEmptyArray.rule,
[preferGlobalThis.name]: preferGlobalThis.rule,
[preferShortestRelativePath.name]: preferShortestRelativePath.rule,
Expand Down
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 packages/eslint-plugin/src/rules/no-unnecessary-template-string.ts
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);
}
},
}),
});

0 comments on commit 18617b4

Please sign in to comment.