UNPKG

@vendure/ngx-translate-extract

Version:

Extract strings from projects using ngx-translate

31 lines (30 loc) 1.15 kB
import pkg from 'typescript'; import { getStringsFromExpression, findSimpleCallExpressions, getAST } from '../utils/ast-helpers.js'; import { TranslationCollection } from '../utils/translation.collection.js'; const { isIdentifier } = pkg; export class FunctionParser { fnName; constructor(fnName) { this.fnName = fnName; } extract(source, filePath) { let collection = new TranslationCollection(); const sourceFile = getAST(source, filePath).parsedFile; if (!sourceFile) { return collection; } const callExpressions = findSimpleCallExpressions(sourceFile, this.fnName); callExpressions.forEach((callExpression) => { if (!isIdentifier(callExpression.expression) || callExpression.expression.escapedText !== this.fnName) { return; } const [firstArg] = callExpression.arguments; if (!firstArg) { return; } const strings = getStringsFromExpression(firstArg); collection = collection.addKeys(strings, filePath); }); return collection; } }