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

Commit

Permalink
fix: make paths relative
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Oct 3, 2017
1 parent a1eb22b commit 7bd544b
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 27 deletions.
25 changes: 25 additions & 0 deletions mocha.fixture.to-copy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
before(() => {
// something
});

beforeEach(() => {
});

suite('Array', function() {
setup(() => {
});

test('Array', (done: MochaDone) => {
done();
});

specify.skip('Array', function(done) {
done();
});
});

describe('', () => {
it('', (done: MochaDone) => {
done();
});
});
28 changes: 16 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"start": "npm test -- --watch",
"clean": "rimraf dist",
"build": "tsc",
"test": "jest",
"test": "npm run build && jest",
"coverage": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
"prerelease": "git checkout master && git pull origin master && npm test",
"release": "standard-version",
Expand All @@ -31,26 +31,27 @@
"devDependencies": {
"@knisterpeter/standard-tslint": "^1.5.1",
"@types/common-tags": "^1.2.5",
"@types/fs-extra": "^3.0.2",
"@types/jest": "^19.2.3",
"@types/fs-extra": "4.0.2",
"@types/jest": "21.1.2",
"@types/meow": "^3.6.2",
"@types/node": "^7.0.18",
"@types/node": "8.0.32",
"common-tags": "^1.4.0",
"coveralls": "^2.11.15",
"execa": "^0.6.3",
"coveralls": "3.0.0",
"execa": "^0.8.0",
"globby": "^6.1.0",
"jest": "^20.0.1",
"jest": "21.2.1",
"jest-cli": "^21.2.1",
"rimraf": "^2.6.1",
"standard-version": "^4.0.0",
"ts-jest": "^20.0.3",
"ts-jest": "21.0.1",
"tslint": "^5.2.0",
"typescript": "^2.3.2"
"typescript": "^2.5.3"
},
"peerDependencies": {
"typescript": "^2.3.2"
},
"dependencies": {
"fs-extra": "^3.0.1",
"fs-extra": "^4.0.2",
"globby": "^6.1.0",
"meow": "^3.7.0",
"ts-emitter": "^0.3.1"
Expand All @@ -59,13 +60,16 @@
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "\\.test\\.(ts|tsx)$",
"testRegex": "\\.test\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"collectCoverage": true
"collectCoverage": true,
"coveragePathIgnorePatterns": [
"dist"
]
}
}
42 changes: 42 additions & 0 deletions relative-paths.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* global test, expect, afterEach */
const execa = require('execa')
const path = require('path')

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

test(`tscodeshift should run files relative to the current working directory`, () => {
expect.assertions(1)
return execa.shell(
`cp mocha.fixture.to-copy mocha.fixture.ts && node ${CLI} -t ./dist/src/transforms/mocha.js 'mocha.fixture.ts' && cat mocha.fixture.ts`
).then((results) => {
expect(results.stdout).toBe(`beforeAll(()=> {
// something
});
beforeEach(()=> {
});
describe('Array', function() {
beforeEach(()=> {
});
it('Array', (done: jest.DoneCallback) => {
done();
});
it.skip('Array', function(done) {
done();
});
});
describe('', () => {
it('', (done: jest.DoneCallback) => {
done();
});
});`)
})
})

afterEach(() => {
return execa.shell(`rm mocha.fixture.ts`)
})
67 changes: 66 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { stripIndent } from 'common-tags';
import * as ts from 'typescript';
import { applyTransforms } from './transform';
import { applyTransforms, api } from './transform';
import * as types from './types';

test('run identifiers transform', () => {
Expand Down Expand Up @@ -123,3 +123,68 @@ it('convert var declarations to let declarations', () => {

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

test(`api`, () => {
expect(typeof api).toBe(`object`);
expect(typeof api.tscodeshift).toBe(`function`);
const removeCircularCrap = (x: any): any => {
delete x.collected[0].endOfFileToken.parent;
delete x.collected[0].statements[0].expression.parent;
delete x.collected[0].statements[0].parent;
const splits = x.collected[0].path.split(`/`);
x.collected[0].path = splits[splits.length - 1];
return x;
};
const expected = JSON.parse(JSON.stringify(removeCircularCrap(api.tscodeshift(`false`))));
// expect(JSON.parse(JSON.stringify(removeCircularCrap(expected)))).toEqual(circular)
expect(JSON.parse(JSON.stringify(api.tscodeshift(expected)))).toEqual({
collected: [
{
collected: [
{
ambientModuleNames: [],
amdDependencies: [],
bindDiagnostics: [],
end: 5,
endOfFileToken: {
end: 5,
flags: 0,
kind: 1,
pos: 5
},
fileName: `source.tsx`,
flags: 0,
identifierCount: 0,
identifiers: {},
imports: [],
isDeclarationFile: false,
kind: 265,
languageVariant: 1,
languageVersion: 5,
moduleAugmentations: [],
nodeCount: 4,
parseDiagnostics: [],
path: `source.tsx`,
pos: 0,
referencedFiles: [],
scriptKind: 4,
statements: [{
end: 5,
expression: {
end: 5,
flags: 0,
kind: 86,
pos: 0},
flags: 0,
kind: 210,
modifierFlagsCache: 536870912,
pos: 0
}],
text: `false`,
typeReferenceDirectives: []
}
]
}
]
});
});
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as fs from 'fs-extra';
import globby = require('globby');
import meow = require('meow');
import * as path from 'path';
import { applyTransforms } from './transform';
import * as types from './types';

Expand All @@ -10,10 +11,12 @@ async function main(cli: meow.Result): Promise<void> {
cli.showHelp(0);
return;
}
const cwd = process.cwd();
const makeRelative = (x: string): string => path.resolve(cwd, x);
const transformPaths = [].concat(
cli.flags['t'] || [],
cli.flags['transform'] || []
);
).map(makeRelative);
const transforms: types.Transform[] = transformPaths.map(path => require(path).default);
const paths = await globby(cli.input);
paths.forEach(async path => {
Expand Down
4 changes: 2 additions & 2 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as ts from 'typescript';
import { Collection } from './collection';
import * as types from './types';

const api = {
tscodeshift: (source: string|ts.Node) => {
export const api = {
tscodeshift: (source: string|ts.Node): Collection<any, any> => {
if (typeof source === 'string') {
return Collection.fromSource(source);
} else {
Expand Down
23 changes: 12 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
/* Basic Options */
/* Basic Options */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es2015'. */
// "lib": [], /* Specify library files to be included in the compilation: */
Expand All @@ -18,41 +18,42 @@
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
"stripInternal": true,
/* Strict Type-Checking Options */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */

/* Additional Checks */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
/* Source Map Options */

/* Source Map Options */
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */

/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
},
"exclude": [
"node_modules",
"relative-paths.test.ts",
"index.d.ts",
"dist"
]
Expand Down

0 comments on commit 7bd544b

Please sign in to comment.