ai-localize-scanner 1.0.1 → 2.0.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/CHANGELOG.md +13 -0
- package/dist/index.js +11 -2
- package/dist/index.mjs +11 -2
- package/package.json +3 -3
- package/src/ast-scanner.ts +19 -4
package/CHANGELOG.md
ADDED
package/dist/index.js
CHANGED
|
@@ -111,9 +111,18 @@ var AstScanner = class {
|
|
|
111
111
|
StringLiteral: (nodePath) => {
|
|
112
112
|
if (t.isImportDeclaration(nodePath.parent)) return;
|
|
113
113
|
if (t.isObjectProperty(nodePath.parent) && nodePath.parent.key === nodePath.node) return;
|
|
114
|
-
if (t.isJSXAttribute(nodePath.parent))
|
|
114
|
+
if (t.isJSXAttribute(nodePath.parent)) {
|
|
115
|
+
const attrName = t.isJSXIdentifier(nodePath.parent.name) ? nodePath.parent.name.name.toLowerCase() : "";
|
|
116
|
+
if (attrName === "classname" || attrName === "class") {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
115
121
|
if (this.isInsideTranslationCall(nodePath)) return;
|
|
116
|
-
|
|
122
|
+
const val = nodePath.node.value;
|
|
123
|
+
if (/^[a-z][a-z0-9_.-]*$/.test(val) || /^#?[0-9a-fA-F]+$/.test(val) || /^[\w-]+\s[\w- ]+$/.test(val) && !val.includes(",")) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
117
126
|
const text = (0, import_ai_localize_shared.normalizeText)(nodePath.node.value);
|
|
118
127
|
if (!(0, import_ai_localize_shared.isHumanReadableText)(text)) return;
|
|
119
128
|
this.addDetected(
|
package/dist/index.mjs
CHANGED
|
@@ -76,9 +76,18 @@ var AstScanner = class {
|
|
|
76
76
|
StringLiteral: (nodePath) => {
|
|
77
77
|
if (t.isImportDeclaration(nodePath.parent)) return;
|
|
78
78
|
if (t.isObjectProperty(nodePath.parent) && nodePath.parent.key === nodePath.node) return;
|
|
79
|
-
if (t.isJSXAttribute(nodePath.parent))
|
|
79
|
+
if (t.isJSXAttribute(nodePath.parent)) {
|
|
80
|
+
const attrName = t.isJSXIdentifier(nodePath.parent.name) ? nodePath.parent.name.name.toLowerCase() : "";
|
|
81
|
+
if (attrName === "classname" || attrName === "class") {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
80
86
|
if (this.isInsideTranslationCall(nodePath)) return;
|
|
81
|
-
|
|
87
|
+
const val = nodePath.node.value;
|
|
88
|
+
if (/^[a-z][a-z0-9_.-]*$/.test(val) || /^#?[0-9a-fA-F]+$/.test(val) || /^[\w-]+\s[\w- ]+$/.test(val) && !val.includes(",")) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
82
91
|
const text = normalizeText(nodePath.node.value);
|
|
83
92
|
if (!isHumanReadableText(text)) return;
|
|
84
93
|
this.addDetected(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-localize-scanner",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "AST-based hardcoded text scanner for frontend applications",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"@babel/traverse": "^7.23.9",
|
|
18
18
|
"@babel/types": "^7.23.9",
|
|
19
19
|
"glob": "^10.3.10",
|
|
20
|
-
"ai-localize-shared": "
|
|
21
|
-
"ai-localize-config": "
|
|
20
|
+
"ai-localize-shared": "2.0.0",
|
|
21
|
+
"ai-localize-config": "2.0.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@types/babel__traverse": "^7.20.5",
|
package/src/ast-scanner.ts
CHANGED
|
@@ -97,17 +97,32 @@ if (!t.isStringLiteral(valueNode)) return;
|
|
|
97
97
|
StringLiteral: (nodePath) => {
|
|
98
98
|
if (t.isImportDeclaration(nodePath.parent)) return;
|
|
99
99
|
if (t.isObjectProperty(nodePath.parent) && nodePath.parent.key === nodePath.node) return;
|
|
100
|
-
|
|
100
|
+
|
|
101
|
+
// Ignore className and similar attributes containing CSS classes
|
|
102
|
+
if (t.isJSXAttribute(nodePath.parent)) {
|
|
103
|
+
const attrName = t.isJSXIdentifier(nodePath.parent.name) ? nodePath.parent.name.name.toLowerCase() : '';
|
|
104
|
+
if (attrName === 'classname' || attrName === 'class') {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
return; // Ignore other unhandled JSX attributes here as well based on previous logic
|
|
108
|
+
}
|
|
109
|
+
|
|
101
110
|
if (this.isInsideTranslationCall(nodePath)) return;
|
|
102
|
-
|
|
111
|
+
|
|
112
|
+
// Improve heuristic to avoid matching CSS classes / HTML tags in standard string literals
|
|
113
|
+
const val = nodePath.node.value;
|
|
114
|
+
if (/^[a-z][a-z0-9_.-]*$/.test(val) || /^#?[0-9a-fA-F]+$/.test(val) || (/^[\w-]+\s[\w- ]+$/.test(val) && !val.includes(','))) {
|
|
115
|
+
return; // Probably a CSS class name, ID, color hex, or just words without punctuation that are often classes
|
|
116
|
+
}
|
|
117
|
+
|
|
103
118
|
const text = normalizeText(nodePath.node.value);
|
|
104
119
|
if (!isHumanReadableText(text)) return;
|
|
105
|
-
|
|
120
|
+
this.addDetected(
|
|
106
121
|
text,
|
|
107
122
|
nodePath.node.loc?.start.line ?? 0,
|
|
108
123
|
nodePath.node.loc?.start.column ?? 0,
|
|
109
124
|
'string-literal',
|
|
110
|
-
|
|
125
|
+
'StringLiteral'
|
|
111
126
|
);
|
|
112
127
|
},
|
|
113
128
|
|