@rpcbase/eslint-config 0.17.0 → 0.18.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/eslint-config",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -33,6 +33,7 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@eslint/js": "9.26.0",
36
+ "@typescript-eslint/experimental-utils": "5.62.0",
36
37
  "eslint-config-flat-gitignore": "2.1.0",
37
38
  "eslint-import-resolver-typescript": "4.3.4",
38
39
  "eslint-plugin-import": "2.31.0",
@@ -43,7 +44,7 @@
43
44
  },
44
45
  "devDependencies": {
45
46
  "@types/eslint": "9.6.1",
46
- "typescript": "^5.x",
47
- "eslint": "^9.x"
47
+ "eslint": "^9.x",
48
+ "typescript": "^5.x"
48
49
  }
49
50
  }
@@ -0,0 +1,72 @@
1
+ import { TSESTree, ESLintUtils } from "@typescript-eslint/experimental-utils"
2
+
3
+ type MessageIds = "loneText"
4
+
5
+ const createRule = ESLintUtils.RuleCreator(
6
+ name => `@rpcbase/eslint-config/rules/${name}`
7
+ )
8
+
9
+ export const rules = {
10
+ "no-lone-text-node": createRule<[], MessageIds>({
11
+ name: "no-lone-text-node",
12
+ meta: {
13
+ type: "problem",
14
+ docs: {
15
+ description: "disallow raw text nodes in JSX",
16
+ recommended: "error"
17
+ },
18
+ messages: {
19
+ loneText: "Wrap text '{{text}}' in an element (e.g. <span>) to avoid Google Translate crashes."
20
+ },
21
+ schema: []
22
+ },
23
+ defaultOptions: [],
24
+ create(context) {
25
+ //----------------------------------------------------------------------
26
+ // helpers
27
+ //----------------------------------------------------------------------
28
+
29
+ function isAllowedWrapper(node: TSESTree.JSXElement) {
30
+ const name = node.openingElement.name
31
+ return (
32
+ name.type === "JSXIdentifier" &&
33
+ ["span", "p", "label", "div", "strong", "em", "Trans", "button"].includes(
34
+ name.name
35
+ )
36
+ )
37
+ }
38
+
39
+ function report(node: TSESTree.Node, value: string) {
40
+ context.report({ node, messageId: "loneText", data: { text: value } })
41
+ }
42
+
43
+ //----------------------------------------------------------------------
44
+ // visitors
45
+ //----------------------------------------------------------------------
46
+
47
+ return {
48
+ JSXElement(node) {
49
+ if (isAllowedWrapper(node)) return
50
+
51
+ node.children.forEach(child => {
52
+ if (
53
+ child.type === "JSXText" &&
54
+ child.value.trim().length > 0
55
+ ) {
56
+ report(child, child.value.trim())
57
+ }
58
+
59
+ if (
60
+ child.type === "JSXExpressionContainer" &&
61
+ child.expression.type === "Literal" &&
62
+ typeof child.expression.value === "string" &&
63
+ child.expression.value.trim().length > 0
64
+ ) {
65
+ report(child, child.expression.value.trim())
66
+ }
67
+ })
68
+ }
69
+ }
70
+ }
71
+ })
72
+ }