diff --git a/mocha.fixture.to-copy b/mocha.fixture.to-copy new file mode 100644 index 0000000..0d6eec2 --- /dev/null +++ b/mocha.fixture.to-copy @@ -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(); + }); +}); diff --git a/package.json b/package.json index da10f5a..95e6712 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" @@ -59,13 +60,16 @@ "transform": { ".(ts|tsx)": "/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" + ] } } diff --git a/relative-paths.test.js b/relative-paths.test.js new file mode 100644 index 0000000..fd299fd --- /dev/null +++ b/relative-paths.test.js @@ -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`) +}) diff --git a/src/index.test.ts b/src/index.test.ts index 5baa796..96058df 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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', () => { @@ -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: [] + } + ] + } + ] + }); +}); diff --git a/src/index.ts b/src/index.ts index 94c67b2..f03d48f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -10,10 +11,12 @@ async function main(cli: meow.Result): Promise { 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 => { diff --git a/src/transform.ts b/src/transform.ts index 6da1fce..8b902bd 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -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 => { if (typeof source === 'string') { return Collection.fromSource(source); } else { diff --git a/tsconfig.json b/tsconfig.json index 927feb6..e99718b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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: */ @@ -18,21 +18,21 @@ // "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'. */ @@ -40,19 +40,20 @@ // "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" ]