1.1.0 • Published 6 years ago
jscodeshift-paths-in-range v1.1.0
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)
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 codeend- the index of the end of the range within the source code (defaults tostart)
Example
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.
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 codeend- the index of the end of the range within the source code (defaults tostart)