jscodeshift-paths-in-range
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

jscodeshift-paths-in-range

CircleCI Coverage Status semantic-release Commitizen friendly npm version

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 code
  • end - the index of the end of the range within the source code (defaults to start)

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 code
  • end - the index of the end of the range within the source code (defaults to start)

Readme

Keywords

Package Sidebar

Install

npm i jscodeshift-paths-in-range

Weekly Downloads

11

Version

1.1.0

License

MIT

Unpacked Size

424 kB

Total Files

9

Last publish

Collaborators

  • jedwards1211