babel-plugin-ko-component-flatten
Advanced tools
Comparing version 0.0.5 to 0.0.6
59
index.js
@@ -34,3 +34,3 @@ var template = require('babel-template'); | ||
// Flatten object exp into const declarations | ||
var flattenObjectExp = function (objectExpNode) { | ||
var parseObjectExp = function (objectExpNode) { | ||
return objectExpNode.properties.map(function (node) { | ||
@@ -46,4 +46,4 @@ var keyword = node.key.name; | ||
// Flatten export named spcifier node | ||
var flattenSpecifierExp = function (node) { | ||
// Flatten export named specifier node | ||
var parseSpecifierExp = function (node) { | ||
return constDeclarationTpl({ | ||
@@ -55,2 +55,18 @@ NAME: node.exported, | ||
// Parse identifier declarartion | ||
var parseIdentifierExp = function (node) { | ||
return constDeclarationTpl({ | ||
NAME: t.identifier(node.name), | ||
VALUE: t.identifier(node.name) | ||
}); | ||
}; | ||
// Parse function declarartion | ||
var parseFunctionExp = function (node) { | ||
return constDeclarationTpl({ | ||
NAME: t.identifier(node.id.name), | ||
VALUE: t.functionExpression(node.id, node.params, node.body) | ||
}); | ||
}; | ||
// Detect whether if a proper component module | ||
@@ -84,8 +100,10 @@ var isComponent = function (node) { | ||
var specifierVisitor = { | ||
ExportSpecifier: function (path) { | ||
var key = path.node.local.name; | ||
ExportSpecifier: { | ||
enter: function (path) { | ||
var key = path.node.local.name; | ||
if (isReserved(key)) { | ||
props[key] = true; | ||
componentDeclarations.push(flattenSpecifierExp(path.node)); | ||
if (isReserved(key)) { | ||
props[key] = true; | ||
componentDeclarations.push(parseSpecifierExp(path.node)); | ||
} | ||
} | ||
@@ -114,3 +132,3 @@ } | ||
componentDeclarations = componentDeclarations.concat( | ||
flattenObjectExp(nodeDeclaration) | ||
parseObjectExp(nodeDeclaration) | ||
); | ||
@@ -120,8 +138,15 @@ break; | ||
case 'Identifier': | ||
componentDeclarations.push(parseIdentifierExp(nodeDeclaration)); | ||
break; | ||
case 'FunctionDeclaration': | ||
break; | ||
if (!nodeDeclaration.id.name) { | ||
nodeDeclaration.id.name = 'constructor'; | ||
} | ||
case 'StringLiteral': | ||
if (nodeDeclaration.id.name === 'constructor') { | ||
componentDeclarations.push(parseFunctionExp(nodeDeclaration)); | ||
defaultPath.node.declaration = t.identifier('constructor'); | ||
} | ||
break; | ||
@@ -133,2 +158,4 @@ } | ||
exit: function (path) { | ||
var defaultDeclarartion = null; | ||
if (isComponent() && defaultPath) { | ||
@@ -139,6 +166,10 @@ componentDeclarations.forEach(function (declaration) { | ||
defaultDeclarartion = defaultPath.node.declaration; | ||
// Sanitize export default object | ||
defaultPath.node.declaration.properties.forEach(function (prop) { | ||
prop.value = t.Identifier(prop.key.name); | ||
}); | ||
if (defaultDeclarartion.properties) { | ||
defaultDeclarartion.properties.forEach(function (prop) { | ||
prop.value = t.Identifier(prop.key.name); | ||
}); | ||
} | ||
} | ||
@@ -145,0 +176,0 @@ |
{ | ||
"name": "babel-plugin-ko-component-flatten", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Flatten ExportDefaultDeclaration", | ||
@@ -5,0 +5,0 @@ "main": "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
5582
147
2