Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-react-component-data-attribute

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-react-component-data-attribute - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

87

lib/index.js

@@ -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>",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc