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 26ae59f..42c49b8 100644 --- a/.vscode/snippets.code-snippets +++ b/.vscode/snippets.code-snippets @@ -1,61 +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()", - "}", - "" - ] - }, - "fixture": { - "prefix": "fixture", - "description": "Test fixture", - "body": [ - "export const file = '${1:test.js}'", - "export const parser = '${2:babylon}'", - "", - "export const options = {", - " $3", - "}", - "", - "export const input = `", - "$4", - "`", - "", - "export const expected = `", - "$5", - "`", - "" - ] - } -} \ 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 836edcd..5a0dd9a 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", @@ -68,7 +69,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/codemodsquad/react-codemorphs.git" + "url": "https://github.com/codemodsquad/jscodeshift-transforms-skeleton.git" }, "keywords": [ "react", @@ -78,9 +79,9 @@ "author": "Andy Edwards", "license": "MIT", "bugs": { - "url": "https://github.com/codemodsquad/react-codemorphs/issues" + "url": "https://github.com/codemodsquad/jscodeshift-transforms-skeleton/issues" }, - "homepage": "https://github.com/codemodsquad/react-codemorphs#readme", + "homepage": "https://github.com/codemodsquad/jscodeshift-transforms-skeleton#readme", "devDependencies": { "@babel/cli": "^7.1.5", "@babel/core": "^7.1.6", @@ -102,7 +103,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 c2177c2..fbb50fe 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"