From bd7ae78fd4a983067b5d78311c4ad39c3047270d Mon Sep 17 00:00:00 2001 From: Andy Edwards Date: Sat, 1 Feb 2020 14:55:47 -0600 Subject: [PATCH 1/3] chore: move repo --- README.md | 4 ++-- package.json | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7ab241b..6e9766b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # jscodeshift-transforms-skeleton -[![CircleCI](https://circleci.com/gh/jedwards1211/jscodeshift-transforms-skeleton.svg?style=svg)](https://circleci.com/gh/jedwards1211/jscodeshift-transforms-skeleton) -[![Coverage Status](https://codecov.io/gh/jedwards1211/jscodeshift-transforms-skeleton/branch/master/graph/badge.svg)](https://codecov.io/gh/jedwards1211/jscodeshift-transforms-skeleton) +[![CircleCI](https://circleci.com/gh/codemodsquad/jscodeshift-transforms-skeleton.svg?style=svg)](https://circleci.com/gh/codemodsquad/jscodeshift-transforms-skeleton) +[![Coverage Status](https://codecov.io/gh/codemodsquad/jscodeshift-transforms-skeleton/branch/master/graph/badge.svg)](https://codecov.io/gh/codemodsquad/jscodeshift-transforms-skeleton) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) [![npm version](https://badge.fury.io/js/jscodeshift-transforms-skeleton.svg)](https://badge.fury.io/js/jscodeshift-transforms-skeleton) diff --git a/package.json b/package.json index 16b3565..cf66fbb 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/jedwards1211/jscodeshift-transforms-skeleton.git" + "url": "https://github.com/codemodsquad/jscodeshift-transforms-skeleton.git" }, "keywords": [ "jscodeshift", @@ -77,9 +77,9 @@ "author": "Andy Edwards", "license": "MIT", "bugs": { - "url": "https://github.com/jedwards1211/jscodeshift-transforms-skeleton/issues" + "url": "https://github.com/codemodsquad/jscodeshift-transforms-skeleton/issues" }, - "homepage": "https://github.com/jedwards1211/jscodeshift-transforms-skeleton#readme", + "homepage": "https://github.com/codemodsquad/jscodeshift-transforms-skeleton#readme", "devDependencies": { "@babel/cli": "^7.1.5", "@babel/core": "^7.1.6", From a7fb750392f4aff0b02e9565b40905650836dda3 Mon Sep 17 00:00:00 2001 From: Andy Edwards Date: Fri, 7 Feb 2020 16:23:07 -0600 Subject: [PATCH 2/3] test: refactor how fixtures are structured --- test/example/example.spec.ts | 13 ------ test/example/{fixtures => }/fixture.ts | 0 test/index.spec.ts | 18 ++++++++ test/testFixtures.ts | 63 +++++++++++++++----------- 4 files changed, 54 insertions(+), 40 deletions(-) delete mode 100644 test/example/example.spec.ts rename test/example/{fixtures => }/fixture.ts (100%) create mode 100644 test/index.spec.ts diff --git a/test/example/example.spec.ts b/test/example/example.spec.ts deleted file mode 100644 index 8e37228..0000000 --- a/test/example/example.spec.ts +++ /dev/null @@ -1,13 +0,0 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ - -import { describe } from 'mocha' -import * as path from 'path' -import testFixtures from '../testFixtures' -const example = require('../../src/example') - -describe(`example`, function() { - testFixtures({ - glob: path.join(__dirname, 'fixtures', '*.ts'), - transform: example, - }) -}) diff --git a/test/example/fixtures/fixture.ts b/test/example/fixture.ts similarity index 100% rename from test/example/fixtures/fixture.ts rename to test/example/fixture.ts diff --git a/test/index.spec.ts b/test/index.spec.ts new file mode 100644 index 0000000..ce1e60d --- /dev/null +++ b/test/index.spec.ts @@ -0,0 +1,18 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ + +import { describe } from 'mocha' +import * as fs from 'fs' +import * as path from 'path' +import testFixtures from './testFixtures' + +const dirs = fs.readdirSync(__dirname) +for (const dir of dirs) { + if (fs.statSync(path.join(__dirname, dir)).isDirectory()) { + describe(dir, function() { + testFixtures({ + glob: path.join(__dirname, dir, '*.ts'), + transform: require(`../src/${dir}`), + }) + }) + } +} diff --git a/test/testFixtures.ts b/test/testFixtures.ts index de5699d..cf24366 100644 --- a/test/testFixtures.ts +++ b/test/testFixtures.ts @@ -52,20 +52,23 @@ export default function textFixtures({ it(path.basename(fixturePath).replace(/\.js$/, ''), function() { let source = input const position = source.indexOf('// position') + let selectionStart + let selectionEnd if (position >= 0) { + selectionStart = selectionEnd = position source = source.replace(/^\s*\/\/ position[^\r\n]*(\r\n?|\n)/gm, '') - } - let selectionStart = source.indexOf('/* selectionStart */') - let selectionEnd - if (selectionStart >= 0) { - source = source.replace('/* selectionStart */', '') - selectionEnd = source.indexOf('/* selectionEnd */') - if (selectionEnd < 0) { - throw new Error( - '/* selectionEnd */ must be given if /* selectionStart */ is' - ) + } else { + selectionStart = source.indexOf('/* selectionStart */') + if (selectionStart >= 0) { + source = source.replace('/* selectionStart */', '') + selectionEnd = source.indexOf('/* selectionEnd */') + if (selectionEnd < 0) { + throw new Error( + '/* selectionEnd */ must be given if /* selectionStart */ is' + ) + } + source = source.replace('/* selectionEnd */', '') } - source = source.replace('/* selectionEnd */', '') } if (selectionStart < 0) selectionStart = position if (selectionEnd < 0) selectionEnd = position @@ -78,23 +81,29 @@ export default function textFixtures({ const report = [] const parser = fixture.parser || defaultParser const j = parser ? jscodeshift.withParser(parser) : jscodeshift - const result = transform( - { path: file, source }, - { - j, - jscodeshift: j, - stats: (name: string, quantity = 1): void => { - const total = stats[name] - stats[name] = total != null ? total + quantity : quantity + const doTransform = (): string | null | void | undefined => + transform( + { path: file, source }, + { + j, + jscodeshift: j, + stats: (name: string, quantity = 1): void => { + const total = stats[name] + stats[name] = total != null ? total + quantity : quantity + }, + report: (msg: string) => report.push(msg), }, - report: (msg: string) => report.push(msg), - }, - options - ) - if (!result) expect(result).to.equal(fixture.expected) - else expect(normalize(result)).to.equal(normalize(expected)) - if (fixture.stats) expect(stats).to.deep.equal(fixture.stats) - if (fixture.report) expect(report).to.deep.equal(fixture.report) + options + ) + if (fixture.expectedError) { + expect(doTransform).to.throw(fixture.expectedError) + } else { + const result = doTransform() + if (!result) expect(result).to.equal(fixture.expected) + else expect(normalize(result)).to.equal(normalize(expected)) + if (fixture.stats) expect(stats).to.deep.equal(fixture.stats) + if (fixture.report) expect(report).to.deep.equal(fixture.report) + } }) } } From 4c9a9653c706bbe56157837829dd3d00ffd6fd5e Mon Sep 17 00:00:00 2001 From: Andy Edwards Date: Sat, 22 Feb 2020 22:46:14 -0600 Subject: [PATCH 3/3] build: add example gen script and fixture snippet --- .npmignore | 1 + .vscode/snippets.code-snippets | 92 ++++++++++++++++++++-------------- package.json | 5 +- scripts/example.babel.js | 2 + scripts/example.ts | 32 ++++++++++++ yarn.lock | 8 +-- 6 files changed, 95 insertions(+), 45 deletions(-) create mode 100644 scripts/example.babel.js create mode 100644 scripts/example.ts diff --git a/.npmignore b/.npmignore index 104bc3e..1abda49 100644 --- a/.npmignore +++ b/.npmignore @@ -6,6 +6,7 @@ !yarn.lock /src /test +/scripts /coverage /flow-typed __tests__ diff --git a/.vscode/snippets.code-snippets b/.vscode/snippets.code-snippets index e0cd79a..42c49b8 100644 --- a/.vscode/snippets.code-snippets +++ b/.vscode/snippets.code-snippets @@ -1,40 +1,54 @@ { - "transform": { - "prefix": "transform", - "description": "JSCodeshift transform", - "body": [ - "import { ASTPath, Node, FileInfo, API, Options } from 'jscodeshift'", - "import pathsInRange from 'jscodeshift-paths-in-range'", - "", - "type Filter = (", - " path: ASTPath,", - " index: number,", - " paths: Array>", - ") => boolean", - "", - "module.exports = function ${TM_FILENAME_BASE}(", - " fileInfo: FileInfo,", - " api: API,", - " options: Options", - "): string | null | undefined | void {", - " const j = api.jscodeshift", - "", - " const root = j(fileInfo.source)", - "", - " let filter: Filter", - " if (options.selectionStart) {", - " const selectionStart = parseInt(options.selectionStart)", - " const selectionEnd = options.selectionEnd", - " ? parseInt(options.selectionEnd)", - " : selectionStart", - " filter = pathsInRange(selectionStart, selectionEnd)", - " } else {", - " filter = (): boolean => true", - " }", - "", - " return root.toSource()", - "}", - "" - ] - } -} \ No newline at end of file + "transform": { + "prefix": "transform", + "description": "JSCodeshift transform", + "body": [ + "import { ASTPath, Node, FileInfo, API, Options } from 'jscodeshift'", + "import pathsInRange from 'jscodeshift-paths-in-range'", + "", + "type Filter = (", + " path: ASTPath,", + " index: number,", + " paths: Array>", + ") => boolean", + "", + "module.exports = function ${TM_FILENAME_BASE}(", + " fileInfo: FileInfo,", + " api: API,", + " options: Options", + "): string | null | undefined | void {", + " const j = api.jscodeshift", + "", + " const root = j(fileInfo.source)", + "", + " let filter: Filter", + " if (options.selectionStart) {", + " const selectionStart = parseInt(options.selectionStart)", + " const selectionEnd = options.selectionEnd", + " ? parseInt(options.selectionEnd)", + " : selectionStart", + " filter = pathsInRange(selectionStart, selectionEnd)", + " } else {", + " filter = (): boolean => true", + " }", + "", + " return root.toSource()", + "}", + "" + ] + }, + "fixture": { + "prefix": "fixture", + "description": "JSCodeshift transform test fixture", + "body": [ + "export const input = `", + "`", + "", + "export const options = {", + "}", + "", + "export const expected = `", + "`" + ] + } +} diff --git a/package.json b/package.json index cf66fbb..8ced318 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "codecov": "nyc report --reporter=text-lcov > coverage.lcov; codecov", "prepublishOnly": "npm run clean && npm run prettier:check && npm run lint && npm test && npm run build", "open:coverage": "open coverage/lcov-report/index.html", - "semantic-release": "semantic-release" + "semantic-release": "semantic-release", + "example": "node scripts/example.babel.js" }, "config": { "lint": "--cache --ext .js,.ts src test", @@ -101,7 +102,7 @@ "@types/chai": "^4.2.0", "@types/jscodeshift": "^0.6.3", "@types/mocha": "^5.2.7", - "@types/node": "^12.12.6", + "@types/node": "^13.7.4", "@types/prettier": "^1.19.0", "babel-eslint": "^10.0.1", "babel-plugin-istanbul": "^5.1.0", diff --git a/scripts/example.babel.js b/scripts/example.babel.js new file mode 100644 index 0000000..c2c5c1d --- /dev/null +++ b/scripts/example.babel.js @@ -0,0 +1,2 @@ +require('@babel/register')({ extensions: ['.js', '.jsx', '.ts', '.tsx'] }) +require('./example.ts') diff --git a/scripts/example.ts b/scripts/example.ts new file mode 100644 index 0000000..0a905d8 --- /dev/null +++ b/scripts/example.ts @@ -0,0 +1,32 @@ +/* eslint disable */ + +const { name: pkgName } = require('../package.json') +const [transformName, fixture] = process.argv.slice(2) +if (!transformName || !fixture) { + console.error('Usage: yarn example ') + process.exit(1) +} + +const { input, expected, file } = require(require.resolve( + `../test/${transformName}/${fixture}.ts` +)) + +const ext = file ? /^\.([^.]+)$/.exec(file)?.[1] || 'ts' : 'ts' + +console.log(`### Before + +\`\`\`${ext} +${input} +\`\`\` + +### Command +\`\`\` +jscodeshift -t path/to/${pkgName}/${transformName}.js +\`\`\` + +### After + +\`\`\`${ext} +${expected} +\`\`\` +`) diff --git a/yarn.lock b/yarn.lock index 751db46..8e92d1f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1240,10 +1240,10 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-13.1.7.tgz#db51d28b8dfacfe4fb2d0da88f5eb0a2eca00675" integrity sha512-HU0q9GXazqiKwviVxg9SI/+t/nAsGkvLDkIdxz+ObejG2nX6Si00TeLqHMoS+a/1tjH7a8YpKVQwtgHuMQsldg== -"@types/node@^12.12.6": - version "12.12.24" - resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.24.tgz#d4606afd8cf6c609036b854360367d1b2c78931f" - integrity sha512-1Ciqv9pqwVtW6FsIUKSZNB82E5Cu1I2bBTj1xuIHXLe/1zYLl3956Nbhg2MzSYHVfl9/rmanjbQIb7LibfCnug== +"@types/node@^13.7.4": + version "13.7.4" + resolved "https://registry.yarnpkg.com/@types/node/-/node-13.7.4.tgz#76c3cb3a12909510f52e5dc04a6298cdf9504ffd" + integrity sha512-oVeL12C6gQS/GAExndigSaLxTrKpQPxewx9bOcwfvJiJge4rr7wNaph4J+ns5hrmIV2as5qxqN8YKthn9qh0jw== "@types/normalize-package-data@^2.4.0": version "2.4.0"