https://github.com/codemodsquad/jscodeshift-paths-in-range
A predicate for jscodeshift Collection.filter that only includes paths in the given source code range.
https://github.com/codemodsquad/jscodeshift-paths-in-range
jscodeshift refactoring
Last synced: 3 months ago
JSON representation
A predicate for jscodeshift Collection.filter that only includes paths in the given source code range.
- Host: GitHub
- URL: https://github.com/codemodsquad/jscodeshift-paths-in-range
- Owner: codemodsquad
- License: mit
- Created: 2020-01-16T05:35:54.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T05:15:29.000Z (almost 3 years ago)
- Last Synced: 2024-04-14T09:40:42.089Z (over 1 year ago)
- Topics: jscodeshift, refactoring
- Language: TypeScript
- Homepage:
- Size: 3.35 MB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# jscodeshift-paths-in-range
[](https://circleci.com/gh/codemodsquad/jscodeshift-paths-in-range)
[](https://codecov.io/gh/codemodsquad/jscodeshift-paths-in-range)
[](https://github.com/semantic-release/semantic-release)
[](http://commitizen.github.io/cz-cli/)
[](https://badge.fury.io/js/jscodeshift-paths-in-range)A predicate for `jscodeshift` `Collection.filter` that only includes paths in the given source code range.
This is for building IDE codemod extensions that operate on the selected code/cursor position in an editor.More specifically:
- If any paths are wholly contained within the range, use them (but not paths inside them)
- Otherwise, pick the topmost node that fully contains the range### `pathsInRange(start: number, end: number = start)`
```js
import pathsInRange from 'jscodeshift-paths-in-range'
```Returns a predicate for `Collection.filter`.
#### Arguments
- `start` - the index of the start of the range within the source code
- `end` - the index of the end of the range within the source code (defaults to `start`)#### Example
```js
import jscodeshift from 'jscodeshift'
import pathsInRange from 'jscodeshift-paths-in-range'
const j = jscodeshift.withParser('babylon')// editor is an example interface to a text editor in an IDE.
j(editor.getText())
.find(j.ArrowFunctionExpression)
.filter(
pathsInRange(editor.getSelectedRange().start, editor.getSelectedRange().end)
)
.forEach(path => console.log(path.node))
```### `pathsIntersectingRange(start: number, end: number = start)`
The only difference between this and `pathsInRange` is it will include nodes that
extend beyond `start` or `end`.```js
import pathsIntersectingRange from 'jscodeshift-paths-in-range'
```Returns a predicate for `Collection.filter`.
#### Arguments
- `start` - the index of the start of the range within the source code
- `end` - the index of the end of the range within the source code (defaults to `start`)