@effector/babel-plugin-react
Advanced tools
Comparing version 0.2.0 to 0.2.1
119
index.js
@@ -7,2 +7,3 @@ 'use strict'; | ||
const componentCreators = new Set(options.componentCreators || [ 'createComponent' ]); | ||
const enableFileName = typeof options.filename === 'undefined' ? true : Boolean(options.filename); | ||
const {types: t} = babel; | ||
@@ -21,10 +22,24 @@ return { | ||
}, | ||
CallExpression(path) { | ||
CallExpression(path, state) { | ||
if (!state.fileNameIdentifier) { | ||
const fileName = enableFileName ? state.filename || '' : ''; | ||
const fileNameIdentifier = path.scope.generateUidIdentifier('_effectorFileName'); | ||
const scope = path.hub.getScope(); | ||
if (scope) { | ||
scope.push({ | ||
id: fileNameIdentifier, | ||
init: t.stringLiteral(fileName) | ||
}); | ||
} | ||
state.fileNameIdentifier = fileNameIdentifier; | ||
} | ||
if (t.isIdentifier(path.node.callee)) { | ||
if (componentCreators.has(path.node.callee.name)) { | ||
const id = findCandidateNameForExpression(path); | ||
if (id) { | ||
setDisplayNameAfter(path, id, babel.types); | ||
} | ||
if (id) setComponentNameAfter(path, state, id, babel.types); | ||
} | ||
if (path.node.callee.name === 'useStore') { | ||
const id = findComponentName(path); | ||
if (id) setUseStoreNameAfter(path, state, id, babel.types); | ||
} | ||
} | ||
@@ -53,20 +68,98 @@ } | ||
function setDisplayNameAfter(path, nameNodeId, t, displayName) { | ||
function isComponentName(node) { | ||
if (node && node.type === 'Identifier') { | ||
return !/^[a-z]/.test(node.name); | ||
} else { | ||
return false; | ||
} | ||
} | ||
function findComponentName(path) { | ||
let id; | ||
path.find(path => { | ||
if (isComponentName(path.node.id)) { | ||
id = path.node.id; | ||
} | ||
if (id) return true; | ||
}); | ||
return id; | ||
} | ||
function makeTrace(fileNameIdentifier, lineNumber, columnNumber, t) { | ||
const fileLineLiteral = lineNumber != null ? t.numericLiteral(lineNumber) : t.numericLiteral(-1); | ||
const fileColumnLiteral = columnNumber != null ? t.numericLiteral(columnNumber) : t.numericLiteral(-1); | ||
const fileProperty = t.objectProperty(t.identifier('file'), fileNameIdentifier); | ||
const lineProperty = t.objectProperty(t.identifier('line'), fileLineLiteral); | ||
const columnProperty = t.objectProperty(t.identifier('column'), fileColumnLiteral); | ||
return t.objectExpression([ fileProperty, lineProperty, columnProperty ]); | ||
} | ||
function setComponentNameAfter(path, state, nameNodeId, t) { | ||
let displayName; | ||
if (!displayName) { | ||
displayName = nameNodeId.name; | ||
} | ||
let blockLevelStmnt; | ||
let args; | ||
let loc; | ||
path.find(path => { | ||
if (path.parentPath.isBlock()) { | ||
blockLevelStmnt = path; | ||
if (path.isCallExpression()) { | ||
args = path.node.arguments; | ||
loc = path.node.loc.start; | ||
return true; | ||
} | ||
}); | ||
if (blockLevelStmnt && displayName) { | ||
const trailingComments = blockLevelStmnt.node.trailingComments; | ||
delete blockLevelStmnt.node.trailingComments; | ||
const setDisplayNameStmn = t.expressionStatement(t.assignmentExpression('=', t.memberExpression(nameNodeId, t.identifier('displayName')), t.stringLiteral(displayName))); | ||
blockLevelStmnt.insertAfter(setDisplayNameStmn); | ||
if (args && displayName) { | ||
const oldConfig = args[2]; | ||
const configExpr = t.objectExpression([]); | ||
const nameProp = t.objectProperty(t.identifier('name'), t.stringLiteral(displayName)); | ||
const locProp = t.objectProperty(t.identifier('loc'), makeTrace(state.fileNameIdentifier, loc.line, loc.column, t)); | ||
const stableID = t.objectProperty(t.identifier('sid'), t.stringLiteral(generateStableID(state.file.opts.root, state.filename, displayName, loc.line, loc.column))); | ||
if (!args[0]) return; | ||
args[2] = configExpr; | ||
if (oldConfig) { | ||
args[2].properties.push(t.objectProperty(t.identifier('ɔ'), oldConfig)); | ||
} | ||
args[2].properties.push(locProp); | ||
args[2].properties.push(nameProp); | ||
args[2].properties.push(stableID); | ||
} | ||
} | ||
function setUseStoreNameAfter(path, state, nameNodeId, t) { | ||
let displayName; | ||
if (!displayName) { | ||
displayName = nameNodeId.name; | ||
} | ||
let args; | ||
let loc; | ||
path.find(path => { | ||
if (path.isCallExpression()) { | ||
args = path.node.arguments; | ||
loc = path.node.loc.start; | ||
return true; | ||
} | ||
}); | ||
if (args && displayName) { | ||
const oldConfig = args[1]; | ||
const configExpr = t.objectExpression([]); | ||
const nameProp = t.objectProperty(t.identifier('name'), t.stringLiteral(displayName)); | ||
const locProp = t.objectProperty(t.identifier('loc'), makeTrace(state.fileNameIdentifier, loc.line, loc.column, t)); | ||
const stableID = t.objectProperty(t.identifier('sid'), t.stringLiteral(generateStableID(state.file.opts.root, state.filename, displayName, loc.line, loc.column))); | ||
if (!args[0]) return; | ||
args[1] = configExpr; | ||
if (oldConfig) { | ||
args[1].properties.push(t.objectProperty(t.identifier('ɔ'), oldConfig)); | ||
} | ||
args[1].properties.push(locProp); | ||
args[1].properties.push(nameProp); | ||
args[1].properties.push(stableID); | ||
} | ||
} | ||
function generateStableID(babelRoot, fileName, varName, line, column) { | ||
const {sep, normalize} = require('path'); | ||
const rawPath = fileName.replace(babelRoot, ''); | ||
const normalizedPath = normalize(rawPath).split(sep).join('/'); | ||
return varName + " " + normalizedPath + " [" + line + ", " + column + "]"; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@effector/babel-plugin-react", | ||
"description": "Add displayName to createComponent call", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"repository": "https://github.com/zerobias/effector", | ||
@@ -6,0 +6,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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7787
153
0