@localeguard/react-analyzer 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 +201 -0
- package/README.md +45 -0
- package/dist/src/analyzer.d.ts +29 -0
- package/dist/src/analyzer.d.ts.map +1 -0
- package/dist/src/analyzer.js +209 -0
- package/dist/src/analyzer.js.map +1 -0
- package/dist/src/glob.d.ts +20 -0
- package/dist/src/glob.d.ts.map +1 -0
- package/dist/src/glob.js +133 -0
- package/dist/src/glob.js.map +1 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +15 -0
- package/dist/src/index.js.map +1 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work, excluding
|
|
103
|
+
those notices that do not pertain to any part of the Derivative
|
|
104
|
+
Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and do
|
|
117
|
+
not modify the License. You may add Your own attribution notices
|
|
118
|
+
within Derivative Works that You distribute, alongside or as an
|
|
119
|
+
addendum to the NOTICE text from the Work, provided that such
|
|
120
|
+
additional attribution notices cannot be construed as modifying
|
|
121
|
+
the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 LocaleGuard contributors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# @localeguard/react-analyzer
|
|
2
|
+
|
|
3
|
+
Static analysis for LocaleGuard that catches **hardcoded** UI text in
|
|
4
|
+
React/TypeScript source — strings that never make it into the locale files at
|
|
5
|
+
all. Powered by the TypeScript compiler API; no project compilation required.
|
|
6
|
+
|
|
7
|
+
It reports two issue types:
|
|
8
|
+
|
|
9
|
+
- **`hardcoded-string`** — literal JSX text, e.g. `<Button>Create Cluster</Button>`
|
|
10
|
+
- **`hardcoded-attribute`** — literal `aria-label`, `title`, `alt`, or
|
|
11
|
+
`placeholder`, e.g. `<button aria-label="Close dialog">`
|
|
12
|
+
|
|
13
|
+
## Design: low false positives
|
|
14
|
+
|
|
15
|
+
Only **literal** values are flagged. Anything already dynamic is left alone:
|
|
16
|
+
|
|
17
|
+
| Code | Flagged? |
|
|
18
|
+
| --- | --- |
|
|
19
|
+
| `<h1>Cluster Manager</h1>` | ✅ hardcoded-string |
|
|
20
|
+
| `<p>{t("app.title")}</p>` | ❌ already localized |
|
|
21
|
+
| `<Trans i18nKey="x">Welcome</Trans>` | ❌ inside a translation component |
|
|
22
|
+
| `<span>100%</span>` | ❌ no letters |
|
|
23
|
+
| `<button aria-label="Close">` | ✅ hardcoded-attribute |
|
|
24
|
+
| `<button aria-label={t("close")}>` | ❌ already localized |
|
|
25
|
+
| `<img alt="" />` | ❌ decorative empty alt |
|
|
26
|
+
|
|
27
|
+
Translation component names (default `Trans`) are configurable via
|
|
28
|
+
`translationComponents` in your LocaleGuard config.
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
This package is composed automatically by the `localeguard` CLI — running
|
|
33
|
+
`localeguard check` includes source-code analysis using your config's `include`,
|
|
34
|
+
`ignore`, and `translationComponents`. Disable it with `localeguard check --no-code`.
|
|
35
|
+
|
|
36
|
+
It can also be used directly:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { analyzeProject } from "@localeguard/react-analyzer";
|
|
40
|
+
|
|
41
|
+
const issues = analyzeProject(
|
|
42
|
+
{ include: ["src/**/*.{ts,tsx}"], translationComponents: ["Trans"] },
|
|
43
|
+
{ rootDir: process.cwd() },
|
|
44
|
+
);
|
|
45
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detect hardcoded, user-facing strings in React/TypeScript source using the
|
|
3
|
+
* TypeScript compiler API.
|
|
4
|
+
*
|
|
5
|
+
* Two categories are reported:
|
|
6
|
+
* - `hardcoded-string` — literal JSX text, e.g. `<Button>Create</Button>`
|
|
7
|
+
* - `hardcoded-attribute` — literal `aria-label`/`title`/`alt`/`placeholder`,
|
|
8
|
+
* e.g. `<button aria-label="Close dialog">`
|
|
9
|
+
*
|
|
10
|
+
* Design goal: a low false-positive rate. We only flag *literal* values; any
|
|
11
|
+
* value that is already an expression (`{t('...')}`, `{label}`) is left alone,
|
|
12
|
+
* and text inside configured translation components (e.g. `<Trans>`) is ignored.
|
|
13
|
+
*/
|
|
14
|
+
import type { Issue } from "@localeguard/core";
|
|
15
|
+
export declare const DEFAULT_INCLUDE: string[];
|
|
16
|
+
export declare const DEFAULT_TRANSLATION_COMPONENTS: string[];
|
|
17
|
+
export interface AnalyzerConfig {
|
|
18
|
+
include?: string[];
|
|
19
|
+
ignore?: string[];
|
|
20
|
+
translationComponents?: string[];
|
|
21
|
+
}
|
|
22
|
+
export interface AnalyzeOptions {
|
|
23
|
+
/** Absolute project root; reported file paths are relative to this. */
|
|
24
|
+
rootDir: string;
|
|
25
|
+
}
|
|
26
|
+
export declare function analyzeProject(config: AnalyzerConfig, opts: AnalyzeOptions): Issue[];
|
|
27
|
+
/** Analyze a single source string. Exported for testing. */
|
|
28
|
+
export declare function analyzeSource(text: string, fileName: string, translationComponents: Set<string>, issues?: Issue[]): Issue[];
|
|
29
|
+
//# sourceMappingURL=analyzer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analyzer.d.ts","sourceRoot":"","sources":["../../src/analyzer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAQH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAI/C,eAAO,MAAM,eAAe,UAAwB,CAAC;AACrD,eAAO,MAAM,8BAA8B,UAAY,CAAC;AAoBxD,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,qBAAqB,CAAC,EAAE,MAAM,EAAE,CAAC;CAClC;AAED,MAAM,WAAW,cAAc;IAC7B,uEAAuE;IACvE,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,cAAc,EAAE,IAAI,EAAE,cAAc,GAAG,KAAK,EAAE,CAmBpF;AAED,4DAA4D;AAC5D,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,qBAAqB,EAAE,GAAG,CAAC,MAAM,CAAC,EAClC,MAAM,GAAE,KAAK,EAAO,GACnB,KAAK,EAAE,CAmBT"}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Detect hardcoded, user-facing strings in React/TypeScript source using the
|
|
4
|
+
* TypeScript compiler API.
|
|
5
|
+
*
|
|
6
|
+
* Two categories are reported:
|
|
7
|
+
* - `hardcoded-string` — literal JSX text, e.g. `<Button>Create</Button>`
|
|
8
|
+
* - `hardcoded-attribute` — literal `aria-label`/`title`/`alt`/`placeholder`,
|
|
9
|
+
* e.g. `<button aria-label="Close dialog">`
|
|
10
|
+
*
|
|
11
|
+
* Design goal: a low false-positive rate. We only flag *literal* values; any
|
|
12
|
+
* value that is already an expression (`{t('...')}`, `{label}`) is left alone,
|
|
13
|
+
* and text inside configured translation components (e.g. `<Trans>`) is ignored.
|
|
14
|
+
*/
|
|
15
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
18
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
19
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
20
|
+
}
|
|
21
|
+
Object.defineProperty(o, k2, desc);
|
|
22
|
+
}) : (function(o, m, k, k2) {
|
|
23
|
+
if (k2 === undefined) k2 = k;
|
|
24
|
+
o[k2] = m[k];
|
|
25
|
+
}));
|
|
26
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
27
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
28
|
+
}) : function(o, v) {
|
|
29
|
+
o["default"] = v;
|
|
30
|
+
});
|
|
31
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
32
|
+
var ownKeys = function(o) {
|
|
33
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
34
|
+
var ar = [];
|
|
35
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
36
|
+
return ar;
|
|
37
|
+
};
|
|
38
|
+
return ownKeys(o);
|
|
39
|
+
};
|
|
40
|
+
return function (mod) {
|
|
41
|
+
if (mod && mod.__esModule) return mod;
|
|
42
|
+
var result = {};
|
|
43
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
44
|
+
__setModuleDefault(result, mod);
|
|
45
|
+
return result;
|
|
46
|
+
};
|
|
47
|
+
})();
|
|
48
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
49
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
50
|
+
};
|
|
51
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
+
exports.DEFAULT_TRANSLATION_COMPONENTS = exports.DEFAULT_INCLUDE = void 0;
|
|
53
|
+
exports.analyzeProject = analyzeProject;
|
|
54
|
+
exports.analyzeSource = analyzeSource;
|
|
55
|
+
const fs = __importStar(require("node:fs"));
|
|
56
|
+
const path = __importStar(require("node:path"));
|
|
57
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
58
|
+
const core_1 = require("@localeguard/core");
|
|
59
|
+
const glob_1 = require("./glob");
|
|
60
|
+
exports.DEFAULT_INCLUDE = ["src/**/*.{ts,tsx}"];
|
|
61
|
+
exports.DEFAULT_TRANSLATION_COMPONENTS = ["Trans"];
|
|
62
|
+
/** Accessibility / UX attributes whose values reach end users. */
|
|
63
|
+
const LOCALIZABLE_ATTRS = new Set(["aria-label", "title", "alt", "placeholder"]);
|
|
64
|
+
/** Elements whose text is technical, not prose — never flagged. */
|
|
65
|
+
const NON_LOCALIZABLE_ELEMENTS = new Set([
|
|
66
|
+
"code",
|
|
67
|
+
"pre",
|
|
68
|
+
"kbd",
|
|
69
|
+
"samp",
|
|
70
|
+
"var",
|
|
71
|
+
"tt",
|
|
72
|
+
"script",
|
|
73
|
+
"style",
|
|
74
|
+
]);
|
|
75
|
+
const HAS_LETTER = /\p{L}/u;
|
|
76
|
+
const HTML_ENTITY = /&[a-zA-Z]+;|&#\d+;/g;
|
|
77
|
+
function analyzeProject(config, opts) {
|
|
78
|
+
const include = config.include ?? exports.DEFAULT_INCLUDE;
|
|
79
|
+
const translationComponents = new Set(config.translationComponents ?? exports.DEFAULT_TRANSLATION_COMPONENTS);
|
|
80
|
+
const files = (0, glob_1.findFiles)({ rootDir: opts.rootDir, include, ignore: config.ignore });
|
|
81
|
+
const issues = [];
|
|
82
|
+
for (const absFile of files) {
|
|
83
|
+
let text;
|
|
84
|
+
try {
|
|
85
|
+
text = fs.readFileSync(absFile, "utf8");
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const relPath = path.relative(opts.rootDir, absFile) || absFile;
|
|
91
|
+
analyzeSource(text, relPath, translationComponents, issues);
|
|
92
|
+
}
|
|
93
|
+
return issues;
|
|
94
|
+
}
|
|
95
|
+
/** Analyze a single source string. Exported for testing. */
|
|
96
|
+
function analyzeSource(text, fileName, translationComponents, issues = []) {
|
|
97
|
+
const sf = typescript_1.default.createSourceFile(fileName, text, typescript_1.default.ScriptTarget.Latest,
|
|
98
|
+
/* setParentNodes */ true, scriptKindFor(fileName));
|
|
99
|
+
const visit = (node) => {
|
|
100
|
+
if (typescript_1.default.isJsxText(node)) {
|
|
101
|
+
checkJsxText(node, sf, fileName, translationComponents, issues);
|
|
102
|
+
}
|
|
103
|
+
else if (typescript_1.default.isJsxAttribute(node)) {
|
|
104
|
+
checkJsxAttribute(node, sf, fileName, issues);
|
|
105
|
+
}
|
|
106
|
+
typescript_1.default.forEachChild(node, visit);
|
|
107
|
+
};
|
|
108
|
+
visit(sf);
|
|
109
|
+
return issues;
|
|
110
|
+
}
|
|
111
|
+
function checkJsxText(node, sf, fileName, translationComponents, issues) {
|
|
112
|
+
const raw = node.getText(sf);
|
|
113
|
+
const collapsed = raw.replace(/\s+/g, " ").trim();
|
|
114
|
+
if (!isLocalizableText(collapsed))
|
|
115
|
+
return;
|
|
116
|
+
if (isInsideSkippedElement(node, translationComponents))
|
|
117
|
+
return;
|
|
118
|
+
const leading = raw.length - raw.replace(/^\s+/, "").length;
|
|
119
|
+
const { line } = sf.getLineAndCharacterOfPosition(node.getStart(sf) + leading);
|
|
120
|
+
issues.push({
|
|
121
|
+
type: "hardcoded-string",
|
|
122
|
+
severity: core_1.SEVERITY_BY_TYPE["hardcoded-string"],
|
|
123
|
+
file: fileName,
|
|
124
|
+
line: line + 1,
|
|
125
|
+
message: `Hardcoded JSX text: ${quote(collapsed)}`,
|
|
126
|
+
suggestion: "Wrap user-facing text in a translation call, e.g. {t('...')}.",
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
function checkJsxAttribute(node, sf, fileName, issues) {
|
|
130
|
+
const name = node.name.getText(sf);
|
|
131
|
+
if (!LOCALIZABLE_ATTRS.has(name))
|
|
132
|
+
return;
|
|
133
|
+
const value = literalValueOf(node.initializer);
|
|
134
|
+
if (value === undefined)
|
|
135
|
+
return; // expression or no value — already dynamic
|
|
136
|
+
if (!HAS_LETTER.test(value))
|
|
137
|
+
return; // empty or non-text (e.g. alt="")
|
|
138
|
+
const { line } = sf.getLineAndCharacterOfPosition(node.getStart(sf));
|
|
139
|
+
issues.push({
|
|
140
|
+
type: "hardcoded-attribute",
|
|
141
|
+
severity: core_1.SEVERITY_BY_TYPE["hardcoded-attribute"],
|
|
142
|
+
file: fileName,
|
|
143
|
+
line: line + 1,
|
|
144
|
+
message: `Hardcoded ${name} value: ${quote(value)}`,
|
|
145
|
+
suggestion: `Localize the ${name} attribute, e.g. ${name}={t('...')}.`,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/** Returns the literal text of an attribute value, or undefined if dynamic. */
|
|
149
|
+
function literalValueOf(init) {
|
|
150
|
+
if (!init)
|
|
151
|
+
return undefined;
|
|
152
|
+
if (typescript_1.default.isStringLiteral(init))
|
|
153
|
+
return init.text;
|
|
154
|
+
// `attr={"literal"}` is still hardcoded; `attr={expr}` is not.
|
|
155
|
+
if (typescript_1.default.isJsxExpression(init) && init.expression && typescript_1.default.isStringLiteralLike(init.expression)) {
|
|
156
|
+
return init.expression.text;
|
|
157
|
+
}
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* True when the node is inside a translation component (e.g. `<Trans>`, whose
|
|
162
|
+
* text is intentional) or a technical element (e.g. `<code>`, whose text is not
|
|
163
|
+
* prose). Either way the text should not be flagged.
|
|
164
|
+
*/
|
|
165
|
+
function isInsideSkippedElement(node, components) {
|
|
166
|
+
let current = node.parent;
|
|
167
|
+
while (current) {
|
|
168
|
+
if (typescript_1.default.isJsxElement(current)) {
|
|
169
|
+
const name = jsxTagName(current.openingElement.tagName);
|
|
170
|
+
if (name && (components.has(name) || NON_LOCALIZABLE_ELEMENTS.has(name))) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
current = current.parent;
|
|
175
|
+
}
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
function jsxTagName(tag) {
|
|
179
|
+
if (typescript_1.default.isIdentifier(tag))
|
|
180
|
+
return tag.text;
|
|
181
|
+
if (typescript_1.default.isPropertyAccessExpression(tag))
|
|
182
|
+
return tag.name.text;
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
function isLocalizableText(collapsed) {
|
|
186
|
+
if (collapsed.length === 0)
|
|
187
|
+
return false;
|
|
188
|
+
return HAS_LETTER.test(collapsed.replace(HTML_ENTITY, ""));
|
|
189
|
+
}
|
|
190
|
+
function quote(value) {
|
|
191
|
+
const max = 50;
|
|
192
|
+
const truncated = value.length > max ? `${value.slice(0, max - 1)}…` : value;
|
|
193
|
+
return JSON.stringify(truncated);
|
|
194
|
+
}
|
|
195
|
+
function scriptKindFor(fileName) {
|
|
196
|
+
switch (path.extname(fileName)) {
|
|
197
|
+
case ".tsx":
|
|
198
|
+
return typescript_1.default.ScriptKind.TSX;
|
|
199
|
+
case ".jsx":
|
|
200
|
+
return typescript_1.default.ScriptKind.JSX;
|
|
201
|
+
case ".js":
|
|
202
|
+
return typescript_1.default.ScriptKind.JS;
|
|
203
|
+
case ".ts":
|
|
204
|
+
return typescript_1.default.ScriptKind.TS;
|
|
205
|
+
default:
|
|
206
|
+
return typescript_1.default.ScriptKind.TSX;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=analyzer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analyzer.js","sourceRoot":"","sources":["../../src/analyzer.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;GAYG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CH,wCAmBC;AAGD,sCAwBC;AAxFD,4CAA8B;AAC9B,gDAAkC;AAElC,4DAA4B;AAE5B,4CAAqD;AAGrD,iCAAmC;AAEtB,QAAA,eAAe,GAAG,CAAC,mBAAmB,CAAC,CAAC;AACxC,QAAA,8BAA8B,GAAG,CAAC,OAAO,CAAC,CAAC;AAExD,kEAAkE;AAClE,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC,CAAC,YAAY,EAAE,OAAO,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC;AAEjF,mEAAmE;AACnE,MAAM,wBAAwB,GAAG,IAAI,GAAG,CAAC;IACvC,MAAM;IACN,KAAK;IACL,KAAK;IACL,MAAM;IACN,KAAK;IACL,IAAI;IACJ,QAAQ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1B,MAAM,GAAG,GAAG,EAAE,CAAC;IACf,MAAM,SAAS,GAAG,KAAK,CAAC,MAAM,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC;IAC7E,OAAO,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;AACnC,CAAC;AAED,SAAS,aAAa,CAAC,QAAgB;IACrC,QAAQ,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC/B,KAAK,MAAM;YACT,OAAO,oBAAE,CAAC,UAAU,CAAC,GAAG,CAAC;QAC3B,KAAK,MAAM;YACT,OAAO,oBAAE,CAAC,UAAU,CAAC,GAAG,CAAC;QAC3B,KAAK,KAAK;YACR,OAAO,oBAAE,CAAC,UAAU,CAAC,EAAE,CAAC;QAC1B,KAAK,KAAK;YACR,OAAO,oBAAE,CAAC,UAAU,CAAC,EAAE,CAAC;QAC1B;YACE,OAAO,oBAAE,CAAC,UAAU,CAAC,GAAG,CAAC;IAC7B,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal glob-based file discovery (zero dependencies).
|
|
3
|
+
*
|
|
4
|
+
* Supports the subset of glob syntax LocaleGuard configs use:
|
|
5
|
+
* - `**` matches any number of path segments
|
|
6
|
+
* - `*` matches anything except a path separator
|
|
7
|
+
* - `?` matches a single non-separator character
|
|
8
|
+
* - `{a,b}` matches any of the comma-separated alternatives
|
|
9
|
+
*
|
|
10
|
+
* Paths are matched in POSIX form relative to the root directory.
|
|
11
|
+
*/
|
|
12
|
+
export declare function globToRegExp(glob: string): RegExp;
|
|
13
|
+
export interface FindFilesOptions {
|
|
14
|
+
rootDir: string;
|
|
15
|
+
include: string[];
|
|
16
|
+
ignore?: string[];
|
|
17
|
+
}
|
|
18
|
+
/** Return absolute paths of files under `rootDir` matching the patterns. */
|
|
19
|
+
export declare function findFiles(opts: FindFilesOptions): string[];
|
|
20
|
+
//# sourceMappingURL=glob.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"glob.d.ts","sourceRoot":"","sources":["../../src/glob.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAWH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAsCjD;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,4EAA4E;AAC5E,wBAAgB,SAAS,CAAC,IAAI,EAAE,gBAAgB,GAAG,MAAM,EAAE,CA6B1D"}
|
package/dist/src/glob.js
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Minimal glob-based file discovery (zero dependencies).
|
|
4
|
+
*
|
|
5
|
+
* Supports the subset of glob syntax LocaleGuard configs use:
|
|
6
|
+
* - `**` matches any number of path segments
|
|
7
|
+
* - `*` matches anything except a path separator
|
|
8
|
+
* - `?` matches a single non-separator character
|
|
9
|
+
* - `{a,b}` matches any of the comma-separated alternatives
|
|
10
|
+
*
|
|
11
|
+
* Paths are matched in POSIX form relative to the root directory.
|
|
12
|
+
*/
|
|
13
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}) : (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
o[k2] = m[k];
|
|
23
|
+
}));
|
|
24
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
25
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
26
|
+
}) : function(o, v) {
|
|
27
|
+
o["default"] = v;
|
|
28
|
+
});
|
|
29
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
30
|
+
var ownKeys = function(o) {
|
|
31
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
32
|
+
var ar = [];
|
|
33
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
34
|
+
return ar;
|
|
35
|
+
};
|
|
36
|
+
return ownKeys(o);
|
|
37
|
+
};
|
|
38
|
+
return function (mod) {
|
|
39
|
+
if (mod && mod.__esModule) return mod;
|
|
40
|
+
var result = {};
|
|
41
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
42
|
+
__setModuleDefault(result, mod);
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
})();
|
|
46
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
47
|
+
exports.globToRegExp = globToRegExp;
|
|
48
|
+
exports.findFiles = findFiles;
|
|
49
|
+
const fs = __importStar(require("node:fs"));
|
|
50
|
+
const path = __importStar(require("node:path"));
|
|
51
|
+
const ALWAYS_SKIP = new Set(["node_modules", ".git", "dist", "build", "coverage"]);
|
|
52
|
+
function escapeLiteral(text) {
|
|
53
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
54
|
+
}
|
|
55
|
+
function globToRegExp(glob) {
|
|
56
|
+
let re = "";
|
|
57
|
+
let i = 0;
|
|
58
|
+
while (i < glob.length) {
|
|
59
|
+
const c = glob[i];
|
|
60
|
+
if (c === "*") {
|
|
61
|
+
if (glob[i + 1] === "*") {
|
|
62
|
+
if (glob[i + 2] === "/") {
|
|
63
|
+
re += "(?:[^/]+/)*"; // **/ — zero or more segments
|
|
64
|
+
i += 3;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
re += ".*"; // ** — anything, including separators
|
|
68
|
+
i += 2;
|
|
69
|
+
}
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
re += "[^/]*";
|
|
73
|
+
i += 1;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (c === "?") {
|
|
77
|
+
re += "[^/]";
|
|
78
|
+
i += 1;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (c === "{") {
|
|
82
|
+
const end = glob.indexOf("}", i);
|
|
83
|
+
if (end !== -1) {
|
|
84
|
+
const alts = glob.slice(i + 1, end).split(",").map(escapeLiteral);
|
|
85
|
+
re += `(?:${alts.join("|")})`;
|
|
86
|
+
i = end + 1;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
re += escapeLiteral(c);
|
|
91
|
+
i += 1;
|
|
92
|
+
}
|
|
93
|
+
return new RegExp(`^${re}$`);
|
|
94
|
+
}
|
|
95
|
+
/** Return absolute paths of files under `rootDir` matching the patterns. */
|
|
96
|
+
function findFiles(opts) {
|
|
97
|
+
const includeRe = opts.include.map(globToRegExp);
|
|
98
|
+
const ignoreRe = (opts.ignore ?? []).map(globToRegExp);
|
|
99
|
+
const results = [];
|
|
100
|
+
const walk = (dir) => {
|
|
101
|
+
let entries;
|
|
102
|
+
try {
|
|
103
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
for (const entry of entries) {
|
|
109
|
+
const abs = path.join(dir, entry.name);
|
|
110
|
+
const rel = toPosix(path.relative(opts.rootDir, abs));
|
|
111
|
+
if (entry.isDirectory()) {
|
|
112
|
+
if (ALWAYS_SKIP.has(entry.name))
|
|
113
|
+
continue;
|
|
114
|
+
if (ignoreRe.some((re) => re.test(rel)))
|
|
115
|
+
continue;
|
|
116
|
+
walk(abs);
|
|
117
|
+
}
|
|
118
|
+
else if (entry.isFile()) {
|
|
119
|
+
if (ignoreRe.some((re) => re.test(rel)))
|
|
120
|
+
continue;
|
|
121
|
+
if (includeRe.some((re) => re.test(rel)))
|
|
122
|
+
results.push(abs);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
walk(opts.rootDir);
|
|
127
|
+
results.sort();
|
|
128
|
+
return results;
|
|
129
|
+
}
|
|
130
|
+
function toPosix(p) {
|
|
131
|
+
return p.split(path.sep).join("/");
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=glob.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"glob.js","sourceRoot":"","sources":["../../src/glob.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWH,oCAsCC;AASD,8BA6BC;AArFD,4CAA8B;AAC9B,gDAAkC;AAElC,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC;AAEnF,SAAS,aAAa,CAAC,IAAY;IACjC,OAAO,IAAI,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC;AACrD,CAAC;AAED,SAAgB,YAAY,CAAC,IAAY;IACvC,IAAI,EAAE,GAAG,EAAE,CAAC;IACZ,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,CAAE,CAAC;QACnB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;YACd,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC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O,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAC9D,CAAC;QACH,CAAC;IACH,CAAC,CAAC;IAEF,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACnB,OAAO,CAAC,IAAI,EAAE,CAAC;IACf,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,SAAS,OAAO,CAAC,CAAS;IACxB,OAAO,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @localeguard/react-analyzer — public API.
|
|
3
|
+
*/
|
|
4
|
+
export { analyzeProject, analyzeSource, DEFAULT_INCLUDE, DEFAULT_TRANSLATION_COMPONENTS, } from "./analyzer";
|
|
5
|
+
export type { AnalyzerConfig, AnalyzeOptions } from "./analyzer";
|
|
6
|
+
export { findFiles, globToRegExp } from "./glob";
|
|
7
|
+
export type { FindFilesOptions } from "./glob";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,cAAc,EACd,aAAa,EACb,eAAe,EACf,8BAA8B,GAC/B,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AACjE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACjD,YAAY,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @localeguard/react-analyzer — public API.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.globToRegExp = exports.findFiles = exports.DEFAULT_TRANSLATION_COMPONENTS = exports.DEFAULT_INCLUDE = exports.analyzeSource = exports.analyzeProject = void 0;
|
|
7
|
+
var analyzer_1 = require("./analyzer");
|
|
8
|
+
Object.defineProperty(exports, "analyzeProject", { enumerable: true, get: function () { return analyzer_1.analyzeProject; } });
|
|
9
|
+
Object.defineProperty(exports, "analyzeSource", { enumerable: true, get: function () { return analyzer_1.analyzeSource; } });
|
|
10
|
+
Object.defineProperty(exports, "DEFAULT_INCLUDE", { enumerable: true, get: function () { return analyzer_1.DEFAULT_INCLUDE; } });
|
|
11
|
+
Object.defineProperty(exports, "DEFAULT_TRANSLATION_COMPONENTS", { enumerable: true, get: function () { return analyzer_1.DEFAULT_TRANSLATION_COMPONENTS; } });
|
|
12
|
+
var glob_1 = require("./glob");
|
|
13
|
+
Object.defineProperty(exports, "findFiles", { enumerable: true, get: function () { return glob_1.findFiles; } });
|
|
14
|
+
Object.defineProperty(exports, "globToRegExp", { enumerable: true, get: function () { return glob_1.globToRegExp; } });
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,uCAKoB;AAJlB,0GAAA,cAAc,OAAA;AACd,yGAAA,aAAa,OAAA;AACb,2GAAA,eAAe,OAAA;AACf,0HAAA,8BAA8B,OAAA;AAGhC,+BAAiD;AAAxC,iGAAA,SAAS,OAAA;AAAE,oGAAA,YAAY,OAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@localeguard/react-analyzer",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Static analysis for LocaleGuard: detect hardcoded JSX text and unlocalized accessibility attributes in React/TypeScript source.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "isamrish",
|
|
7
|
+
"homepage": "https://github.com/isamrish/localeguard/tree/main/packages/react-analyzer#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/isamrish/localeguard.git",
|
|
11
|
+
"directory": "packages/react-analyzer"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/isamrish/localeguard/issues"
|
|
15
|
+
},
|
|
16
|
+
"main": "dist/src/index.js",
|
|
17
|
+
"types": "dist/src/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/src"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=18"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"prepublishOnly": "tsc -b"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@localeguard/core": "0.1.0",
|
|
29
|
+
"typescript": "^5.7.0"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"localization",
|
|
36
|
+
"i18n",
|
|
37
|
+
"react",
|
|
38
|
+
"jsx",
|
|
39
|
+
"static-analysis",
|
|
40
|
+
"accessibility"
|
|
41
|
+
]
|
|
42
|
+
}
|