@samitouri / QOS-React-2 / commits / 808730df59

Add support for a --strip-types flag to bin/jsx

jeffmo committed Sep 5, 2014 at 16:27 UTC 808730df59b8977f1b5e2b878fc44615a4fa9e56
3 files changed +66 -10
bin/jsx
+5 -1
@@ -11,6 +11,9 @@ require('commoner').version(
11 }).option(
12 '--harmony',
13 'Turns on JS transformations such as ES6 Classes etc.'
14 +).option(
15 + '--strip-types',
16 + 'Strips out type annotations.'
17 ).option(
18 '--source-map-inline',
19 'Embed inline sourcemap in transformed source'
@@ -18,7 +21,8 @@ require('commoner').version(
21 // This is where JSX, ES6, etc. desugaring happens.
22 var options = {
23 harmony: this.options.harmony,
21 - sourceMap: this.options.sourceMapInline
24 + sourceMap: this.options.sourceMapInline,
25 + stripTypes: this.options.stripTypes
26 };
27 return transform(source, options);
28 });
main.js
+9 -8
@@ -31,16 +31,17 @@ module.exports = {
31
32 function innerTransform(input, options) {
33 options = options || {};
34 - var visitorList = getVisitors(options.harmony);
35 - return transform(visitorList, input, options);
36 -}
34
38 -function getVisitors(harmony) {
39 - if (harmony) {
40 - return visitors.getAllVisitors();
41 - } else {
42 - return visitors.transformVisitors.react;
35 + var visitorSets = ['react'];
36 + if (options.harmony) {
37 + visitorSets.push('harmony');
38 + }
39 + if (options.stripTypes) {
40 + visitorSets.push('type-annotations');
41 }
42 +
43 + var visitorList = visitors.getVisitorsBySet(visitorSets);
44 + return transform(visitorList, input, options);
45 }
46
47 function inlineSourceMap(sourceMap, sourceCode, sourceFilename) {
vendor/fbtransform/visitors.js
+52 -1
@@ -9,6 +9,7 @@ var es6Templates = require('jstransform/visitors/es6-template-visitors');
9 var es7SpreadProperty = require('jstransform/visitors/es7-spread-property-visitors');
10 var react = require('./transforms/react');
11 var reactDisplayName = require('./transforms/reactDisplayName');
12 +var typesSyntax = require('jstransform/visitors/type-syntax');
13
14 /**
15 * Map from transformName => orderedListOfVisitors.
@@ -22,13 +23,34 @@ var transformVisitors = {
23 'es6-rest-params': es6RestParameters.visitorList,
24 'es6-templates': es6Templates.visitorList,
25 'es7-spread-property': es7SpreadProperty.visitorList,
25 - 'react': react.visitorList.concat(reactDisplayName.visitorList)
26 + 'react': react.visitorList.concat(reactDisplayName.visitorList),
27 + 'types': typesSyntax.visitorList
28 +};
29 +
30 +var transformSets = {
31 + 'harmony': [
32 + 'es6-arrow-functions',
33 + 'es6-object-concise-method',
34 + 'es6-object-short-notation',
35 + 'es6-classes',
36 + 'es6-rest-params',
37 + 'es6-templates',
38 + 'es6-destructuring',
39 + 'es7-spread-property'
40 + ],
41 + 'react': [
42 + 'react'
43 + ],
44 + 'type-annotations': [
45 + 'types'
46 + ]
47 };
48
49 /**
50 * Specifies the order in which each transform should run.
51 */
52 var transformRunOrder = [
53 + 'types',
54 'es6-arrow-functions',
55 'es6-object-concise-method',
56 'es6-object-short-notation',
@@ -57,5 +79,34 @@ function getAllVisitors(excludes) {
79 return ret;
80 }
81
82 +/**
83 + * Given a list of visitor set names, return the ordered list of visitors to be
84 + * passed to jstransform.
85 + *
86 + * @param {array}
87 + * @return {array}
88 + */
89 +function getVisitorsBySet(sets) {
90 + var visitorsToInclude = sets.reduce(function(visitors, set) {
91 + if (!transformSets.hasOwnProperty(set)) {
92 + throw new Error('Unknown visitor set: ' + set);
93 + }
94 + transformSets[set].forEach(function(visitor) {
95 + visitors[visitor] = true;
96 + });
97 + return visitors;
98 + }, {});
99 +
100 + var visitorList = [];
101 + for (var i = 0; i < transformRunOrder.length; i++) {
102 + if (visitorsToInclude.hasOwnProperty(transformRunOrder[i])) {
103 + visitorList = visitorList.concat(transformVisitors[transformRunOrder[i]]);
104 + }
105 + }
106 +
107 + return visitorList;
108 +}
109 +
110 +exports.getVisitorsBySet = getVisitorsBySet;
111 exports.getAllVisitors = getAllVisitors;
112 exports.transformVisitors = transformVisitors;