localization-drift 0.1.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/LICENSE +21 -0
- package/README.md +93 -0
- package/action.yml +18 -0
- package/package.json +45 -0
- package/src/cli.mjs +75 -0
- package/src/index.mjs +161 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 m1chlcz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# localization-drift
|
|
2
|
+
|
|
3
|
+
Fail a build when customer-visible text bypasses translation keys.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Flags hardcoded JSX text and localized attributes in `.tsx` files.
|
|
8
|
+
- Flags configured forbidden patterns in the scanned source files.
|
|
9
|
+
- Honors a reviewed exception comment on the line before a finding.
|
|
10
|
+
- Runs as a CLI, an npm package, or a GitHub Action.
|
|
11
|
+
- Pairs with [localize](https://github.com/m1chlcz/localize) for the workbook and ARB side.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
npm install --save-dev localization-drift
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Node.js 22 or later is required.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
npx localization-drift
|
|
25
|
+
npx localization-drift --root app --root components
|
|
26
|
+
npx localization-drift --json
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The output is one line per finding: `path:line: kind: message`. The exit code is 1 for findings and 2 for a usage or configuration error.
|
|
30
|
+
|
|
31
|
+
## Configuration
|
|
32
|
+
|
|
33
|
+
Create `localization-drift.json` in the repository root:
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"roots": ["app", "components"],
|
|
38
|
+
"extensions": [".tsx", ".ts"],
|
|
39
|
+
"attributes": ["alt", "aria-label", "placeholder", "title"],
|
|
40
|
+
"exclude": ["\\.test\\.", "/generated/", "node_modules"],
|
|
41
|
+
"forbidden": [
|
|
42
|
+
{
|
|
43
|
+
"pattern": "widget_type\\s*===\\s*[\"'][^\"']+[\"']",
|
|
44
|
+
"message": "use stored capabilities instead of a widget value"
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
| Field | Default | Meaning |
|
|
51
|
+
| --- | --- | --- |
|
|
52
|
+
| `roots` | `["src"]` | The directories to scan. |
|
|
53
|
+
| `extensions` | `[".tsx", ".ts"]` | The file extensions to scan. |
|
|
54
|
+
| `attributes` | `["alt", "aria-label", "placeholder", "title"]` | The JSX attributes that must use a key. |
|
|
55
|
+
| `exclude` | tests, `/generated/`, `node_modules` | Regular expressions for paths to skip. |
|
|
56
|
+
| `forbidden` | `[]` | Regular expression rules with a message. |
|
|
57
|
+
| `allowComment` | the marker below | The exception marker prefix. |
|
|
58
|
+
|
|
59
|
+
## Exception
|
|
60
|
+
|
|
61
|
+
Put the marker and a reason on the line before the finding:
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
// localization-drift: allow-next-line -- registered brand name
|
|
65
|
+
<strong>WhatsApp</strong>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## GitHub Action
|
|
69
|
+
|
|
70
|
+
```yaml
|
|
71
|
+
- uses: actions/checkout@v7
|
|
72
|
+
- uses: m1chlcz/localization-drift@v0.1.0
|
|
73
|
+
with:
|
|
74
|
+
args: --root app
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Combine it with `localize` to catch generated-file drift as well:
|
|
78
|
+
|
|
79
|
+
```yaml
|
|
80
|
+
- run: go run github.com/m1chlcz/localize/cmd/localize@latest generate --input translations.xlsx --output-dir arb
|
|
81
|
+
- run: git diff --exit-code -- arb
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Development
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
npm ci
|
|
88
|
+
npm test
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
MIT. See `LICENSE`.
|
package/action.yml
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
name: localization-drift
|
|
2
|
+
description: Fail when customer-visible text bypasses translation keys.
|
|
3
|
+
inputs:
|
|
4
|
+
args:
|
|
5
|
+
description: Extra arguments for the audit.
|
|
6
|
+
required: false
|
|
7
|
+
default: ""
|
|
8
|
+
runs:
|
|
9
|
+
using: composite
|
|
10
|
+
steps:
|
|
11
|
+
- name: Install
|
|
12
|
+
shell: bash
|
|
13
|
+
run: npm ci --prefix "${{ github.action_path }}" --no-audit --no-fund
|
|
14
|
+
- name: Audit localization
|
|
15
|
+
shell: bash
|
|
16
|
+
env:
|
|
17
|
+
INPUT_ARGS: ${{ inputs.args }}
|
|
18
|
+
run: node "${{ github.action_path }}/src/cli.mjs" $INPUT_ARGS
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "localization-drift",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Fail a build when customer-visible text bypasses translation keys.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/m1chlcz/localization-drift.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/m1chlcz/localization-drift",
|
|
12
|
+
"bugs": "https://github.com/m1chlcz/localization-drift/issues",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"i18n",
|
|
15
|
+
"localization",
|
|
16
|
+
"translation",
|
|
17
|
+
"lint",
|
|
18
|
+
"typescript",
|
|
19
|
+
"react",
|
|
20
|
+
"jsx",
|
|
21
|
+
"github-action"
|
|
22
|
+
],
|
|
23
|
+
"bin": {
|
|
24
|
+
"localization-drift": "src/cli.mjs"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
".": "./src/index.mjs",
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"src",
|
|
32
|
+
"action.yml",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=22"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "node --test"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"typescript": "6.0.3"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/cli.mjs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
import { parseArgs } from "node:util";
|
|
6
|
+
|
|
7
|
+
import { scanRepository } from "./index.mjs";
|
|
8
|
+
|
|
9
|
+
const version = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
10
|
+
|
|
11
|
+
const usage = `Usage: localization-drift [options]
|
|
12
|
+
|
|
13
|
+
Options:
|
|
14
|
+
--config FILE read a JSON configuration file
|
|
15
|
+
--root DIR scan DIR (repeatable; overrides the configured roots)
|
|
16
|
+
--json print the findings as JSON
|
|
17
|
+
-h, --help show this help
|
|
18
|
+
-v, --version show the version`;
|
|
19
|
+
|
|
20
|
+
function readConfig(path, required) {
|
|
21
|
+
if (!path) return {};
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error.code === "ENOENT" && !required) return {};
|
|
26
|
+
if (error.code === "ENOENT") throw new Error(`configuration file not found: ${path}`);
|
|
27
|
+
throw new Error(`invalid configuration file: ${path}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function main() {
|
|
32
|
+
const { values } = parseArgs({
|
|
33
|
+
args: process.argv.slice(2),
|
|
34
|
+
options: {
|
|
35
|
+
config: { type: "string" },
|
|
36
|
+
root: { type: "string", multiple: true },
|
|
37
|
+
json: { type: "boolean" },
|
|
38
|
+
help: { type: "boolean", short: "h" },
|
|
39
|
+
version: { type: "boolean", short: "v" },
|
|
40
|
+
},
|
|
41
|
+
strict: true,
|
|
42
|
+
});
|
|
43
|
+
if (values.version) {
|
|
44
|
+
process.stdout.write(`${version}\n`);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (values.help) {
|
|
48
|
+
process.stdout.write(`${usage}\n`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const root = process.cwd();
|
|
53
|
+
const config = values.config
|
|
54
|
+
? readConfig(resolve(root, values.config), true)
|
|
55
|
+
: readConfig(resolve(root, "localization-drift.json"), false);
|
|
56
|
+
if (values.root?.length) config.roots = values.root;
|
|
57
|
+
|
|
58
|
+
const findings = await scanRepository(root, config);
|
|
59
|
+
if (values.json) {
|
|
60
|
+
process.stdout.write(`${JSON.stringify(findings, null, 2)}\n`);
|
|
61
|
+
} else if (findings.length === 0) {
|
|
62
|
+
process.stdout.write("Localization audit passed.\n");
|
|
63
|
+
} else {
|
|
64
|
+
for (const item of findings) {
|
|
65
|
+
process.stderr.write(`${item.path}:${item.line}: ${item.kind}: ${item.message}\n`);
|
|
66
|
+
}
|
|
67
|
+
process.stderr.write(`${findings.length} localization finding(s).\n`);
|
|
68
|
+
}
|
|
69
|
+
if (findings.length > 0) process.exitCode = 1;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
main().catch((error) => {
|
|
73
|
+
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
74
|
+
process.exitCode = 2;
|
|
75
|
+
});
|
package/src/index.mjs
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
2
|
+
import { extname, join, relative, sep } from "node:path";
|
|
3
|
+
import ts from "typescript";
|
|
4
|
+
|
|
5
|
+
export const ALLOW_COMMENT = "// localization-drift: allow-next-line -- ";
|
|
6
|
+
|
|
7
|
+
export const DEFAULT_CONFIG = Object.freeze({
|
|
8
|
+
roots: ["src"],
|
|
9
|
+
extensions: [".tsx", ".ts"],
|
|
10
|
+
attributes: ["alt", "aria-label", "placeholder", "title"],
|
|
11
|
+
exclude: ["\\.test\\.", "/test/", "/generated/", "node_modules", "\\.d\\.ts$"],
|
|
12
|
+
forbidden: [],
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
function lineDetails(source, index) {
|
|
16
|
+
const before = source.slice(0, index);
|
|
17
|
+
const line = before.split("\n").length;
|
|
18
|
+
const lines = source.split("\n");
|
|
19
|
+
return {
|
|
20
|
+
line,
|
|
21
|
+
text: lines[line - 1]?.trim() ?? "",
|
|
22
|
+
previous: lines[line - 2]?.trim() ?? "",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function reviewedException(source, index, allowComment) {
|
|
27
|
+
return lineDetails(source, index).previous.startsWith(allowComment);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function finding(path, source, index, kind, message) {
|
|
31
|
+
const details = lineDetails(source, index);
|
|
32
|
+
return { path, line: details.line, kind, message, text: details.text };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* analyzeSource reports customer-visible JSX text, localized attributes, and
|
|
37
|
+
* configured forbidden patterns in one source file.
|
|
38
|
+
*/
|
|
39
|
+
export function analyzeSource(path, source, options = {}) {
|
|
40
|
+
const {
|
|
41
|
+
checkJSX = path.endsWith(".tsx"),
|
|
42
|
+
attributes = DEFAULT_CONFIG.attributes,
|
|
43
|
+
forbidden = [],
|
|
44
|
+
allowComment = ALLOW_COMMENT,
|
|
45
|
+
} = options;
|
|
46
|
+
const localizedAttributes = new Set(attributes);
|
|
47
|
+
const indexed = [];
|
|
48
|
+
|
|
49
|
+
if (checkJSX) {
|
|
50
|
+
const sourceFile = ts.createSourceFile(path, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
51
|
+
const visit = (node) => {
|
|
52
|
+
if (ts.isJsxAttribute(node) &&
|
|
53
|
+
localizedAttributes.has(node.name.text) &&
|
|
54
|
+
node.initializer && ts.isStringLiteral(node.initializer) &&
|
|
55
|
+
/\p{L}/u.test(node.initializer.text)) {
|
|
56
|
+
const index = node.getStart(sourceFile);
|
|
57
|
+
if (!reviewedException(source, index, allowComment)) {
|
|
58
|
+
indexed.push({
|
|
59
|
+
index,
|
|
60
|
+
finding: finding(
|
|
61
|
+
path,
|
|
62
|
+
source,
|
|
63
|
+
index,
|
|
64
|
+
"localized-attribute",
|
|
65
|
+
`customer-visible ${node.name.text} must come from a translation key`,
|
|
66
|
+
),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (ts.isJsxText(node)) {
|
|
71
|
+
const value = node.getText(sourceFile).replace(/\s+/g, " ").trim();
|
|
72
|
+
const index = node.getStart(sourceFile);
|
|
73
|
+
if (value && /\p{L}/u.test(value) && !reviewedException(source, index, allowComment)) {
|
|
74
|
+
indexed.push({
|
|
75
|
+
index,
|
|
76
|
+
finding: finding(
|
|
77
|
+
path,
|
|
78
|
+
source,
|
|
79
|
+
index,
|
|
80
|
+
"localized-jsx-text",
|
|
81
|
+
`customer-visible JSX text must come from a translation key: ${JSON.stringify(value)}`,
|
|
82
|
+
),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
ts.forEachChild(node, visit);
|
|
87
|
+
};
|
|
88
|
+
visit(sourceFile);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const rule of forbidden) {
|
|
92
|
+
if (!rule || typeof rule.pattern !== "string" || typeof rule.message !== "string") continue;
|
|
93
|
+
const pattern = new RegExp(rule.pattern, "g");
|
|
94
|
+
for (const match of source.matchAll(pattern)) {
|
|
95
|
+
if (reviewedException(source, match.index, allowComment)) continue;
|
|
96
|
+
indexed.push({
|
|
97
|
+
index: match.index,
|
|
98
|
+
finding: finding(path, source, match.index, "forbidden-pattern", rule.message),
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return indexed
|
|
104
|
+
.sort((left, right) => left.index - right.index)
|
|
105
|
+
.map(({ finding: result }) => result);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function excludedPath(path, exclude) {
|
|
109
|
+
return exclude.some((pattern) => new RegExp(pattern).test(path));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function* walk(directory, config) {
|
|
113
|
+
let entries;
|
|
114
|
+
try {
|
|
115
|
+
entries = await readdir(directory, { withFileTypes: true });
|
|
116
|
+
} catch {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
for (const entry of entries) {
|
|
120
|
+
if ([".git", ".next", ".tmp", "node_modules"].includes(entry.name)) continue;
|
|
121
|
+
const path = join(directory, entry.name);
|
|
122
|
+
if (entry.isDirectory()) {
|
|
123
|
+
yield* walk(path, config);
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (!config.extensions.includes(extname(entry.name))) continue;
|
|
127
|
+
yield path;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* scanRepository walks the configured roots and returns every finding, sorted
|
|
133
|
+
* by path and line.
|
|
134
|
+
*/
|
|
135
|
+
export async function scanRepository(root, config = {}) {
|
|
136
|
+
const settings = {
|
|
137
|
+
roots: config.roots ?? DEFAULT_CONFIG.roots,
|
|
138
|
+
extensions: config.extensions ?? DEFAULT_CONFIG.extensions,
|
|
139
|
+
attributes: config.attributes ?? DEFAULT_CONFIG.attributes,
|
|
140
|
+
exclude: config.exclude ?? DEFAULT_CONFIG.exclude,
|
|
141
|
+
forbidden: config.forbidden ?? DEFAULT_CONFIG.forbidden,
|
|
142
|
+
allowComment: config.allowComment ?? ALLOW_COMMENT,
|
|
143
|
+
};
|
|
144
|
+
const findings = [];
|
|
145
|
+
for (const directory of settings.roots) {
|
|
146
|
+
for await (const absolute of walk(join(root, directory), settings)) {
|
|
147
|
+
const path = relative(root, absolute).split(sep).join("/");
|
|
148
|
+
if (excludedPath(path, settings.exclude)) continue;
|
|
149
|
+
const source = await readFile(absolute, "utf8");
|
|
150
|
+
findings.push(...analyzeSource(path, source, {
|
|
151
|
+
checkJSX: settings.extensions.includes(extname(path)) && path.endsWith(".tsx"),
|
|
152
|
+
attributes: settings.attributes,
|
|
153
|
+
forbidden: settings.forbidden,
|
|
154
|
+
allowComment: settings.allowComment,
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return findings.sort((left, right) =>
|
|
159
|
+
left.path.localeCompare(right.path) || left.line - right.line
|
|
160
|
+
);
|
|
161
|
+
}
|