Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

make input paths relative to current working directory #20

Merged
merged 7 commits into from
Oct 5, 2017

Conversation

brekk
Copy link
Contributor

@brekk brekk commented Oct 3, 2017

Thanks for this library, it's exactly what I was looking for!

I was unable to run this library without augmenting it:

tscodeshift -t dist/src/transforms/mocha.js 'my-fixture-file.ts'

Always resulted in:

Failed to run tscodeshift. {
    Error: Cannot find module 'dist/src/transforms/mocha.js'
    [...]
}

node version: v8.5.0

I also added a test to prove that this works as expected.

Please let me know if there are additional things you'd like me to change.

@KnisterPeter
Copy link
Owner

Awesome. Thanks for the contribution. 🚀
After a first look, I would like to have the yarn file removed. I favor npm and even without yarn.lock it will be installable with yarn. The yarn.lock file will be too quickly out of date.

@KnisterPeter
Copy link
Owner

For relative paths you could also used 'tscodeshift -t ./dist/src/transforms/mocha.js 'my-fixture-file.ts'The./` in front of the relative path makes it a node file and not a node module.

@KnisterPeter
Copy link
Owner

KnisterPeter commented Oct 3, 2017

And since you updated typescript to the latest version it my be required to update the test harness as well.
This is a manuall npm script and maybe a few parser updates to implement.
The harness is cached and only runs manually and on CI for performance reasons.

EDIT: Oh, don't mind. This would be located in the ts-emitter project. Not in here. So this is fine.

@brekk
Copy link
Contributor Author

brekk commented Oct 3, 2017

@KnisterPeter Cool, I'll remove the yarn.lock file. I could be wrong here but I believe I tried it with ./ relative pathing and had the same issue -- will verify.

@@ -13,7 +13,7 @@
"start": "npm test -- --watch",
"clean": "rimraf dist",
"build": "tsc",
"test": "jest",
"test": "npm run build && jest",
Copy link
Owner

Choose a reason for hiding this comment

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

Running build before jest is not required with ts-jest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, relative to how I am building stuff, this is; basically I wanted to be able to ask the compiled / for-publication version to build things at the command-line, and dist/src doesn't exist at that point.

More comments below.

package.json Outdated
@@ -59,13 +60,16 @@
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "\\.test\\.(ts|tsx)$",
"testRegex": "\\.test\\.(ts|tsx|js)$",
Copy link
Owner

Choose a reason for hiding this comment

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

Why do you need to find js files for tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See comment here

@@ -123,3 +123,68 @@ it('convert var declarations to let declarations', () => {

expect(actual).toBe(expected);
});

test(`api`, () => {
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure what you are testing here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, there's an else case that is very hard to get to inside of the ./transform#api.tscodeshift function, and this test covers that else case. Sorry, I should have clarified my test case after I got it working. (Basically the coverage was lowered by me adding the files I added, so by covering the else case, I was able to get coveralls to pass.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -0,0 +1,42 @@
/* global test, expect, afterEach */
Copy link
Owner

Choose a reason for hiding this comment

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

This files belongs into the src folder. Even tests are sources.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, in general the reason I have this .js file is more from my lack of familiarity with TypeScript. I am more than happy to have this file be in src, but relative to the problem I was trying to solve, I didn't wanna jump through hoops of TypeScript fixing while trying to get the gist of this thing working. I think specifically I was running into some issues trying to use path -- this evening I'll try and regroup and clarify my intentions in this PR.

const api = {
tscodeshift: (source: string|ts.Node) => {
export const api = {
tscodeshift: (source: string|ts.Node): Collection<any, any> => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KnisterPeter I had to make this change in order to get tsc to compile properly -- I'm guessing there's a better way to do this, but I was running into some problems locally.

const execa = require('execa')
const path = require('path')

const CLI = path.resolve(__dirname, `./dist/src/index.js`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KnisterPeter This is the reason for that additional build step -- I want to be able to reference the compiled version of this file in the test.

@@ -124,7 +124,7 @@ it('convert var declarations to let declarations', () => {
expect(actual).toBe(expected);
});

test(`api`, () => {
test(`api.tscodeshift can process raw ts.Nodes`, () => {
expect(typeof api).toBe(`object`);
expect(typeof api.tscodeshift).toBe(`function`);
const removeCircularCrap = (x: any): any => {
Copy link
Owner

Choose a reason for hiding this comment

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

Rename this from 'Crap' to 'References' 😉

@@ -136,7 +136,6 @@ test(`api`, () => {
return x;
};
const expected = JSON.parse(JSON.stringify(removeCircularCrap(api.tscodeshift(`false`))));
// expect(JSON.parse(JSON.stringify(removeCircularCrap(expected)))).toEqual(circular)
Copy link
Owner

Choose a reason for hiding this comment

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

Remove commented out lines

@@ -0,0 +1,50 @@
/* global test, expect, afterEach */
// https://stackoverflow.com/questions/39415661
Copy link
Owner

Choose a reason for hiding this comment

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

You do not need this reference for the import-require statement.
Its part of the official typescript documentation. No need to quote these.

https://www.typescriptlang.org/docs/handbook/modules.html

@KnisterPeter
Copy link
Owner

Besides all my comments. Thanks for you awesome contribution. 👍

@KnisterPeter KnisterPeter merged commit 218f9b8 into KnisterPeter:master Oct 5, 2017
@KnisterPeter KnisterPeter mentioned this pull request Oct 5, 2017
@KnisterPeter
Copy link
Owner

I've updated your test code to do not require npm run build before jest.
This means npm run jest -- --watch is possible again.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants