Skip to content

Instantly share code, notes, and snippets.

@elidupuis
Last active November 13, 2023 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elidupuis/22fb1e8d72f4cad348330f7319f62f4a to your computer and use it in GitHub Desktop.
Save elidupuis/22fb1e8d72f4cad348330f7319f62f4a to your computer and use it in GitHub Desktop.
ember-intl v6 migration codemod
// Install jscodeshift globally if you haven't already
// npm install -g jscodeshift
/**
* ember-intl-setup-codemod.js
* This codemod is to help deal with the ember-intl v5 to v6 migration.
* Specfically, the "Missing setupIntl() results in a runtime error" error.
*
* All it does is add the following lines:
*
* Immediately after the 'ember-qunit' import statement:
* ```
* import { setupIntl } from "ember-intl/test-support";
* ```
*
* And, immediately after the `setupRenderingTest(hooks);` call:
* ```
* setupIntl(hooks);
* ```
*
* Usage: jscodeshift -t ember-intl-setup-codemod.js <path_to_your_files>
* Given the above, you should only run this codemod on integration test files.
* @see https://ember-intl.github.io/ember-intl/docs/guide/migration-5-0-to-6-1#missing-setupintl-results-in-a-runtime-error
*/
module.exports = function (file, api) {
const j = api.jscodeshift;
const root = j(file.source);
// Find all existing import paths
const existingImports = new Set();
root.find(j.ImportDeclaration).forEach((path) => {
const importSource = path.node.source.value;
existingImports.add(importSource);
});
// Define the new import path
const newImportPath = "ember-intl/test-support";
// Only make changes if the setupIntl import does not exist already.
if (!existingImports.has(newImportPath)) {
// Define the specific import path after which you want to inject the new import
const specificImportPath = "ember-qunit";
// Find the specific import statement
const specificImport = root.find(j.ImportDeclaration, {
source: {
value: specificImportPath,
},
});
// Inject a new import statement after the specific import
if (specificImport.size() > 0) {
specificImport.forEach((path) => {
path.insertAfter(
j.importDeclaration(
[j.importSpecifier(j.identifier("setupIntl"))],
j.literal("ember-intl/test-support")
)
);
});
}
// Find the specific import statement
const setupRenderingTestExpression = root.find(j.ExpressionStatement, {
expression: {
callee: {
type: "Identifier",
name: "setupRenderingTest",
},
},
});
// Inject a new call expression after the specific call expression
if (setupRenderingTestExpression.size() > 0) {
setupRenderingTestExpression.forEach((path) => {
const newCallExpression = j.expressionStatement(
j.callExpression(
j.identifier("setupIntl"),
[j.identifier("hooks")]
)
);
path.insertAfter(newCallExpression);
});
}
}
return root.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment