@localey/react 0.1.2
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/README.md +22 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/package.json +23 -0
- package/src/index.d.ts +12 -0
- package/src/index.d.ts.map +1 -0
- package/src/index.js +123 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +104 -0
- package/tsconfig.json +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# @localey/adapter-react
|
|
2
|
+
|
|
3
|
+
React adapter for Localey. Uses Babel AST traversal to identify hardcoded strings in JSX and component props.
|
|
4
|
+
|
|
5
|
+
## Supported Patterns
|
|
6
|
+
|
|
7
|
+
### JSX Text
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
<div>Hello World</div> -> <div>{t("hello.world")}</div>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Component Props
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
<Button label="Save" /> -> <Button label={t("save")} />
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Internal Tools
|
|
20
|
+
|
|
21
|
+
- **Babel Parser**: Supports TypeScript and JSX.
|
|
22
|
+
- **AST Visitor**: Framework-aware detection logic.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FrameworkAdapter, ScanResult, ExtractResult } from "@localey/core";
|
|
2
|
+
export declare class ReactAdapter extends FrameworkAdapter {
|
|
3
|
+
name: string;
|
|
4
|
+
extensions: string[];
|
|
5
|
+
scan(content: string, filePath: string): ScanResult[];
|
|
6
|
+
extract(content: string, filePath: string, replacements: Array<{
|
|
7
|
+
oldValue: string;
|
|
8
|
+
newValue: string;
|
|
9
|
+
key: string;
|
|
10
|
+
}>): ExtractResult;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAS5E,qBAAa,YAAa,SAAQ,gBAAgB;IAChD,IAAI,SAAW;IACf,UAAU,WAAkC;IAE5C,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,UAAU,EAAE;IA2CrD,OAAO,CACL,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,KAAK,CAAC;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC,GACvE,aAAa;CA2CjB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { FrameworkAdapter } from "@localey/core";
|
|
2
|
+
import * as parser from "@babel/parser";
|
|
3
|
+
import _traverse from "@babel/traverse";
|
|
4
|
+
import _generate from "@babel/generator";
|
|
5
|
+
import * as t from "@babel/types";
|
|
6
|
+
const traverse = _traverse.default || _traverse;
|
|
7
|
+
const generate = _generate.default || _generate;
|
|
8
|
+
export class ReactAdapter extends FrameworkAdapter {
|
|
9
|
+
name = "react";
|
|
10
|
+
extensions = [".jsx", ".tsx", ".js", ".ts"];
|
|
11
|
+
scan(content, filePath) {
|
|
12
|
+
const results = [];
|
|
13
|
+
const ast = parser.parse(content, {
|
|
14
|
+
sourceType: "module",
|
|
15
|
+
plugins: ["jsx", "typescript"],
|
|
16
|
+
});
|
|
17
|
+
traverse(ast, {
|
|
18
|
+
JSXText(path) {
|
|
19
|
+
const value = path.node.value.trim();
|
|
20
|
+
if (value && !/^[ \t\n\r]*$/.test(value)) {
|
|
21
|
+
results.push({
|
|
22
|
+
file: filePath,
|
|
23
|
+
line: path.node.loc?.start.line || 0,
|
|
24
|
+
column: path.node.loc?.start.column || 0,
|
|
25
|
+
value,
|
|
26
|
+
context: "JSXText",
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
JSXAttribute(path) {
|
|
31
|
+
const skipAttrs = ["className", "id", "key", "ref", "style"];
|
|
32
|
+
const attrName = (path.node.name.type === "JSXIdentifier" ? path.node.name.name : "");
|
|
33
|
+
if (skipAttrs.includes(attrName))
|
|
34
|
+
return;
|
|
35
|
+
if (path.node.value?.type === "StringLiteral") {
|
|
36
|
+
results.push({
|
|
37
|
+
file: filePath,
|
|
38
|
+
line: path.node.value.loc?.start.line || 0,
|
|
39
|
+
column: path.node.value.loc?.start.column || 0,
|
|
40
|
+
value: path.node.value.value,
|
|
41
|
+
context: `JSXAttribute:${attrName}`,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
return results;
|
|
47
|
+
}
|
|
48
|
+
extract(content, filePath, replacements) {
|
|
49
|
+
const ast = parser.parse(content, {
|
|
50
|
+
sourceType: "module",
|
|
51
|
+
plugins: ["jsx", "typescript"],
|
|
52
|
+
});
|
|
53
|
+
const keys = {};
|
|
54
|
+
traverse(ast, {
|
|
55
|
+
JSXText(path) {
|
|
56
|
+
const value = path.node.value.trim();
|
|
57
|
+
const replacement = replacements.find((r) => r.oldValue === value);
|
|
58
|
+
if (replacement) {
|
|
59
|
+
path.replaceWith(t.jsxExpressionContainer(t.callExpression(t.identifier("t"), [
|
|
60
|
+
t.stringLiteral(replacement.key),
|
|
61
|
+
])));
|
|
62
|
+
keys[replacement.key] = replacement.oldValue;
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
JSXAttribute(path) {
|
|
66
|
+
if (path.node.value?.type === "StringLiteral") {
|
|
67
|
+
const value = path.node.value.value;
|
|
68
|
+
const replacement = replacements.find((r) => r.oldValue === value);
|
|
69
|
+
if (replacement) {
|
|
70
|
+
path.node.value = t.jsxExpressionContainer(t.callExpression(t.identifier("t"), [
|
|
71
|
+
t.stringLiteral(replacement.key),
|
|
72
|
+
]));
|
|
73
|
+
keys[replacement.key] = replacement.oldValue;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
const { code } = generate(ast, {}, content);
|
|
79
|
+
return { code, keys };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAA6B,MAAM,eAAe,CAAC;AAC5E,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,SAAuB,MAAM,iBAAiB,CAAC;AACtD,OAAO,SAAS,MAAM,kBAAkB,CAAC;AACzC,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,MAAM,QAAQ,GAAI,SAAiB,CAAC,OAAO,IAAI,SAAS,CAAC;AACzD,MAAM,QAAQ,GAAI,SAAiB,CAAC,OAAO,IAAI,SAAS,CAAC;AAEzD,MAAM,OAAO,YAAa,SAAQ,gBAAgB;IAChD,IAAI,GAAG,OAAO,CAAC;IACf,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAE5C,IAAI,CAAC,OAAe,EAAE,QAAgB;QACpC,MAAM,OAAO,GAAiB,EAAE,CAAC;QACjC,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE;YAChC,UAAU,EAAE,QAAQ;YACpB,OAAO,EAAE,CAAC,KAAK,EAAE,YAAY,CAAC;SAC/B,CAAC,CAAC;QAEH,QAAQ,CAAC,GAAG,EAAE;YACZ,OAAO,CAAC,IAAyB;gBAC/B,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CA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EAAE,CAAC;wBAChB,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,sBAAsB,CACxC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;4BAClC,CAAC,CAAC,aAAa,CAAC,WAAW,CAAC,GAAG,CAAC;yBACjC,CAAC,CACH,CAAC;wBACF,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,QAAQ,CAAC;oBAC/C,CAAC;gBACH,CAAC;YACH,CAAC;SACF,CAAC,CAAC;QAEH,MAAM,EAAE,IAAI,EAAE,GAAG,QAAQ,CAAC,GAAG,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;QAE5C,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IACxB,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@localey/react",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@localey/core": "*",
|
|
8
|
+
"@babel/core": "^7.22.0",
|
|
9
|
+
"@babel/preset-typescript": "^7.22.0",
|
|
10
|
+
"@babel/preset-react": "^7.22.0",
|
|
11
|
+
"@babel/traverse": "^7.22.0",
|
|
12
|
+
"@babel/generator": "^7.22.0",
|
|
13
|
+
"@babel/types": "^7.22.0"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"typescript": "^5.0.0",
|
|
17
|
+
"@types/node": "^20.0.0"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "tsc",
|
|
21
|
+
"dev": "tsc -w"
|
|
22
|
+
}
|
|
23
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FrameworkAdapter, ScanResult, ExtractResult } from "@localey/core";
|
|
2
|
+
export declare class ReactAdapter extends FrameworkAdapter {
|
|
3
|
+
name: string;
|
|
4
|
+
extensions: string[];
|
|
5
|
+
scan(content: string, filePath: string): ScanResult[];
|
|
6
|
+
extract(content: string, filePath: string, replacements: Array<{
|
|
7
|
+
oldValue: string;
|
|
8
|
+
newValue: string;
|
|
9
|
+
key: string;
|
|
10
|
+
}>): ExtractResult;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAO5E,qBAAa,YAAa,SAAQ,gBAAgB;IAChD,IAAI,SAAW;IACf,UAAU,WAAkC;IAE5C,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,UAAU,EAAE;IA2CrD,OAAO,CACL,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,KAAK,CAAC;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,CAAC,GACvE,aAAa;CA8CjB"}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.ReactAdapter = void 0;
|
|
40
|
+
const core_1 = require("@localey/core");
|
|
41
|
+
const parser = __importStar(require("@babel/parser"));
|
|
42
|
+
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
43
|
+
const generator_1 = __importDefault(require("@babel/generator"));
|
|
44
|
+
const t = __importStar(require("@babel/types"));
|
|
45
|
+
class ReactAdapter extends core_1.FrameworkAdapter {
|
|
46
|
+
name = "react";
|
|
47
|
+
extensions = [".jsx", ".tsx", ".js", ".ts"];
|
|
48
|
+
scan(content, filePath) {
|
|
49
|
+
const results = [];
|
|
50
|
+
const ast = parser.parse(content, {
|
|
51
|
+
sourceType: "module",
|
|
52
|
+
plugins: ["jsx", "typescript"],
|
|
53
|
+
});
|
|
54
|
+
(0, traverse_1.default)(ast, {
|
|
55
|
+
JSXText(path) {
|
|
56
|
+
const value = path.node.value.trim();
|
|
57
|
+
if (value && !/^[ \t\n\r]*$/.test(value)) {
|
|
58
|
+
results.push({
|
|
59
|
+
file: filePath,
|
|
60
|
+
line: path.node.loc?.start.line || 0,
|
|
61
|
+
column: path.node.loc?.start.column || 0,
|
|
62
|
+
value,
|
|
63
|
+
context: "JSXText",
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
JSXAttribute(path) {
|
|
68
|
+
const skipAttrs = ["className", "id", "key", "ref", "style"];
|
|
69
|
+
const attrName = (path.node.name.type === "JSXIdentifier" ? path.node.name.name : "");
|
|
70
|
+
if (skipAttrs.includes(attrName))
|
|
71
|
+
return;
|
|
72
|
+
if (path.node.value?.type === "StringLiteral") {
|
|
73
|
+
results.push({
|
|
74
|
+
file: filePath,
|
|
75
|
+
line: path.node.value.loc?.start.line || 0,
|
|
76
|
+
column: path.node.value.loc?.start.column || 0,
|
|
77
|
+
value: path.node.value.value,
|
|
78
|
+
context: `JSXAttribute:${attrName}`,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
return results;
|
|
84
|
+
}
|
|
85
|
+
extract(content, filePath, replacements) {
|
|
86
|
+
const ast = parser.parse(content, {
|
|
87
|
+
sourceType: "module",
|
|
88
|
+
plugins: ["jsx", "typescript"],
|
|
89
|
+
});
|
|
90
|
+
const keys = {};
|
|
91
|
+
(0, traverse_1.default)(ast, {
|
|
92
|
+
JSXText(path) {
|
|
93
|
+
const value = path.node.value.trim();
|
|
94
|
+
const replacement = replacements.find((r) => r.oldValue === value);
|
|
95
|
+
if (replacement) {
|
|
96
|
+
path.replaceWith(t.jsxExpressionContainer(t.callExpression(t.identifier("t"), [
|
|
97
|
+
t.stringLiteral(replacement.key),
|
|
98
|
+
])));
|
|
99
|
+
keys[replacement.key] = replacement.oldValue;
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
JSXAttribute(path) {
|
|
103
|
+
if (path.node.value?.type === "StringLiteral") {
|
|
104
|
+
const value = path.node.value.value;
|
|
105
|
+
const replacement = replacements.find((r) => r.oldValue === value);
|
|
106
|
+
if (replacement) {
|
|
107
|
+
path.node.value = t.jsxExpressionContainer(t.callExpression(t.identifier("t"), [
|
|
108
|
+
t.stringLiteral(replacement.key),
|
|
109
|
+
]));
|
|
110
|
+
keys[replacement.key] = replacement.oldValue;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
// @ts-ignore
|
|
116
|
+
const { code } = generator_1.default.default
|
|
117
|
+
? generator_1.default.default(ast, {}, content)
|
|
118
|
+
: (0, generator_1.default)(ast, {}, content);
|
|
119
|
+
return { code, keys };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
exports.ReactAdapter = ReactAdapter;
|
|
123
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wCAA4E;AAC5E,sDAAwC;AAExC,+DAAuC;AACvC,iEAAwC;AACxC,gDAAkC;AAElC,MAAa,YAAa,SAAQ,uBAAgB;IAChD,IAAI,GAAG,OAAO,CAAC;IACf,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAE5C,IAAI,CAAC,OAAe,EAAE,QAAgB;QACpC,MAAM,OAAO,GAAiB,EAAE,CAAC;QACjC,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE;YAChC,UAAU,EAAE,QAAQ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AI,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC;oBACpC,MAAM,WAAW,GAAG,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,KAAK,KAAK,CAAC,CAAC;oBACnE,IAAI,WAAW,EAAE,CAAC;wBAChB,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,sBAAsB,CACxC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;4BAClC,CAAC,CAAC,aAAa,CAAC,WAAW,CAAC,GAAG,CAAC;yBACjC,CAAC,CACH,CAAC;wBACF,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,QAAQ,CAAC;oBAC/C,CAAC;gBACH,CAAC;YACH,CAAC;SACF,CAAC,CAAC;QAEH,aAAa;QACb,MAAM,EAAE,IAAI,EAAE,GAAG,mBAAQ,CAAC,OAAO;YAC/B,CAAC,CAAC,mBAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,EAAE,OAAO,CAAC;YACpC,CAAC,CAAC,IAAA,mBAAQ,EAAC,GAAG,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;QAE/B,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IACxB,CAAC;CACF;AAjGD,oCAiGC"}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { FrameworkAdapter, ScanResult, ExtractResult } from "@localey/core";
|
|
2
|
+
import * as parser from "@babel/parser";
|
|
3
|
+
import _traverse, { NodePath } from "@babel/traverse";
|
|
4
|
+
import _generate from "@babel/generator";
|
|
5
|
+
import * as t from "@babel/types";
|
|
6
|
+
|
|
7
|
+
const traverse = (_traverse as any).default || _traverse;
|
|
8
|
+
const generate = (_generate as any).default || _generate;
|
|
9
|
+
|
|
10
|
+
export class ReactAdapter extends FrameworkAdapter {
|
|
11
|
+
name = "react";
|
|
12
|
+
extensions = [".jsx", ".tsx", ".js", ".ts"];
|
|
13
|
+
|
|
14
|
+
scan(content: string, filePath: string): ScanResult[] {
|
|
15
|
+
const results: ScanResult[] = [];
|
|
16
|
+
const ast = parser.parse(content, {
|
|
17
|
+
sourceType: "module",
|
|
18
|
+
plugins: ["jsx", "typescript"],
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
traverse(ast, {
|
|
22
|
+
JSXText(path: NodePath<t.JSXText>) {
|
|
23
|
+
const value = path.node.value.trim();
|
|
24
|
+
if (value && !/^[ \t\n\r]*$/.test(value)) {
|
|
25
|
+
results.push({
|
|
26
|
+
file: filePath,
|
|
27
|
+
line: path.node.loc?.start.line || 0,
|
|
28
|
+
column: path.node.loc?.start.column || 0,
|
|
29
|
+
value,
|
|
30
|
+
context: "JSXText",
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
JSXAttribute(path: NodePath<t.JSXAttribute>) {
|
|
35
|
+
const skipAttrs = ["className", "id", "key", "ref", "style"];
|
|
36
|
+
const attrName = (
|
|
37
|
+
path.node.name.type === "JSXIdentifier" ? path.node.name.name : ""
|
|
38
|
+
) as string;
|
|
39
|
+
|
|
40
|
+
if (skipAttrs.includes(attrName)) return;
|
|
41
|
+
|
|
42
|
+
if (path.node.value?.type === "StringLiteral") {
|
|
43
|
+
results.push({
|
|
44
|
+
file: filePath,
|
|
45
|
+
line: path.node.value.loc?.start.line || 0,
|
|
46
|
+
column: path.node.value.loc?.start.column || 0,
|
|
47
|
+
value: path.node.value.value,
|
|
48
|
+
context: `JSXAttribute:${attrName}`,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return results;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
extract(
|
|
58
|
+
content: string,
|
|
59
|
+
filePath: string,
|
|
60
|
+
replacements: Array<{ oldValue: string; newValue: string; key: string }>,
|
|
61
|
+
): ExtractResult {
|
|
62
|
+
const ast = parser.parse(content, {
|
|
63
|
+
sourceType: "module",
|
|
64
|
+
plugins: ["jsx", "typescript"],
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const keys: Record<string, string> = {};
|
|
68
|
+
|
|
69
|
+
traverse(ast, {
|
|
70
|
+
JSXText(path: NodePath<t.JSXText>) {
|
|
71
|
+
const value = path.node.value.trim();
|
|
72
|
+
const replacement = replacements.find((r) => r.oldValue === value);
|
|
73
|
+
if (replacement) {
|
|
74
|
+
path.replaceWith(
|
|
75
|
+
t.jsxExpressionContainer(
|
|
76
|
+
t.callExpression(t.identifier("t"), [
|
|
77
|
+
t.stringLiteral(replacement.key),
|
|
78
|
+
]),
|
|
79
|
+
),
|
|
80
|
+
);
|
|
81
|
+
keys[replacement.key] = replacement.oldValue;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
JSXAttribute(path: NodePath<t.JSXAttribute>) {
|
|
85
|
+
if (path.node.value?.type === "StringLiteral") {
|
|
86
|
+
const value = path.node.value.value;
|
|
87
|
+
const replacement = replacements.find((r) => r.oldValue === value);
|
|
88
|
+
if (replacement) {
|
|
89
|
+
path.node.value = t.jsxExpressionContainer(
|
|
90
|
+
t.callExpression(t.identifier("t"), [
|
|
91
|
+
t.stringLiteral(replacement.key),
|
|
92
|
+
]),
|
|
93
|
+
);
|
|
94
|
+
keys[replacement.key] = replacement.oldValue;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const { code } = generate(ast, {}, content);
|
|
101
|
+
|
|
102
|
+
return { code, keys };
|
|
103
|
+
}
|
|
104
|
+
}
|