Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jscodeshift

Package Overview
Dependencies
Maintainers
5
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jscodeshift - npm Package Compare versions

Comparing version 0.13.1 to 0.14.0

.github/workflows/test.yml

22

bin/jscodeshift.js

@@ -20,2 +20,3 @@ #!/usr/bin/env node

transform: {
display_index: 15,
abbr: 't',

@@ -28,2 +29,3 @@ default: './transform.js',

cpus: {
display_index: 1,
abbr: 'c',

@@ -36,2 +38,3 @@ help: 'start at most N child processes to process source files',

verbose: {
display_index: 16,
abbr: 'v',

@@ -45,2 +48,3 @@ choices: [0, 1, 2],

dry: {
display_index: 2,
abbr: 'd',

@@ -52,2 +56,3 @@ flag: true,

print: {
display_index: 11,
abbr: 'p',

@@ -59,2 +64,3 @@ flag: true,

babel: {
display_index: 0,
flag: true,

@@ -65,2 +71,3 @@ default: true,

extensions: {
display_index: 3,
default: 'js',

@@ -71,2 +78,3 @@ help: 'transform files with these file extensions (comma separated list)',

ignorePattern: {
display_index: 7,
full: 'ignore-pattern',

@@ -78,2 +86,3 @@ list: true,

ignoreConfig: {
display_index: 6,
full: 'ignore-config',

@@ -84,3 +93,10 @@ list: true,

},
gitignore: {
display_index: 8,
flag: true,
default: false,
help: 'adds entries the current directory\'s .gitignore file',
},
runInBand: {
display_index: 12,
flag: true,

@@ -92,2 +108,3 @@ default: false,

silent: {
display_index: 13,
abbr: 's',

@@ -99,2 +116,3 @@ flag: true,

parser: {
display_index: 9,
choices: ['babel', 'babylon', 'flow', 'ts', 'tsx'],

@@ -105,2 +123,3 @@ default: 'babel',

parserConfig: {
display_index: 10,
full: 'parser-config',

@@ -112,2 +131,3 @@ help: 'path to a JSON file containing a custom parser configuration for flow or babylon',

failOnError: {
display_index: 4,
flag: true,

@@ -119,2 +139,3 @@ help: 'Return a non-zero code when there are errors',

version: {
display_index: 17,
help: 'print version and exit',

@@ -133,2 +154,3 @@ callback: function() {

stdin: {
display_index: 14,
help: 'read file/directory list from stdin',

@@ -135,0 +157,0 @@ flag: true,

@@ -7,2 +7,17 @@ # Changelog

## [0.14.0] 2022-10-04
### Added
- Added a `defineSnapshotTestFromFixture` test util (#471, @shriuken)
- Added `renameTo` filters for Babel 6+ node types (#412 and #504, @elonvolo and @henryqdineen)
- Added `childNodesOfType` to JSX traversal methods (#415, @j13huang)
### Changed
- Bumped dependency versions
- Allow arguments in `--help` to be listed in an order other than alphabetically, so they can instead be grouped thematically (#507, @elonvolo)
- Allow the `j` shortcut in test utils (#515, @no23reason)
## [0.13.1] 2022-01-10

@@ -9,0 +24,0 @@

0

CODE_OF_CONDUCT.md
# Code of Conduct
Facebook has adopted a Code of Conduct that we expect project participants to adhere to. Please [read the full text](https://code.facebook.com/codeofconduct) so that you can understand what actions will and will not be tolerated.

@@ -0,0 +0,0 @@ # Contributing to jscodeshift

3

dist/argsParser.js

@@ -51,3 +51,3 @@ /**

.map(k => options[k])
.sort((a,b) => a.full.localeCompare(b.full));
.sort((a,b) => a.display_index - b.display_index);

@@ -93,2 +93,3 @@ const text = `

options.help = {
display_index: 5,
abbr: 'h',

@@ -95,0 +96,0 @@ help: 'print this help and exit',

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -174,2 +174,21 @@

},
/**
* Returns all children that are of jsxElementType.
*
* @return {Collection<jsxElementType>}
*/
childNodesOfType: function(jsxChildElementType) {
const paths = [];
this.forEach(function(path) {
const children = path.get('children');
const l = children.value.length;
for (let i = 0; i < l; i++) {
if (jsxChildElementType.check(children.value[i])) {
paths.push(children.get(i));
}
}
});
return Collection.fromPaths(paths, this, jsxChildElementType);
},
};

@@ -176,0 +195,0 @@

@@ -0,0 +0,0 @@

@@ -107,2 +107,20 @@

if (
types.ObjectProperty.check(parent) &&
parent.key === path.node &&
!parent.computed
) {
// { oldName: 3 }
return false;
}
if (
types.ObjectMethod.check(parent) &&
parent.key === path.node &&
!parent.computed
) {
// { oldName() {} }
return false;
}
if (
types.MethodDefinition.check(parent) &&

@@ -117,2 +135,11 @@ parent.key === path.node &&

if (
types.ClassMethod.check(parent) &&
parent.key === path.node &&
!parent.computed
) {
// class A { oldName() {} }
return false;
}
if (
types.ClassProperty.check(parent) &&

@@ -119,0 +146,0 @@ parent.key === path.node &&

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@ 'use strict';

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -30,2 +30,3 @@ /**

jscodeshift,
j: jscodeshift,
stats: () => {},

@@ -138,1 +139,13 @@ },

exports.defineSnapshotTest = defineSnapshotTest;
/**
* Handles file-loading boilerplates, using same defaults as defineTest
*/
function defineSnapshotTestFromFixture(dirName, module, options, testFilePrefix, testName, testOptions = {}) {
const extension = extensionForParser(testOptions.parser)
const fixtureDir = path.join(dirName, '..', '__testfixtures__');
const inputPath = path.join(fixtureDir, testFilePrefix + `.input.${extension}`);
const source = fs.readFileSync(inputPath, 'utf8');
defineSnapshotTest(module, options, source, testName)
}
exports.defineSnapshotTestFromFixture = defineSnapshotTestFromFixture;

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

{
"name": "jscodeshift",
"version": "0.13.1",
"version": "0.14.0",
"description": "A toolkit for JavaScript codemods",

@@ -12,3 +12,2 @@ "repository": {

"scripts": {
"prepare": "cp -R src/ dist/",
"test": "jest --bail",

@@ -41,6 +40,6 @@ "docs": "rm -rf docs && jsdoc -d docs -R README.md src/collections/* src/core.js src/Collection.js"

"graceful-fs": "^4.2.4",
"micromatch": "^3.1.10",
"micromatch": "^4.0.4",
"neo-async": "^2.5.0",
"node-dir": "^0.1.17",
"recast": "^0.20.4",
"recast": "^0.21.0",
"temp": "^0.8.4",

@@ -55,4 +54,4 @@ "write-file-atomic": "^2.3.0"

"eslint": "^5.9.0",
"jest": "^21",
"jsdoc": "^3.4.0",
"jest": "^26",
"jsdoc": "3.6.7",
"mkdirp": "^0.5.1"

@@ -59,0 +58,0 @@ },

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -1,2 +0,2 @@

# jscodeshift [![Build Status](https://travis-ci.org/facebook/jscodeshift.svg?branch=master)](https://travis-ci.org/facebook/jscodeshift)
# jscodeshift [![Support Ukraine](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB)](https://opensource.fb.com/support-ukraine) [![Build Status](https://travis-ci.org/facebook/jscodeshift.svg?branch=master)](https://travis-ci.org/facebook/jscodeshift)

@@ -23,2 +23,6 @@ jscodeshift is a toolkit for running codemods over multiple JavaScript or

## VSCode Debugger
[Configure VSCode to debug codemods](#vscode-debugging)
## Usage (CLI)

@@ -77,2 +81,30 @@

## Usage (JS)
```js
const {run: jscodeshift} = require('jscodeshift/src/Runner')
const transformPath = 'transform.js'
const paths = ['foo.js', 'bar']
const options = {
dry: true,
print: true,
verbose: 1,
// ...
}
const res = await jscodeshift(transformPath, paths, options)
console.log(res)
/*
{
stats: {},
timeElapsed: '0.001',
error: 0,
ok: 0,
nochange: 0,
skip: 0
}
*/
```
## Transform module

@@ -123,3 +155,3 @@

*/
module.exports = function(fileInfo, api) {
module.exports = function(fileInfo, api, options) {
return api.jscodeshift(fileInfo.source)

@@ -176,14 +208,38 @@ .findVariableDeclarators('foo')

The transform can let jscodeshift know with which parser to parse the source
files (and features like templates).
The transform file can let jscodeshift know with which parser to parse the source files (and features like templates).
To do that, the transform module can export `parser`, which can either be one
of the strings `"babel"`, `"babylon"`, `"flow"`, `"ts"`, or `"tsx"`,
or it can be a parser object that is compatible with recast.
or it can be a parser object that is compatible with recast and follows the estree spec.
For example:
__Example: specifying parser type string in the transform file__
```js
module.exports.parser = 'flow'; // use the flow parser
// or
module.exports = function transformer(file, api, options) {
const j = api.jscodeshift;
const rootSource = j(file.source);
// whatever other code...
return rootSource.toSource();
}
// use the flow parser
module.exports.parser = 'flow';
```
__Example: specifying a custom parser object in the transform file__
```js
module.exports = function transformer(file, api, options) {
const j = api.jscodeshift;
const rootSource = j(file.source);
// whatever other code...
return rootSource.toSource();
}
module.exports.parser = {

@@ -327,7 +383,7 @@ parse: function(source) {

jscodeshift.registerMethods({
logNames: function() {
return this.forEach(function(path) {
console.log(path.node.name);
});
}
logNames: function() {
return this.forEach(function(path) {
console.log(path.node.name);
});
}
}, jscodeshift.Identifier);

@@ -337,5 +393,5 @@

jscodeshift.registerMethods({
findIdentifiers: function() {
return this.find(jscodeshift.Identifier);
}
findIdentifiers: function() {
return this.find(jscodeshift.Identifier);
}
});

@@ -433,2 +489,13 @@

#### `defineSnapshotTestFromFixture`
Similar to `defineSnapshotTest` but will load the file using same file-directory defaults as `defineTest`
```js
const defineSnapshotTestDefault = require('jscodeshift/dist/testUtils').defineSnapshotTestDefault;
const transform = require('../myTransform');
const transformOptions = {};
defineSnapshotTestFromFixture(__dirname, transform, transformOptions, 'FirstFixture', 'test name (optional)');
```
#### `applyTransform`

@@ -453,3 +520,3 @@

export const parser = 'flow'
export default function MyTransform(fileInfo, api) {
export default function MyTransform(fileInfo, api, options) {
// ...

@@ -473,3 +540,91 @@ }

- [js-transforms](https://github.com/jhgg/js-transforms) - Some documented codemod experiments to help you learn.
- [fix-js](https://github.com/anshckr/fix-js) - Codemods to fix some ESLint issues
### Local Documentation Server
To update docs in `/docs`, use `npm run docs`.
To view these docs locally, use `npx http-server ./docs`
## VSCode Debugging
It's recommended that you set up your codemod project to all debugging via the VSCode IDE. When you open your project in VSCode, add the following configuration to your launch.json debugging configuration.
```
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Debug Transform",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceRoot}/node_modules/.bin/jscodeshift",
"stopOnEntry": false,
"args": ["--dry", "--print", "-t", "${input:transformFile}", "--parser", "${input:parser}", "--run-in-band", "${file}"],
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"console": "internalConsole",
"sourceMaps": true,
"outFiles": []
},
{
"name": "Debug All JSCodeshift Jest Tests",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/jest/bin/jest.js",
"--runInBand",
"--testPathPattern=${fileBasenameNoExtension}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
],
"inputs": [
{
"type": "pickString",
"id": "parser",
"description": "jscodeshift parser",
"options": [
"babel",
"babylon",
"flow",
"ts",
"tsx",
],
"default": "babel"
},
{
"type": "promptString",
"id": "transformFile",
"description": "jscodeshift transform file",
"default": "transform.js"
}
]
}
```
Once this has been added to the configuration
1. Install jscodeshift as a package if you haven't done so already by running the command **npm install --save jscodeshift**. The debug configuration will not work otherwise.
2. Once the jscodeshift local package has been installed, go to the VSCode file tree and select the file on which you want to run the transform. For example, if you wanted to run codemod transforms of foo.js file, you would click on the entry for foo.js file in your project tree.
3. Select "Debug Transform" from the debugging menu's options menu.
4. Click the **"Start Debugging"** button on the VSCode debugger.
5. You will be then prompted for the name of jscodeshift transform file. Enter in the name of the transform file to use. If no name is given it will default to **transform.js**
6. Select the parser to use from the presented selection list of parsers. The transform will otherwise default to using the **babel** parser.
7. The transform will then be run, stopping at any breakpoints that have been set.
8. If there are no errors and the transform is complete, then the results of the transform will be printed in the VSCode debugging console. The file with the contents that have been transformed will not be changed, as the debug configuration makes use the jscodeshift **--dry** option.
### Recipes

@@ -476,0 +631,0 @@

@@ -51,3 +51,3 @@ /**

.map(k => options[k])
.sort((a,b) => a.full.localeCompare(b.full));
.sort((a,b) => a.display_index - b.display_index);

@@ -93,2 +93,3 @@ const text = `

options.help = {
display_index: 5,
abbr: 'h',

@@ -95,0 +96,0 @@ help: 'print this help and exit',

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -174,2 +174,21 @@

},
/**
* Returns all children that are of jsxElementType.
*
* @return {Collection<jsxElementType>}
*/
childNodesOfType: function(jsxChildElementType) {
const paths = [];
this.forEach(function(path) {
const children = path.get('children');
const l = children.value.length;
for (let i = 0; i < l; i++) {
if (jsxChildElementType.check(children.value[i])) {
paths.push(children.get(i));
}
}
});
return Collection.fromPaths(paths, this, jsxChildElementType);
},
};

@@ -176,0 +195,0 @@

@@ -0,0 +0,0 @@

@@ -107,2 +107,20 @@

if (
types.ObjectProperty.check(parent) &&
parent.key === path.node &&
!parent.computed
) {
// { oldName: 3 }
return false;
}
if (
types.ObjectMethod.check(parent) &&
parent.key === path.node &&
!parent.computed
) {
// { oldName() {} }
return false;
}
if (
types.MethodDefinition.check(parent) &&

@@ -117,2 +135,11 @@ parent.key === path.node &&

if (
types.ClassMethod.check(parent) &&
parent.key === path.node &&
!parent.computed
) {
// class A { oldName() {} }
return false;
}
if (
types.ClassProperty.check(parent) &&

@@ -119,0 +146,0 @@ parent.key === path.node &&

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@ 'use strict';

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -30,2 +30,3 @@ /**

jscodeshift,
j: jscodeshift,
stats: () => {},

@@ -138,1 +139,13 @@ },

exports.defineSnapshotTest = defineSnapshotTest;
/**
* Handles file-loading boilerplates, using same defaults as defineTest
*/
function defineSnapshotTestFromFixture(dirName, module, options, testFilePrefix, testName, testOptions = {}) {
const extension = extensionForParser(testOptions.parser)
const fixtureDir = path.join(dirName, '..', '__testfixtures__');
const inputPath = path.join(fixtureDir, testFilePrefix + `.input.${extension}`);
const source = fs.readFileSync(inputPath, 'utf8');
defineSnapshotTest(module, options, source, testName)
}
exports.defineSnapshotTestFromFixture = defineSnapshotTestFromFixture;

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@ /**

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

@@ -0,0 +0,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc