codemodsquad / jscodeshift-choose-parser

pick a jscodeshift parser intelligently based upon local project config

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jscodeshift-choose-parser

CircleCI Coverage Status semantic-release Commitizen friendly npm version

chooseJSCodeshiftParser(file: string): string | Parser

const chooseJSCodeshiftParser = require('jscodeshift-choose-parser')

Intelligently chooses a parser for a given file. Basically:

  • If extension is .ts or .tsx return 'ts'/'tsx' depending on extension
  • Else if @babel/core is installed, return parser that uses with @babel/core using local babel config
  • Else return undefined

The default babylon parser in jscodeshift does not use your local babel config AFAIK. This package returns a parser that does use your local babel config.

Example in a transform

const chooseJSCodeshiftParser = require('jscodeshift-choose-parser')

module.exports = function(fileInfo, api) {
  const parser = chooseJSCodeshiftParser(fileInfo.path)
  return api.jscodeshift
    .withParser(parser)(fileInfo.source)
    .findVariableDeclarators('foo')
    .renameTo('bar')
    .toSource()
}

About

pick a jscodeshift parser intelligently based upon local project config

License:MIT License


Languages

Language:JavaScript 100.0%