@samitouri / QOS-React-2 / commits / 2d048f1f34

Move to using `jstransform` and `esprima-fb` npm modules

JeffMo committed Aug 19, 2013 at 14:37 UTC 2d048f1f34a7d10cca81336446920822ad7ea67c
13 files changed +26 -1086
bin/jsx
+1 -1
@@ -2,7 +2,7 @@
2 "use strict";
3
4 var visitors = require('../vendor/fbtransform/visitors').transformVisitors;
5 -var transform = require('../vendor/fbtransform/lib/transform').transform;
5 +var transform = require('jstransform').transform;
6 var propagate = require("../vendor/constants").propagate;
7
8 require("commoner").resolve(function(id) {
main.js
+1 -1
@@ -2,7 +2,7 @@
2
3 var React = require('./build/modules/React');
4 var visitors = require('./vendor/fbtransform/visitors').transformVisitors;
5 -var transform = require('./vendor/fbtransform/lib/transform').transform;
5 +var transform = require('jstransform').transform;
6
7 module.exports = {
8 React: React,
package.json
+3 -4
@@ -35,11 +35,10 @@
35 "test": "grunt build && grunt test"
36 },
37 "dependencies": {
38 - "base62": "~0.1.1",
38 "commoner": "~0.8.4",
40 - "esprima": "https://github.com/facebook/esprima/tarball/a3e0ea3979eb8d54d8bfade220c272903f928b1e",
41 - "recast": "~0.4.8",
42 - "source-map": "~0.1.22"
39 + "esprima-fb": "1001.1001.1000-dev-harmony-fb",
40 + "jstransform": "~1.0.0",
41 + "recast": "~0.4.8"
42 },
43 "devDependencies": {
44 "browserify": "~2.24.1",
vendor/browser-transforms.js
+2 -2
@@ -20,10 +20,10 @@
20 var runScripts;
21 var headEl;
22
23 -var transform = require('./fbtransform/lib/transform').transform;
23 +var transform = require('jstransform').transform;
24 var visitors = require('./fbtransform/visitors').transformVisitors;
25 var transform = transform.bind(null, visitors.react);
26 -var docblock = require('./fbtransform/lib/docblock');
26 +var docblock = require('jstransform/src/docblock');
27
28
29 exports.transform = transform;
vendor/fbtransform/lib/docblock.js deleted
-86
@@ -1,86 +0,0 @@
1 -/**
2 - * Copyright 2013 Facebook, Inc.
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and
14 - * limitations under the License.
15 - */
16 -
17 -var docblockRe = /^\s*(\/\*\*(.|\n)*?\*\/)/;
18 -var ltrimRe = /^\s*/;
19 -/**
20 - * @param {String} contents
21 - * @return {String}
22 - */
23 -function extract(contents) {
24 - var match = contents.match(docblockRe);
25 - if (match) {
26 - return match[0].replace(ltrimRe, '') || '';
27 - }
28 - return '';
29 -}
30 -
31 -
32 -var commentStartRe = /^\/\*\*?/;
33 -var commentEndRe = /\*\/$/;
34 -var wsRe = /[\t ]+/g;
35 -var stringStartRe = /(\n|^) *\*/g;
36 -var multilineRe = /(?:^|\n) *(@[^\n]*?) *\n *([^@\n\s][^@\n]+?) *\n/g;
37 -var propertyRe = /(?:^|\n) *@(\S+) *([^\n]*)/g;
38 -
39 -/**
40 - * @param {String} contents
41 - * @return {Array}
42 - */
43 -function parse(docblock) {
44 - docblock = docblock
45 - .replace(commentStartRe, '')
46 - .replace(commentEndRe, '')
47 - .replace(wsRe, ' ')
48 - .replace(stringStartRe, '$1');
49 -
50 - // Normalize multi-line directives
51 - var prev = '';
52 - while (prev != docblock) {
53 - prev = docblock;
54 - docblock = docblock.replace(multilineRe, "\n$1 $2\n");
55 - }
56 - docblock = docblock.trim();
57 -
58 - var result = [];
59 - var match;
60 - while (match = propertyRe.exec(docblock)) {
61 - result.push([match[1], match[2]]);
62 - }
63 -
64 - return result;
65 -}
66 -
67 -/**
68 - * Same as parse but returns an object of prop: value instead of array of paris
69 - * If a property appers more than once the last one will be returned
70 - *
71 - * @param {String} contents
72 - * @return {Object}
73 - */
74 -function parseAsObject(docblock) {
75 - var pairs = parse(docblock);
76 - var result = {};
77 - for (var i = 0; i < pairs.length; i++) {
78 - result[pairs[i][0]] = pairs[i][1];
79 - }
80 - return result;
81 -}
82 -
83 -
84 -exports.extract = extract;
85 -exports.parse = parse;
86 -exports.parseAsObject = parseAsObject;
vendor/fbtransform/lib/transform.js deleted
-141
@@ -1,141 +0,0 @@
1 -/**
2 - * Copyright 2013 Facebook, Inc.
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and
14 - * limitations under the License.
15 - */
16 -/*global exports:true*/
17 -/*jslint node: true*/
18 -"use strict";
19 -
20 -/**
21 - * Syntax transfomer for javascript. Takes the source in, spits the source
22 - * out. Tries to maintain readability and preserve whitespace and line numbers
23 - * where posssible.
24 - *
25 - * Support
26 - * - ES6 class transformation + private property munging, see ./classes.js
27 - * - React XHP style syntax transformations, see ./react.js
28 - * - Bolt XHP style syntax transformations, see ./bolt.js
29 - *
30 - * The general flow is the following:
31 - * - Parse the source with our customized esprima-parser
32 - * https://github.com/voloko/esprima. We have to customize the parser to
33 - * support non-standard XHP-style syntax. We parse the source range: true
34 - * option that forces esprima to return positions in the source within
35 - * resulting parse tree.
36 - *
37 - * - Traverse resulting syntax tree, trying to apply a set of visitors to each
38 - * node. Each visitor should provide a .test() function that tests if the
39 - * visitor can process a given node.
40 - *
41 - * - Visitor is responsible for code generation for a given syntax node.
42 - * Generated code is stored in state.g.buffer that is passed to every
43 - * visitor. It's up to the visitor to process the code the way it sees fit.
44 - * All of the current visitors however use both the node and the original
45 - * source to generate transformed code. They use nodes to generate new
46 - * code and they copy the original source, preserving whitespace and comments,
47 - * for the parts they don't care about.
48 - */
49 -var esprima = require('esprima');
50 -
51 -var createState = require('./utils').createState;
52 -var catchup = require('./utils').catchup;
53 -
54 -/**
55 - * @param {object} object
56 - * @param {function} visitor
57 - * @param {array} path
58 - * @param {object} state
59 - */
60 -function traverse(object, path, state) {
61 - var key, child;
62 -
63 - if (walker(traverse, object, path, state) === false) {
64 - return;
65 - }
66 - path.unshift(object);
67 - for (key in object) {
68 - // skip obviously wrong attributes
69 - if (key === 'range' || key === 'loc') {
70 - continue;
71 - }
72 - if (object.hasOwnProperty(key)) {
73 - child = object[key];
74 - if (typeof child === 'object' && child !== null) {
75 - child.range && catchup(child.range[0], state);
76 - traverse(child, path, state);
77 - child.range && catchup(child.range[1], state);
78 - }
79 - }
80 - }
81 - path.shift();
82 -}
83 -
84 -function walker(traverse, object, path, state) {
85 - var visitors = state.g.visitors;
86 - for (var i = 0; i < visitors.length; i++) {
87 - if (visitors[i].test(object, path, state)) {
88 - return visitors[i](traverse, object, path, state);
89 - }
90 - }
91 -}
92 -
93 -function runPass(source, visitors, options) {
94 - var ast;
95 - try {
96 - ast = esprima.parse(source, { comment: true, loc: true, range: true });
97 - } catch (e) {
98 - e.message = 'Parse Error: ' + e.message;
99 - throw e;
100 - }
101 - var state = createState(source, options);
102 - state.g.originalProgramAST = ast;
103 - state.g.visitors = visitors;
104 -
105 - if (options.sourceMap) {
106 - var SourceMapGenerator = require('source-map').SourceMapGenerator;
107 - state.g.sourceMap = new SourceMapGenerator({ file: 'transformed.js' });
108 - }
109 - traverse(ast, [], state);
110 - catchup(source.length, state);
111 - return state;
112 -}
113 -
114 -/**
115 - * Applies all available transformations to the source
116 - * @param {array} visitors
117 - * @param {string} source
118 - * @param {?object} options
119 - * @return {object}
120 - */
121 -function transform(visitors, source, options) {
122 - options = options || {};
123 -
124 - var state = runPass(source, visitors, options);
125 - var sourceMap = state.g.sourceMap;
126 -
127 - if (sourceMap) {
128 - return {
129 - sourceMap: sourceMap,
130 - sourceMapFilename: options.filename || 'source.js',
131 - code: state.g.buffer
132 - };
133 - } else {
134 - return {
135 - code: state.g.buffer
136 - };
137 - }
138 -}
139 -
140 -
141 -exports.transform = transform;
vendor/fbtransform/lib/utils.js deleted
-332
@@ -1,332 +0,0 @@
1 -/**
2 - * Copyright 2013 Facebook, Inc.
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and
14 - * limitations under the License.
15 - */
16 -/*global exports:true*/
17 -
18 -/**
19 - * State represents the given parser state. It has a local and global parts.
20 - * Global contains parser position, source, etc. Local contains scope based
21 - * properties, like current class name. State should contain all the info
22 - * required for transformation. It's the only mandatory object that is being
23 - * passed to every function in transform chain.
24 - *
25 - * @param {String} source
26 - * @param {Object} transformOptions
27 - * @return {Object}
28 - */
29 -function createState(source, transformOptions) {
30 - return {
31 - /**
32 - * Name of the super class variable
33 - * @type {String}
34 - */
35 - superVar: '',
36 - /**
37 - * Name of the enclosing class scope
38 - * @type {String}
39 - */
40 - scopeName: '',
41 - /**
42 - * Global state (not affected by updateState)
43 - * @type {Object}
44 - */
45 - g: {
46 - /**
47 - * A set of general options that transformations can consider while doing
48 - * a transformation:
49 - *
50 - * - minify
51 - * Specifies that transformation steps should do their best to minify
52 - * the output source when possible. This is useful for places where
53 - * minification optimizations are possible with higher-level context
54 - * info than what jsxmin can provide.
55 - *
56 - * For example, the ES6 class transform will minify munged private
57 - * variables if this flag is set.
58 - */
59 - opts: transformOptions,
60 - /**
61 - * Current position in the source code
62 - * @type {Number}
63 - */
64 - position: 0,
65 - /**
66 - * Buffer containing the result
67 - * @type {String}
68 - */
69 - buffer: '',
70 - /**
71 - * Indentation offset (only negative offset is supported now)
72 - * @type {Number}
73 - */
74 - indentBy: 0,
75 - /**
76 - * Source that is being transformed
77 - * @type {String}
78 - */
79 - source: source,
80 -
81 - /**
82 - * Cached parsed docblock (see getDocblock)
83 - * @type {object}
84 - */
85 - docblock: null,
86 -
87 - /**
88 - * Whether the thing was used
89 - * @type {Boolean}
90 - */
91 - tagNamespaceUsed: false,
92 -
93 - /**
94 - * If using bolt xjs transformation
95 - * @type {Boolean}
96 - */
97 - isBolt: undefined,
98 -
99 - /**
100 - * Whether to record source map (expensive) or not
101 - * @type {SourceMapGenerator|null}
102 - */
103 - sourceMap: null,
104 -
105 - /**
106 - * Filename of the file being processed. Will be returned as a source
107 - * attribute in the source map
108 - */
109 - sourceMapFilename: 'source.js',
110 -
111 - /**
112 - * Only when source map is used: last line in the source for which
113 - * source map was generated
114 - * @type {Number}
115 - */
116 - sourceLine: 1,
117 -
118 - /**
119 - * Only when source map is used: last line in the buffer for which
120 - * source map was generated
121 - * @type {Number}
122 - */
123 - bufferLine: 1,
124 -
125 - /**
126 - * The top-level Program AST for the original file.
127 - */
128 - originalProgramAST: null,
129 -
130 - sourceColumn: 0,
131 - bufferColumn: 0
132 - }
133 - };
134 -}
135 -
136 -/**
137 - * Updates a copy of a given state with "update" and returns an updated state.
138 - *
139 - * @param {Object} state
140 - * @param {Object} update
141 - * @return {Object}
142 - */
143 -function updateState(state, update) {
144 - return {
145 - g: state.g,
146 - superVar: update.superVar || state.superVar,
147 - scopeName: update.scopeName || state.scopeName
148 - };
149 -}
150 -
151 -/**
152 - * Given a state fill the resulting buffer from the original source up to
153 - * the end
154 - * @param {Number} end
155 - * @param {Object} state
156 - * @param {Function?} contentTransformer Optional callback to transform newly
157 - * added content.
158 - */
159 -function catchup(end, state, contentTransformer) {
160 - if (end < state.g.position) {
161 - // cannot move backwards
162 - return;
163 - }
164 - var source = state.g.source.substring(state.g.position, end);
165 - var transformed = updateIndent(source, state);
166 - if (state.g.sourceMap && transformed) {
167 - // record where we are
168 - state.g.sourceMap.addMapping({
169 - generated: { line: state.g.bufferLine, column: state.g.bufferColumn },
170 - original: { line: state.g.sourceLine, column: state.g.sourceColumn },
171 - source: state.g.sourceMapFilename
172 - });
173 -
174 - // record line breaks in transformed source
175 - var sourceLines = source.split('\n');
176 - var transformedLines = transformed.split('\n');
177 - // Add line break mappings between last known mapping and the end of the
178 - // added piece. So for the code piece
179 - // (foo, bar);
180 - // > var x = 2;
181 - // > var b = 3;
182 - // var c =
183 - // only add lines marked with ">": 2, 3.
184 - for (var i = 1; i < sourceLines.length - 1; i++) {
185 - state.g.sourceMap.addMapping({
186 - generated: { line: state.g.bufferLine, column: 0 },
187 - original: { line: state.g.sourceLine, column: 0 },
188 - source: state.g.sourceMapFilename
189 - });
190 - state.g.sourceLine++;
191 - state.g.bufferLine++;
192 - }
193 - // offset for the last piece
194 - if (sourceLines.length > 1) {
195 - state.g.sourceLine++;
196 - state.g.bufferLine++;
197 - state.g.sourceColumn = 0;
198 - state.g.bufferColumn = 0;
199 - }
200 - state.g.sourceColumn += sourceLines[sourceLines.length - 1].length;
201 - state.g.bufferColumn +=
202 - transformedLines[transformedLines.length - 1].length;
203 - }
204 - state.g.buffer +=
205 - contentTransformer ? contentTransformer(transformed) : transformed;
206 - state.g.position = end;
207 -}
208 -
209 -/**
210 - * Applies `catchup` but passing in a function that removes any non-whitespace
211 - * characters.
212 - */
213 -var re = /(\S)/g;
214 -function stripNonWhite(value) {
215 - return value.replace(re, function() {
216 - return '';
217 - });
218 -}
219 -/**
220 - * Catches up as `catchup` but turns each non-white character into a space.
221 - */
222 -function catchupWhiteSpace(end, state) {
223 - catchup(end, state, stripNonWhite);
224 -}
225 -
226 -/**
227 - * Same as catchup but does not touch the buffer
228 - * @param {Number} end
229 - * @param {Object} state
230 - */
231 -function move(end, state) {
232 - // move the internal cursors
233 - if (state.g.sourceMap) {
234 - if (end < state.g.position) {
235 - state.g.position = 0;
236 - state.g.sourceLine = 1;
237 - state.g.sourceColumn = 0;
238 - }
239 -
240 - var source = state.g.source.substring(state.g.position, end);
241 - var sourceLines = source.split('\n');
242 - if (sourceLines.length > 1) {
243 - state.g.sourceLine += sourceLines.length - 1;
244 - state.g.sourceColumn = 0;
245 - }
246 - state.g.sourceColumn += sourceLines[sourceLines.length - 1].length;
247 - }
248 - state.g.position = end;
249 -}
250 -
251 -/**
252 - * Appends a string of text to the buffer
253 - * @param {String} string
254 - * @param {Object} state
255 - */
256 -function append(string, state) {
257 - if (state.g.sourceMap && string) {
258 - state.g.sourceMap.addMapping({
259 - generated: { line: state.g.bufferLine, column: state.g.bufferColumn },
260 - original: { line: state.g.sourceLine, column: state.g.sourceColumn },
261 - source: state.g.sourceMapFilename
262 - });
263 - var transformedLines = string.split('\n');
264 - if (transformedLines.length > 1) {
265 - state.g.bufferLine += transformedLines.length - 1;
266 - state.g.bufferColumn = 0;
267 - }
268 - state.g.bufferColumn +=
269 - transformedLines[transformedLines.length - 1].length;
270 - }
271 - state.g.buffer += string;
272 -}
273 -
274 -/**
275 - * Update indent using state.indentBy property. Indent is measured in
276 - * double spaces. Updates a single line only.
277 - *
278 - * @param {String} str
279 - * @param {Object} state
280 - * @return {String}
281 - */
282 -function updateIndent(str, state) {
283 - for (var i = 0; i < -state.g.indentBy; i++) {
284 - str = str.replace(/(^|\n)( {2}|\t)/g, '$1');
285 - }
286 - return str;
287 -}
288 -
289 -/**
290 - * Calculates indent from the beginning of the line until "start" or the first
291 - * character before start.
292 - * @example
293 - * " foo.bar()"
294 - * ^
295 - * start
296 - * indent will be 2
297 - *
298 - * @param {Number} start
299 - * @param {Object} state
300 - * @return {Number}
301 - */
302 -function indentBefore(start, state) {
303 - var end = start;
304 - start = start - 1;
305 -
306 - while (start > 0 && state.g.source[start] != '\n') {
307 - if (!state.g.source[start].match(/[ \t]/)) {
308 - end = start;
309 - }
310 - start--;
311 - }
312 - return state.g.source.substring(start + 1, end);
313 -}
314 -
315 -function getDocblock(state) {
316 - if (!state.g.docblock) {
317 - var docblock = require('./docblock');
318 - state.g.docblock =
319 - docblock.parseAsObject(docblock.extract(state.g.source));
320 - }
321 - return state.g.docblock;
322 -}
323 -
324 -exports.catchup = catchup;
325 -exports.catchupWhiteSpace = catchupWhiteSpace;
326 -exports.append = append;
327 -exports.move = move;
328 -exports.updateIndent = updateIndent;
329 -exports.indentBefore = indentBefore;
330 -exports.updateState = updateState;
331 -exports.createState = createState;
332 -exports.getDocblock = getDocblock;
vendor/fbtransform/syntax.js
+1 -1
@@ -3,7 +3,7 @@
3 /*global exports:true*/
4 "use strict";
5
6 -var transform = require('./lib/transform').transform;
6 +var transform = require('jstransform').transform;
7 var visitors = require('./visitors');
8
9 /**
vendor/fbtransform/transforms/classes.js deleted
-492
@@ -1,492 +0,0 @@
1 -/**
2 - * Copyright 2013 Facebook, Inc.
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and
14 - * limitations under the License.
15 - */
16 -/*global exports:true*/
17 -"use strict";
18 -
19 -/**
20 - * Desugarizer for ES6 minimal class proposal. See
21 - * http://wiki.ecmascript.org/doku.php?id=harmony:proposals
22 - *
23 - * Does not require any runtime. Preserves whitespace and comments.
24 - * Supports a class declaration with methods, super calls and inheritance.
25 - * Currently does not support for getters and setters, since there's a very
26 - * low probability we're going to use them anytime soon.
27 - *
28 - * Additional features:
29 - * - Any member with private name (the name with prefix _, such _name) inside
30 - * the class's scope will be munged. This would will to eliminate the case
31 - * of sub-class accidentally overriding the super-class's provate properties
32 - * also discouage people from accessing private members that they should not
33 - * access. However, quoted property names don't get munged.
34 - *
35 - * class SkinnedMesh extends require('THREE').Mesh {
36 - *
37 - * update(camera) {
38 - * camera.code = 'iphone'
39 - * super.update(camera);
40 - * }
41 - *
42 - * /
43 - * * @constructor
44 - * /
45 - * constructor(geometry, materials) {
46 - * super(geometry, materials);
47 - *
48 - * super.update(1);
49 - *
50 - * this.identityMatrix = new THREE.Matrix4();
51 - * this.bones = [];
52 - * this.boneMatrices = [];
53 - * this._name = 'foo';
54 - * }
55 - *
56 - * /
57 - * * some other code
58 - * /
59 - * readMore() {
60 - *
61 - * }
62 - *
63 - * _doSomething() {
64 - *
65 - * }
66 - * }
67 - *
68 - * should be converted to
69 - *
70 - * var SkinnedMesh = (function() {
71 - * var __super = require('parent').Mesh;
72 - *
73 - * /
74 - * * @constructor
75 - * /
76 - * function SkinnedMesh(geometry, materials) {
77 - * __super.call(this, geometry, materials);
78 - *
79 - * __super.prototype.update.call(this, 1);
80 - *
81 - * this.identityMatrix = new THREE.Matrix4();
82 - * this.bones = [];
83 - * this.boneMatrices = [];
84 - * this.$SkinnedMesh_name = 'foo';
85 - * }
86 - * SkinnedMesh.prototype = Object.create(__super.prototype);
87 - * SkinnedMesh.prototype.constructor = SkinnedMesh;
88 - *
89 - * /
90 - * * @param camera
91 - * /
92 - * SkinnedMesh.prototype.update = function(camera) {
93 - * camera.code = 'iphone'
94 - * __super.prototype.update.call(this, camera);
95 - * };
96 - *
97 - * SkinnedMesh.prototype.readMore = function() {
98 - *
99 - * };
100 - *
101 - * SkinnedMesh.prototype.$SkinnedMesh_doSomething = function() {
102 - *
103 - * };
104 - *
105 - * return SkinnedMesh;
106 - * })();
107 - *
108 - */
109 -var Syntax = require('esprima').Syntax;
110 -var base62 = require('base62');
111 -
112 -var catchup = require('../lib/utils').catchup;
113 -var append = require('../lib/utils').append;
114 -var move = require('../lib/utils').move;
115 -var indentBefore = require('../lib/utils').indentBefore;
116 -var updateIndent = require('../lib/utils').updateIndent;
117 -var updateState = require('../lib/utils').updateState;
118 -
119 -function findConstructorIndex(object) {
120 - var classElements = object.body && object.body.body || [];
121 - for (var i = 0; i < classElements.length; i++) {
122 - if (classElements[i].type === Syntax.MethodDefinition &&
123 - classElements[i].key.name === 'constructor') {
124 - return i;
125 - }
126 - }
127 - return -1;
128 -}
129 -
130 -var _mungedSymbolMaps = {};
131 -function getMungedName(scopeName, name, minify) {
132 - if (minify) {
133 - if (!_mungedSymbolMaps[scopeName]) {
134 - _mungedSymbolMaps[scopeName] = {
135 - symbolMap: {},
136 - identifierUUIDCounter: 0
137 - };
138 - }
139 -
140 - var symbolMap = _mungedSymbolMaps[scopeName].symbolMap;
141 - if (!symbolMap[name]) {
142 - symbolMap[name] =
143 - base62.encode(_mungedSymbolMaps[scopeName].identifierUUIDCounter);
144 - _mungedSymbolMaps[scopeName].identifierUUIDCounter++;
145 - }
146 - name = symbolMap[name];
147 - }
148 - return '$' + scopeName + name;
149 -}
150 -
151 -function shouldMungeName(scopeName, name, state) {
152 - // only run when @preventMunge is not present in the docblock
153 - if (state.g.preventMunge === undefined) {
154 - var docblock = require('../lib/docblock');
155 - state.g.preventMunge = docblock.parseAsObject(
156 - docblock.extract(state.g.source)).preventMunge !== undefined;
157 - }
158 - // Starts with only a single underscore (i.e. don't count double-underscores)
159 - return !state.g.preventMunge && scopeName ? /^_(?!_)/.test(name) : false;
160 -}
161 -
162 -
163 -function getProtoOfPrototypeVariableName(superVar) {
164 - return superVar + 'ProtoOfPrototype';
165 -}
166 -
167 -function getSuperKeyName(superVar) {
168 - return superVar + 'Key';
169 -}
170 -
171 -function getSuperProtoOfPrototypeVariable(superVariableName, indent) {
172 - var string = (indent +
173 - 'var $proto = $superName && $superName.prototype ? ' +
174 - '$superName.prototype : $superName;\n'
175 - ).replace(/\$proto/g, getProtoOfPrototypeVariableName(superVariableName))
176 - .replace(/\$superName/g, superVariableName);
177 - return string;
178 -}
179 -
180 -
181 -function getInheritanceSetup(superClassToken, className, indent, superName) {
182 - var string = '';
183 - if (superClassToken) {
184 - string += getStaticMethodsOnConstructorSetup(className, indent, superName);
185 - string += getPrototypeOnConstructorSetup(className, indent, superName);
186 - string += getConstructorPropertySetup(className, indent);
187 - }
188 - return string;
189 -}
190 -
191 -function getStaticMethodsOnConstructorSetup(className, indent, superName) {
192 - var string = ( indent +
193 - 'for (var $keyName in $superName) {\n' + indent +
194 - ' if ($superName.hasOwnProperty($keyName)) {\n' + indent +
195 - ' $className[$keyName] = $superName[$keyName];\n' + indent +
196 - ' }\n' + indent +
197 - '}\n')
198 - .replace(/\$className/g, className)
199 - .replace(/\$keyName/g, getSuperKeyName(superName))
200 - .replace(/\$superName/g, superName);
201 - return string;
202 -}
203 -
204 -function getPrototypeOnConstructorSetup(className, indent, superName) {
205 - var string = ( indent +
206 - '$className.prototype = Object.create($protoPrototype);\n')
207 - .replace(/\$protoPrototype/g, getProtoOfPrototypeVariableName(superName))
208 - .replace(/\$className/g, className);
209 - return string;
210 -}
211 -
212 -function getConstructorPropertySetup(className, indent) {
213 - var string = ( indent +
214 - '$className.prototype.constructor = $className;\n')
215 - .replace(/\$className/g, className);
216 -
217 - return string;
218 -}
219 -
220 -function getSuperConstructorSetup(superClassToken, indent, superName) {
221 - if (!superClassToken) return '';
222 - var string = ( '\n' + indent +
223 - ' if ($superName && $superName.prototype) {\n' + indent +
224 - ' $superName.apply(this, arguments);\n' + indent +
225 - ' }\n' + indent)
226 - .replace(/\$superName/g, superName);
227 - return string;
228 -}
229 -
230 -function getMemberFunctionCall(superVar, propertyName, superArgs) {
231 - var string = (
232 - '$superPrototype.$propertyName.call($superArguments)')
233 - .replace(/\$superPrototype/g, getProtoOfPrototypeVariableName(superVar))
234 - .replace(/\$propertyName/g, propertyName)
235 - .replace(/\$superArguments/g, superArgs);
236 - return string;
237 -}
238 -
239 -function getCallParams(classElement, state) {
240 - var params = classElement.value.params;
241 - if (!params.length) {
242 - return '';
243 - }
244 - return state.g.source.substring(
245 - params[0].range[0],
246 - params[params.length - 1].range[1]);
247 -}
248 -
249 -function getSuperArguments(callExpression, state) {
250 - var args = callExpression.arguments;
251 - if (!args.length) {
252 - return 'this';
253 - }
254 - return 'this, ' + state.g.source.substring(
255 - args[0].range[0],
256 - args[args.length - 1].range[1]);
257 -}
258 -
259 -// The seed is used to generate the name for an anonymous class,
260 -// and this seed should be unique per browser's session.
261 -// The value of the seed looks like this: 1229588505.2969012.
262 -var classIDSeed = Date.now() % (60 * 60 * 1000) + Math.random();
263 -
264 -/**
265 - * Generates a name for an anonymous class. The generated value looks like
266 - * this: "Classkc6pcn_mniza1yvi"
267 - * @param {String} scopeName
268 - * @return {string} the scope name for Anonymous Class
269 - */
270 -function generateAnonymousClassName(scopeName) {
271 - classIDSeed++;
272 - return 'Class' +
273 - (classIDSeed).toString(36).replace('.', '_') +
274 - (scopeName || '');
275 -}
276 -
277 -function renderMethods(traverse, object, name, path, state) {
278 - var classElements = object.body && object.body.body || [];
279 -
280 - move(object.body.range[0] + 1, state);
281 - for (var i = 0; i < classElements.length; i++) {
282 - if (classElements[i].key.name !== 'constructor') {
283 - catchup(classElements[i].range[0], state);
284 -
285 - var memberName = classElements[i].key.name;
286 - if (shouldMungeName(state.scopeName, memberName, state)) {
287 - memberName = getMungedName(
288 - state.scopeName,
289 - memberName,
290 - state.g.opts.minify
291 - );
292 - }
293 -
294 - var prototypeOrStatic;
295 - if (classElements[i]['static']) {
296 - prototypeOrStatic = '';
297 - } else {
298 - prototypeOrStatic = 'prototype.';
299 - }
300 -
301 - append(name + '.' + prototypeOrStatic + memberName + ' = ', state);
302 - renderMethod(traverse, classElements[i], null, path, state);
303 - append(';', state);
304 - }
305 - move(classElements[i].range[1], state);
306 - }
307 - if (classElements.length) {
308 - append('\n', state);
309 - }
310 - move(object.range[1], state);
311 -}
312 -
313 -function renderMethod(traverse, method, name, path, state) {
314 - append(name ? 'function ' + name + '(' : 'function(', state);
315 - append(getCallParams(method, state) + ') {', state);
316 - move(method.value.body.range[0] + 1, state);
317 - traverse(method.value.body, path, state);
318 - catchup(method.value.body.range[1] - 1, state);
319 - append('}', state);
320 -}
321 -
322 -function renderSuperClass(traverse, superClass, path, state) {
323 - append('var ' + state.superVar + ' = ', state);
324 - move(superClass.range[0], state);
325 - traverse(superClass, path, state);
326 - catchup(superClass.range[1], state);
327 - append(';\n', state);
328 -}
329 -
330 -function renderConstructor(traverse, object, name, indent, path, state) {
331 - var classElements = object.body && object.body.body || [];
332 - var constructorIndex = findConstructorIndex(object);
333 - var constructor = constructorIndex === -1 ?
334 - null :
335 - classElements[constructorIndex];
336 - if (constructor) {
337 - move(constructorIndex === 0 ?
338 - object.body.range[0] + 1 :
339 - classElements[constructorIndex - 1].range[1], state);
340 - catchup(constructor.range[0], state);
341 - renderMethod(traverse, constructor, name, path, state);
342 - append('\n', state);
343 - } else {
344 - if (object.superClass) {
345 - append('\n' + indent, state);
346 - }
347 - append('function ', state);
348 - if (object.id) {
349 - move(object.id.range[0], state);
350 - }
351 - append(name, state);
352 - if (object.id) {
353 - move(object.id.range[1], state);
354 - }
355 - append('(){ ', state);
356 - if (object.body) {
357 - move(object.body.range[0], state);
358 - }
359 - append(getSuperConstructorSetup(
360 - object.superClass,
361 - indent,
362 - state.superVar), state);
363 - append('}\n', state);
364 - }
365 -}
366 -
367 -var superId = 0;
368 -function renderClassBody(traverse, object, path, state) {
369 - var name = object.id ? object.id.name : 'constructor';
370 - var superClass = object.superClass;
371 - var indent = updateIndent(
372 - indentBefore(object.range[0], state) + ' ',
373 - state);
374 -
375 - state = updateState(
376 - state,
377 - {
378 - scopeName: object.id ? object.id.name :
379 - generateAnonymousClassName(state.scopeName),
380 - superVar: superClass ? '__super' + superId++ : ''
381 - });
382 -
383 - // super class
384 - if (superClass) {
385 - append(indent, state);
386 - renderSuperClass(traverse, superClass, path, state);
387 - append(getSuperProtoOfPrototypeVariable(state.superVar, indent), state);
388 - }
389 -
390 - renderConstructor(traverse, object, name, indent, path, state);
391 - append(getInheritanceSetup(superClass, name, indent, state.superVar), state);
392 - renderMethods(traverse, object, name, path, state);
393 -}
394 -
395 -
396 -/**
397 - * @public
398 - */
399 -function visitClassExpression(traverse, object, path, state) {
400 - var indent = updateIndent(
401 - indentBefore(object.range[0], state) + ' ',
402 - state);
403 - var name = object.id ? object.id.name : 'constructor';
404 -
405 - append('(function() {\n', state);
406 - renderClassBody(traverse, object, path, state);
407 - append(indent + 'return ' + name + ';\n', state);
408 - append(indent.substring(0, indent.length - 2) + '})()', state);
409 - return false
410 -}
411 -
412 -visitClassExpression.test = function(object, path, state) {
413 - return object.type === Syntax.ClassExpression;
414 -};
415 -
416 -/**
417 - * @public
418 - */
419 -function visitClassDeclaration(traverse, object, path, state) {
420 - state.g.indentBy--;
421 - renderClassBody(traverse, object, path, state);
422 - state.g.indentBy++;
423 - return false;
424 -}
425 -
426 -visitClassDeclaration.test = function(object, path, state) {
427 - return object.type === Syntax.ClassDeclaration;
428 -};
429 -
430 -
431 -/**
432 - * @public
433 - */
434 -function visitSuperCall(traverse, object, path, state) {
435 - if (path[0].type === Syntax.CallExpression) {
436 - append(state.superVar +
437 - '.call(' + getSuperArguments(path[0], state) + ')', state);
438 - move(path[0].range[1], state);
439 - } else if (path[0].type === Syntax.MemberExpression) {
440 - append(getMemberFunctionCall(
441 - state.superVar,
442 - path[0].property.name,
443 - getSuperArguments(path[1], state)), state);
444 - move(path[1].range[1], state);
445 - }
446 - return false;
447 -}
448 -
449 -visitSuperCall.test = function(object, path, state) {
450 - return state.superVar && object.type === Syntax.Identifier &&
451 - object.name === 'super';
452 -};
453 -
454 -/**
455 - * @public
456 - */
457 -function visitPrivateProperty(traverse, object, path, state) {
458 - var type = path[0] ? path[0].type : null;
459 - if (type !== Syntax.Property) {
460 - if (type === Syntax.MemberExpression) {
461 - type = path[0].object ? path[0].object.type : null;
462 - if (type === Syntax.Identifier &&
463 - path[0].object.range[0] === object.range[0]) {
464 - // Identifier is a variable that appears "private".
465 - return;
466 - }
467 - } else {
468 - // Other syntax that are neither Property nor MemberExpression.
469 - return;
470 - }
471 - }
472 -
473 - var oldName = object.name;
474 - var newName = getMungedName(
475 - state.scopeName,
476 - oldName,
477 - state.g.opts.minify
478 - );
479 - append(newName, state);
480 - move(object.range[1], state);
481 -}
482 -
483 -visitPrivateProperty.test = function(object, path, state) {
484 - return object.type === Syntax.Identifier &&
485 - shouldMungeName(state.scopeName, object.name, state);
486 -};
487 -
488 -
489 -exports.visitClassDeclaration = visitClassDeclaration;
490 -exports.visitClassExpression = visitClassExpression;
491 -exports.visitSuperCall = visitSuperCall;
492 -exports.visitPrivateProperty = visitPrivateProperty;
vendor/fbtransform/transforms/react.js
+5 -5
@@ -16,12 +16,12 @@
16 /*global exports:true*/
17 "use strict";
18
19 -var Syntax = require('esprima').Syntax;
19 +var Syntax = require('esprima-fb').Syntax;
20
21 -var catchup = require('../lib/utils').catchup;
22 -var append = require('../lib/utils').append;
23 -var move = require('../lib/utils').move;
24 -var getDocblock = require('../lib/utils').getDocblock;
21 +var catchup = require('jstransform/src/utils').catchup;
22 +var append = require('jstransform/src/utils').append;
23 +var move = require('jstransform/src/utils').move;
24 +var getDocblock = require('jstransform/src/utils').getDocblock;
25
26 var FALLBACK_TAGS = require('./xjs').knownTags;
27 var renderXJSExpressionContainer =
vendor/fbtransform/transforms/reactDisplayName.js
+4 -4
@@ -16,10 +16,10 @@
16 /*global exports:true*/
17 "use strict";
18
19 -var Syntax = require('esprima').Syntax;
20 -var catchup = require('../lib/utils').catchup;
21 -var append = require('../lib/utils').append;
22 -var getDocblock = require('../lib/utils').getDocblock;
19 +var Syntax = require('esprima-fb').Syntax;
20 +var catchup = require('jstransform/src/utils').catchup;
21 +var append = require('jstransform/src/utils').append;
22 +var getDocblock = require('jstransform/src/utils').getDocblock;
23
24 /**
25 * Transforms the following:
vendor/fbtransform/transforms/xjs.js
+4 -4
@@ -15,10 +15,10 @@
15 */
16 /*global exports:true*/
17 "use strict";
18 -var append = require('../lib/utils').append;
19 -var catchup = require('../lib/utils').catchup;
20 -var move = require('../lib/utils').move;
21 -var Syntax = require('esprima').Syntax;
18 +var append = require('jstransform/src/utils').append;
19 +var catchup = require('jstransform/src/utils').catchup;
20 +var move = require('jstransform/src/utils').move;
21 +var Syntax = require('esprima-fb').Syntax;
22
23 var knownTags = {
24 a: true,
vendor/fbtransform/visitors.js
+5 -13
@@ -1,5 +1,5 @@
1 /*global exports:true*/
2 -var classes = require('./transforms/classes');
2 +var es6Classes = require('jstransform/visitors/es6-class-visitors').visitorList;
3 var react = require('./transforms/react');
4 var reactDisplayName = require('./transforms/reactDisplayName');
5
@@ -7,21 +7,13 @@ var reactDisplayName = require('./transforms/reactDisplayName');
7 * Map from transformName => orderedListOfVisitors.
8 */
9 var transformVisitors = {
10 - 'es6-classes': [
11 - classes.visitClassExpression,
12 - classes.visitClassDeclaration,
13 - classes.visitSuperCall,
14 - classes.visitPrivateProperty
10 + 'es6-classes': es6Classes,
11 + 'react': [
12 + react.visitReactTag,
13 + reactDisplayName.visitReactDisplayName
14 ]
15 };
16
18 -transformVisitors.react = transformVisitors[
19 - "es6-classes"
20 -].concat([
21 - react.visitReactTag,
22 - reactDisplayName.visitReactDisplayName
23 -]);
24 -
17 /**
18 * Specifies the order in which each transform should run.
19 */