-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/jedwards1211/jscodeshift-…
- Loading branch information
Showing
6 changed files
with
98 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
!yarn.lock | ||
/src | ||
/test | ||
/scripts | ||
/coverage | ||
/flow-typed | ||
__tests__ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = `", | ||
"`" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
\`\`\` | ||
`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters