babel-plugin-react-component-data-attribute
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -32,3 +32,21 @@ 'use strict'; | ||
function shouldProcessPossibleComponent(path, state) { | ||
function isExported(path, name) { | ||
if (path.parentPath.isExportDefaultDeclaration() || path.parentPath.isExportNamedDeclaration()) { | ||
return true; | ||
} | ||
const binding = path.scope.getBinding(name); | ||
return binding ? binding.referencePaths.some(referencePath => referencePath.getAncestry().some(ancestorPath => ancestorPath.isExportDefaultDeclaration() || ancestorPath.isExportSpecifier() || ancestorPath.isExportNamedDeclaration())) : false; | ||
} | ||
function evaluatePotentialComponent(path, state) { | ||
const name = nameForReactComponent(path, state.file); | ||
return { | ||
name: name || '', | ||
process: name != null && shouldProcessPotentialComponent(path, name, state) | ||
}; | ||
} | ||
function shouldProcessPotentialComponent(path, name, state) { | ||
if (!path.getFunctionParent().isProgram()) { | ||
@@ -58,4 +76,3 @@ return false; | ||
const statementParent = path.getStatementParent(); | ||
return statementParent.isExportDefaultDeclaration() || statementParent.isExportNamedDeclaration(); | ||
return isExported(path, name); | ||
} | ||
@@ -144,3 +161,3 @@ | ||
const renderMethodVisitor = { | ||
const functionVisitor = { | ||
ReturnStatement(path, { name, source }) { | ||
@@ -161,37 +178,43 @@ const arg = path.get('argument'); | ||
return { | ||
name: 'babel-plugin-react-component-data-attribute', | ||
visitor: { | ||
'ClassDeclaration|ClassExpression': (path, state) => { | ||
if (!shouldProcessPossibleComponent(path, state)) { | ||
return; | ||
} | ||
const programVisitor = { | ||
'ClassDeclaration|ClassExpression': (path, state) => { | ||
var _evaluatePotentialCom = evaluatePotentialComponent(path, state); | ||
const name = nameForReactComponent(path, state.file); | ||
if (name == null) { | ||
return; | ||
} | ||
const name = _evaluatePotentialCom.name, | ||
process = _evaluatePotentialCom.process; | ||
path.get('body.body').filter(bodyPath => bodyPath.isClassMethod() && bodyPath.get('key').isIdentifier({ name: 'render' })).forEach(renderPath => { | ||
renderPath.traverse(renderMethodVisitor, { name, source: renderPath }); | ||
}); | ||
}, | ||
'FunctionDeclaration|FunctionExpression|ArrowFunctionExpression': (path, state) => { | ||
if (!shouldProcessPossibleComponent(path, state)) { | ||
return; | ||
} | ||
if (!process) { | ||
return; | ||
} | ||
const name = nameForReactComponent(path, state.file); | ||
if (name == null) { | ||
return; | ||
} | ||
path.get('body.body').filter(bodyPath => bodyPath.isClassMethod() && bodyPath.get('key').isIdentifier({ name: 'render' })).forEach(renderPath => { | ||
renderPath.traverse(functionVisitor, { name, source: renderPath }); | ||
}); | ||
}, | ||
'FunctionDeclaration|FunctionExpression|ArrowFunctionExpression': (path, state) => { | ||
var _evaluatePotentialCom2 = evaluatePotentialComponent(path, state); | ||
if (path.isArrowFunctionExpression() && !path.get('body').isBlockStatement()) { | ||
path.traverse(returnStatementVisitor, { name, source: path }); | ||
} else { | ||
path.traverse(renderMethodVisitor, { name, source: path }); | ||
} | ||
const name = _evaluatePotentialCom2.name, | ||
process = _evaluatePotentialCom2.process; | ||
if (!process) { | ||
return; | ||
} | ||
if (path.isArrowFunctionExpression() && !path.get('body').isBlockStatement()) { | ||
path.traverse(returnStatementVisitor, { name, source: path }); | ||
} else { | ||
path.traverse(functionVisitor, { name, source: path }); | ||
} | ||
} | ||
}; | ||
return { | ||
name: 'babel-plugin-react-component-data-attribute', | ||
visitor: { | ||
Program(path, state) { | ||
path.traverse(programVisitor, state); | ||
} | ||
} | ||
}; | ||
} |
{ | ||
"name": "babel-plugin-react-component-data-attribute", | ||
"description": "Babel plugin to add a data attribute containing its component’s name to the top-level DOM node", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"main": "lib/index.js", | ||
@@ -6,0 +6,0 @@ "author": "Chris Sauve <chrismsauve@gmail.com>", |
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
9564
173