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
/
Copy pathindex.js
161 lines (147 loc) · 5.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/*global exports:true*/
'use strict';
var es6ArrowFunction = require('./es6-arrow-function-visitors');
var es6CallSpread = require('./es6-call-spread-visitors');
var es6Class = require('./es6-class-visitors');
var es6Destructuring = require('./es6-destructuring-visitors');
var es6ForOf = require('./es6-for-of-visitors');
var es6ObjectComputedProperty =
require('./es6-object-computed-property-visitors');
var es6ObjectConciseMethod = require('./es6-object-concise-method-visitors');
var es6ObjectShortNotation = require('./es6-object-short-notation-visitors');
var es6RestParam = require('./es6-rest-param-visitors');
var es6Template = require('./es6-template-visitors');
var es7SpreadProperty = require('./es7-spread-property-visitors');
var es7TrailingComma = require('./es7-trailing-comma-visitors');
var reactDisplayName = require('./react-display-name-visitors');
var reactJSX = require('./react-jsx-visitors');
var reservedWords = require('./reserved-words-visitors');
var trailingComma = require('./trailing-comma-visitors');
var undefinedToVoid0 = require('./undefined-to-void-0-visitors');
// Map from transformName => orderedListOfVisitors.
var transformVisitors = {
'es6-arrow-function': es6ArrowFunction.visitorList,
'es6-call-spread': es6CallSpread.visitorList,
'es6-class': es6Class.visitorList,
'es6-destructuring': es6Destructuring.visitorList,
'es6-for-of': es6ForOf.visitorList,
'es6-object-computed-property': es6ObjectComputedProperty.visitorList,
'es6-object-concise-method': es6ObjectConciseMethod.visitorList,
'es6-object-short-notation': es6ObjectShortNotation.visitorList,
'es6-rest-param': es6RestParam.visitorList,
'es6-template': es6Template.visitorList,
'es7-spread-property': es7SpreadProperty.visitorList,
'es7-trailing-comma': es7TrailingComma.visitorList,
'react-display-name': reactDisplayName.visitorList,
'react-jsx': reactJSX.visitorList,
'reserved-words': reservedWords.visitorList,
'trailing-comma': trailingComma.visitorList,
'undefined-to-void-0': undefinedToVoid0.visitorList
};
// Sets of transforms. Useful for quickly building up with simple options.
var transformSets = {
'es6': [
'es6-arrow-function',
'es6-call-spread',
'es6-class',
'es6-destructuring',
'es6-for-of',
'es6-object-computed-property',
'es6-object-concise-method',
'es6-object-short-notation',
'es6-rest-param',
'es6-template'
],
'es7': [
'es7-spread-property',
'es7-trailing-comma'
],
'react': [
'react-jsx',
'react-display-name'
],
'target:es3': [
'reserved-words',
'trailing-comma'
],
'utility': [
'undefined-to-void-0'
]
};
// harmony is all newer transforms. Define it here so we don't duplicate.
transformSets.harmony = transformSets.es6.concat(transformSets.es7);
// Specifies the order in which each transform should run.
var transformRunOrder = [
'reserved-words',
'es6-destructuring',
'es6-arrow-function',
// needs to be before concice-methods, short-notation, spread-property
'es6-object-computed-property',
'es6-object-concise-method',
'es6-object-short-notation',
'es6-class',
'es6-rest-param',
'es6-template',
'es6-call-spread',
'es6-for-of',
'es7-spread-property',
// These are 2 distinct transforms - 'trailing-comma' handles array & object
// literals. 'es7-trailing-comma' handles extra arguments in function calls.
'trailing-comma',
'es7-trailing-comma',
'react-jsx',
'react-display-name',
'undefined-to-void-0'
];
/**
* 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;