@pob/eslint-plugin 66.6.0 → 66.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,5 +2,6 @@ export default {
2
2
  name: "@pob/eslint-plugin/configs/base",
3
3
  rules: {
4
4
  "@pob/forbid-non-native-fetch-import": "error",
5
+ "@pob/no-useless-template-literal": "error",
5
6
  },
6
7
  };
package/lib/loadRules.js CHANGED
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import path from "node:path";
3
3
  import forbidNonNativeFetchImportRule from "./rules/forbid-non-native-fetch-import.js";
4
4
  import forbidNonNativeNodeImportsRule from "./rules/forbid-non-native-node-imports.js";
5
+ import noUselessTemplateLiteralRule from "./rules/no-useless-template-literal.js";
5
6
  import reactFunctionComponentReturnReactNodeRule from "./rules/react-function-component-return-react-node.js";
6
7
  import reactNamedImportRule from "./rules/react-named-import.js";
7
8
 
@@ -42,6 +43,10 @@ export function loadRules() {
42
43
  "forbid-non-native-node-imports",
43
44
  forbidNonNativeNodeImportsRule,
44
45
  ),
46
+ "no-useless-template-literal": loadRule(
47
+ "no-useless-template-literal",
48
+ noUselessTemplateLiteralRule,
49
+ ),
45
50
  "react-function-component-return-react-node": loadRule(
46
51
  "react-function-component-return-react-node",
47
52
  reactFunctionComponentReturnReactNodeRule,
@@ -0,0 +1,57 @@
1
+ // An unescaped line break is preceded by an even number of backslashes.
2
+ const UNESCAPED_LINEBREAK_PATTERN = /(?:^|[^\\])(?:\\\\)*[\n\r\u2028\u2029]/u;
3
+
4
+ const toDoubleQuotedString = (raw) =>
5
+ `"${raw.slice(1, -1).replaceAll(/\\(\$\{|.)|"/gu, (match, escaped) => {
6
+ if (escaped === "`" || escaped === "${") return escaped;
7
+ if (match === '"') return '\\"';
8
+ return match;
9
+ })}"`;
10
+
11
+ export default {
12
+ meta: {
13
+ type: "suggestion",
14
+
15
+ docs: {
16
+ description:
17
+ "Disallow template literals without interpolation, line break or tag",
18
+ category: "Best Practices",
19
+ recommended: true,
20
+ },
21
+
22
+ fixable: "code",
23
+
24
+ messages: {
25
+ useless:
26
+ "Template literal without interpolation, line break or tag: use a string literal.",
27
+ },
28
+ },
29
+
30
+ create: (context) => {
31
+ return {
32
+ TemplateLiteral(node) {
33
+ if (
34
+ node.expressions.length > 0 ||
35
+ (node.parent.type === "TaggedTemplateExpression" &&
36
+ node.parent.quasi === node) ||
37
+ UNESCAPED_LINEBREAK_PATTERN.test(node.quasis[0].value.raw)
38
+ ) {
39
+ return;
40
+ }
41
+
42
+ context.report({
43
+ node,
44
+ messageId: "useless",
45
+ fix: (fixer) =>
46
+ // a string literal statement can become a directive ("use strict")
47
+ node.parent.type === "ExpressionStatement"
48
+ ? null
49
+ : fixer.replaceText(
50
+ node,
51
+ toDoubleQuotedString(context.sourceCode.getText(node)),
52
+ ),
53
+ });
54
+ },
55
+ };
56
+ },
57
+ };
@@ -0,0 +1,54 @@
1
+ /* oxlint-disable no-template-curly-in-string -- code samples */
2
+ import { RuleTester } from "eslint";
3
+ import rule from "./no-useless-template-literal.js";
4
+
5
+ const ruleTester = new RuleTester({
6
+ languageOptions: {
7
+ ecmaVersion: 2022,
8
+ sourceType: "module",
9
+ },
10
+ });
11
+
12
+ // @ts-ignore
13
+ ruleTester.run("no-useless-template-literal", rule, {
14
+ valid: [
15
+ { code: "const a = 'single';" },
16
+ { code: 'const a = "double";' },
17
+ { code: "const a = `with ${b}`;" },
18
+ { code: "const a = String.raw`tagged`;" },
19
+ { code: "const a = `multi\nline`;" },
20
+ { code: "const a = `multi\\\\\nline`;" },
21
+ ],
22
+ invalid: [
23
+ {
24
+ code: "const a = `plain`;",
25
+ output: 'const a = "plain";',
26
+ errors: [{ messageId: "useless" }],
27
+ },
28
+ {
29
+ code: 'const a = `say "hi"`;',
30
+ output: 'const a = "say \\"hi\\"";',
31
+ errors: [{ messageId: "useless" }],
32
+ },
33
+ {
34
+ code: "const a = `it's`;",
35
+ output: 'const a = "it\'s";',
36
+ errors: [{ messageId: "useless" }],
37
+ },
38
+ {
39
+ code: "const a = `\\`a\\` \\${b} \\n`;",
40
+ output: 'const a = "`a` ${b} \\n";',
41
+ errors: [{ messageId: "useless" }],
42
+ },
43
+ {
44
+ code: "const a = `escaped \\\nline break`;",
45
+ output: 'const a = "escaped \\\nline break";',
46
+ errors: [{ messageId: "useless" }],
47
+ },
48
+ {
49
+ code: "`use strict`;",
50
+ output: null,
51
+ errors: [{ messageId: "useless" }],
52
+ },
53
+ ],
54
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pob/eslint-plugin",
3
- "version": "66.6.0",
3
+ "version": "66.7.0",
4
4
  "description": "eslint plugin for react",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/christophehurpeau/pob",