Skip to content
This repository was archived by the owner on Apr 9, 2020. It is now read-only.

Commit 292e5ad

Browse files
committed
upgrade to Babel 6 (WIP)
1 parent 81f15e5 commit 292e5ad

File tree

1 file changed

+34
-50
lines changed

1 file changed

+34
-50
lines changed

src/index.js

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function ({ Plugin, types: t }) {
1+
export default function ({ types: t }) {
22
const depthKey = '__reactTransformDepth';
33
const recordsKey = '__reactTransformRecords';
44
const wrapComponentIdKey = '__reactTransformWrapComponentId';
@@ -55,10 +55,7 @@ export default function ({ Plugin, types: t }) {
5555
return false;
5656
}
5757
const first = args[0];
58-
if (!t.isObjectExpression(first)) {
59-
return false;
60-
}
61-
return true;
58+
return t.isObjectExpression(first);
6259
}
6360

6461
/**
@@ -86,26 +83,6 @@ export default function ({ Plugin, types: t }) {
8683
Array.isArray(options.transforms);
8784
}
8885

89-
/**
90-
* Enforces plugin options to be defined and returns them.
91-
*/
92-
function getPluginOptions(file) {
93-
if (!file.opts || !file.opts.extra) {
94-
return;
95-
}
96-
97-
let pluginOptions = file.opts.extra['react-transform'];
98-
if (!isValidOptions(pluginOptions)) {
99-
throw new Error(
100-
'babel-plugin-react-transform requires that you specify ' +
101-
'extras["react-transform"] in .babelrc ' +
102-
'or in your Babel Node API call options, and that it is an object with ' +
103-
'a transforms property which is an array.'
104-
);
105-
}
106-
return pluginOptions;
107-
}
108-
10986
/**
11087
* Creates a record about us having visited a valid React component.
11188
* Such records will later be merged into a single object.
@@ -118,15 +95,15 @@ export default function ({ Plugin, types: t }) {
11895

11996
let props = [];
12097
if (typeof displayName === 'string') {
121-
props.push(t.property('init',
98+
props.push(t.objectProperty(
12299
t.identifier('displayName'),
123-
t.literal(displayName)
100+
t.stringLiteral(displayName)
124101
));
125102
}
126103
if (state[depthKey] > 0) {
127-
props.push(t.property('init',
104+
props.push(t.objectProperty(
128105
t.identifier('isInFunction'),
129-
t.literal(true)
106+
t.booleanLiteral(true)
130107
));
131108
}
132109

@@ -140,7 +117,7 @@ export default function ({ Plugin, types: t }) {
140117
function addComponentRecord(node, scope, file, state) {
141118
const [uniqueId, definition] = createComponentRecord(node, scope, file, state);
142119
state[recordsKey] = state[recordsKey] || [];
143-
state[recordsKey].push(t.property('init',
120+
state[recordsKey].push(t.objectProperty(
144121
t.identifier(uniqueId),
145122
definition
146123
));
@@ -172,21 +149,20 @@ export default function ({ Plugin, types: t }) {
172149
* Imports and calls a particular transformation target function.
173150
* You may specify several such transformations, so they are handled separately.
174151
*/
175-
function defineInitTransformCall(scope, file, recordsId, targetOptions) {
152+
function defineInitTransformCall(scope, { file }, recordsId, targetOptions) {
176153
const id = scope.generateUidIdentifier('reactComponentWrapper');
177154
const { transform, imports = [], locals = [] } = targetOptions;
178155
const { filename } = file.opts;
179-
180156
return [id, t.variableDeclaration('var', [
181157
t.variableDeclarator(id,
182158
t.callExpression(file.addImport(transform), [
183159
t.objectExpression([
184-
t.property('init', t.identifier('filename'), t.literal(filename)),
185-
t.property('init', t.identifier('components'), recordsId),
186-
t.property('init', t.identifier('locals'), t.arrayExpression(
160+
t.objectProperty(t.identifier('filename'), t.stringLiteral(filename)),
161+
t.objectProperty(t.identifier('components'), recordsId),
162+
t.objectProperty(t.identifier('locals'), t.arrayExpression(
187163
locals.map(local => t.identifier(local))
188164
)),
189-
t.property('init', t.identifier('imports'), t.arrayExpression(
165+
t.objectProperty(t.identifier('imports'), t.arrayExpression(
190166
imports.map(imp => file.addImport(imp, null, 'absolute'))
191167
))
192168
])
@@ -216,21 +192,21 @@ export default function ({ Plugin, types: t }) {
216192
);
217193
}
218194

219-
return new Plugin('babel-plugin-react-transform', {
195+
return {
220196
visitor: {
221197
Function: {
222-
enter(node, parent, scope, file) {
198+
enter({ node, parent, scope }, file) {
223199
if (!this.state[depthKey]) {
224200
this.state[depthKey] = 0;
225201
}
226202
this.state[depthKey]++;
227203
},
228-
exit(node, parent, scope, file) {
204+
exit({ node, parent, scope }, file) {
229205
this.state[depthKey]--;
230206
}
231207
},
232208

233-
Class(node, parent, scope, file) {
209+
Class({ node, scope }, file) {
234210
if (!isComponentishClass(node)) {
235211
return;
236212
}
@@ -240,12 +216,12 @@ export default function ({ Plugin, types: t }) {
240216

241217
node.decorators = node.decorators || [];
242218
node.decorators.push(t.decorator(
243-
t.callExpression(wrapReactComponentId, [t.literal(uniqueId)])
219+
t.callExpression(wrapReactComponentId, [t.stringLiteral(uniqueId)])
244220
));
245221
},
246222

247223
CallExpression: {
248-
exit(node, parent, scope, file) {
224+
exit({ node, scope }, file) {
249225
const { isCreateClassCallExpression } = this.state[cacheKey];
250226
if (!isCreateClass(node, isCreateClassCallExpression)) {
251227
return;
@@ -255,25 +231,34 @@ export default function ({ Plugin, types: t }) {
255231
const uniqueId = addComponentRecord(node, scope, file, this.state);
256232

257233
return t.callExpression(
258-
t.callExpression(wrapReactComponentId, [t.literal(uniqueId)]),
234+
t.callExpression(wrapReactComponentId, [t.stringLiteral(uniqueId)]),
259235
[node]
260236
);
261237
}
262238
},
263239

264240
Program: {
265-
enter(node, parent, scope, file) {
266-
const options = getPluginOptions(file);
267-
const factoryMethods = options.factoryMethods || ['React.createClass', 'createClass'];
268-
this.state[optionsKey] = options;
241+
enter({ scope }, file) {
242+
const { opts } = file;
243+
if (!isValidOptions(opts)) {
244+
throw new Error(
245+
'babel-plugin-react-transform requires that you specify options in .babelrc ' +
246+
'or in your Babel Node API call options, and that it is an object with ' +
247+
'a transforms property which is an array.'
248+
);
249+
}
250+
const factoryMethods = opts.factoryMethods || ['React.createClass', 'createClass'];
251+
252+
this.state = {};
253+
this.state[optionsKey] = opts;
269254
this.state[cacheKey] = {
270255
isCreateClassCallExpression: buildIsCreateClassCallExpression(factoryMethods),
271256
};
272257

273258
this.state[wrapComponentIdKey] = scope.generateUidIdentifier('wrapComponent');
274259
},
275260

276-
exit(node, parent, scope, file) {
261+
exit({ node, scope }, file) {
277262
if (!foundComponentRecords(this.state)) {
278263
return;
279264
}
@@ -293,7 +278,6 @@ export default function ({ Plugin, types: t }) {
293278
// Create one uber function calling each transformation
294279
const wrapComponentId = this.state[wrapComponentIdKey];
295280
const wrapComponent = defineWrapComponent(wrapComponentId, initTransformIds);
296-
297281
return t.program([
298282
recordsVar,
299283
...initTransformVars,
@@ -303,5 +287,5 @@ export default function ({ Plugin, types: t }) {
303287
}
304288
}
305289
}
306-
});
290+
};
307291
}

0 commit comments

Comments
 (0)