@generaltranslation/python-extractor 0.1.3 → 0.1.5

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.
@@ -2,10 +2,12 @@ export declare const PYTHON_GT_PACKAGES: readonly ["gt_flask", "gt_fastapi"];
2
2
  export declare const PYTHON_GT_DEPENDENCIES: readonly ["gt-flask", "gt-fastapi"];
3
3
  export declare const PYTHON_T_FUNCTION = "t";
4
4
  export declare const PYTHON_MSG_FUNCTION = "msg";
5
+ export declare const PYTHON_DERIVE = "derive";
6
+ /** @deprecated Use PYTHON_DERIVE instead */
5
7
  export declare const PYTHON_DECLARE_STATIC = "declare_static";
6
8
  export declare const PYTHON_DECLARE_VAR = "declare_var";
7
- /** These imported names are tracked (translation functions + static helpers) */
8
- export declare const PYTHON_TRANSLATION_FUNCTIONS: readonly ["t", "msg", "declare_static", "declare_var"];
9
+ /** These imported names are tracked (translation functions + derive helpers) */
10
+ export declare const PYTHON_TRANSLATION_FUNCTIONS: readonly ["t", "msg", "derive", "declare_static", "declare_var"];
9
11
  export declare const PYTHON_METADATA_KWARGS: {
10
12
  readonly _id: "id";
11
13
  readonly _context: "context";
package/dist/constants.js CHANGED
@@ -2,12 +2,15 @@ export const PYTHON_GT_PACKAGES = ['gt_flask', 'gt_fastapi'];
2
2
  export const PYTHON_GT_DEPENDENCIES = ['gt-flask', 'gt-fastapi'];
3
3
  export const PYTHON_T_FUNCTION = 't';
4
4
  export const PYTHON_MSG_FUNCTION = 'msg';
5
+ export const PYTHON_DERIVE = 'derive';
6
+ /** @deprecated Use PYTHON_DERIVE instead */
5
7
  export const PYTHON_DECLARE_STATIC = 'declare_static';
6
8
  export const PYTHON_DECLARE_VAR = 'declare_var';
7
- /** These imported names are tracked (translation functions + static helpers) */
9
+ /** These imported names are tracked (translation functions + derive helpers) */
8
10
  export const PYTHON_TRANSLATION_FUNCTIONS = [
9
11
  't',
10
12
  'msg',
13
+ 'derive',
11
14
  'declare_static',
12
15
  'declare_var',
13
16
  ];
@@ -1,4 +1,4 @@
1
- import { PYTHON_METADATA_KWARGS } from './constants.js';
1
+ import { PYTHON_METADATA_KWARGS, PYTHON_DERIVE, PYTHON_DECLARE_STATIC, PYTHON_DECLARE_VAR, } from './constants.js';
2
2
  import { containsStaticCalls, parseStringExpression, } from './parseStringExpression.js';
3
3
  import { nodeToStrings } from './stringNode.js';
4
4
  import { indexVars } from 'generaltranslation/internal';
@@ -11,10 +11,11 @@ export async function extractCalls(rootNode, imports, filePath) {
11
11
  const calls = [];
12
12
  const errors = [];
13
13
  const warnings = [];
14
- // Only track t/msg as translation functions (not declare_static/declare_var)
14
+ // Only track t/msg as translation functions (not derive/declare_static/declare_var)
15
15
  const trackedNames = new Set(imports
16
- .filter((imp) => imp.originalName !== 'declare_static' &&
17
- imp.originalName !== 'declare_var')
16
+ .filter((imp) => imp.originalName !== PYTHON_DERIVE &&
17
+ imp.originalName !== PYTHON_DECLARE_STATIC &&
18
+ imp.originalName !== PYTHON_DECLARE_VAR)
18
19
  .map((imp) => imp.localName));
19
20
  if (trackedNames.size === 0)
20
21
  return { calls, errors, warnings };
@@ -116,7 +117,7 @@ async function processCall(callNode, imports, filePath, calls, errors, _warnings
116
117
  }
117
118
  // Check for f-strings (without declare_static/declare_var)
118
119
  if (isFString(firstArg)) {
119
- errors.push(`${locationStr(callNode)}: translation call uses an f-string — use a plain string literal or declare_static()/declare_var()`);
120
+ errors.push(`${locationStr(callNode)}: translation call uses an f-string — use a plain string literal or derive()/declare_var()`);
120
121
  return;
121
122
  }
122
123
  const source = extractStringContent(firstArg);
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { ExtractionResult } from './types.js';
2
2
  export type { ExtractionResult, ExtractionMetadata } from './types.js';
3
3
  export type { ImportAlias } from './extractImports.js';
4
- export { PYTHON_GT_PACKAGES, PYTHON_GT_DEPENDENCIES, PYTHON_T_FUNCTION, PYTHON_MSG_FUNCTION, PYTHON_DECLARE_STATIC, PYTHON_DECLARE_VAR, PYTHON_TRANSLATION_FUNCTIONS, PYTHON_METADATA_KWARGS, } from './constants.js';
4
+ export { PYTHON_GT_PACKAGES, PYTHON_GT_DEPENDENCIES, PYTHON_T_FUNCTION, PYTHON_MSG_FUNCTION, PYTHON_DERIVE, PYTHON_DECLARE_STATIC, PYTHON_DECLARE_VAR, PYTHON_TRANSLATION_FUNCTIONS, PYTHON_METADATA_KWARGS, } from './constants.js';
5
5
  export declare function extractFromPythonSource(sourceCode: string, filePath: string): Promise<{
6
6
  results: ExtractionResult[];
7
7
  errors: string[];
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { getParser } from './parser.js';
2
2
  import { extractImports } from './extractImports.js';
3
3
  import { extractCalls } from './extractCalls.js';
4
- export { PYTHON_GT_PACKAGES, PYTHON_GT_DEPENDENCIES, PYTHON_T_FUNCTION, PYTHON_MSG_FUNCTION, PYTHON_DECLARE_STATIC, PYTHON_DECLARE_VAR, PYTHON_TRANSLATION_FUNCTIONS, PYTHON_METADATA_KWARGS, } from './constants.js';
4
+ export { PYTHON_GT_PACKAGES, PYTHON_GT_DEPENDENCIES, PYTHON_T_FUNCTION, PYTHON_MSG_FUNCTION, PYTHON_DERIVE, PYTHON_DECLARE_STATIC, PYTHON_DECLARE_VAR, PYTHON_TRANSLATION_FUNCTIONS, PYTHON_METADATA_KWARGS, } from './constants.js';
5
5
  export async function extractFromPythonSource(sourceCode, filePath) {
6
6
  const parser = await getParser();
7
7
  const tree = parser.parse(sourceCode);
@@ -8,13 +8,13 @@ type ParseContext = {
8
8
  errors: string[];
9
9
  };
10
10
  /**
11
- * Checks if an expression contains declare_static or declare_var calls.
11
+ * Checks if an expression contains derive/declare_static or declare_var calls.
12
12
  */
13
13
  export declare function containsStaticCalls(node: SyntaxNode, imports: ImportAlias[]): boolean;
14
14
  /**
15
15
  * Parses the first argument of t() into a StringNode tree.
16
- * Handles: plain strings, f-strings with declare_static/declare_var,
17
- * binary + concatenation, and standalone declare_static calls.
16
+ * Handles: plain strings, f-strings with derive/declare_var,
17
+ * binary + concatenation, and standalone derive calls.
18
18
  */
19
19
  export declare function parseStringExpression(node: SyntaxNode, ctx: ParseContext): Promise<StringNode | null>;
20
20
  export {};
@@ -1,18 +1,24 @@
1
- import { PYTHON_DECLARE_STATIC, PYTHON_DECLARE_VAR } from './constants.js';
1
+ import { PYTHON_DERIVE, PYTHON_DECLARE_STATIC, PYTHON_DECLARE_VAR, } from './constants.js';
2
2
  import { resolveFunctionInCurrentFile, resolveFunctionInFile, } from './resolveFunctionVariants.js';
3
3
  import { extractImports } from './extractImports.js';
4
4
  import { resolveImportPath } from './resolveImport.js';
5
5
  import { declareVar } from 'generaltranslation/internal';
6
6
  /**
7
- * Checks if an expression contains declare_static or declare_var calls.
7
+ * Returns true if the original import name is derive() or declare_static() (deprecated).
8
+ */
9
+ function isDeriveFunction(originalName) {
10
+ return (originalName === PYTHON_DERIVE || originalName === PYTHON_DECLARE_STATIC);
11
+ }
12
+ /**
13
+ * Checks if an expression contains derive/declare_static or declare_var calls.
8
14
  */
9
15
  export function containsStaticCalls(node, imports) {
10
- const staticNames = getStaticImportNames(imports);
16
+ const staticNames = getDeriveImportNames(imports);
11
17
  if (staticNames.size === 0)
12
18
  return false;
13
- return hasStaticCallRecursive(node, staticNames);
19
+ return hasDeriveCallRecursive(node, staticNames);
14
20
  }
15
- function hasStaticCallRecursive(node, names) {
21
+ function hasDeriveCallRecursive(node, names) {
16
22
  if (node.type === 'call') {
17
23
  const funcNode = node.childForFieldName('function');
18
24
  if (funcNode &&
@@ -23,15 +29,15 @@ function hasStaticCallRecursive(node, names) {
23
29
  }
24
30
  for (let i = 0; i < node.childCount; i++) {
25
31
  const child = node.child(i);
26
- if (child && hasStaticCallRecursive(child, names))
32
+ if (child && hasDeriveCallRecursive(child, names))
27
33
  return true;
28
34
  }
29
35
  return false;
30
36
  }
31
37
  /**
32
38
  * Parses the first argument of t() into a StringNode tree.
33
- * Handles: plain strings, f-strings with declare_static/declare_var,
34
- * binary + concatenation, and standalone declare_static calls.
39
+ * Handles: plain strings, f-strings with derive/declare_var,
40
+ * binary + concatenation, and standalone derive calls.
35
41
  */
36
42
  export async function parseStringExpression(node, ctx) {
37
43
  // Parenthesized expression: unwrap and recurse
@@ -59,12 +65,12 @@ export async function parseStringExpression(node, ctx) {
59
65
  if (node.type === 'binary_operator') {
60
66
  return parseBinaryOperator(node, ctx);
61
67
  }
62
- // Standalone call: declare_static(...)
68
+ // Standalone call: derive/declare_static(...)
63
69
  if (node.type === 'call') {
64
70
  const funcNode = node.childForFieldName('function');
65
71
  if (funcNode && funcNode.type === 'identifier') {
66
72
  const originalName = getOriginalImportName(funcNode.text, ctx.imports);
67
- if (originalName === PYTHON_DECLARE_STATIC) {
73
+ if (isDeriveFunction(originalName)) {
68
74
  return resolveDeclareStaticArg(node, ctx);
69
75
  }
70
76
  if (originalName === PYTHON_DECLARE_VAR) {
@@ -77,7 +83,7 @@ export async function parseStringExpression(node, ctx) {
77
83
  }
78
84
  /**
79
85
  * Parses an f-string into a StringNode tree.
80
- * string_content → text nodes, interpolation → check for declare_static/declare_var
86
+ * string_content → text nodes, interpolation → check for derive/declare_var
81
87
  */
82
88
  async function parseFString(node, ctx) {
83
89
  const parts = [];
@@ -108,7 +114,7 @@ async function parseFString(node, ctx) {
108
114
  }
109
115
  /**
110
116
  * Parses an interpolation within an f-string.
111
- * Must be a declare_static() or declare_var() call.
117
+ * Must be a derive() or declare_var() call.
112
118
  */
113
119
  async function parseInterpolation(interpNode, ctx) {
114
120
  // Find the expression inside the interpolation (skip { and })
@@ -132,7 +138,7 @@ async function parseInterpolation(interpNode, ctx) {
132
138
  const funcNode = expr.childForFieldName('function');
133
139
  if (funcNode && funcNode.type === 'identifier') {
134
140
  const originalName = getOriginalImportName(funcNode.text, ctx.imports);
135
- if (originalName === PYTHON_DECLARE_STATIC) {
141
+ if (isDeriveFunction(originalName)) {
136
142
  return resolveDeclareStaticArg(expr, ctx);
137
143
  }
138
144
  if (originalName === PYTHON_DECLARE_VAR) {
@@ -140,8 +146,8 @@ async function parseInterpolation(interpNode, ctx) {
140
146
  }
141
147
  }
142
148
  }
143
- // Not a declare_static/declare_var call — error
144
- ctx.errors.push(`${locationStr(interpNode)}: f-string interpolation must use declare_static() or declare_var(), got "${expr.text}"`);
149
+ // Not a derive/declare_var call — error
150
+ ctx.errors.push(`${locationStr(interpNode)}: f-string interpolation must use derive() or declare_var(), got "${expr.text}"`);
145
151
  return null;
146
152
  }
147
153
  /**
@@ -181,13 +187,13 @@ async function parseBinaryOperator(node, ctx) {
181
187
  return { type: 'sequence', nodes: parts };
182
188
  }
183
189
  /**
184
- * Resolves the argument of a declare_static() call into a StringNode.
190
+ * Resolves the argument of a derive() call into a StringNode.
185
191
  * Handles: string literals, ternary expressions, function calls.
186
192
  */
187
193
  async function resolveDeclareStaticArg(callNode, ctx) {
188
194
  const arg = getFirstPositionalArg(callNode);
189
195
  if (!arg) {
190
- ctx.errors.push(`${locationStr(callNode)}: declare_static() requires an argument`);
196
+ ctx.errors.push(`${locationStr(callNode)}: derive() / declare_static() requires an argument`);
191
197
  return null;
192
198
  }
193
199
  return resolveStaticValue(arg, ctx);
@@ -195,7 +201,7 @@ async function resolveDeclareStaticArg(callNode, ctx) {
195
201
  /**
196
202
  * Resolves a value expression that should produce string variants.
197
203
  * Handles: string literals, ternary, function calls, binary concat,
198
- * and declare_var() calls (nested inside declare_static).
204
+ * and declare_var() calls (nested inside derive).
199
205
  */
200
206
  async function resolveStaticValue(node, ctx) {
201
207
  // Parenthesized expression: unwrap and recurse
@@ -234,7 +240,7 @@ async function resolveStaticValue(node, ctx) {
234
240
  }
235
241
  return resolveFunctionCall(node, ctx);
236
242
  }
237
- ctx.errors.push(`${locationStr(node)}: unsupported declare_static argument type "${node.type}"`);
243
+ ctx.errors.push(`${locationStr(node)}: unsupported derive() argument type "${node.type}"`);
238
244
  return null;
239
245
  }
240
246
  /**
@@ -383,7 +389,7 @@ function extractImportsFromRoot(rootNode, parentImports) {
383
389
  const fileImports = extractImports(rootNode);
384
390
  // Carry over GT declare_* imports from the calling context
385
391
  // (in case the helper file doesn't import them directly)
386
- const parentDeclareImports = parentImports.filter((imp) => imp.originalName === PYTHON_DECLARE_STATIC ||
392
+ const parentDeclareImports = parentImports.filter((imp) => isDeriveFunction(imp.originalName) ||
387
393
  imp.originalName === PYTHON_DECLARE_VAR);
388
394
  // Deduplicate: prefer the target file's own imports over parent's
389
395
  const seen = new Set(fileImports.map((imp) => imp.localName));
@@ -457,10 +463,10 @@ function getOriginalImportName(localName, imports) {
457
463
  }
458
464
  return null;
459
465
  }
460
- function getStaticImportNames(imports) {
466
+ function getDeriveImportNames(imports) {
461
467
  const names = new Set();
462
468
  for (const imp of imports) {
463
- if (imp.originalName === PYTHON_DECLARE_STATIC ||
469
+ if (isDeriveFunction(imp.originalName) ||
464
470
  imp.originalName === PYTHON_DECLARE_VAR) {
465
471
  names.add(imp.localName);
466
472
  }
@@ -3,7 +3,7 @@ import type { StringNode } from './stringNode.js';
3
3
  /**
4
4
  * Callback to parse a return expression into a StringNode.
5
5
  * Provided by the caller so function resolution doesn't need to know about
6
- * declare_var, declare_static, imports, etc.
6
+ * declare_var, derive, imports, etc.
7
7
  *
8
8
  * @param node - The return expression AST node
9
9
  * @param rootNode - The root AST node of the file containing the function
@@ -141,7 +141,7 @@ function collectReturnExpressions(node, results) {
141
141
  }
142
142
  /**
143
143
  * Checks if a function name is re-exported from another module in the given file.
144
- * e.g., `from static_test import get_gender` makes `get_gender` a re-export.
144
+ * e.g., `from derive_test import get_gender` makes `get_gender` a re-export.
145
145
  */
146
146
  function findReExport(functionName, rootNode, currentFilePath) {
147
147
  for (let i = 0; i < rootNode.childCount; i++) {
@@ -1,5 +1,5 @@
1
1
  // ===== Tree Construction ===== //
2
- // Used for declare_static / declare_var parsing
2
+ // Used for derive / declare_var parsing
3
3
  /**
4
4
  * Converts a StringNode tree into all possible string variants.
5
5
  * - TextNode → single string
package/dist/types.d.ts CHANGED
@@ -11,6 +11,6 @@ export type ExtractionMetadata = {
11
11
  context?: string;
12
12
  maxChars?: number;
13
13
  filePaths?: string[];
14
- /** Groups related static content variants together (for declareStatic/declare_static) */
14
+ /** Groups related derive content variants together (for derive/declare_static) */
15
15
  staticId?: string;
16
16
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@generaltranslation/python-extractor",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Python source code extraction for General Translation",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -47,7 +47,7 @@
47
47
  "dependencies": {
48
48
  "tree-sitter-python": "^0.25.0",
49
49
  "web-tree-sitter": "^0.26.6",
50
- "generaltranslation": "8.1.17"
50
+ "generaltranslation": "8.1.19"
51
51
  },
52
52
  "scripts": {
53
53
  "build": "tsc",