@mattsjones/css-babel-plugin
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,20 +0,20 @@ | ||
import { types as t, NodePath } from '@babel/core'; | ||
import { Options } from './types'; | ||
declare type Context = { | ||
filename: string; | ||
hasTreatImports: boolean; | ||
}; | ||
export default function (): { | ||
pre(this: Context, state: any): void; | ||
visitor: { | ||
ImportDeclaration(this: Context, path: NodePath<t.ImportDeclaration>, { opts }: { | ||
opts: Options; | ||
}): void; | ||
Program: { | ||
exit(this: Context, path: NodePath<t.Program>, { opts }: { | ||
opts: Options; | ||
}): void; | ||
}; | ||
import { PluginObj, PluginPass } from '@babel/core'; | ||
declare const exportConfig: { | ||
style: { | ||
maxParams: number; | ||
}; | ||
createTheme: { | ||
maxParams: number; | ||
}; | ||
}; | ||
declare type RelevantExport = keyof typeof exportConfig; | ||
interface PluginOptions { | ||
alias?: string; | ||
} | ||
declare type Context = PluginPass & { | ||
opts?: PluginOptions; | ||
namespaceImport: string; | ||
importIdentifiers: Map<string, RelevantExport>; | ||
}; | ||
export default function (): PluginObj<Context>; | ||
export {}; |
@@ -7,38 +7,117 @@ 'use strict'; | ||
const exportConfig = { | ||
style: { | ||
maxParams: 2 | ||
}, | ||
createTheme: { | ||
maxParams: 3 | ||
} | ||
}; | ||
const relevantExports = Object.keys(exportConfig); | ||
const extractName = node => { | ||
if (core.types.isObjectProperty(node) && core.types.isIdentifier(node.key)) { | ||
return node.key.name; | ||
} else if ((core.types.isVariableDeclarator(node) || core.types.isFunctionDeclaration(node)) && core.types.isIdentifier(node.id)) { | ||
return node.id.name; | ||
} else if (core.types.isExportDefaultDeclaration(node)) { | ||
return 'default'; | ||
} | ||
}; | ||
const getDebugId = path => { | ||
const { | ||
parent | ||
} = path; | ||
if (core.types.isObjectProperty(parent) || core.types.isReturnStatement(parent) || core.types.isArrayExpression(parent) || core.types.isSpreadElement(parent)) { | ||
const names = []; | ||
path.findParent(({ | ||
node: parentNode | ||
}) => { | ||
const name = extractName(parentNode); | ||
if (name) { | ||
names.unshift(name); | ||
} // Traverse all the way to the root | ||
return false; | ||
}); | ||
return names.join('_'); | ||
} else { | ||
return extractName(parent); | ||
} | ||
}; | ||
const getRelevantCall = (node, namespaceImport, importIdentifiers) => { | ||
const { | ||
callee | ||
} = node; | ||
if (namespaceImport && core.types.isMemberExpression(callee) && core.types.isIdentifier(callee.object, { | ||
name: namespaceImport | ||
})) { | ||
return relevantExports.find(exportName => core.types.isIdentifier(callee.property, { | ||
name: exportName | ||
})); | ||
} else { | ||
const importInfo = Array.from(importIdentifiers.entries()).find(([identifier]) => core.types.isIdentifier(callee, { | ||
name: identifier | ||
})); | ||
if (importInfo) { | ||
return importInfo[1]; | ||
} | ||
} | ||
}; | ||
function index () { | ||
return { | ||
pre(state) { | ||
this.filename = state.opts.filename; | ||
this.hasTreatImports = false; | ||
pre() { | ||
var _this$opts; | ||
this.importIdentifiers = new Map(); | ||
this.namespaceImport = ''; | ||
this.packageIdentifier = ((_this$opts = this.opts) === null || _this$opts === void 0 ? void 0 : _this$opts.alias) || '@mattsjones/css-core'; | ||
}, | ||
visitor: { | ||
ImportDeclaration(path, { | ||
opts | ||
}) { | ||
const treatImportIdentifier = opts.alias || '@mattsjones/css-core'; | ||
ImportDeclaration(path) { | ||
if (path.node.source.value === this.packageIdentifier) { | ||
path.node.specifiers.forEach(specifier => { | ||
if (core.types.isImportNamespaceSpecifier(specifier)) { | ||
this.namespaceImport = specifier.local.name; | ||
} else if (core.types.isImportSpecifier(specifier)) { | ||
const { | ||
imported, | ||
local | ||
} = specifier; | ||
const importName = core.types.isIdentifier(imported) ? imported.name : imported.value; | ||
if (path.node.source.value === treatImportIdentifier) { | ||
this.hasTreatImports = true; | ||
if (relevantExports.includes(importName)) { | ||
this.importIdentifiers.set(local.name, importName); | ||
} | ||
} | ||
}); | ||
} | ||
}, | ||
Program: { | ||
exit(path, { | ||
opts | ||
}) { | ||
if (this.hasTreatImports) { | ||
const treatPackageName = opts.alias || '@mattsjones/css-core'; // TODO ensure filename is escaped | ||
CallExpression(path) { | ||
const { | ||
node | ||
} = path; | ||
const usedExport = getRelevantCall(node, this.namespaceImport, this.importIdentifiers); | ||
const setFileScope = core.template(` | ||
import { setFileScope } from "${treatPackageName}"; | ||
setFileScope('${this.filename}'); | ||
`, { | ||
sourceType: 'module' | ||
}); | ||
path.get('body')[0].insertBefore(setFileScope()); | ||
if (usedExport) { | ||
if (node.arguments.length < exportConfig[usedExport].maxParams) { | ||
const debugIdent = getDebugId(path); | ||
if (debugIdent) { | ||
node.arguments.push(core.types.stringLiteral(debugIdent)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -45,0 +124,0 @@ }; |
@@ -7,38 +7,117 @@ 'use strict'; | ||
const exportConfig = { | ||
style: { | ||
maxParams: 2 | ||
}, | ||
createTheme: { | ||
maxParams: 3 | ||
} | ||
}; | ||
const relevantExports = Object.keys(exportConfig); | ||
const extractName = node => { | ||
if (core.types.isObjectProperty(node) && core.types.isIdentifier(node.key)) { | ||
return node.key.name; | ||
} else if ((core.types.isVariableDeclarator(node) || core.types.isFunctionDeclaration(node)) && core.types.isIdentifier(node.id)) { | ||
return node.id.name; | ||
} else if (core.types.isExportDefaultDeclaration(node)) { | ||
return 'default'; | ||
} | ||
}; | ||
const getDebugId = path => { | ||
const { | ||
parent | ||
} = path; | ||
if (core.types.isObjectProperty(parent) || core.types.isReturnStatement(parent) || core.types.isArrayExpression(parent) || core.types.isSpreadElement(parent)) { | ||
const names = []; | ||
path.findParent(({ | ||
node: parentNode | ||
}) => { | ||
const name = extractName(parentNode); | ||
if (name) { | ||
names.unshift(name); | ||
} // Traverse all the way to the root | ||
return false; | ||
}); | ||
return names.join('_'); | ||
} else { | ||
return extractName(parent); | ||
} | ||
}; | ||
const getRelevantCall = (node, namespaceImport, importIdentifiers) => { | ||
const { | ||
callee | ||
} = node; | ||
if (namespaceImport && core.types.isMemberExpression(callee) && core.types.isIdentifier(callee.object, { | ||
name: namespaceImport | ||
})) { | ||
return relevantExports.find(exportName => core.types.isIdentifier(callee.property, { | ||
name: exportName | ||
})); | ||
} else { | ||
const importInfo = Array.from(importIdentifiers.entries()).find(([identifier]) => core.types.isIdentifier(callee, { | ||
name: identifier | ||
})); | ||
if (importInfo) { | ||
return importInfo[1]; | ||
} | ||
} | ||
}; | ||
function index () { | ||
return { | ||
pre(state) { | ||
this.filename = state.opts.filename; | ||
this.hasTreatImports = false; | ||
pre() { | ||
var _this$opts; | ||
this.importIdentifiers = new Map(); | ||
this.namespaceImport = ''; | ||
this.packageIdentifier = ((_this$opts = this.opts) === null || _this$opts === void 0 ? void 0 : _this$opts.alias) || '@mattsjones/css-core'; | ||
}, | ||
visitor: { | ||
ImportDeclaration(path, { | ||
opts | ||
}) { | ||
const treatImportIdentifier = opts.alias || '@mattsjones/css-core'; | ||
ImportDeclaration(path) { | ||
if (path.node.source.value === this.packageIdentifier) { | ||
path.node.specifiers.forEach(specifier => { | ||
if (core.types.isImportNamespaceSpecifier(specifier)) { | ||
this.namespaceImport = specifier.local.name; | ||
} else if (core.types.isImportSpecifier(specifier)) { | ||
const { | ||
imported, | ||
local | ||
} = specifier; | ||
const importName = core.types.isIdentifier(imported) ? imported.name : imported.value; | ||
if (path.node.source.value === treatImportIdentifier) { | ||
this.hasTreatImports = true; | ||
if (relevantExports.includes(importName)) { | ||
this.importIdentifiers.set(local.name, importName); | ||
} | ||
} | ||
}); | ||
} | ||
}, | ||
Program: { | ||
exit(path, { | ||
opts | ||
}) { | ||
if (this.hasTreatImports) { | ||
const treatPackageName = opts.alias || '@mattsjones/css-core'; // TODO ensure filename is escaped | ||
CallExpression(path) { | ||
const { | ||
node | ||
} = path; | ||
const usedExport = getRelevantCall(node, this.namespaceImport, this.importIdentifiers); | ||
const setFileScope = core.template(` | ||
import { setFileScope } from "${treatPackageName}"; | ||
setFileScope('${this.filename}'); | ||
`, { | ||
sourceType: 'module' | ||
}); | ||
path.get('body')[0].insertBefore(setFileScope()); | ||
if (usedExport) { | ||
if (node.arguments.length < exportConfig[usedExport].maxParams) { | ||
const debugIdent = getDebugId(path); | ||
if (debugIdent) { | ||
node.arguments.push(core.types.stringLiteral(debugIdent)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -45,0 +124,0 @@ }; |
@@ -1,39 +0,118 @@ | ||
import { template } from '@babel/core'; | ||
import { types } from '@babel/core'; | ||
const exportConfig = { | ||
style: { | ||
maxParams: 2 | ||
}, | ||
createTheme: { | ||
maxParams: 3 | ||
} | ||
}; | ||
const relevantExports = Object.keys(exportConfig); | ||
const extractName = node => { | ||
if (types.isObjectProperty(node) && types.isIdentifier(node.key)) { | ||
return node.key.name; | ||
} else if ((types.isVariableDeclarator(node) || types.isFunctionDeclaration(node)) && types.isIdentifier(node.id)) { | ||
return node.id.name; | ||
} else if (types.isExportDefaultDeclaration(node)) { | ||
return 'default'; | ||
} | ||
}; | ||
const getDebugId = path => { | ||
const { | ||
parent | ||
} = path; | ||
if (types.isObjectProperty(parent) || types.isReturnStatement(parent) || types.isArrayExpression(parent) || types.isSpreadElement(parent)) { | ||
const names = []; | ||
path.findParent(({ | ||
node: parentNode | ||
}) => { | ||
const name = extractName(parentNode); | ||
if (name) { | ||
names.unshift(name); | ||
} // Traverse all the way to the root | ||
return false; | ||
}); | ||
return names.join('_'); | ||
} else { | ||
return extractName(parent); | ||
} | ||
}; | ||
const getRelevantCall = (node, namespaceImport, importIdentifiers) => { | ||
const { | ||
callee | ||
} = node; | ||
if (namespaceImport && types.isMemberExpression(callee) && types.isIdentifier(callee.object, { | ||
name: namespaceImport | ||
})) { | ||
return relevantExports.find(exportName => types.isIdentifier(callee.property, { | ||
name: exportName | ||
})); | ||
} else { | ||
const importInfo = Array.from(importIdentifiers.entries()).find(([identifier]) => types.isIdentifier(callee, { | ||
name: identifier | ||
})); | ||
if (importInfo) { | ||
return importInfo[1]; | ||
} | ||
} | ||
}; | ||
function index () { | ||
return { | ||
pre(state) { | ||
this.filename = state.opts.filename; | ||
this.hasTreatImports = false; | ||
pre() { | ||
var _this$opts; | ||
this.importIdentifiers = new Map(); | ||
this.namespaceImport = ''; | ||
this.packageIdentifier = ((_this$opts = this.opts) === null || _this$opts === void 0 ? void 0 : _this$opts.alias) || '@mattsjones/css-core'; | ||
}, | ||
visitor: { | ||
ImportDeclaration(path, { | ||
opts | ||
}) { | ||
const treatImportIdentifier = opts.alias || '@mattsjones/css-core'; | ||
ImportDeclaration(path) { | ||
if (path.node.source.value === this.packageIdentifier) { | ||
path.node.specifiers.forEach(specifier => { | ||
if (types.isImportNamespaceSpecifier(specifier)) { | ||
this.namespaceImport = specifier.local.name; | ||
} else if (types.isImportSpecifier(specifier)) { | ||
const { | ||
imported, | ||
local | ||
} = specifier; | ||
const importName = types.isIdentifier(imported) ? imported.name : imported.value; | ||
if (path.node.source.value === treatImportIdentifier) { | ||
this.hasTreatImports = true; | ||
if (relevantExports.includes(importName)) { | ||
this.importIdentifiers.set(local.name, importName); | ||
} | ||
} | ||
}); | ||
} | ||
}, | ||
Program: { | ||
exit(path, { | ||
opts | ||
}) { | ||
if (this.hasTreatImports) { | ||
const treatPackageName = opts.alias || '@mattsjones/css-core'; // TODO ensure filename is escaped | ||
CallExpression(path) { | ||
const { | ||
node | ||
} = path; | ||
const usedExport = getRelevantCall(node, this.namespaceImport, this.importIdentifiers); | ||
const setFileScope = template(` | ||
import { setFileScope } from "${treatPackageName}"; | ||
setFileScope('${this.filename}'); | ||
`, { | ||
sourceType: 'module' | ||
}); | ||
path.get('body')[0].insertBefore(setFileScope()); | ||
if (usedExport) { | ||
if (node.arguments.length < exportConfig[usedExport].maxParams) { | ||
const debugIdent = getDebugId(path); | ||
if (debugIdent) { | ||
node.arguments.push(types.stringLiteral(debugIdent)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
@@ -40,0 +119,0 @@ }; |
{ | ||
"name": "@mattsjones/css-babel-plugin", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "dist/mattsjones-css-babel-plugin.cjs.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/mattsjones-css-babel-plugin.esm.js", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11126
341
7
1