babel-plugin-react-cssmoduleify
Advanced tools
Comparing version 1.0.0-beta.5 to 1.0.0-beta.7
@@ -33,8 +33,7 @@ "use strict"; | ||
/* eslint no-extra-parens: 0 */ | ||
var logOnce = function logOnce(fnName, node, path) { | ||
console.log("logOnce", node); | ||
var LogOnceImplementation = function LogOnceImplementation(fnName, node, path) { | ||
var name = fnName + "-" + node.type; | ||
if (!LOG_CACHE[name]) { | ||
LOG_CACHE[name] = true; | ||
console.log("TODO(%s): handle %s.\n%s\n", fnName, node.type, (0, _babelGenerator2.default)(path ? path.node : node).code); | ||
console.log("babel-plugin-react-cssmoduleify WARNING(%s): unhandled node type `%s`.\n%s\n", fnName, node.type, (0, _babelGenerator2.default)(path ? path.node : node).code); | ||
} | ||
@@ -52,2 +51,5 @@ }; | ||
var t = _ref.types; | ||
// eslint-disable-line | ||
// by default logging is a noop, but it is configurable | ||
var logOnce = function logOnce() {}; | ||
@@ -170,2 +172,3 @@ var ROOT_CSSNAMES_IDENTIFIER = "cssmodule"; | ||
var replaceIdentifier = function replaceIdentifier(path, cssmodule) { | ||
// eslint-disable-line | ||
var binding = path.scope.getBinding(path.node.name); | ||
@@ -179,27 +182,29 @@ | ||
// if there is only one reference, we can mutate that directly | ||
if (binding.references === 1) { | ||
if (t.isVariableDeclarator(binding.path)) { | ||
var sourceNode = binding.path.get("init"); | ||
if (t.isNullLiteral(sourceNode)) { | ||
return; | ||
} else if (t.isCallExpression(sourceNode)) { | ||
sourceNode.get("argument").forEach(function (arg) { | ||
if (t.isObjectExpression(arg)) { | ||
arg.get("properties").forEach(updateProperty); | ||
} | ||
}); | ||
} else if (t.isObjectExpression(sourceNode)) { | ||
sourceNode.get("properties").forEach(updateProperty); | ||
} else if (t.isIdentifier(sourceNode)) { | ||
replaceIdentifier(sourceNode, cssmodule); | ||
} else { | ||
// updateClassName(binding.path.get("init"), cssmodule); // eslint-disable-line | ||
} | ||
if (t.isVariableDeclarator(binding.path)) { | ||
// if there is only one reference, we can mutate that directly | ||
// we're assuming a props identifier is only used in local props so this | ||
// is technically a destructive transform. | ||
if (binding.references > 1) { | ||
logOnce("replaceIdentifier", { type: "with multiple references" }, binding.path); | ||
} | ||
var sourceNode = binding.path.get("init"); | ||
if (t.isNullLiteral(sourceNode)) { | ||
return; | ||
} else if (t.isCallExpression(sourceNode)) { | ||
sourceNode.get("arguments").forEach(function (arg) { | ||
if (t.isObjectExpression(arg)) { | ||
arg.get("properties").forEach(updateProperty); | ||
} | ||
}); | ||
} else if (t.isObjectExpression(sourceNode)) { | ||
sourceNode.get("properties").forEach(updateProperty); | ||
} else if (t.isIdentifier(sourceNode)) { | ||
replaceIdentifier(sourceNode, cssmodule); | ||
} else { | ||
logOnce("replaceIdentifier[maybe]", binding.path); | ||
} | ||
updateClassName(sourceNode, cssmodule); // eslint-disable-line | ||
} | ||
} else { | ||
console.warn("TODO: replaceIdentifier multiple references"); | ||
} | ||
logOnce("replaceIdentifier[maybe]", binding.path.node, binding.path); | ||
} | ||
}; | ||
@@ -229,4 +234,4 @@ | ||
var binding = value.scope.getBinding(value.node.name); | ||
if (t.isVariableDeclarator(binding.path.node)) { | ||
console.log("updateClassName with variablie declarator init"); | ||
updateClassName(binding.path.get("init"), cssmodule); | ||
@@ -334,2 +339,6 @@ } else { | ||
enter: function enter(path, state) { | ||
if (state.opts.log) { | ||
logOnce = LogOnceImplementation; | ||
} | ||
if (!state.opts.path || new RegExp(state.opts.path).test(state.file.opts.filename)) { | ||
@@ -336,0 +345,0 @@ // detect if this is likely compiled source |
{ | ||
"name": "babel-plugin-react-cssmoduleify", | ||
"version": "1.0.0-beta.5", | ||
"version": "1.0.0-beta.7", | ||
"description": "Babel plugin to transform traditional classNames to CSS Modules", | ||
@@ -12,3 +12,3 @@ "main": "lib/index.js", | ||
"compile-tests": "babel --presets es2015,react test/fixtures/jsx/*/ --out-dir test/fixtures/compiled", | ||
"pretest": "npm run build && npm run compile-tests", | ||
"pretest": "npm run build", | ||
"test": "mocha ./test/index.js" | ||
@@ -15,0 +15,0 @@ }, |
@@ -36,3 +36,4 @@ # babel-plugin-react-cssmoduleify | ||
"cssmodule": "client/styles/base.styl" | ||
"modules": "es6" | ||
"modules": "es6", | ||
"log": true | ||
}] | ||
@@ -48,2 +49,3 @@ ], | ||
* `modules`: `"es6"|"commonjs"` the type of module system cssmodule should be required as. | ||
* `log`: `boolean` log potentially unhandled cases. Default to false. | ||
@@ -50,0 +52,0 @@ ## Examples |
@@ -32,4 +32,3 @@ /* eslint no-extra-parens: 0 */ | ||
**/ | ||
const logOnce = (fnName, node, path) => { | ||
console.log("logOnce", node); | ||
const LogOnceImplementation = (fnName, node, path) => { | ||
const name = `${fnName}-${node.type}`; | ||
@@ -39,3 +38,3 @@ if (!LOG_CACHE[name]) { | ||
console.log( | ||
"TODO(%s): handle %s.\n%s\n", | ||
"babel-plugin-react-cssmoduleify WARNING(%s): unhandled node type `%s`.\n%s\n", | ||
fnName, | ||
@@ -53,3 +52,6 @@ node.type, | ||
export default ({types: t}) => { | ||
export default ({types: t}) => { // eslint-disable-line | ||
// by default logging is a noop, but it is configurable | ||
let logOnce = () => {}; | ||
const ROOT_CSSNAMES_IDENTIFIER = "cssmodule"; | ||
@@ -192,3 +194,3 @@ const BAIL_OUT = "__dontTransformMe"; | ||
*/ | ||
const replaceIdentifier = (path, cssmodule) => { | ||
const replaceIdentifier = (path, cssmodule) => { // eslint-disable-line | ||
const binding = path.scope.getBinding(path.node.name); | ||
@@ -202,26 +204,28 @@ | ||
// if there is only one reference, we can mutate that directly | ||
if (binding.references === 1) { | ||
if (t.isVariableDeclarator(binding.path)) { | ||
const sourceNode = binding.path.get("init"); | ||
if (t.isNullLiteral(sourceNode)) { | ||
return; | ||
} else if (t.isCallExpression(sourceNode)) { | ||
sourceNode.get("argument").forEach((arg) => { | ||
if (t.isObjectExpression(arg)) { | ||
arg.get("properties").forEach(updateProperty); | ||
} | ||
}); | ||
} else if (t.isObjectExpression(sourceNode)) { | ||
sourceNode.get("properties").forEach(updateProperty); | ||
} else if (t.isIdentifier(sourceNode)) { | ||
replaceIdentifier(sourceNode, cssmodule); | ||
} else { | ||
// updateClassName(binding.path.get("init"), cssmodule); // eslint-disable-line | ||
} | ||
if (t.isVariableDeclarator(binding.path)) { | ||
// if there is only one reference, we can mutate that directly | ||
// we're assuming a props identifier is only used in local props so this | ||
// is technically a destructive transform. | ||
if (binding.references > 1) { | ||
logOnce("replaceIdentifier", {type: "with multiple references"}, binding.path); | ||
} | ||
const sourceNode = binding.path.get("init"); | ||
if (t.isNullLiteral(sourceNode)) { | ||
return; | ||
} else if (t.isCallExpression(sourceNode)) { | ||
sourceNode.get("arguments").forEach((arg) => { | ||
if (t.isObjectExpression(arg)) { | ||
arg.get("properties").forEach(updateProperty); | ||
} | ||
}); | ||
} else if (t.isObjectExpression(sourceNode)) { | ||
sourceNode.get("properties").forEach(updateProperty); | ||
} else if (t.isIdentifier(sourceNode)) { | ||
replaceIdentifier(sourceNode, cssmodule); | ||
} else { | ||
logOnce("replaceIdentifier[maybe]", binding.path); | ||
updateClassName(sourceNode, cssmodule); // eslint-disable-line | ||
} | ||
} else { | ||
console.warn("TODO: replaceIdentifier multiple references"); | ||
logOnce("replaceIdentifier[maybe]", binding.path.node, binding.path); | ||
} | ||
@@ -273,4 +277,4 @@ }; | ||
const binding = value.scope.getBinding(value.node.name); | ||
if (t.isVariableDeclarator(binding.path.node)) { | ||
console.log("updateClassName with variablie declarator init"); | ||
updateClassName(binding.path.get("init"), cssmodule); | ||
@@ -383,2 +387,6 @@ } else { | ||
enter(path, state:State) { | ||
if (state.opts.log) { | ||
logOnce = LogOnceImplementation; | ||
} | ||
if (!state.opts.path || new RegExp(state.opts.path).test(state.file.opts.filename)) { | ||
@@ -385,0 +393,0 @@ // detect if this is likely compiled source |
33159
705
65
19