|
|
|
@@ -5,10 +5,8 @@
|
|
|
|
|
|
|
|
|
|
import { parseAst } from 'rolldown/parseAst';
|
|
|
|
|
import * as estreeWalker from 'estree-walker';
|
|
|
|
|
import { assertNever, assertType } from '../utils.js';
|
|
|
|
|
import type { ESTree as RolldownESTree } from 'rolldown/utils';
|
|
|
|
|
import type { AstNode } from 'rollup';
|
|
|
|
|
import type * as estree from 'estree';
|
|
|
|
|
import { assertNever } from '../utils.js';
|
|
|
|
|
import type { ESTree } from 'rolldown/utils';
|
|
|
|
|
import type { LocaleInliner, TextModification } from '../locale-inliner.js';
|
|
|
|
|
import type { Logger } from '../logger.js';
|
|
|
|
|
|
|
|
|
@@ -17,9 +15,15 @@ interface WalkerContext {
|
|
|
|
|
skip: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const walk = estreeWalker.walk as {
|
|
|
|
|
(node: ESTree.Node, callback: {
|
|
|
|
|
enter?: (this: WalkerContext, node: ESTree.Node, parent: ESTree.Node | null, property: string | number | symbol | null | undefined) => void;
|
|
|
|
|
}): void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function collectModifications(sourceCode: string, fileName: string, fileLogger: Logger, inliner: LocaleInliner): TextModification[] {
|
|
|
|
|
if (sourceCode === '') return [];
|
|
|
|
|
let programNode: RolldownESTree.Program;
|
|
|
|
|
let programNode: ESTree.Program;
|
|
|
|
|
try {
|
|
|
|
|
programNode = parseAst(sourceCode);
|
|
|
|
|
} catch (err) {
|
|
|
|
@@ -37,11 +41,8 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
// 1) replace all `scripts/` path literals with locale code
|
|
|
|
|
// 2) replace all `localStorage.getItem("lang")` with `localeName` variable
|
|
|
|
|
// 3) replace all `await window.fetch(`/assets/locales/${d}.${x}.json`).then(u=>u.json())` with `localeJson` variable
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
(estreeWalker.walk as any)(programNode, {
|
|
|
|
|
enter(this: WalkerContext, node: Node) {
|
|
|
|
|
assertType<AstNode>(node);
|
|
|
|
|
|
|
|
|
|
walk(programNode, {
|
|
|
|
|
enter(this: WalkerContext, node: ESTree.Node) {
|
|
|
|
|
if (node.type === 'Literal' && typeof node.value === 'string' && node.raw) {
|
|
|
|
|
if (node.raw.substring(1).startsWith(inliner.scriptsDir)) {
|
|
|
|
|
// we find `scripts/\w+\.js` literal and replace 'scripts' part with locale code
|
|
|
|
@@ -92,7 +93,7 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const importSpecifierResult = findImportSpecifier(programNode, inliner.i18nFileName, 'i18n');
|
|
|
|
|
const importSpecifierResult = findImportSpecifier(programNode, inliner.i18nFileName, inliner.i18nSymbol);
|
|
|
|
|
|
|
|
|
|
switch (importSpecifierResult.type) {
|
|
|
|
|
case 'no-import':
|
|
|
|
@@ -108,7 +109,7 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
});
|
|
|
|
|
return modifications;
|
|
|
|
|
case 'unexpected-specifiers':
|
|
|
|
|
fileLogger.info(`Importing ${inliner.i18nFileName} found but with unexpected specifiers. Skipping inlining.`);
|
|
|
|
|
fileLogger.error(`Importing ${inliner.i18nFileName} found but with unexpected specifiers. Skipping inlining.`);
|
|
|
|
|
return modifications;
|
|
|
|
|
case 'specifier':
|
|
|
|
|
fileLogger.debug(`Found import i18n as ${importSpecifierResult.localI18nIdentifier}`);
|
|
|
|
@@ -118,42 +119,18 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
const i18nImport = importSpecifierResult.importNode;
|
|
|
|
|
const localI18nIdentifier = importSpecifierResult.localI18nIdentifier;
|
|
|
|
|
|
|
|
|
|
// Check if the identifier is already declared in the file.
|
|
|
|
|
// If it is, we may overwrite it and cause issues so we skip inlining
|
|
|
|
|
let isSupported = true;
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
(estreeWalker.walk as any)(programNode, {
|
|
|
|
|
enter(node: Node) {
|
|
|
|
|
if (node.type === 'VariableDeclaration') {
|
|
|
|
|
assertType<estree.VariableDeclaration>(node);
|
|
|
|
|
for (const id of node.declarations.flatMap(x => declsOfPattern(x.id))) {
|
|
|
|
|
if (id === localI18nIdentifier) {
|
|
|
|
|
isSupported = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
|
if (!isSupported) {
|
|
|
|
|
fileLogger.error(`Duplicated identifier "${localI18nIdentifier}" in variable declaration. Skipping inlining.`);
|
|
|
|
|
return modifications;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileLogger.debug(`imports i18n as ${localI18nIdentifier}`);
|
|
|
|
|
fileLogger.debug(`imports ${inliner.i18nSymbol} /*i18n*/ as ${localI18nIdentifier}`);
|
|
|
|
|
|
|
|
|
|
// In case of substitution failure, we will preserve the import statement
|
|
|
|
|
// otherwise we will remove it.
|
|
|
|
|
let preserveI18nImport = false;
|
|
|
|
|
|
|
|
|
|
const codeModifications: TextModification[] = [];
|
|
|
|
|
|
|
|
|
|
const toSkip = new Set();
|
|
|
|
|
toSkip.add(i18nImport);
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
(estreeWalker.walk as any)(programNode, {
|
|
|
|
|
enter(this: WalkerContext, node: Node, parent: Node | null, property: string | number | symbol | null | undefined) {
|
|
|
|
|
assertType<AstNode>(node);
|
|
|
|
|
assertType<AstNode>(parent);
|
|
|
|
|
walk(programNode, {
|
|
|
|
|
enter(this: WalkerContext, node, parent, property) {
|
|
|
|
|
if (toSkip.has(node)) {
|
|
|
|
|
// This is the import specifier, skip processing it
|
|
|
|
|
this.skip();
|
|
|
|
@@ -164,25 +141,24 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
if (node.type === 'ImportDeclaration') this.skip();
|
|
|
|
|
|
|
|
|
|
if (node.type === 'Identifier') {
|
|
|
|
|
assertType<estree.Identifier>(node);
|
|
|
|
|
assertType<estree.Property | estree.MemberExpression | estree.ExportSpecifier>(parent);
|
|
|
|
|
if (parent == null) throw new Error();
|
|
|
|
|
if (parent.type === 'Property' && !parent.computed && property === 'key') return; // we don't care 'id' part of { id: expr }
|
|
|
|
|
if (parent.type === 'MemberExpression' && !parent.computed && property === 'property') return; // we don't care 'id' part of { id: expr }
|
|
|
|
|
if (parent.type === 'ExportSpecifier' && property === 'exported') return; // we don't care 'id' part of { id: expr }
|
|
|
|
|
if (node.name === localI18nIdentifier) {
|
|
|
|
|
// the use of identifier is either direct reference to i18n, or unsupported conflict of the identifier, which should report error.
|
|
|
|
|
fileLogger.error(`${lineCol(sourceCode, node)}: Using i18n identifier "${localI18nIdentifier}" directly. Skipping inlining.`);
|
|
|
|
|
preserveI18nImport = true;
|
|
|
|
|
}
|
|
|
|
|
} else if (node.type === 'MemberExpression') {
|
|
|
|
|
assertType<estree.MemberExpression>(node);
|
|
|
|
|
const i18nPath = parseI18nPropertyAccess(node);
|
|
|
|
|
if (i18nPath != null && i18nPath.length >= 2 && i18nPath[0] === 'ts') {
|
|
|
|
|
if (parent.type === 'CallExpression' && property === 'callee') return; // we don't want to process `i18n.ts.property.stringBuiltinMethod()`
|
|
|
|
|
if (parent != null && parent.type === 'CallExpression' && property === 'callee') return; // we don't want to process `i18n.ts.property.stringBuiltinMethod()`
|
|
|
|
|
if (i18nPath.at(-1)?.startsWith('_')) fileLogger.debug(`found i18n grouped property access ${i18nPath.join('.')}`);
|
|
|
|
|
else fileLogger.debug(`${lineCol(sourceCode, node)}: found i18n property access ${i18nPath.join('.')}`);
|
|
|
|
|
// it's i18n.ts.propertyAccess
|
|
|
|
|
// i18n.ts.* will always be resolved to string or object containing strings
|
|
|
|
|
modifications.push({
|
|
|
|
|
codeModifications.push({
|
|
|
|
|
type: 'localized',
|
|
|
|
|
begin: node.start,
|
|
|
|
|
end: node.end,
|
|
|
|
@@ -194,7 +170,7 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
// it's parameterized locale substitution (`i18n.tsx.property(parameters)`)
|
|
|
|
|
// we expect the parameter to be an object literal
|
|
|
|
|
fileLogger.debug(`${lineCol(sourceCode, node)}: found i18n function access (object) ${i18nPath.join('.')}`);
|
|
|
|
|
modifications.push({
|
|
|
|
|
codeModifications.push({
|
|
|
|
|
type: 'parameterized-function',
|
|
|
|
|
begin: node.start,
|
|
|
|
|
end: node.end,
|
|
|
|
@@ -203,10 +179,41 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
});
|
|
|
|
|
this.skip();
|
|
|
|
|
}
|
|
|
|
|
} else if (node.type === 'ArrowFunctionExpression') {
|
|
|
|
|
assertType<estree.ArrowFunctionExpression>(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scope check
|
|
|
|
|
if (node.type === 'FunctionDeclaration'
|
|
|
|
|
|| node.type === 'FunctionExpression'
|
|
|
|
|
|| node.type === 'ArrowFunctionExpression') {
|
|
|
|
|
// if i18n is introduced as the Named Function Expression, interior of the function does not matter
|
|
|
|
|
if (node.id?.name === localI18nIdentifier) this.skip();
|
|
|
|
|
// If there is 'i18n' in the parameters, we care interior of the function
|
|
|
|
|
if (node.params.flatMap(param => declsOfPattern(param)).includes(localI18nIdentifier)) this.skip();
|
|
|
|
|
|
|
|
|
|
// We find var declation inside the function and if there are
|
|
|
|
|
if (findFunctionScopeDecls(node).includes(localI18nIdentifier)) this.skip();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node.type === 'BlockStatement') {
|
|
|
|
|
// We find block-scope declaration inside the block, or from parent node if the block is part of for statement or catch clause
|
|
|
|
|
if (findBlockScopeDecls(node).includes(localI18nIdentifier)) this.skip();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// statements and clauses introduces new variables in variable scope
|
|
|
|
|
if (node.type === 'CatchClause') {
|
|
|
|
|
if (node.param != null) {
|
|
|
|
|
if (declsOfPattern(node.param).includes(localI18nIdentifier)) this.skip();
|
|
|
|
|
}
|
|
|
|
|
} else if (node.type === 'ForStatement') {
|
|
|
|
|
if (node.init?.type === 'VariableDeclaration') {
|
|
|
|
|
if (node.init.declarations.flatMap(x => declsOfPattern(x.id)).includes(localI18nIdentifier)) this.skip();
|
|
|
|
|
}
|
|
|
|
|
} else if (node.type === 'ForInStatement' || node.type === 'ForOfStatement') {
|
|
|
|
|
if (node.left.type === 'VariableDeclaration') {
|
|
|
|
|
if (node.left.declarations.flatMap(x => declsOfPattern(x.id)).includes(localI18nIdentifier)) this.skip();
|
|
|
|
|
} else {
|
|
|
|
|
if (declsOfPattern(node.left).includes(localI18nIdentifier)) this.skip();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
@@ -214,7 +221,7 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
|
|
|
if (!preserveI18nImport) {
|
|
|
|
|
fileLogger.debug('removing i18n import statement');
|
|
|
|
|
modifications.push({
|
|
|
|
|
codeModifications.push({
|
|
|
|
|
type: 'delete',
|
|
|
|
|
begin: i18nImport.start,
|
|
|
|
|
end: i18nImport.end,
|
|
|
|
@@ -222,7 +229,7 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseI18nPropertyAccess(node: estree.Expression | estree.Super): string[] | null {
|
|
|
|
|
function parseI18nPropertyAccess(node: ESTree.Expression | ESTree.Super): string[] | null {
|
|
|
|
|
if (node.type === 'Identifier' && node.name === localI18nIdentifier) return []; // i18n itself
|
|
|
|
|
if (node.type !== 'MemberExpression') return null;
|
|
|
|
|
// super.*
|
|
|
|
@@ -236,6 +243,9 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
if (node.property.type === 'Literal' && typeof node.property.value === 'string') {
|
|
|
|
|
id = node.property.value;
|
|
|
|
|
}
|
|
|
|
|
if (node.property.type === 'TemplateLiteral' && node.property.quasis.length === 1) {
|
|
|
|
|
id = node.property.quasis[0].value.cooked;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (node.property.type === 'Identifier') {
|
|
|
|
|
id = node.property.name;
|
|
|
|
@@ -249,10 +259,10 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|
|
|
|
return [...parentAccess, id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return modifications;
|
|
|
|
|
return [...modifications, ...codeModifications];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function declsOfPattern(pattern: estree.Pattern | null): string[] {
|
|
|
|
|
function declsOfPattern(pattern: ESTree.BindingPattern | ESTree.ParamPattern | ESTree.ArrayAssignmentTarget | ESTree.ObjectAssignmentTarget | ESTree.AssignmentTargetMaybeDefault | ESTree.AssignmentTargetRest | null): string[] {
|
|
|
|
|
if (pattern == null) return [];
|
|
|
|
|
switch (pattern.type) {
|
|
|
|
|
case 'Identifier':
|
|
|
|
@@ -275,15 +285,19 @@ function declsOfPattern(pattern: estree.Pattern | null): string[] {
|
|
|
|
|
case 'AssignmentPattern':
|
|
|
|
|
return declsOfPattern(pattern.left);
|
|
|
|
|
case 'MemberExpression':
|
|
|
|
|
// assignment pattern so no new variable is declared
|
|
|
|
|
return [];
|
|
|
|
|
case 'TSAsExpression':
|
|
|
|
|
case 'TSSatisfiesExpression':
|
|
|
|
|
case 'TSTypeAssertion':
|
|
|
|
|
case 'TSNonNullExpression':
|
|
|
|
|
return []; // not introducing new symbol
|
|
|
|
|
case 'TSParameterProperty':
|
|
|
|
|
throw new Error();
|
|
|
|
|
default:
|
|
|
|
|
assertNever(pattern);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function lineCol(sourceCode: string, node: estree.Node): string {
|
|
|
|
|
assertType<AstNode>(node);
|
|
|
|
|
function lineCol(sourceCode: string, node: ESTree.Node): string {
|
|
|
|
|
const leading = sourceCode.slice(0, node.start);
|
|
|
|
|
const lines = leading.split('\n');
|
|
|
|
|
const line = lines.length;
|
|
|
|
@@ -291,35 +305,65 @@ function lineCol(sourceCode: string, node: estree.Node): string {
|
|
|
|
|
return `(${line}:${col})`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findFunctionScopeDecls(fn: ESTree.Function | ESTree.ArrowFunctionExpression): string[] {
|
|
|
|
|
if (fn.body == null) return [];
|
|
|
|
|
const decls: string[] = [];
|
|
|
|
|
walk(fn.body, {
|
|
|
|
|
enter(node) {
|
|
|
|
|
// The only function-scoped symbol declaration in strict mode is 'var'
|
|
|
|
|
// If it's non-strict mode, function declaration will also in function scope.
|
|
|
|
|
if (node.type === 'VariableDeclaration' && node.kind === 'var') {
|
|
|
|
|
decls.push(...node.declarations.flatMap(x => declsOfPattern(x.id)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node.type === 'FunctionDeclaration'
|
|
|
|
|
|| node.type === 'FunctionExpression'
|
|
|
|
|
|| node.type === 'ArrowFunctionExpression') {
|
|
|
|
|
// The function makes new inner scope
|
|
|
|
|
this.skip();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return decls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findBlockScopeDecls(block: ESTree.BlockStatement): string[] {
|
|
|
|
|
const decls: string[] = [];
|
|
|
|
|
|
|
|
|
|
for (const body of block.body) {
|
|
|
|
|
walk(body, {
|
|
|
|
|
enter(node) {
|
|
|
|
|
if (node.type === 'VariableDeclaration' && node.kind !== 'var') {
|
|
|
|
|
decls.push(...node.declarations.flatMap(x => declsOfPattern(x.id)));
|
|
|
|
|
} else if (node.type === 'FunctionDeclaration') {
|
|
|
|
|
if (node.id != null) decls.push(node.id.name);
|
|
|
|
|
} else if (node.type === 'ClassDeclaration') {
|
|
|
|
|
if (node.id != null) decls.push(node.id.name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
node.type === 'FunctionDeclaration'
|
|
|
|
|
|| node.type === 'FunctionExpression'
|
|
|
|
|
|| node.type === 'ArrowFunctionExpression'
|
|
|
|
|
|| node.type === 'BlockStatement'
|
|
|
|
|
|| node.type === 'CatchClause'
|
|
|
|
|
|| node.type === 'ForStatement'
|
|
|
|
|
|| node.type === 'ForInStatement'
|
|
|
|
|
|| node.type === 'ForOfStatement'
|
|
|
|
|
) {
|
|
|
|
|
// The function makes new inner scope
|
|
|
|
|
this.skip();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return decls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//region checker functions
|
|
|
|
|
|
|
|
|
|
type Node =
|
|
|
|
|
| estree.AssignmentProperty
|
|
|
|
|
| estree.CatchClause
|
|
|
|
|
| estree.Class
|
|
|
|
|
| estree.ClassBody
|
|
|
|
|
| estree.Expression
|
|
|
|
|
| estree.Function
|
|
|
|
|
| estree.Identifier
|
|
|
|
|
| estree.Literal
|
|
|
|
|
| estree.MethodDefinition
|
|
|
|
|
| estree.ModuleDeclaration
|
|
|
|
|
| estree.ModuleSpecifier
|
|
|
|
|
| estree.Pattern
|
|
|
|
|
| estree.PrivateIdentifier
|
|
|
|
|
| estree.Program
|
|
|
|
|
| estree.Property
|
|
|
|
|
| estree.PropertyDefinition
|
|
|
|
|
| estree.SpreadElement
|
|
|
|
|
| estree.Statement
|
|
|
|
|
| estree.Super
|
|
|
|
|
| estree.SwitchCase
|
|
|
|
|
| estree.TemplateElement
|
|
|
|
|
| estree.VariableDeclarator
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
// localStorage.getItem("lang")
|
|
|
|
|
function isLocalStorageGetItemLang(getItemCall: Node): boolean {
|
|
|
|
|
function isLocalStorageGetItemLang(getItemCall: ESTree.Node): boolean {
|
|
|
|
|
if (getItemCall.type !== 'CallExpression') return false;
|
|
|
|
|
if (getItemCall.arguments.length !== 1) return false;
|
|
|
|
|
|
|
|
|
@@ -336,7 +380,7 @@ function isLocalStorageGetItemLang(getItemCall: Node): boolean {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// await window.fetch(`/assets/locales/${d}.${x}.json`).then(u => u.json(), ....)
|
|
|
|
|
function isAwaitFetchLocaleThenJson(awaitNode: Node): boolean {
|
|
|
|
|
function isAwaitFetchLocaleThenJson(awaitNode: ESTree.Node): boolean {
|
|
|
|
|
if (awaitNode.type !== 'AwaitExpression') return false;
|
|
|
|
|
|
|
|
|
|
const thenCall = awaitNode.argument;
|
|
|
|
@@ -379,16 +423,15 @@ function isAwaitFetchLocaleThenJson(awaitNode: Node): boolean {
|
|
|
|
|
|
|
|
|
|
type SpecifierResult =
|
|
|
|
|
| { type: 'no-import' }
|
|
|
|
|
| { type: 'no-specifiers', importNode: estree.ImportDeclaration & AstNode }
|
|
|
|
|
| { type: 'unexpected-specifiers', importNode: estree.ImportDeclaration & AstNode }
|
|
|
|
|
| { type: 'specifier', localI18nIdentifier: string, importNode: estree.ImportDeclaration & AstNode }
|
|
|
|
|
| { type: 'no-specifiers', importNode: ESTree.ImportDeclaration }
|
|
|
|
|
| { type: 'unexpected-specifiers', importNode: ESTree.ImportDeclaration }
|
|
|
|
|
| { type: 'specifier', localI18nIdentifier: string, importNode: ESTree.ImportDeclaration }
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
function findImportSpecifier(programNode: RolldownESTree.Program, i18nFileName: string, i18nSymbol: string): SpecifierResult {
|
|
|
|
|
function findImportSpecifier(programNode: ESTree.Program, i18nFileName: string, i18nSymbol: string): SpecifierResult {
|
|
|
|
|
const imports = programNode.body.filter(x => x.type === 'ImportDeclaration');
|
|
|
|
|
const importNode = imports.find(x => x.source.value === `./${i18nFileName}`) as estree.ImportDeclaration | undefined;
|
|
|
|
|
const importNode = imports.find(x => x.source.value === `./${i18nFileName}`);
|
|
|
|
|
if (!importNode) return { type: 'no-import' };
|
|
|
|
|
assertType<AstNode>(importNode);
|
|
|
|
|
|
|
|
|
|
if (importNode.specifiers.length === 0) {
|
|
|
|
|
return { type: 'no-specifiers', importNode };
|
|
|
|
@@ -415,15 +458,15 @@ function findImportSpecifier(programNode: RolldownESTree.Program, i18nFileName:
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// checker helpers
|
|
|
|
|
function isMemberExpression(node: Node, property: string): node is estree.MemberExpression {
|
|
|
|
|
function isMemberExpression(node: ESTree.Node, property: string): node is ESTree.MemberExpression {
|
|
|
|
|
return node.type === 'MemberExpression' && !node.computed && node.property.type === 'Identifier' && node.property.name === property;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isStringLiteral(node: Node, value: string): node is estree.Literal {
|
|
|
|
|
function isStringLiteral(node: ESTree.Node, value: string): node is ESTree.StringLiteral {
|
|
|
|
|
return node.type === 'Literal' && typeof node.value === 'string' && node.value === value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isIdentifier(node: Node, name: string): node is estree.Identifier {
|
|
|
|
|
function isIdentifier(node: ESTree.Node, name: string): node is ESTree.IdentifierReference {
|
|
|
|
|
return node.type === 'Identifier' && node.name === name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|