Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support passing custom parser to recast #354

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ import {transform} from 'lebab';
const {code, warnings} = transform(
'var f = function(a) { return a; };', // code to transform
['let', 'arrow', 'arrow-return'] // transforms to apply
{ // options
parser: ... // custom parser compatible with recast
}
);
console.log(code); // -> "const f = a => a;"
```
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const createTransformer = require('./lib/createTransformer').default;
*
* @param {String} code The code to transform
* @param {String[]} transformNames The transforms to apply
* @param {Object} options Options to configure the transforms
* @return {Object} An object with code and warnings props
*/
exports.transform = function(code, transformNames) {
return createTransformer(transformNames).run(code);
exports.transform = function(code, transformNames, options) {
return createTransformer(transformNames, options).run(code);
};
8 changes: 5 additions & 3 deletions src/Transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import Logger from './Logger';
export default class Transformer {
/**
* @param {Function[]} transforms List of transforms to perform
* @param {Object} options Options to configure the transforms
*/
constructor(transforms = []) {
constructor(transforms = [], options = {}) {
this.transforms = transforms;
this.parser = options.parser || parser;
}

/**
* Tranforms code using all configured transforms.
* Transform code using all configured transforms.
*
* @param {String} code Input ES5 code
* @return {Object} Output ES6 code
Expand All @@ -30,7 +32,7 @@ export default class Transformer {

applyAllTransforms(code, logger) {
return this.ignoringHashBangComment(code, (js) => {
const ast = parse(js, {parser});
const ast = parse(js, {parser: this.parser});

this.transforms.forEach(transformer => {
transformer(ast.program, logger);
Expand Down
5 changes: 3 additions & 2 deletions src/createTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ const transformsMap = {
* Factory for creating a Transformer
* by just specifying the names of the transforms.
* @param {String[]} transformNames
* @param {Object} options
* @return {Transformer}
*/
export default function createTransformer(transformNames) {
export default function createTransformer(transformNames, options) {
validate(transformNames);
return new Transformer(transformNames.map(name => transformsMap[name]));
return new Transformer(transformNames.map(name => transformsMap[name]), options);
}

function validate(transformNames) {
Expand Down