This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is working towards a simple API. This is taken from how we end up using jstransform in React and internally. We build up these lists and expose some APIs to get them. We can tweak this (it might make sense to expose the keys of tranformVisitors).
- Loading branch information
Showing
1 changed file
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/*global exports:true*/ | ||
|
||
'use strict'; | ||
|
||
var es3ReservedWords = require('./es3/reserved-words'); | ||
var es6ArrowFunctions = require('./es6/arrow-function'); | ||
var es6CallSpread = require('./es6/call-spread'); | ||
var es6Classes = require('./es6/class'); | ||
var es6Destructuring = require('./es6/destructuring'); | ||
var es6ObjectConciseMethod = require('./es6/object-concise-method'); | ||
var es6ObjectShortNotation = require('./es6/object-short-notation'); | ||
var es6RestParameters = require('./es6/rest-param'); | ||
var es6Templates = require('./es6/template-string'); | ||
var es7SpreadProperty = require('./es7/spread-property'); | ||
var react = require('./react/react'); | ||
var reactDisplayName = require('./react/reactDisplayName'); | ||
|
||
/** | ||
* Map from transformName => orderedListOfVisitors. | ||
*/ | ||
var transformVisitors = { | ||
'es6-arrow-functions': es6ArrowFunctions.visitorList, | ||
'es6-classes': es6Classes.visitorList, | ||
'es6-destructuring': es6Destructuring.visitorList, | ||
'es6-object-concise-method': es6ObjectConciseMethod.visitorList, | ||
'es6-object-short-notation': es6ObjectShortNotation.visitorList, | ||
'es6-rest-params': es6RestParameters.visitorList, | ||
'es6-templates': es6Templates.visitorList, | ||
'es6-call-spread': es6CallSpread.visitorList, | ||
'es7-spread-property': es7SpreadProperty.visitorList, | ||
'react': react.visitorList | ||
'react-display-name': reactDisplayName.visitorList, | ||
'reserved-words': es3ReservedWords.visitorList, | ||
'flow': flow | ||
}; | ||
|
||
var transformSets = { | ||
'harmony': [ | ||
'es6-arrow-functions', | ||
'es6-object-concise-method', | ||
'es6-object-short-notation', | ||
'es6-classes', | ||
'es6-rest-params', | ||
'es6-templates', | ||
'es6-destructuring', | ||
'es6-call-spread', | ||
'es7-spread-property' | ||
], | ||
'es3': [ | ||
'reserved-words' | ||
], | ||
'react': [ | ||
'react', | ||
'react-display-name' | ||
] | ||
}; | ||
|
||
/** | ||
* Specifies the order in which each transform should run. | ||
*/ | ||
var transformRunOrder = [ | ||
'reserved-words', | ||
'es6-arrow-functions', | ||
'es6-object-concise-method', | ||
'es6-object-short-notation', | ||
'es6-classes', | ||
'es6-rest-params', | ||
'es6-templates', | ||
'es6-destructuring', | ||
'es6-call-spread', | ||
'es7-spread-property', | ||
'react', | ||
'react-display-name' | ||
]; | ||
|
||
/** | ||
* Given a list of transform names, return the ordered list of visitors to be | ||
* passed to the transform() function. | ||
* | ||
* @param {array?} excludes | ||
* @return {array} | ||
*/ | ||
function getAllVisitors(excludes) { | ||
var ret = []; | ||
for (var i = 0, il = transformRunOrder.length; i < il; i++) { | ||
if (!excludes || excludes.indexOf(transformRunOrder[i]) === -1) { | ||
ret = ret.concat(transformVisitors[transformRunOrder[i]]); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
/** | ||
* Given a list of visitor set names, return the ordered list of visitors to be | ||
* passed to jstransform. | ||
* | ||
* @param {array} | ||
* @return {array} | ||
*/ | ||
function getVisitorsBySet(sets) { | ||
var visitorsToInclude = sets.reduce(function(visitors, set) { | ||
if (!transformSets.hasOwnProperty(set)) { | ||
throw new Error('Unknown visitor set: ' + set); | ||
} | ||
transformSets[set].forEach(function(visitor) { | ||
visitors[visitor] = true; | ||
}); | ||
return visitors; | ||
}, {}); | ||
|
||
var visitorList = []; | ||
for (var i = 0; i < transformRunOrder.length; i++) { | ||
if (visitorsToInclude.hasOwnProperty(transformRunOrder[i])) { | ||
visitorList = visitorList.concat(transformVisitors[transformRunOrder[i]]); | ||
} | ||
} | ||
|
||
return visitorList; | ||
} | ||
|
||
exports.getVisitorsBySet = getVisitorsBySet; | ||
exports.getAllVisitors = getAllVisitors; | ||
exports.transformVisitors = transformVisitors; | ||
|