diff --git a/src/__tests__/cli/transformer.test.ts b/src/__tests__/cli/transformer.test.ts index 5fad4a4..393a311 100644 --- a/src/__tests__/cli/transformer.test.ts +++ b/src/__tests__/cli/transformer.test.ts @@ -13,61 +13,61 @@ describe("transformer(...)", () => { name: "with no i18next import", actual: ``, expected: - 'import { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n', + 'import { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\n', }, { name: "with i18next import", actual: `import i18next from "i18next";`, expected: - 'import i18next, { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n', + 'import i18next, { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\n', }, { name: "with i18next import + named imports", actual: `import {t} from "i18next";`, expected: - 'import { t, changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n', + 'import { t, changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\n', }, { name: "with i18next import + named imports + language switch", actual: `import { t, changeLanguage } from "i18next";changeLanguage("en");\n`, expected: - 'import { t, changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n', + 'import { t, changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\n', }, { name: "with i18next import + global import + named imports + language switch", actual: `import i18next, { t, changeLanguage } from "i18next";changeLanguage("en");`, expected: - 'import i18next, { t, changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n', + 'import i18next, { t, changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\n', }, { name: "with changeLanguage being called from i18next import", actual: `import i18next from "i18next";i18next.changeLanguage("en");`, expected: - 'import i18next, { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n', + 'import i18next, { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\n', }, { name: "with changeLanguage being called from i18next import + named imports", actual: `import i18next, { changeLanguage } from "i18next";i18next.changeLanguage("en");`, expected: - 'import i18next, { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n', + 'import i18next, { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\n', }, { name: "with other imports", actual: `import foo from "foo";\nimport bar from "bar";\nimport {baz} from "baz";\nimport { changeLanguage } from "i18next";changeLanguage("en");`, expected: - 'import foo from "foo";\nimport bar from "bar";\nimport { baz } from "baz";\nimport { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n', + 'import foo from "foo";\nimport bar from "bar";\nimport { baz } from "baz";\nimport { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\n', }, { name: "with logic after imports", actual: `import { changeLanguage } from "i18next";\n\nconst a = 1 + 2;\nchangeLanguage("en");`, expected: - 'import { changeLanguage } from "i18next";\nconst a = 1 + 2;\n\nchangeLanguage("fr");\n', + 'import { changeLanguage } from "i18next";\n\nchangeLanguage("fr");\n\nconst a = 1 + 2;\n', }, { name: "with relative imports", actual: `import Foo from "../Foo.astro";\nimport Bar from "../../Bar.astro";\nimport { baz } from "./baz";`, expected: - 'import { changeLanguage } from "i18next";\nimport Foo from "../../Foo.astro";\nimport Bar from "../../../Bar.astro";\nimport { baz } from "../baz";\n\nchangeLanguage("fr");\n', + 'import { changeLanguage } from "i18next";\nimport Foo from "../../Foo.astro";\nimport Bar from "../../../Bar.astro";\nimport { baz } from "../baz";\n\nchangeLanguage("fr");\n\n', }, ]; diff --git a/src/cli/transformer.ts b/src/cli/transformer.ts index c2d9b7b..eab6c71 100644 --- a/src/cli/transformer.ts +++ b/src/cli/transformer.ts @@ -1,3 +1,4 @@ +import { cp } from "fs-extra"; import ts from "typescript"; import { addDepthToRelativePath } from "./utils"; @@ -13,6 +14,8 @@ export const transformer: ts.TransformerFactory = const { language, fileDepth } = getCompilerOptions(); + // get latest import declaration + function visit(node: ts.Node): ts.Node { // isolate i18next import statement if ( @@ -26,7 +29,6 @@ export const transformer: ts.TransformerFactory = if (!node.importClause.namedBindings) { return factory.updateImportDeclaration( node, - node.decorators, node.modifiers, factory.createImportClause( node.importClause.isTypeOnly, @@ -54,7 +56,6 @@ export const transformer: ts.TransformerFactory = // import changeLanguage function in i18next import declaration return factory.updateImportDeclaration( node, - node.decorators, node.modifiers, factory.updateImportClause( node.importClause, @@ -85,7 +86,6 @@ export const transformer: ts.TransformerFactory = ) { return factory.updateImportDeclaration( node, - node.decorators, node.modifiers, node.importClause, factory.createStringLiteral( @@ -115,23 +115,11 @@ export const transformer: ts.TransformerFactory = let visitedNode = ts.visitNode(rootNode, visit); - let statements: ts.NodeArray = factory.createNodeArray([ - ...visitedNode.statements, - // append the changeLanguageStatement after a new line - factory.createIdentifier("\n") as unknown as ts.Statement, - factory.createExpressionStatement( - factory.createCallExpression( - factory.createIdentifier("changeLanguage"), - undefined, - [factory.createStringLiteral(language as string)] - ) - ), - ]); + const statements = [...visitedNode.statements]; if (!doesI18nextImportExist) { - statements = factory.createNodeArray([ + statements.unshift( factory.createImportDeclaration( - undefined, undefined, factory.createImportClause( false, @@ -146,11 +134,43 @@ export const transformer: ts.TransformerFactory = ), factory.createStringLiteral("i18next"), undefined - ), - ...statements, - ]); + ) + ); } + // append the changeLanguage statement after imports + + // get an array with + const importDeclarationsMap = statements.map((statement) => + ts.isImportDeclaration(statement) + ); + + const lastIndexOfImportDeclaration = + importDeclarationsMap.lastIndexOf(true); + + // insert changeLanguage statement after the imports + statements.splice( + lastIndexOfImportDeclaration + 1, + 0, + factory.createIdentifier("\n") as unknown as ts.Statement + ); + statements.splice( + lastIndexOfImportDeclaration + 1, + 0, + factory.createExpressionStatement( + factory.createCallExpression( + factory.createIdentifier("changeLanguage"), + undefined, + [factory.createStringLiteral(language as string)] + ) + ) + ); + statements.splice( + lastIndexOfImportDeclaration + 1, + 0, + factory.createIdentifier("\n") as unknown as ts.Statement + ); + visitedNode = factory.updateSourceFile( visitedNode, statements,