Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Apr 22, 2020
2 parents 640282d + 4c9a965 commit d39c9bc
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 69 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
!yarn.lock
/src
/test
/scripts
/coverage
/flow-typed
__tests__
Expand Down
113 changes: 53 additions & 60 deletions .vscode/snippets.code-snippets
Original file line number Diff line number Diff line change
@@ -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<Node>,",
" index: number,",
" paths: Array<ASTPath<Node>>",
") => 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",
"`",
""
]
}
}
"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<Node>,",
" index: number,",
" paths: Array<ASTPath<Node>>",
") => 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 = `",
"`"
]
}
}
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions scripts/example.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('@babel/register')({ extensions: ['.js', '.jsx', '.ts', '.tsx'] })
require('./example.ts')
32 changes: 32 additions & 0 deletions scripts/example.ts
Original file line number Diff line number Diff line change
@@ -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 <transform> <fixture>')
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 <file>
\`\`\`
### After
\`\`\`${ext}
${expected}
\`\`\`
`)
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d39c9bc

Please sign in to comment.