babel-plugin-cerebral
Advanced tools
Comparing version 1.0.0-1526409797341 to 1.0.0-1527015081471
137
lib/index.js
@@ -14,9 +14,18 @@ 'use strict'; | ||
// if nothing was found | ||
function getUsedVariable(scope, name, importedVariable) { | ||
function getUsedVariable(scope, name) { | ||
var binding = scope.getBinding(name); | ||
if (!binding) return null; | ||
if (t.isVariableDeclarator(binding.path.node) && binding.path.node.init) { | ||
return getUsedVariable(binding.path.scope, binding.path.node.init.name, importedVariable); | ||
if (!binding) return {}; | ||
if (binding.path.getData('cerebral_proxy_tagged')) { | ||
return { | ||
tagged: true, | ||
name: name | ||
}; | ||
} else if (t.isVariableDeclarator(binding.path.node) && binding.path.node.init) { | ||
return getUsedVariable(binding.path.scope, binding.path.node.init.name); | ||
} | ||
return importedVariable.has(name) && binding.kind === 'module' ? name : null; | ||
return { | ||
tagged: false, | ||
name: binding.kind === 'module' ? name : null | ||
}; | ||
} | ||
@@ -29,2 +38,3 @@ | ||
this.importedVariable = new Set(); | ||
this.namespace = null; | ||
}, | ||
@@ -47,3 +57,5 @@ | ||
var indirectImport = isIndirectImport(importLocation); // 2) | ||
var specifiers = path.node.specifiers; | ||
if (directImport || indirectImport) { | ||
@@ -53,3 +65,3 @@ // 1,2) a. Trigger conversion to tags | ||
// Complain about default import | ||
if (path.node.specifiers.some(function (item) { | ||
if (specifiers.some(function (item) { | ||
return t.isImportDefaultSpecifier(item); | ||
@@ -60,34 +72,37 @@ })) { | ||
// Verify that all imports are allowed and track the localName | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
if (specifiers.length === 1 && t.isImportNamespaceSpecifier(specifiers[0])) { | ||
this.importedVariable = new Set(allowedImportVariables); | ||
this.namespace = specifiers[0].local.name; | ||
} else { | ||
// Verify that all imports are allowed and track the localName | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = path.node.specifiers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _step$value = _step.value, | ||
importName = _step$value.imported.name, | ||
localName = _step$value.local.name; | ||
try { | ||
for (var _iterator = path.node.specifiers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _step$value = _step.value, | ||
importName = _step$value.imported.name, | ||
localName = _step$value.local.name; | ||
if (isAllowedImport(importName)) { | ||
this.importedVariable.add(localName); | ||
} else if (directImport) { | ||
// Only complain about unkown imports | ||
// if it is a directImport | ||
throw path.buildCodeFrameError('"' + importName + '" is not a valid import'); | ||
if (isAllowedImport(importName)) { | ||
this.importedVariable.add(localName); | ||
} else if (directImport) { | ||
// Only complain about unkown imports | ||
// if it is a directImport | ||
throw path.buildCodeFrameError('"' + importName + '" is not a valid import'); | ||
} | ||
} | ||
} | ||
// 1) b. Rewrite import to cerebral/tags | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
@@ -97,2 +112,3 @@ } | ||
// 1) b. Rewrite import to cerebral/tags | ||
if (directImport) { | ||
@@ -103,4 +119,4 @@ // Change import to the 'cerebral/tags' | ||
// Remove "path" from imports | ||
path.node.specifiers = path.node.specifiers.filter(function (item) { | ||
return item.imported.name !== 'statePath' && item.imported.name !== 'computedPath'; | ||
path.node.specifiers = specifiers.filter(function (item) { | ||
return t.isImportNamespaceSpecifier(item) || item.imported.name !== 'statePath' && item.imported.name !== 'computedPath'; | ||
}); | ||
@@ -118,10 +134,39 @@ | ||
// Always use the innermost MemberExpression | ||
if (!t.isIdentifier(path.node.object)) { | ||
// and don't process taggedTemplateExpressions | ||
if (!t.isIdentifier(path.node.object) || t.isTaggedTemplateExpression(path.parent)) { | ||
return; | ||
} | ||
var tagName = getUsedVariable(path.scope, path.node.object.name, this.importedVariable); | ||
var objectName = path.node.object.name; | ||
if (!tagName) { | ||
return; | ||
var _getUsedVariable = getUsedVariable(path.scope, objectName), | ||
tagged = _getUsedVariable.tagged, | ||
tagRoot = _getUsedVariable.name; | ||
// Contains the target of the taggedTemplate | ||
// either e.g. `state` or proxies.state | ||
var targetExpression = void 0; | ||
var tagName = tagRoot; | ||
// Is tagRoot a reference to the namespace e.g. proxies.state | ||
if (this.namespace && tagRoot === this.namespace) { | ||
tagName = path.node.property.name; | ||
// Show compile time warning for | ||
// unkown properties | ||
if (!this.importedVariable.has(tagName)) { | ||
throw path.get('property').buildCodeFrameError('"' + tagName + '" is not a valid tag'); | ||
} | ||
targetExpression = t.memberExpression(t.identifier(objectName), t.identifier(tagName)); | ||
path = path.parentPath; | ||
} else { | ||
// if this variable was tagged before | ||
// we don't want to bail here | ||
if (!tagged && !this.importedVariable.has(tagRoot)) { | ||
return; | ||
} | ||
targetExpression = t.identifier(tagRoot); | ||
} | ||
@@ -166,5 +211,11 @@ | ||
// Bail on proxies.state usage | ||
if (quasis.length === 1 && quasis[0].length === 0) { | ||
// but tag the path to detect it in getUsedVariable | ||
path.setData('cerebral_proxy_tagged', true); | ||
return; | ||
} | ||
// Rewrite with a tagged template like | ||
// state`foo.bar` | ||
rootMemberExpression.replaceWith(t.taggedTemplateExpression(t.identifier(tagName), t.templateLiteral(quasis.map(function (v) { | ||
// state`foo.bar` or proxies.state`foo.bar` | ||
rootMemberExpression.replaceWith(t.taggedTemplateExpression(targetExpression, t.templateLiteral(quasis.map(function (v) { | ||
var str = v.join(''); | ||
@@ -179,3 +230,3 @@ return t.templateElement({ raw: str, cooked: str }); | ||
var allowedImportPath = ['cerebral/proxy']; | ||
var allowedImportVariables = ['props', 'state', 'signals', 'computed', 'moduleState', 'moduleComputed', 'moduleSignals']; | ||
var allowedImportVariables = ['path', 'props', 'state', 'sequences', 'computed', 'moduleState', 'moduleComputed', 'moduleSequences']; | ||
@@ -182,0 +233,0 @@ function isDirectImport(location) { |
{ | ||
"name": "babel-plugin-cerebral", | ||
"version": "1.0.0-1526409797341", | ||
"version": "1.0.0-1527015081471", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9553
197