Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for template codemods #81

Merged
merged 7 commits into from
Jul 15, 2020

Conversation

simonihmig
Copy link
Contributor

Closes #8

@simonihmig simonihmig changed the title Add support for Template support Add support for template codemods May 29, 2020
bin/cli.js Outdated Show resolved Hide resolved
@simonihmig
Copy link
Contributor Author

So tests were passing for me locally. This fails in CI due to:

  • ember-template-recase requiring node 10+. Which we could rely when Drop Node 8 support. #76 is merged
  • but that PR and even the master branch is failing CI, due to somehow execa not being able to execute yarn in a test. Idk why, briefly tried to fix this, e.g. by upgrading execa, but that didn't help either...

@rwjblue any clue what's going on?

@rwjblue
Copy link
Owner

rwjblue commented Jun 6, 2020

No, not sure. I was also having similar issues, and then ran out of time 😢.

I need to try to revive my Node version bump PR and figure out what is up 🤔

@rwjblue rwjblue added the enhancement New feature or request label Jun 24, 2020
@simonihmig
Copy link
Contributor Author

After rebasing, this is green now, so waiting for your review @rwjblue! 🙂

Btw, this is already in use (through my github fork) in https://github.com/kaliber5/ember-bootstrap-codemods...

commands/local/generate/codemod.js Outdated Show resolved Hide resolved
commands/local/generate/fixture.js Outdated Show resolved Hide resolved
src/bin-support.js Outdated Show resolved Hide resolved
src/bin-support.js Outdated Show resolved Hide resolved
src/bin-support.js Outdated Show resolved Hide resolved
src/test-support.js Outdated Show resolved Hide resolved
src/test-support/template.js Outdated Show resolved Hide resolved
src/test-support/template.js Outdated Show resolved Hide resolved
src/test-support/template.js Outdated Show resolved Hide resolved
A generated transform now exports its own type. This allows us to
* remove the `type` arguments from `runTransform()`, which was problematic anyway as that type would have to passed in the project's `cli.js`, so would not have allowed to have different transform types per project. Instead the type is taken from the transform's exported type itself.
* same for `runTransformTest()`: `options.type` is not needed anymore, as the type is also taken from the transform itself
* instead of failing when a user tries to generate a fixture for a transform with non-matching types, the generate fixture command now implicitly determines the fixture type from its transform type
@simonihmig
Copy link
Contributor Author

I refactored the previous implementation quite a bit, inspired by your feedback. A generated transform now exports its own type. This allows us to

  • remove the type arguments from runTransform(), which was problematic anyway as that type would have to passed in the project's cli.js, so would not have allowed to have different transform types per project. Instead the type is taken from the transform's exported type itself.
  • same for runTransformTest(): options.type is not needed anymore, as the type is also taken from the transform itself
  • instead of failing when a user tries to generate a fixture for a transform with non-matching types, the generate fixture command now implicitly determines the fixture type from "its" transform type

I hope this makes sense, @rwjblue please re-review!

Copy link
Owner

@rwjblue rwjblue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks great, thanks for doing that refactor!


let { codemodName, fixtureName } = options;
let codemodDir = `${process.cwd()}/transforms/${codemodName}`;
let codemodTransform = `${codemodDir}/index.js`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add index.js here? I wonder if we can basically just let it fall through via the require that is done down in getTransformType (since require(codemodDir) and require(codemodDir + '/index.js') are effectively the same thing)? That would allow folks to use other extensions (for example .ts)...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

function getTransformPath(binRoot, transformName) {
const path = require('path');

return path.join(binRoot, '..', 'transforms', transformName, 'index.js');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do require.resolve(path.join(binRoot, '..', 'transforms', transformName))?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -0,0 +1,67 @@
'use strict';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that this is (or could be) basically the same as the jscodeshift one but we run a different function for actually testing (jscodeshift runs runInlineTest and this one runs runTemplateTest).

I would prefer to refactor these together to avoid the duplication that exists right now, and to also ensure that both support the same feature set. At the moment, I think this file is missing the ability to test passing different command line options (that the template transform can access with getOptions).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, done.

Comment on lines 6 to 7
let root = process.cwd() + `/transforms/${options.name}/`;
let transformPath = root + 'index.js';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this should be using getTransformPath

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I now put getTransformPath into transform-support.js, and changed the signature a tiny bit, to expect the project's root folder (instead of binRoot, as we don't have/need binRoot here).

Comment on lines 6 to 8
if (!fs.existsSync(transformPath)) {
throw new Error(`Transform ${transformPath} not found.`);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use require.resolve in getTransformPath then I think this can be removed (since require.resolve would throw)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@rwjblue rwjblue merged commit 1e08083 into rwjblue:master Jul 15, 2020
@rwjblue
Copy link
Owner

rwjblue commented Jul 15, 2020

Thank you very much @simonihmig! I'll try to get a release out in the morning...

simonihmig added a commit to ember-bootstrap/ember-bootstrap-codemods that referenced this pull request Aug 4, 2020
Updated for the further changes to codemod-cli landed in rwjblue/codemod-cli#81 after review. (still waiting for an official release though)

Fixes #5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for template-recast codemods
2 participants