i18next-cli 1.75.1 → 1.75.3
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/dist/cjs/cli.js +2 -2
- package/dist/cjs/extractor/core/ast-visitors.js +3 -2
- package/dist/cjs/extractor/core/key-finder.js +8 -6
- package/dist/cjs/extractor/parsers/call-expression-handler.js +4 -7
- package/dist/cjs/extractor/parsers/expression-resolver.js +176 -22
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/localize/localize.js +1 -1
- package/dist/esm/cli.js +2 -2
- package/dist/esm/extractor/core/ast-visitors.js +4 -3
- package/dist/esm/extractor/core/key-finder.js +2 -0
- package/dist/esm/extractor/parsers/call-expression-handler.js +4 -7
- package/dist/esm/extractor/parsers/expression-resolver.js +177 -22
- package/dist/esm/index.js +1 -1
- package/dist/esm/localize/localize.js +1 -1
- package/package.json +1 -1
- package/types/extractor/core/ast-visitors.d.ts.map +1 -1
- package/types/extractor/core/key-finder.d.ts.map +1 -1
- package/types/extractor/parsers/call-expression-handler.d.ts.map +1 -1
- package/types/extractor/parsers/expression-resolver.d.ts +22 -2
- package/types/extractor/parsers/expression-resolver.d.ts.map +1 -1
package/dist/cjs/cli.js
CHANGED
|
@@ -9,8 +9,8 @@ var node_util = require('node:util');
|
|
|
9
9
|
var config = require('./config.js');
|
|
10
10
|
var heuristicConfig = require('./heuristic-config.js');
|
|
11
11
|
var extractor = require('./extractor/core/extractor.js');
|
|
12
|
-
require('node:module');
|
|
13
12
|
require('node:path');
|
|
13
|
+
require('node:module');
|
|
14
14
|
var nestedObject = require('./utils/nested-object.js');
|
|
15
15
|
require('node:fs/promises');
|
|
16
16
|
require('jiti');
|
|
@@ -37,7 +37,7 @@ const program = new commander.Command();
|
|
|
37
37
|
program
|
|
38
38
|
.name('i18next-cli')
|
|
39
39
|
.description('A unified, high-performance i18next CLI.')
|
|
40
|
-
.version('1.75.
|
|
40
|
+
.version('1.75.3') // This string is replaced with the actual version at build time by rollup
|
|
41
41
|
.addHelpText('after', '\nUsing an AI coding agent? Install the i18next-localization skill: npx skills add i18next/i18next-cli\n');
|
|
42
42
|
// new: global config override option
|
|
43
43
|
program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)');
|
|
@@ -826,8 +826,8 @@ class ASTVisitors {
|
|
|
826
826
|
let members;
|
|
827
827
|
if (tsType.type === 'TsTypeLiteral')
|
|
828
828
|
members = tsType.members;
|
|
829
|
-
else if (tsType.type === 'TsTypeReference' && expressionResolver.typeRefName(tsType)) {
|
|
830
|
-
members = this.expressionResolver.getObjectTypeMembersRaw(expressionResolver.typeRefName(tsType));
|
|
829
|
+
else if (tsType.type === 'TsTypeReference' && this.expressionResolver.typeRefName(tsType)) {
|
|
830
|
+
members = this.expressionResolver.getObjectTypeMembersRaw(this.expressionResolver.typeRefName(tsType));
|
|
831
831
|
}
|
|
832
832
|
if (!Array.isArray(members))
|
|
833
833
|
return [];
|
|
@@ -1053,6 +1053,7 @@ class ASTVisitors {
|
|
|
1053
1053
|
setCurrentFile(file, code) {
|
|
1054
1054
|
this.currentFile = file;
|
|
1055
1055
|
this.currentCode = code;
|
|
1056
|
+
this.expressionResolver.setCurrentFile(file);
|
|
1056
1057
|
}
|
|
1057
1058
|
/**
|
|
1058
1059
|
* Returns the currently set file path.
|
|
@@ -6,6 +6,7 @@ var logger = require('../../utils/logger.js');
|
|
|
6
6
|
var pluginManager = require('../plugin-manager.js');
|
|
7
7
|
var astVisitors = require('./ast-visitors.js');
|
|
8
8
|
var expressionResolver = require('../parsers/expression-resolver.js');
|
|
9
|
+
var config = require('../../config.js');
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Main function for finding translation keys across all source files in a project.
|
|
@@ -35,10 +36,10 @@ var expressionResolver = require('../parsers/expression-resolver.js');
|
|
|
35
36
|
* console.log(`Found ${keys.size} unique translation keys`)
|
|
36
37
|
* ```
|
|
37
38
|
*/
|
|
38
|
-
async function findKeys(config, logger$1 = new logger.ConsoleLogger(), fileErrors) {
|
|
39
|
-
const { plugins: pluginsOrUndefined, ...otherConfig } = config;
|
|
39
|
+
async function findKeys(config$1, logger$1 = new logger.ConsoleLogger(), fileErrors) {
|
|
40
|
+
const { plugins: pluginsOrUndefined, ...otherConfig } = config$1;
|
|
40
41
|
const plugins = pluginsOrUndefined || [];
|
|
41
|
-
const sourceFiles = await processSourceFiles(config);
|
|
42
|
+
const sourceFiles = await processSourceFiles(config$1);
|
|
42
43
|
const allKeys = new Map();
|
|
43
44
|
// 1. Create the base context with config and logger.
|
|
44
45
|
const pluginContext = pluginManager.createPluginContext(allKeys, plugins, otherConfig, logger$1);
|
|
@@ -57,7 +58,7 @@ async function findKeys(config, logger$1 = new logger.ConsoleLogger(), fileError
|
|
|
57
58
|
resolvePossibleKeyStringValues: (expression) => {
|
|
58
59
|
return plugins.flatMap(plugin => {
|
|
59
60
|
try {
|
|
60
|
-
return plugin.extractKeysFromExpression?.(expression, config, logger$1) ?? [];
|
|
61
|
+
return plugin.extractKeysFromExpression?.(expression, config$1, logger$1) ?? [];
|
|
61
62
|
}
|
|
62
63
|
catch (err) {
|
|
63
64
|
logger$1.warn(`Plugin ${plugin.name} extractKeysFromExpression failed:`, err);
|
|
@@ -68,7 +69,7 @@ async function findKeys(config, logger$1 = new logger.ConsoleLogger(), fileError
|
|
|
68
69
|
resolvePossibleContextStringValues: (expression) => {
|
|
69
70
|
return plugins.flatMap(plugin => {
|
|
70
71
|
try {
|
|
71
|
-
return plugin.extractContextFromExpression?.(expression, config, logger$1) ?? [];
|
|
72
|
+
return plugin.extractContextFromExpression?.(expression, config$1, logger$1) ?? [];
|
|
72
73
|
}
|
|
73
74
|
catch (err) {
|
|
74
75
|
logger$1.warn(`Plugin ${plugin.name} extractContextFromExpression failed:`, err);
|
|
@@ -80,7 +81,8 @@ async function findKeys(config, logger$1 = new logger.ConsoleLogger(), fileError
|
|
|
80
81
|
// 3. Create the visitor instance, passing it the context.
|
|
81
82
|
// Use a shared ExpressionResolver so captured enums/objects in one file are available when resolving MemberExpressions
|
|
82
83
|
const sharedExpressionResolver = new expressionResolver.ExpressionResolver(hooks);
|
|
83
|
-
sharedExpressionResolver.maxMemberDepth = config.extract.maxTypeDepth ?? 5;
|
|
84
|
+
sharedExpressionResolver.maxMemberDepth = config$1.extract.maxTypeDepth ?? 5;
|
|
85
|
+
sharedExpressionResolver.pathAliases = await config.getTsConfigAliases();
|
|
84
86
|
const astVisitors$1 = new astVisitors.ASTVisitors(otherConfig, pluginContext, logger$1, hooks, sharedExpressionResolver);
|
|
85
87
|
// 4. "Wire up" the visitor's scope method to the context.
|
|
86
88
|
// This avoids a circular dependency while giving plugins access to the scope.
|
|
@@ -184,11 +184,11 @@ class CallExpressionHandler {
|
|
|
184
184
|
}
|
|
185
185
|
let defaultValue;
|
|
186
186
|
let options;
|
|
187
|
+
// Options inline or in a variable: `t('key', { count })`, `t('key', params)` (#307)
|
|
187
188
|
if (node.arguments.length > 1) {
|
|
188
189
|
const arg2 = astUtils.unwrapParens(node.arguments[1].expression);
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
190
|
+
options = this.expressionResolver.objectLiteralOf(arg2);
|
|
191
|
+
if (options) ;
|
|
192
192
|
else if (arg2.type === 'StringLiteral') {
|
|
193
193
|
defaultValue = arg2.value;
|
|
194
194
|
}
|
|
@@ -200,10 +200,7 @@ class CallExpressionHandler {
|
|
|
200
200
|
}
|
|
201
201
|
}
|
|
202
202
|
if (node.arguments.length > 2) {
|
|
203
|
-
|
|
204
|
-
if (arg3.type === 'ObjectExpression') {
|
|
205
|
-
options = arg3;
|
|
206
|
-
}
|
|
203
|
+
options = this.expressionResolver.objectLiteralOf(node.arguments[2].expression) ?? options;
|
|
207
204
|
}
|
|
208
205
|
const defaultValueFromOptions = options ? this.getDefaultValueProp(options, 'defaultValue') : undefined;
|
|
209
206
|
const finalDefaultValue = (typeof defaultValueFromOptions === 'string' ? defaultValueFromOptions : defaultValue);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var node_path = require('node:path');
|
|
3
4
|
var astUtils = require('./ast-utils.js');
|
|
4
5
|
|
|
5
6
|
// Whether `next` holds everything `prev` does: every value of a list, every
|
|
@@ -61,9 +62,45 @@ const memberByKey = (members, key) => {
|
|
|
61
62
|
}
|
|
62
63
|
return index.get(key);
|
|
63
64
|
};
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
// `/src/api/github-api.ts`, `.d.ts`, or an import of `./github-api.js` -> `/src/api/github-api`
|
|
66
|
+
const moduleKey = (path) => path.replace(/(\.d)?\.[cm]?[jt]sx?$/, '');
|
|
67
|
+
/**
|
|
68
|
+
* A cross-file table keyed by declared name. Every entry is also kept under the module that
|
|
69
|
+
* declares it (`/src/api/github-api#components`), and a lookup made while a file is being read
|
|
70
|
+
* first tries where that file gets the name from: the module it imported it from, then its own
|
|
71
|
+
* declaration. The bare name, held by whichever file declared it last, is the fallback, so two
|
|
72
|
+
* generated clients that both declare `components` no longer overwrite each other (#306).
|
|
73
|
+
*/
|
|
74
|
+
class ModuleTable extends Map {
|
|
75
|
+
scope;
|
|
76
|
+
constructor(scope) {
|
|
77
|
+
super();
|
|
78
|
+
this.scope = scope;
|
|
79
|
+
}
|
|
80
|
+
get(name) {
|
|
81
|
+
for (const key of this.scope.keysOf(name)) {
|
|
82
|
+
const value = super.get(key);
|
|
83
|
+
if (value !== undefined)
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
has(name) {
|
|
89
|
+
return this.scope.keysOf(name).some(key => super.has(key));
|
|
90
|
+
}
|
|
91
|
+
set(name, value) {
|
|
92
|
+
const key = this.scope.declaringKey(name);
|
|
93
|
+
if (key)
|
|
94
|
+
super.set(key, value);
|
|
95
|
+
return super.set(name, value);
|
|
96
|
+
}
|
|
97
|
+
delete(name) {
|
|
98
|
+
const key = this.scope.declaringKey(name);
|
|
99
|
+
if (key)
|
|
100
|
+
super.delete(key);
|
|
101
|
+
return super.delete(name);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
67
104
|
// `T['a']` / `T[200]` -> 'a' / '200'
|
|
68
105
|
const indexedKey = (type) => type?.indexType?.type === 'TsLiteralType' ? literalKey(type.indexType.literal) : undefined;
|
|
69
106
|
// Whether replacing `prev` by `next` only adds to it.
|
|
@@ -96,33 +133,33 @@ class ExpressionResolver {
|
|
|
96
133
|
// - Record<string, string> (object of static string properties)
|
|
97
134
|
variableTable = new Map();
|
|
98
135
|
// Shared (cross-file) table for enums / exported object maps that should persist
|
|
99
|
-
sharedEnumTable = new
|
|
136
|
+
sharedEnumTable = new ModuleTable(this);
|
|
100
137
|
// Per-file table for type aliases: Maps typeName -> string[]
|
|
101
138
|
// e.g. `type ChangeType = 'all' | 'next' | 'this'` -> { ChangeType: ['all', 'next', 'this'] }
|
|
102
139
|
typeAliasTable = new Map();
|
|
103
140
|
// Shared (cross-file) table for string-array constants (e.g. `as const` arrays).
|
|
104
141
|
// Persists across resetFileSymbols() so exported arrays are visible to importers.
|
|
105
|
-
sharedVariableTable = new
|
|
142
|
+
sharedVariableTable = new ModuleTable(this);
|
|
106
143
|
// Shared (cross-file) table for as-const object maps, `export const LABELS = { … } as const`,
|
|
107
144
|
// so `LABELS.save` and `Object.keys(LABELS)` resolve in importing files.
|
|
108
|
-
sharedObjectMapTable = new
|
|
145
|
+
sharedObjectMapTable = new ModuleTable(this);
|
|
109
146
|
// Shared (cross-file) table for type aliases — populated alongside typeAliasTable.
|
|
110
147
|
// Persists across resetFileSymbols() so exported type aliases are visible to importers.
|
|
111
|
-
sharedTypeAliasTable = new
|
|
148
|
+
sharedTypeAliasTable = new ModuleTable(this);
|
|
112
149
|
// Shared (cross-file) table for function return-value sets. Populated from
|
|
113
150
|
// both explicit return-type annotations and body-inferred return values so
|
|
114
151
|
// that `t(fn())` / `const x = fn(); t(\`...${x}...\`)` work across files.
|
|
115
152
|
// Persists across resetFileSymbols() just like the other shared tables.
|
|
116
|
-
sharedFunctionReturnTable = new
|
|
153
|
+
sharedFunctionReturnTable = new ModuleTable(this);
|
|
117
154
|
// Temporary per-scope variable overrides, used to inject .map() / .forEach()
|
|
118
155
|
// callback parameters while the callback body is being walked.
|
|
119
156
|
temporaryVariables = new Map();
|
|
120
157
|
// Shared (cross-file) table for object-shaped types: interfaces and object
|
|
121
158
|
// type aliases. Maps typeName -> { memberName: possible string values }.
|
|
122
159
|
// e.g. `interface IProps { size: ChangeType }` -> { IProps: { size: ['all','next'] } }
|
|
123
|
-
objectTypeTable = new
|
|
160
|
+
objectTypeTable = new ModuleTable(this);
|
|
124
161
|
// Raw TS member nodes of local interfaces / object type aliases, keyed by type name.
|
|
125
|
-
objectTypeMembersRaw = new
|
|
162
|
+
objectTypeMembersRaw = new ModuleTable(this);
|
|
126
163
|
// Temporary per-scope bindings for identifiers holding an object-shaped type,
|
|
127
164
|
// e.g. `function f(props: IProps)` -> { props: { size: ['all','next'] } }.
|
|
128
165
|
temporaryObjectVariables = new Map();
|
|
@@ -139,15 +176,18 @@ class ExpressionResolver {
|
|
|
139
176
|
// members are declared under their own names, so `Types.COLORS` resolves as `COLORS`.
|
|
140
177
|
sharedNamespaceNames = new Set();
|
|
141
178
|
// Type parameters of generic object types, e.g. `type Base<T> = { type: T }`.
|
|
142
|
-
objectTypeParams = new
|
|
179
|
+
objectTypeParams = new ModuleTable(this);
|
|
143
180
|
// Values bound to type parameters while a generic type is being resolved.
|
|
144
181
|
typeParamBindings = new Map();
|
|
145
182
|
// Shared (cross-file) member maps of functions returning an object type,
|
|
146
183
|
// e.g. `const getAnimal = (): Animal => …` -> { getAnimal: { type: ['DUCK', 'DOG'] } }
|
|
147
|
-
sharedFunctionReturnMembers = new
|
|
184
|
+
sharedFunctionReturnMembers = new ModuleTable(this);
|
|
148
185
|
// Per-file identifiers holding an object type, from their annotation or from the
|
|
149
186
|
// function they were initialised by: `const animal = getAnimal()` -> { animal: { type: [...] } }
|
|
150
187
|
objectVariables = new Map();
|
|
188
|
+
// Per-file object literals by variable, so `const params = { count }; t('key', params)`
|
|
189
|
+
// reads its options like an inline object (#307)
|
|
190
|
+
objectLiterals = new Map();
|
|
151
191
|
// Set while the pre-scan runs. Captures that read other declarations queue a
|
|
152
192
|
// replay here, because what they read may be declared in a file scanned later.
|
|
153
193
|
recording = false;
|
|
@@ -159,9 +199,92 @@ class ExpressionResolver {
|
|
|
159
199
|
// this many levels below a declared or indexed type, so recursive types stay small
|
|
160
200
|
// (`extract.maxTypeDepth`).
|
|
161
201
|
maxMemberDepth = 5;
|
|
202
|
+
// The module being read (its path without extension), and where each module gets its
|
|
203
|
+
// names from (see ModuleTable)
|
|
204
|
+
currentModule;
|
|
205
|
+
knownModules = new Set();
|
|
206
|
+
// module -> local name -> { from: the import's resolved path, name: the declared name }
|
|
207
|
+
fileImports = new Map();
|
|
208
|
+
// module -> namespace import's local name -> the import's resolved path
|
|
209
|
+
fileNamespaces = new Map();
|
|
210
|
+
// module -> the variables it declares, which shadow the same names declared in other files
|
|
211
|
+
fileVariables = new Map();
|
|
212
|
+
// tsconfig `paths`, `@api` -> `/abs/src/api`
|
|
213
|
+
pathAliases = {};
|
|
162
214
|
constructor(hooks) {
|
|
163
215
|
this.hooks = hooks;
|
|
164
216
|
}
|
|
217
|
+
setCurrentFile(file) {
|
|
218
|
+
this.currentModule = moduleKey(node_path.resolve(file));
|
|
219
|
+
this.knownModules.add(this.currentModule);
|
|
220
|
+
}
|
|
221
|
+
// `./api/github-api` / `@api/github-api` imported in the current file -> its path, or undefined
|
|
222
|
+
// for a package or anything else that isn't a file of this project
|
|
223
|
+
importPath(source) {
|
|
224
|
+
if (!this.currentModule)
|
|
225
|
+
return undefined;
|
|
226
|
+
if (source.startsWith('.'))
|
|
227
|
+
return moduleKey(node_path.resolve(node_path.dirname(this.currentModule), source));
|
|
228
|
+
for (const [alias, path] of Object.entries(this.pathAliases)) {
|
|
229
|
+
if (source === alias || source.startsWith(`${alias}/`))
|
|
230
|
+
return moduleKey(node_path.resolve(path, `.${source.slice(alias.length)}`));
|
|
231
|
+
}
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
// The scanned module an import path points to: the file itself or its `index`
|
|
235
|
+
moduleOf(path) {
|
|
236
|
+
return [path, `${path}/index`].find(m => this.knownModules.has(m));
|
|
237
|
+
}
|
|
238
|
+
// ModuleTable: the keys `name` read in the current file may be stored under, most specific first
|
|
239
|
+
keysOf(name) {
|
|
240
|
+
if (name.includes('#') || !this.currentModule)
|
|
241
|
+
return [name];
|
|
242
|
+
const keys = [];
|
|
243
|
+
const imported = this.fileImports.get(this.currentModule)?.get(name);
|
|
244
|
+
const module = imported && this.moduleOf(imported.from);
|
|
245
|
+
if (module)
|
|
246
|
+
keys.push(`${module}#${imported.name}`);
|
|
247
|
+
keys.push(`${this.currentModule}#${name}`);
|
|
248
|
+
// A variable of this file never means another file's same-named one, even when its own
|
|
249
|
+
// value isn't captured (`const params = { count }` vs another file's `params` array) (#307)
|
|
250
|
+
if (!this.fileVariables.get(this.currentModule)?.has(name))
|
|
251
|
+
keys.push(name);
|
|
252
|
+
return keys;
|
|
253
|
+
}
|
|
254
|
+
// ModuleTable: the key a declaration made in the current file is also stored under
|
|
255
|
+
declaringKey(name) {
|
|
256
|
+
return this.currentModule && !name.includes('#') ? `${this.currentModule}#${name}` : undefined;
|
|
257
|
+
}
|
|
258
|
+
// Replays run after every file was read: run each in the file it was recorded in
|
|
259
|
+
inCurrentFile(fn) {
|
|
260
|
+
const module = this.currentModule;
|
|
261
|
+
return () => {
|
|
262
|
+
const saved = this.currentModule;
|
|
263
|
+
this.currentModule = module;
|
|
264
|
+
try {
|
|
265
|
+
fn();
|
|
266
|
+
}
|
|
267
|
+
finally {
|
|
268
|
+
this.currentModule = saved;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* `Color` / `Types.Color` / `NS.Inner.Color` -> 'Color'. Declarations are looked up by their
|
|
274
|
+
* own name, so a qualified name resolves by its last part, whatever namespace holds it, and
|
|
275
|
+
* through a namespace import of a module (`GH.components`) by that module's declaration.
|
|
276
|
+
*/
|
|
277
|
+
typeRefName(type) {
|
|
278
|
+
const typeName = type?.typeName;
|
|
279
|
+
if (typeName?.type === 'Identifier')
|
|
280
|
+
return typeName.value;
|
|
281
|
+
if (typeName?.type !== 'TsQualifiedName')
|
|
282
|
+
return undefined;
|
|
283
|
+
const name = typeName.right?.value;
|
|
284
|
+
const from = typeName.left?.type === 'Identifier' && this.currentModule ? this.fileNamespaces.get(this.currentModule)?.get(typeName.left.value) : undefined;
|
|
285
|
+
const module = from && this.moduleOf(from);
|
|
286
|
+
return module && name ? `${module}#${name}` : name;
|
|
287
|
+
}
|
|
165
288
|
/**
|
|
166
289
|
* Clear per-file captured variables. Enums / shared maps are kept.
|
|
167
290
|
*/
|
|
@@ -171,6 +294,7 @@ class ExpressionResolver {
|
|
|
171
294
|
this.temporaryVariables.clear();
|
|
172
295
|
this.arrayElementMembers.clear();
|
|
173
296
|
this.objectVariables.clear();
|
|
297
|
+
this.objectLiterals.clear();
|
|
174
298
|
}
|
|
175
299
|
/**
|
|
176
300
|
* Call once every file has been pre-scanned. Captures ran in file order, so one
|
|
@@ -181,6 +305,8 @@ class ExpressionResolver {
|
|
|
181
305
|
*/
|
|
182
306
|
finishPreScan() {
|
|
183
307
|
this.resetFileSymbols();
|
|
308
|
+
// Mirroring onto import aliases writes bare names only; each replay restores its own file
|
|
309
|
+
this.currentModule = undefined;
|
|
184
310
|
// ponytail: each round fixes one more link of a chain declared against file order; 10 is plenty
|
|
185
311
|
for (let round = 0; round < 10; round++) {
|
|
186
312
|
this.applyImportAliases();
|
|
@@ -284,6 +410,11 @@ class ExpressionResolver {
|
|
|
284
410
|
if (node.id.type !== 'Identifier')
|
|
285
411
|
return;
|
|
286
412
|
const name = node.id.value;
|
|
413
|
+
if (this.currentModule) {
|
|
414
|
+
if (!this.fileVariables.has(this.currentModule))
|
|
415
|
+
this.fileVariables.set(this.currentModule, new Set());
|
|
416
|
+
this.fileVariables.get(this.currentModule).add(name);
|
|
417
|
+
}
|
|
287
418
|
// `const items: IProps[] = …` → remember the element's member map so
|
|
288
419
|
// `items.map(({ size }) => …)` can bind the callback parameter.
|
|
289
420
|
const idTypeAnnotation = this.extractTypeAnnotation(node.id);
|
|
@@ -318,6 +449,7 @@ class ExpressionResolver {
|
|
|
318
449
|
const unwrappedInit = unwrapExpr(init);
|
|
319
450
|
// ObjectExpression -> map of string props
|
|
320
451
|
if (unwrappedInit.type === 'ObjectExpression' && Array.isArray(unwrappedInit.properties)) {
|
|
452
|
+
this.objectLiterals.set(name, unwrappedInit);
|
|
321
453
|
const map = {};
|
|
322
454
|
for (const p of unwrappedInit.properties) {
|
|
323
455
|
if (!p || p.type !== 'KeyValueProperty')
|
|
@@ -404,7 +536,7 @@ class ExpressionResolver {
|
|
|
404
536
|
if (!tsType)
|
|
405
537
|
return;
|
|
406
538
|
if (this.recording)
|
|
407
|
-
this.replays.push(() => this.captureTypeAliasDeclaration(node));
|
|
539
|
+
this.replays.push(this.inCurrentFile(() => this.captureTypeAliasDeclaration(node)));
|
|
408
540
|
const params = node.typeParams?.parameters;
|
|
409
541
|
// `type IProps = { size: ChangeType }` — object shape, not a string union.
|
|
410
542
|
if (tsType.type === 'TsTypeLiteral') {
|
|
@@ -441,18 +573,31 @@ class ExpressionResolver {
|
|
|
441
573
|
*/
|
|
442
574
|
captureImportDeclaration(node) {
|
|
443
575
|
try {
|
|
576
|
+
const from = node?.type === 'ImportDeclaration' && typeof node.source?.value === 'string' ? this.importPath(node.source.value) : undefined;
|
|
577
|
+
const imports = from && this.currentModule ? (this.fileImports.get(this.currentModule) ?? new Map()) : undefined;
|
|
578
|
+
if (imports)
|
|
579
|
+
this.fileImports.set(this.currentModule, imports);
|
|
444
580
|
for (const spec of node?.specifiers ?? []) {
|
|
445
581
|
// `import * as Types from …` / `export * as Types from …`
|
|
446
|
-
if (spec?.type === 'ImportNamespaceSpecifier' && spec.local?.value)
|
|
582
|
+
if (spec?.type === 'ImportNamespaceSpecifier' && spec.local?.value) {
|
|
447
583
|
this.sharedNamespaceNames.add(spec.local.value);
|
|
584
|
+
if (from) {
|
|
585
|
+
if (!this.fileNamespaces.has(this.currentModule))
|
|
586
|
+
this.fileNamespaces.set(this.currentModule, new Map());
|
|
587
|
+
this.fileNamespaces.get(this.currentModule).set(spec.local.value, from);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
448
590
|
if (spec?.type === 'ExportNamespaceSpecifier' && spec.name?.value)
|
|
449
591
|
this.sharedNamespaceNames.add(spec.name.value);
|
|
450
592
|
if (spec?.type !== 'ImportSpecifier')
|
|
451
593
|
continue;
|
|
452
594
|
const local = spec.local?.value;
|
|
453
|
-
const imported = spec.imported?.value;
|
|
595
|
+
const imported = spec.imported?.value ?? local;
|
|
454
596
|
if (local && imported && local !== imported)
|
|
455
597
|
this.sharedImportAliases.set(local, imported);
|
|
598
|
+
// `import { components as githubComponents } from './github-api'` → that module's `components`
|
|
599
|
+
if (local && imported && imports)
|
|
600
|
+
imports.set(local, { from: from, name: imported });
|
|
456
601
|
}
|
|
457
602
|
}
|
|
458
603
|
catch {
|
|
@@ -469,7 +614,10 @@ class ExpressionResolver {
|
|
|
469
614
|
const ns = expr.object.value;
|
|
470
615
|
if (!this.sharedNamespaceNames.has(this.sharedImportAliases.get(ns) ?? ns))
|
|
471
616
|
return expr;
|
|
472
|
-
|
|
617
|
+
// `GH.components` with `import * as GH from './github-api'` → that module's `components`
|
|
618
|
+
const from = this.currentModule ? this.fileNamespaces.get(this.currentModule)?.get(ns) : undefined;
|
|
619
|
+
const module = from && this.moduleOf(from);
|
|
620
|
+
return { type: 'Identifier', value: module ? `${module}#${expr.property.value}` : expr.property.value, span: expr.span };
|
|
473
621
|
}
|
|
474
622
|
// The variable a `typeof` names: `typeof LABELS`, or `typeof Types.LABELS` through a namespace import
|
|
475
623
|
typeQueryName(exprName) {
|
|
@@ -519,7 +667,7 @@ class ExpressionResolver {
|
|
|
519
667
|
if (!name)
|
|
520
668
|
return;
|
|
521
669
|
if (this.recording)
|
|
522
|
-
this.replays.push(() => this.captureInterfaceDeclaration(node));
|
|
670
|
+
this.replays.push(this.inCurrentFile(() => this.captureInterfaceDeclaration(node)));
|
|
523
671
|
const params = node.typeParams?.parameters;
|
|
524
672
|
if (!this.replaying && Array.isArray(node?.body?.body))
|
|
525
673
|
this.setObjectTypeRaw(name, node.body.body, params);
|
|
@@ -658,7 +806,7 @@ class ExpressionResolver {
|
|
|
658
806
|
}
|
|
659
807
|
return Object.keys(merged).length > 0 ? merged : undefined;
|
|
660
808
|
}
|
|
661
|
-
const name = tsType.type === 'TsTypeReference' ? typeRefName(tsType) : undefined;
|
|
809
|
+
const name = tsType.type === 'TsTypeReference' ? this.typeRefName(tsType) : undefined;
|
|
662
810
|
if (name) {
|
|
663
811
|
// `Base<'DUCK'>` → instantiate the generic type's members with the argument
|
|
664
812
|
const args = tsType.typeParams?.params;
|
|
@@ -713,7 +861,7 @@ class ExpressionResolver {
|
|
|
713
861
|
obj = obj.typeAnnotation;
|
|
714
862
|
if (obj?.type === 'TsIndexedAccessType')
|
|
715
863
|
obj = this.indexedAccessTarget(obj);
|
|
716
|
-
const name = obj?.type === 'TsTypeReference' && !obj.typeParams ? typeRefName(obj) : undefined;
|
|
864
|
+
const name = obj?.type === 'TsTypeReference' && !obj.typeParams ? this.typeRefName(obj) : undefined;
|
|
717
865
|
const members = obj?.type === 'TsTypeLiteral'
|
|
718
866
|
? obj.members
|
|
719
867
|
: name && !this.objectTypeParams.has(name) ? this.objectTypeMembersRaw.get(name) : undefined;
|
|
@@ -871,7 +1019,7 @@ class ExpressionResolver {
|
|
|
871
1019
|
};
|
|
872
1020
|
store();
|
|
873
1021
|
if (this.recording && (returnType || refs.length > 0))
|
|
874
|
-
this.replays.push(store);
|
|
1022
|
+
this.replays.push(this.inCurrentFile(store));
|
|
875
1023
|
}
|
|
876
1024
|
/**
|
|
877
1025
|
* Collect a function's return expressions (the expression body of an arrow
|
|
@@ -971,6 +1119,13 @@ class ExpressionResolver {
|
|
|
971
1119
|
return v;
|
|
972
1120
|
return this.sharedVariableTable.get(name);
|
|
973
1121
|
}
|
|
1122
|
+
/** The object literal `expr` is, inline or through a variable of this file, or undefined. */
|
|
1123
|
+
objectLiteralOf(expr) {
|
|
1124
|
+
expr = unwrapExpr(expr);
|
|
1125
|
+
if (expr?.type === 'ObjectExpression')
|
|
1126
|
+
return expr;
|
|
1127
|
+
return expr?.type === 'Identifier' ? this.objectLiterals.get(expr.value) : undefined;
|
|
1128
|
+
}
|
|
974
1129
|
/**
|
|
975
1130
|
* Return the as-const object map stored for a variable name.
|
|
976
1131
|
* Returns undefined if the name is not a known object map.
|
|
@@ -1332,7 +1487,7 @@ class ExpressionResolver {
|
|
|
1332
1487
|
// where `type ChangeType = 'all' | 'next' | 'this'` was captured earlier.
|
|
1333
1488
|
// Also handles `declare const d: SomeEnum` where SomeEnum is a TS enum with string values.
|
|
1334
1489
|
if (type.type === 'TsTypeReference') {
|
|
1335
|
-
const typeName = typeRefName(type);
|
|
1490
|
+
const typeName = this.typeRefName(type);
|
|
1336
1491
|
if (typeName) {
|
|
1337
1492
|
// Inside a generic type: the parameter's argument, default or constraint
|
|
1338
1493
|
const bound = this.typeParamBindings.get(typeName);
|
|
@@ -1482,4 +1637,3 @@ class ExpressionResolver {
|
|
|
1482
1637
|
|
|
1483
1638
|
exports.ExpressionResolver = ExpressionResolver;
|
|
1484
1639
|
exports.nestedMembers = nestedMembers;
|
|
1485
|
-
exports.typeRefName = typeRefName;
|
package/dist/cjs/index.js
CHANGED
|
@@ -4,6 +4,7 @@ var config = require('./config.js');
|
|
|
4
4
|
var extractor = require('./extractor/core/extractor.js');
|
|
5
5
|
var keyFinder = require('./extractor/core/key-finder.js');
|
|
6
6
|
var translationManager = require('./extractor/core/translation-manager.js');
|
|
7
|
+
require('node:path');
|
|
7
8
|
require('node:module');
|
|
8
9
|
var linter = require('./linter.js');
|
|
9
10
|
var syncer = require('./syncer.js');
|
|
@@ -15,7 +16,6 @@ require('./utils/jsx-attributes.js');
|
|
|
15
16
|
require('magic-string');
|
|
16
17
|
var localize = require('./localize/localize.js');
|
|
17
18
|
require('node:fs/promises');
|
|
18
|
-
require('node:path');
|
|
19
19
|
var agentPrompt = require('./localize/agent-prompt.js');
|
|
20
20
|
|
|
21
21
|
|
|
@@ -8,8 +8,8 @@ var glob = require('glob');
|
|
|
8
8
|
var config = require('../config.js');
|
|
9
9
|
var heuristicConfig = require('../heuristic-config.js');
|
|
10
10
|
var extractor = require('../extractor/core/extractor.js');
|
|
11
|
-
require('node:module');
|
|
12
11
|
require('node:path');
|
|
12
|
+
require('node:module');
|
|
13
13
|
var nestedObject = require('../utils/nested-object.js');
|
|
14
14
|
require('jiti');
|
|
15
15
|
require('@croct/json5-parser');
|
package/dist/esm/cli.js
CHANGED
|
@@ -7,8 +7,8 @@ import { styleText } from 'node:util';
|
|
|
7
7
|
import { ensureConfig, loadConfig } from './config.js';
|
|
8
8
|
import { detectConfig } from './heuristic-config.js';
|
|
9
9
|
import { runExtractor } from './extractor/core/extractor.js';
|
|
10
|
-
import 'node:module';
|
|
11
10
|
import 'node:path';
|
|
11
|
+
import 'node:module';
|
|
12
12
|
import { getNestedKeys, getNestedValue } from './utils/nested-object.js';
|
|
13
13
|
import 'node:fs/promises';
|
|
14
14
|
import 'jiti';
|
|
@@ -31,7 +31,7 @@ const program = new Command();
|
|
|
31
31
|
program
|
|
32
32
|
.name('i18next-cli')
|
|
33
33
|
.description('A unified, high-performance i18next CLI.')
|
|
34
|
-
.version('1.75.
|
|
34
|
+
.version('1.75.3') // This string is replaced with the actual version at build time by rollup
|
|
35
35
|
.addHelpText('after', '\nUsing an AI coding agent? Install the i18next-localization skill: npx skills add i18next/i18next-cli\n');
|
|
36
36
|
// new: global config override option
|
|
37
37
|
program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)');
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ScopeManager } from '../parsers/scope-manager.js';
|
|
2
|
-
import { ExpressionResolver, nestedMembers
|
|
2
|
+
import { ExpressionResolver, nestedMembers } from '../parsers/expression-resolver.js';
|
|
3
3
|
import { CallExpressionHandler } from '../parsers/call-expression-handler.js';
|
|
4
4
|
import { JSXHandler } from '../parsers/jsx-handler.js';
|
|
5
5
|
import { unwrapParens } from '../parsers/ast-utils.js';
|
|
@@ -824,8 +824,8 @@ class ASTVisitors {
|
|
|
824
824
|
let members;
|
|
825
825
|
if (tsType.type === 'TsTypeLiteral')
|
|
826
826
|
members = tsType.members;
|
|
827
|
-
else if (tsType.type === 'TsTypeReference' && typeRefName(tsType)) {
|
|
828
|
-
members = this.expressionResolver.getObjectTypeMembersRaw(typeRefName(tsType));
|
|
827
|
+
else if (tsType.type === 'TsTypeReference' && this.expressionResolver.typeRefName(tsType)) {
|
|
828
|
+
members = this.expressionResolver.getObjectTypeMembersRaw(this.expressionResolver.typeRefName(tsType));
|
|
829
829
|
}
|
|
830
830
|
if (!Array.isArray(members))
|
|
831
831
|
return [];
|
|
@@ -1051,6 +1051,7 @@ class ASTVisitors {
|
|
|
1051
1051
|
setCurrentFile(file, code) {
|
|
1052
1052
|
this.currentFile = file;
|
|
1053
1053
|
this.currentCode = code;
|
|
1054
|
+
this.expressionResolver.setCurrentFile(file);
|
|
1054
1055
|
}
|
|
1055
1056
|
/**
|
|
1056
1057
|
* Returns the currently set file path.
|
|
@@ -4,6 +4,7 @@ import { ConsoleLogger } from '../../utils/logger.js';
|
|
|
4
4
|
import { createPluginContext, initializePlugins } from '../plugin-manager.js';
|
|
5
5
|
import { ASTVisitors } from './ast-visitors.js';
|
|
6
6
|
import { ExpressionResolver } from '../parsers/expression-resolver.js';
|
|
7
|
+
import { getTsConfigAliases } from '../../config.js';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Main function for finding translation keys across all source files in a project.
|
|
@@ -79,6 +80,7 @@ async function findKeys(config, logger = new ConsoleLogger(), fileErrors) {
|
|
|
79
80
|
// Use a shared ExpressionResolver so captured enums/objects in one file are available when resolving MemberExpressions
|
|
80
81
|
const sharedExpressionResolver = new ExpressionResolver(hooks);
|
|
81
82
|
sharedExpressionResolver.maxMemberDepth = config.extract.maxTypeDepth ?? 5;
|
|
83
|
+
sharedExpressionResolver.pathAliases = await getTsConfigAliases();
|
|
82
84
|
const astVisitors = new ASTVisitors(otherConfig, pluginContext, logger, hooks, sharedExpressionResolver);
|
|
83
85
|
// 4. "Wire up" the visitor's scope method to the context.
|
|
84
86
|
// This avoids a circular dependency while giving plugins access to the scope.
|
|
@@ -182,11 +182,11 @@ class CallExpressionHandler {
|
|
|
182
182
|
}
|
|
183
183
|
let defaultValue;
|
|
184
184
|
let options;
|
|
185
|
+
// Options inline or in a variable: `t('key', { count })`, `t('key', params)` (#307)
|
|
185
186
|
if (node.arguments.length > 1) {
|
|
186
187
|
const arg2 = unwrapParens(node.arguments[1].expression);
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
188
|
+
options = this.expressionResolver.objectLiteralOf(arg2);
|
|
189
|
+
if (options) ;
|
|
190
190
|
else if (arg2.type === 'StringLiteral') {
|
|
191
191
|
defaultValue = arg2.value;
|
|
192
192
|
}
|
|
@@ -198,10 +198,7 @@ class CallExpressionHandler {
|
|
|
198
198
|
}
|
|
199
199
|
}
|
|
200
200
|
if (node.arguments.length > 2) {
|
|
201
|
-
|
|
202
|
-
if (arg3.type === 'ObjectExpression') {
|
|
203
|
-
options = arg3;
|
|
204
|
-
}
|
|
201
|
+
options = this.expressionResolver.objectLiteralOf(node.arguments[2].expression) ?? options;
|
|
205
202
|
}
|
|
206
203
|
const defaultValueFromOptions = options ? this.getDefaultValueProp(options, 'defaultValue') : undefined;
|
|
207
204
|
const finalDefaultValue = (typeof defaultValueFromOptions === 'string' ? defaultValueFromOptions : defaultValue);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { resolve, dirname } from 'node:path';
|
|
1
2
|
import { unwrapParens } from './ast-utils.js';
|
|
2
3
|
|
|
3
4
|
// Whether `next` holds everything `prev` does: every value of a list, every
|
|
@@ -59,9 +60,45 @@ const memberByKey = (members, key) => {
|
|
|
59
60
|
}
|
|
60
61
|
return index.get(key);
|
|
61
62
|
};
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
// `/src/api/github-api.ts`, `.d.ts`, or an import of `./github-api.js` -> `/src/api/github-api`
|
|
64
|
+
const moduleKey = (path) => path.replace(/(\.d)?\.[cm]?[jt]sx?$/, '');
|
|
65
|
+
/**
|
|
66
|
+
* A cross-file table keyed by declared name. Every entry is also kept under the module that
|
|
67
|
+
* declares it (`/src/api/github-api#components`), and a lookup made while a file is being read
|
|
68
|
+
* first tries where that file gets the name from: the module it imported it from, then its own
|
|
69
|
+
* declaration. The bare name, held by whichever file declared it last, is the fallback, so two
|
|
70
|
+
* generated clients that both declare `components` no longer overwrite each other (#306).
|
|
71
|
+
*/
|
|
72
|
+
class ModuleTable extends Map {
|
|
73
|
+
scope;
|
|
74
|
+
constructor(scope) {
|
|
75
|
+
super();
|
|
76
|
+
this.scope = scope;
|
|
77
|
+
}
|
|
78
|
+
get(name) {
|
|
79
|
+
for (const key of this.scope.keysOf(name)) {
|
|
80
|
+
const value = super.get(key);
|
|
81
|
+
if (value !== undefined)
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
has(name) {
|
|
87
|
+
return this.scope.keysOf(name).some(key => super.has(key));
|
|
88
|
+
}
|
|
89
|
+
set(name, value) {
|
|
90
|
+
const key = this.scope.declaringKey(name);
|
|
91
|
+
if (key)
|
|
92
|
+
super.set(key, value);
|
|
93
|
+
return super.set(name, value);
|
|
94
|
+
}
|
|
95
|
+
delete(name) {
|
|
96
|
+
const key = this.scope.declaringKey(name);
|
|
97
|
+
if (key)
|
|
98
|
+
super.delete(key);
|
|
99
|
+
return super.delete(name);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
65
102
|
// `T['a']` / `T[200]` -> 'a' / '200'
|
|
66
103
|
const indexedKey = (type) => type?.indexType?.type === 'TsLiteralType' ? literalKey(type.indexType.literal) : undefined;
|
|
67
104
|
// Whether replacing `prev` by `next` only adds to it.
|
|
@@ -94,33 +131,33 @@ class ExpressionResolver {
|
|
|
94
131
|
// - Record<string, string> (object of static string properties)
|
|
95
132
|
variableTable = new Map();
|
|
96
133
|
// Shared (cross-file) table for enums / exported object maps that should persist
|
|
97
|
-
sharedEnumTable = new
|
|
134
|
+
sharedEnumTable = new ModuleTable(this);
|
|
98
135
|
// Per-file table for type aliases: Maps typeName -> string[]
|
|
99
136
|
// e.g. `type ChangeType = 'all' | 'next' | 'this'` -> { ChangeType: ['all', 'next', 'this'] }
|
|
100
137
|
typeAliasTable = new Map();
|
|
101
138
|
// Shared (cross-file) table for string-array constants (e.g. `as const` arrays).
|
|
102
139
|
// Persists across resetFileSymbols() so exported arrays are visible to importers.
|
|
103
|
-
sharedVariableTable = new
|
|
140
|
+
sharedVariableTable = new ModuleTable(this);
|
|
104
141
|
// Shared (cross-file) table for as-const object maps, `export const LABELS = { … } as const`,
|
|
105
142
|
// so `LABELS.save` and `Object.keys(LABELS)` resolve in importing files.
|
|
106
|
-
sharedObjectMapTable = new
|
|
143
|
+
sharedObjectMapTable = new ModuleTable(this);
|
|
107
144
|
// Shared (cross-file) table for type aliases — populated alongside typeAliasTable.
|
|
108
145
|
// Persists across resetFileSymbols() so exported type aliases are visible to importers.
|
|
109
|
-
sharedTypeAliasTable = new
|
|
146
|
+
sharedTypeAliasTable = new ModuleTable(this);
|
|
110
147
|
// Shared (cross-file) table for function return-value sets. Populated from
|
|
111
148
|
// both explicit return-type annotations and body-inferred return values so
|
|
112
149
|
// that `t(fn())` / `const x = fn(); t(\`...${x}...\`)` work across files.
|
|
113
150
|
// Persists across resetFileSymbols() just like the other shared tables.
|
|
114
|
-
sharedFunctionReturnTable = new
|
|
151
|
+
sharedFunctionReturnTable = new ModuleTable(this);
|
|
115
152
|
// Temporary per-scope variable overrides, used to inject .map() / .forEach()
|
|
116
153
|
// callback parameters while the callback body is being walked.
|
|
117
154
|
temporaryVariables = new Map();
|
|
118
155
|
// Shared (cross-file) table for object-shaped types: interfaces and object
|
|
119
156
|
// type aliases. Maps typeName -> { memberName: possible string values }.
|
|
120
157
|
// e.g. `interface IProps { size: ChangeType }` -> { IProps: { size: ['all','next'] } }
|
|
121
|
-
objectTypeTable = new
|
|
158
|
+
objectTypeTable = new ModuleTable(this);
|
|
122
159
|
// Raw TS member nodes of local interfaces / object type aliases, keyed by type name.
|
|
123
|
-
objectTypeMembersRaw = new
|
|
160
|
+
objectTypeMembersRaw = new ModuleTable(this);
|
|
124
161
|
// Temporary per-scope bindings for identifiers holding an object-shaped type,
|
|
125
162
|
// e.g. `function f(props: IProps)` -> { props: { size: ['all','next'] } }.
|
|
126
163
|
temporaryObjectVariables = new Map();
|
|
@@ -137,15 +174,18 @@ class ExpressionResolver {
|
|
|
137
174
|
// members are declared under their own names, so `Types.COLORS` resolves as `COLORS`.
|
|
138
175
|
sharedNamespaceNames = new Set();
|
|
139
176
|
// Type parameters of generic object types, e.g. `type Base<T> = { type: T }`.
|
|
140
|
-
objectTypeParams = new
|
|
177
|
+
objectTypeParams = new ModuleTable(this);
|
|
141
178
|
// Values bound to type parameters while a generic type is being resolved.
|
|
142
179
|
typeParamBindings = new Map();
|
|
143
180
|
// Shared (cross-file) member maps of functions returning an object type,
|
|
144
181
|
// e.g. `const getAnimal = (): Animal => …` -> { getAnimal: { type: ['DUCK', 'DOG'] } }
|
|
145
|
-
sharedFunctionReturnMembers = new
|
|
182
|
+
sharedFunctionReturnMembers = new ModuleTable(this);
|
|
146
183
|
// Per-file identifiers holding an object type, from their annotation or from the
|
|
147
184
|
// function they were initialised by: `const animal = getAnimal()` -> { animal: { type: [...] } }
|
|
148
185
|
objectVariables = new Map();
|
|
186
|
+
// Per-file object literals by variable, so `const params = { count }; t('key', params)`
|
|
187
|
+
// reads its options like an inline object (#307)
|
|
188
|
+
objectLiterals = new Map();
|
|
149
189
|
// Set while the pre-scan runs. Captures that read other declarations queue a
|
|
150
190
|
// replay here, because what they read may be declared in a file scanned later.
|
|
151
191
|
recording = false;
|
|
@@ -157,9 +197,92 @@ class ExpressionResolver {
|
|
|
157
197
|
// this many levels below a declared or indexed type, so recursive types stay small
|
|
158
198
|
// (`extract.maxTypeDepth`).
|
|
159
199
|
maxMemberDepth = 5;
|
|
200
|
+
// The module being read (its path without extension), and where each module gets its
|
|
201
|
+
// names from (see ModuleTable)
|
|
202
|
+
currentModule;
|
|
203
|
+
knownModules = new Set();
|
|
204
|
+
// module -> local name -> { from: the import's resolved path, name: the declared name }
|
|
205
|
+
fileImports = new Map();
|
|
206
|
+
// module -> namespace import's local name -> the import's resolved path
|
|
207
|
+
fileNamespaces = new Map();
|
|
208
|
+
// module -> the variables it declares, which shadow the same names declared in other files
|
|
209
|
+
fileVariables = new Map();
|
|
210
|
+
// tsconfig `paths`, `@api` -> `/abs/src/api`
|
|
211
|
+
pathAliases = {};
|
|
160
212
|
constructor(hooks) {
|
|
161
213
|
this.hooks = hooks;
|
|
162
214
|
}
|
|
215
|
+
setCurrentFile(file) {
|
|
216
|
+
this.currentModule = moduleKey(resolve(file));
|
|
217
|
+
this.knownModules.add(this.currentModule);
|
|
218
|
+
}
|
|
219
|
+
// `./api/github-api` / `@api/github-api` imported in the current file -> its path, or undefined
|
|
220
|
+
// for a package or anything else that isn't a file of this project
|
|
221
|
+
importPath(source) {
|
|
222
|
+
if (!this.currentModule)
|
|
223
|
+
return undefined;
|
|
224
|
+
if (source.startsWith('.'))
|
|
225
|
+
return moduleKey(resolve(dirname(this.currentModule), source));
|
|
226
|
+
for (const [alias, path] of Object.entries(this.pathAliases)) {
|
|
227
|
+
if (source === alias || source.startsWith(`${alias}/`))
|
|
228
|
+
return moduleKey(resolve(path, `.${source.slice(alias.length)}`));
|
|
229
|
+
}
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
// The scanned module an import path points to: the file itself or its `index`
|
|
233
|
+
moduleOf(path) {
|
|
234
|
+
return [path, `${path}/index`].find(m => this.knownModules.has(m));
|
|
235
|
+
}
|
|
236
|
+
// ModuleTable: the keys `name` read in the current file may be stored under, most specific first
|
|
237
|
+
keysOf(name) {
|
|
238
|
+
if (name.includes('#') || !this.currentModule)
|
|
239
|
+
return [name];
|
|
240
|
+
const keys = [];
|
|
241
|
+
const imported = this.fileImports.get(this.currentModule)?.get(name);
|
|
242
|
+
const module = imported && this.moduleOf(imported.from);
|
|
243
|
+
if (module)
|
|
244
|
+
keys.push(`${module}#${imported.name}`);
|
|
245
|
+
keys.push(`${this.currentModule}#${name}`);
|
|
246
|
+
// A variable of this file never means another file's same-named one, even when its own
|
|
247
|
+
// value isn't captured (`const params = { count }` vs another file's `params` array) (#307)
|
|
248
|
+
if (!this.fileVariables.get(this.currentModule)?.has(name))
|
|
249
|
+
keys.push(name);
|
|
250
|
+
return keys;
|
|
251
|
+
}
|
|
252
|
+
// ModuleTable: the key a declaration made in the current file is also stored under
|
|
253
|
+
declaringKey(name) {
|
|
254
|
+
return this.currentModule && !name.includes('#') ? `${this.currentModule}#${name}` : undefined;
|
|
255
|
+
}
|
|
256
|
+
// Replays run after every file was read: run each in the file it was recorded in
|
|
257
|
+
inCurrentFile(fn) {
|
|
258
|
+
const module = this.currentModule;
|
|
259
|
+
return () => {
|
|
260
|
+
const saved = this.currentModule;
|
|
261
|
+
this.currentModule = module;
|
|
262
|
+
try {
|
|
263
|
+
fn();
|
|
264
|
+
}
|
|
265
|
+
finally {
|
|
266
|
+
this.currentModule = saved;
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* `Color` / `Types.Color` / `NS.Inner.Color` -> 'Color'. Declarations are looked up by their
|
|
272
|
+
* own name, so a qualified name resolves by its last part, whatever namespace holds it, and
|
|
273
|
+
* through a namespace import of a module (`GH.components`) by that module's declaration.
|
|
274
|
+
*/
|
|
275
|
+
typeRefName(type) {
|
|
276
|
+
const typeName = type?.typeName;
|
|
277
|
+
if (typeName?.type === 'Identifier')
|
|
278
|
+
return typeName.value;
|
|
279
|
+
if (typeName?.type !== 'TsQualifiedName')
|
|
280
|
+
return undefined;
|
|
281
|
+
const name = typeName.right?.value;
|
|
282
|
+
const from = typeName.left?.type === 'Identifier' && this.currentModule ? this.fileNamespaces.get(this.currentModule)?.get(typeName.left.value) : undefined;
|
|
283
|
+
const module = from && this.moduleOf(from);
|
|
284
|
+
return module && name ? `${module}#${name}` : name;
|
|
285
|
+
}
|
|
163
286
|
/**
|
|
164
287
|
* Clear per-file captured variables. Enums / shared maps are kept.
|
|
165
288
|
*/
|
|
@@ -169,6 +292,7 @@ class ExpressionResolver {
|
|
|
169
292
|
this.temporaryVariables.clear();
|
|
170
293
|
this.arrayElementMembers.clear();
|
|
171
294
|
this.objectVariables.clear();
|
|
295
|
+
this.objectLiterals.clear();
|
|
172
296
|
}
|
|
173
297
|
/**
|
|
174
298
|
* Call once every file has been pre-scanned. Captures ran in file order, so one
|
|
@@ -179,6 +303,8 @@ class ExpressionResolver {
|
|
|
179
303
|
*/
|
|
180
304
|
finishPreScan() {
|
|
181
305
|
this.resetFileSymbols();
|
|
306
|
+
// Mirroring onto import aliases writes bare names only; each replay restores its own file
|
|
307
|
+
this.currentModule = undefined;
|
|
182
308
|
// ponytail: each round fixes one more link of a chain declared against file order; 10 is plenty
|
|
183
309
|
for (let round = 0; round < 10; round++) {
|
|
184
310
|
this.applyImportAliases();
|
|
@@ -282,6 +408,11 @@ class ExpressionResolver {
|
|
|
282
408
|
if (node.id.type !== 'Identifier')
|
|
283
409
|
return;
|
|
284
410
|
const name = node.id.value;
|
|
411
|
+
if (this.currentModule) {
|
|
412
|
+
if (!this.fileVariables.has(this.currentModule))
|
|
413
|
+
this.fileVariables.set(this.currentModule, new Set());
|
|
414
|
+
this.fileVariables.get(this.currentModule).add(name);
|
|
415
|
+
}
|
|
285
416
|
// `const items: IProps[] = …` → remember the element's member map so
|
|
286
417
|
// `items.map(({ size }) => …)` can bind the callback parameter.
|
|
287
418
|
const idTypeAnnotation = this.extractTypeAnnotation(node.id);
|
|
@@ -316,6 +447,7 @@ class ExpressionResolver {
|
|
|
316
447
|
const unwrappedInit = unwrapExpr(init);
|
|
317
448
|
// ObjectExpression -> map of string props
|
|
318
449
|
if (unwrappedInit.type === 'ObjectExpression' && Array.isArray(unwrappedInit.properties)) {
|
|
450
|
+
this.objectLiterals.set(name, unwrappedInit);
|
|
319
451
|
const map = {};
|
|
320
452
|
for (const p of unwrappedInit.properties) {
|
|
321
453
|
if (!p || p.type !== 'KeyValueProperty')
|
|
@@ -402,7 +534,7 @@ class ExpressionResolver {
|
|
|
402
534
|
if (!tsType)
|
|
403
535
|
return;
|
|
404
536
|
if (this.recording)
|
|
405
|
-
this.replays.push(() => this.captureTypeAliasDeclaration(node));
|
|
537
|
+
this.replays.push(this.inCurrentFile(() => this.captureTypeAliasDeclaration(node)));
|
|
406
538
|
const params = node.typeParams?.parameters;
|
|
407
539
|
// `type IProps = { size: ChangeType }` — object shape, not a string union.
|
|
408
540
|
if (tsType.type === 'TsTypeLiteral') {
|
|
@@ -439,18 +571,31 @@ class ExpressionResolver {
|
|
|
439
571
|
*/
|
|
440
572
|
captureImportDeclaration(node) {
|
|
441
573
|
try {
|
|
574
|
+
const from = node?.type === 'ImportDeclaration' && typeof node.source?.value === 'string' ? this.importPath(node.source.value) : undefined;
|
|
575
|
+
const imports = from && this.currentModule ? (this.fileImports.get(this.currentModule) ?? new Map()) : undefined;
|
|
576
|
+
if (imports)
|
|
577
|
+
this.fileImports.set(this.currentModule, imports);
|
|
442
578
|
for (const spec of node?.specifiers ?? []) {
|
|
443
579
|
// `import * as Types from …` / `export * as Types from …`
|
|
444
|
-
if (spec?.type === 'ImportNamespaceSpecifier' && spec.local?.value)
|
|
580
|
+
if (spec?.type === 'ImportNamespaceSpecifier' && spec.local?.value) {
|
|
445
581
|
this.sharedNamespaceNames.add(spec.local.value);
|
|
582
|
+
if (from) {
|
|
583
|
+
if (!this.fileNamespaces.has(this.currentModule))
|
|
584
|
+
this.fileNamespaces.set(this.currentModule, new Map());
|
|
585
|
+
this.fileNamespaces.get(this.currentModule).set(spec.local.value, from);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
446
588
|
if (spec?.type === 'ExportNamespaceSpecifier' && spec.name?.value)
|
|
447
589
|
this.sharedNamespaceNames.add(spec.name.value);
|
|
448
590
|
if (spec?.type !== 'ImportSpecifier')
|
|
449
591
|
continue;
|
|
450
592
|
const local = spec.local?.value;
|
|
451
|
-
const imported = spec.imported?.value;
|
|
593
|
+
const imported = spec.imported?.value ?? local;
|
|
452
594
|
if (local && imported && local !== imported)
|
|
453
595
|
this.sharedImportAliases.set(local, imported);
|
|
596
|
+
// `import { components as githubComponents } from './github-api'` → that module's `components`
|
|
597
|
+
if (local && imported && imports)
|
|
598
|
+
imports.set(local, { from: from, name: imported });
|
|
454
599
|
}
|
|
455
600
|
}
|
|
456
601
|
catch {
|
|
@@ -467,7 +612,10 @@ class ExpressionResolver {
|
|
|
467
612
|
const ns = expr.object.value;
|
|
468
613
|
if (!this.sharedNamespaceNames.has(this.sharedImportAliases.get(ns) ?? ns))
|
|
469
614
|
return expr;
|
|
470
|
-
|
|
615
|
+
// `GH.components` with `import * as GH from './github-api'` → that module's `components`
|
|
616
|
+
const from = this.currentModule ? this.fileNamespaces.get(this.currentModule)?.get(ns) : undefined;
|
|
617
|
+
const module = from && this.moduleOf(from);
|
|
618
|
+
return { type: 'Identifier', value: module ? `${module}#${expr.property.value}` : expr.property.value, span: expr.span };
|
|
471
619
|
}
|
|
472
620
|
// The variable a `typeof` names: `typeof LABELS`, or `typeof Types.LABELS` through a namespace import
|
|
473
621
|
typeQueryName(exprName) {
|
|
@@ -517,7 +665,7 @@ class ExpressionResolver {
|
|
|
517
665
|
if (!name)
|
|
518
666
|
return;
|
|
519
667
|
if (this.recording)
|
|
520
|
-
this.replays.push(() => this.captureInterfaceDeclaration(node));
|
|
668
|
+
this.replays.push(this.inCurrentFile(() => this.captureInterfaceDeclaration(node)));
|
|
521
669
|
const params = node.typeParams?.parameters;
|
|
522
670
|
if (!this.replaying && Array.isArray(node?.body?.body))
|
|
523
671
|
this.setObjectTypeRaw(name, node.body.body, params);
|
|
@@ -656,7 +804,7 @@ class ExpressionResolver {
|
|
|
656
804
|
}
|
|
657
805
|
return Object.keys(merged).length > 0 ? merged : undefined;
|
|
658
806
|
}
|
|
659
|
-
const name = tsType.type === 'TsTypeReference' ? typeRefName(tsType) : undefined;
|
|
807
|
+
const name = tsType.type === 'TsTypeReference' ? this.typeRefName(tsType) : undefined;
|
|
660
808
|
if (name) {
|
|
661
809
|
// `Base<'DUCK'>` → instantiate the generic type's members with the argument
|
|
662
810
|
const args = tsType.typeParams?.params;
|
|
@@ -711,7 +859,7 @@ class ExpressionResolver {
|
|
|
711
859
|
obj = obj.typeAnnotation;
|
|
712
860
|
if (obj?.type === 'TsIndexedAccessType')
|
|
713
861
|
obj = this.indexedAccessTarget(obj);
|
|
714
|
-
const name = obj?.type === 'TsTypeReference' && !obj.typeParams ? typeRefName(obj) : undefined;
|
|
862
|
+
const name = obj?.type === 'TsTypeReference' && !obj.typeParams ? this.typeRefName(obj) : undefined;
|
|
715
863
|
const members = obj?.type === 'TsTypeLiteral'
|
|
716
864
|
? obj.members
|
|
717
865
|
: name && !this.objectTypeParams.has(name) ? this.objectTypeMembersRaw.get(name) : undefined;
|
|
@@ -869,7 +1017,7 @@ class ExpressionResolver {
|
|
|
869
1017
|
};
|
|
870
1018
|
store();
|
|
871
1019
|
if (this.recording && (returnType || refs.length > 0))
|
|
872
|
-
this.replays.push(store);
|
|
1020
|
+
this.replays.push(this.inCurrentFile(store));
|
|
873
1021
|
}
|
|
874
1022
|
/**
|
|
875
1023
|
* Collect a function's return expressions (the expression body of an arrow
|
|
@@ -969,6 +1117,13 @@ class ExpressionResolver {
|
|
|
969
1117
|
return v;
|
|
970
1118
|
return this.sharedVariableTable.get(name);
|
|
971
1119
|
}
|
|
1120
|
+
/** The object literal `expr` is, inline or through a variable of this file, or undefined. */
|
|
1121
|
+
objectLiteralOf(expr) {
|
|
1122
|
+
expr = unwrapExpr(expr);
|
|
1123
|
+
if (expr?.type === 'ObjectExpression')
|
|
1124
|
+
return expr;
|
|
1125
|
+
return expr?.type === 'Identifier' ? this.objectLiterals.get(expr.value) : undefined;
|
|
1126
|
+
}
|
|
972
1127
|
/**
|
|
973
1128
|
* Return the as-const object map stored for a variable name.
|
|
974
1129
|
* Returns undefined if the name is not a known object map.
|
|
@@ -1330,7 +1485,7 @@ class ExpressionResolver {
|
|
|
1330
1485
|
// where `type ChangeType = 'all' | 'next' | 'this'` was captured earlier.
|
|
1331
1486
|
// Also handles `declare const d: SomeEnum` where SomeEnum is a TS enum with string values.
|
|
1332
1487
|
if (type.type === 'TsTypeReference') {
|
|
1333
|
-
const typeName = typeRefName(type);
|
|
1488
|
+
const typeName = this.typeRefName(type);
|
|
1334
1489
|
if (typeName) {
|
|
1335
1490
|
// Inside a generic type: the parameter's argument, default or constraint
|
|
1336
1491
|
const bound = this.typeParamBindings.get(typeName);
|
|
@@ -1478,4 +1633,4 @@ class ExpressionResolver {
|
|
|
1478
1633
|
}
|
|
1479
1634
|
}
|
|
1480
1635
|
|
|
1481
|
-
export { ExpressionResolver, nestedMembers
|
|
1636
|
+
export { ExpressionResolver, nestedMembers };
|
package/dist/esm/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export { defineConfig } from './config.js';
|
|
|
2
2
|
export { extract, runExtractor } from './extractor/core/extractor.js';
|
|
3
3
|
export { findKeys } from './extractor/core/key-finder.js';
|
|
4
4
|
export { getTranslations } from './extractor/core/translation-manager.js';
|
|
5
|
+
import 'node:path';
|
|
5
6
|
import 'node:module';
|
|
6
7
|
export { recommendedAcceptedAttributes, recommendedAcceptedTags, runLinter } from './linter.js';
|
|
7
8
|
export { runSyncer } from './syncer.js';
|
|
@@ -13,5 +14,4 @@ import './utils/jsx-attributes.js';
|
|
|
13
14
|
import 'magic-string';
|
|
14
15
|
export { runLocalize } from './localize/localize.js';
|
|
15
16
|
import 'node:fs/promises';
|
|
16
|
-
import 'node:path';
|
|
17
17
|
export { AGENT_PROMPT } from './localize/agent-prompt.js';
|
|
@@ -6,8 +6,8 @@ import { glob } from 'glob';
|
|
|
6
6
|
import { loadConfig, ensureConfig } from '../config.js';
|
|
7
7
|
import { detectConfig } from '../heuristic-config.js';
|
|
8
8
|
import { runExtractor } from '../extractor/core/extractor.js';
|
|
9
|
-
import 'node:module';
|
|
10
9
|
import 'node:path';
|
|
10
|
+
import 'node:module';
|
|
11
11
|
import { getNestedKeys, getNestedValue } from '../utils/nested-object.js';
|
|
12
12
|
import 'jiti';
|
|
13
13
|
import '@croct/json5-parser';
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast-visitors.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/ast-visitors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAQ,MAAM,WAAW,CAAA;AAC7C,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC3H,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAA;AAC1D,OAAO,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"ast-visitors.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/ast-visitors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAQ,MAAM,WAAW,CAAA;AAC7C,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB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O,CAAC,oBAAoB;IAoB5B;;;;;;;;OAQG;IACH,OAAO,CAAC,gCAAgC;IAiDxC;;;;;;;OAOG;IACH,OAAO,CAAC,gBAAgB;IA+CxB;;;;;;;;;OASG;IACH,OAAO,CAAC,0BAA0B;IA8ClC;;;;OAIG;IACH,OAAO,CAAC,oBAAoB;IAqB5B;;;;;;;;OAQG;IACI,eAAe,CAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS;IAI5D;;OAEG;IACI,cAAc,CAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAMxD;;;;;;OAMG;IACI,cAAc,IAAK,MAAM;IAIhC;;OAEG;IACI,cAAc,IAAK,MAAM;CAGjC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key-finder.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/key-finder.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,oBAAoB,EAAmB,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"key-finder.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/key-finder.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,oBAAoB,EAAmB,MAAM,gBAAgB,CAAA;AAQjG;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAsB,QAAQ,CAC5B,MAAM,EAAE,oBAAoB,EAC5B,MAAM,GAAE,MAA4B,EACpC,UAAU,CAAC,EAAE,MAAM,EAAE,GACpB,OAAO,CAAC;IAAE,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAAC,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAAC,YAAY,EAAE,YAAY,EAAE,CAAA;CAAE,CAAC,CA8FxG"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"call-expression-handler.d.ts","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AI,EAAE,MAAM,KAAK,SAAS,GAAG,SAAS,GAAG,IAAI;IA8CxG,OAAO,CAAC,eAAe;
|
|
1
|
+
{"version":3,"file":"call-expression-handler.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/call-expression-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAyD,MAAM,WAAW,CAAA;AACtG,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,EAAgB,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1G,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAc7D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,sBAAsB,CAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,GAAG,SAAS,EAAE,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,GAAG,OAAO,CAAC,GAAG,MAAM,CAyBtJ;AAED,qBAAa,qBAAqB;IAChC,OAAO,CAAC,aAAa,CAAe;IACpC,OAAO,CAAC,MAAM,CAAuC;IACrD,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,kBAAkB,CAAoB;IACvC,UAAU,cAAoB;IACrC,OAAO,CAAC,cAAc,CAAc;IACpC,OAAO,CAAC,cAAc,CAAc;IACpC,OAAO,CAAC,iBAAiB,CAAsC;gBAG7D,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,EAC7C,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,MAAM,EACd,kBAAkB,EAAE,kBAAkB,EACtC,cAAc,EAAE,MAAM,MAAM,EAC5B,cAAc,EAAE,MAAM,MAAM,EAC5B,iBAAiB,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAA2B;IAW3E;;;OAGG;IACH,OAAO,CAAC,mBAAmB,CAG1B;IAED,uGAAuG;IACvG,OAAO,CAAC,mBAAmB;IAI3B;;;;;OAKG;IACH,OAAO,CAAC,mBAAmB;IAiB3B;;;;;;;;;;;;;;OAcG;IACH,oBAAoB,CAAE,IAAI,EAAE,cAAc,EAAE,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,GAAG,SAAS,GAAG,IAAI;IA8CxG,OAAO,CAAC,eAAe;IAyXvB;;;;OAIG;IACH,OAAO,CAAC,iBAAiB;IAmBzB,OAAO,CAAC,wBAAwB;IAyEhC;;;;;;OAMG;IACH,OAAO,CAAC,4BAA4B;IAsDpC;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,CAAC,uBAAuB;IAiB/B;;;;;;;;OAQG;IACH,OAAO,CAAC,iCAAiC;IAwFzC;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,gBAAgB;IAyMxB;;;;;;;;;OASG;IACH,OAAO,CAAC,eAAe;CA2BxB"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { Expression } from '@swc/core';
|
|
1
|
+
import type { Expression, ObjectExpression } from '@swc/core';
|
|
2
2
|
import type { ASTVisitorHooks } from '../../types.js';
|
|
3
3
|
export declare const nestedMembers: (members: Record<string, string[]> | undefined, name: string) => Record<string, string[]> | undefined;
|
|
4
|
-
export declare const typeRefName: (type: any) => string | undefined;
|
|
5
4
|
export declare class ExpressionResolver {
|
|
6
5
|
private hooks;
|
|
7
6
|
private variableTable;
|
|
@@ -22,13 +21,32 @@ export declare class ExpressionResolver {
|
|
|
22
21
|
private typeParamBindings;
|
|
23
22
|
private sharedFunctionReturnMembers;
|
|
24
23
|
private objectVariables;
|
|
24
|
+
private objectLiterals;
|
|
25
25
|
recording: boolean;
|
|
26
26
|
private replays;
|
|
27
27
|
private replaying;
|
|
28
28
|
private grew;
|
|
29
29
|
private memberDepth;
|
|
30
30
|
maxMemberDepth: number;
|
|
31
|
+
private currentModule;
|
|
32
|
+
private knownModules;
|
|
33
|
+
private fileImports;
|
|
34
|
+
private fileNamespaces;
|
|
35
|
+
private fileVariables;
|
|
36
|
+
pathAliases: Record<string, string>;
|
|
31
37
|
constructor(hooks: ASTVisitorHooks);
|
|
38
|
+
setCurrentFile(file: string): void;
|
|
39
|
+
private importPath;
|
|
40
|
+
private moduleOf;
|
|
41
|
+
keysOf(name: string): string[];
|
|
42
|
+
declaringKey(name: string): string | undefined;
|
|
43
|
+
private inCurrentFile;
|
|
44
|
+
/**
|
|
45
|
+
* `Color` / `Types.Color` / `NS.Inner.Color` -> 'Color'. Declarations are looked up by their
|
|
46
|
+
* own name, so a qualified name resolves by its last part, whatever namespace holds it, and
|
|
47
|
+
* through a namespace import of a module (`GH.components`) by that module's declaration.
|
|
48
|
+
*/
|
|
49
|
+
typeRefName(type: any): string | undefined;
|
|
32
50
|
/**
|
|
33
51
|
* Clear per-file captured variables. Enums / shared maps are kept.
|
|
34
52
|
*/
|
|
@@ -214,6 +232,8 @@ export declare class ExpressionResolver {
|
|
|
214
232
|
* Returns undefined if the name is not a known string array.
|
|
215
233
|
*/
|
|
216
234
|
getVariableValues(name: string): string[] | undefined;
|
|
235
|
+
/** The object literal `expr` is, inline or through a variable of this file, or undefined. */
|
|
236
|
+
objectLiteralOf(expr: any): ObjectExpression | undefined;
|
|
217
237
|
/**
|
|
218
238
|
* Return the as-const object map stored for a variable name.
|
|
219
239
|
* Returns undefined if the name is not a known object map.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"expression-resolver.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/expression-resolver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAkD,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"expression-resolver.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/expression-resolver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,gBAAgB,EAAkD,MAAM,WAAW,CAAA;AAC7G,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AA2BrD,eAAO,MAAM,aA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