Skip to content

Commit

Permalink
fix(generate): inject changeLanguage statement after imports and befo…
Browse files Browse the repository at this point in the history
…re frontmatter logic

fixes #23
  • Loading branch information
yassinedoghri committed Aug 27, 2022
1 parent 3818054 commit 9c365f0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
20 changes: 10 additions & 10 deletions src/__tests__/cli/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
];

Expand Down
60 changes: 40 additions & 20 deletions src/cli/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cp } from "fs-extra";
import ts from "typescript";
import { addDepthToRelativePath } from "./utils";

Expand All @@ -13,6 +14,8 @@ export const transformer: ts.TransformerFactory<ts.SourceFile> =

const { language, fileDepth } = getCompilerOptions();

// get latest import declaration

function visit(node: ts.Node): ts.Node {
// isolate i18next import statement
if (
Expand All @@ -26,7 +29,6 @@ export const transformer: ts.TransformerFactory<ts.SourceFile> =
if (!node.importClause.namedBindings) {
return factory.updateImportDeclaration(
node,
node.decorators,
node.modifiers,
factory.createImportClause(
node.importClause.isTypeOnly,
Expand Down Expand Up @@ -54,7 +56,6 @@ export const transformer: ts.TransformerFactory<ts.SourceFile> =
// import changeLanguage function in i18next import declaration
return factory.updateImportDeclaration(
node,
node.decorators,
node.modifiers,
factory.updateImportClause(
node.importClause,
Expand Down Expand Up @@ -85,7 +86,6 @@ export const transformer: ts.TransformerFactory<ts.SourceFile> =
) {
return factory.updateImportDeclaration(
node,
node.decorators,
node.modifiers,
node.importClause,
factory.createStringLiteral(
Expand Down Expand Up @@ -115,23 +115,11 @@ export const transformer: ts.TransformerFactory<ts.SourceFile> =

let visitedNode = ts.visitNode(rootNode, visit);

let statements: ts.NodeArray<ts.Statement> = 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,
Expand All @@ -146,11 +134,43 @@ export const transformer: ts.TransformerFactory<ts.SourceFile> =
),
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,
Expand Down

0 comments on commit 9c365f0

Please sign in to comment.