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

babel-plugin-function-time

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-function-time - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

139

lib/index.js

@@ -7,94 +7,93 @@ 'use strict';

var _visitor;
exports.default = function (_ref) {
var _visitor;
exports.default = function (_ref4) {
var t = _ref4.types;
var t = _ref.types;
return {
visitor: visitor
};
};
visitor: (_visitor = {}, _defineProperty(_visitor, 'FunctionExpression|ArrowFunctionExpression', function FunctionExpressionArrowFunctionExpression(path, _ref2) {
var opts = _ref2.opts;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var parentIdentifier = function () {
var left = path.parent.left;
if (path.parent.id) return path.parent.id;else if (path.parent.key) return path.parent.key;else if (left && left.type === 'Identifier') return left;else if (left && left.type === 'MemberExpression') return left.property;
}();
var template = require('babel-template');
var fs = require('fs');
if (!parentIdentifier) return;
/* ------------------------------------------------------------
* plugins
* ------------------------------------------------------------ */
var callName = parentIdentifier.name;
if (opts.promise.indexOf(callName) === -1) return;
var timeData = fs.readFileSync('./time.js');
var buildTimeFunction = template('\n\t (function (FUNCTION_KEY) {\n\t\t ' + timeData.toString() + '\n\t\t return promiseTime(REPORT, FUNCTION, NAME)\n })()\n ');
var uid = path.scope.generateUidIdentifier(callName);
var buildReportFunction = template('\n\t (function (FUNCTION_KEY) {\n\t\t return REPORT_FUNCTION\n })()\n ');
var ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: path.node,
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
var visitor = (_visitor = {}, _defineProperty(_visitor, 'FunctionExpression|ArrowFunctionExpression', function FunctionExpressionArrowFunctionExpression(path, _ref) {
var opts = _ref.opts;
path.replaceWith(ast);
}), _defineProperty(_visitor, 'FunctionDeclaration', function FunctionDeclaration(path, _ref3) {
var opts = _ref3.opts;
var parentIdentifier = function () {
var left = path.parent.left;
if (path.parent.id) return path.parent.id;else if (path.parent.key) return path.parent.key;else if (left && left.type === 'Identifier') return left;else if (left && left.type === 'MemberExpression') return left.property;
}();
var parentIdentifier = path.node.id;
var callName = parentIdentifier.name;
if (opts.promise.indexOf(callName) === -1) return;
if (!parentIdentifier) return;
var uid = path.scope.generateUidIdentifier(callName);
var ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: t.functionExpression(null, [], path.node.body),
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
var callName = parentIdentifier.name;
if (opts.promise.indexOf(callName) === -1) return;
path.replaceWith(t.variableDeclaration('var', [t.variableDeclarator(parentIdentifier, ast.expression)]));
}), _defineProperty(_visitor, 'ObjectMethod', function ObjectMethod(path, _ref4) {
var opts = _ref4.opts;
var uid = path.scope.generateUidIdentifier(callName);
if (!t.isObjectMethod(path.node, { kind: 'method' })) return;
var ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: path.node,
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
var identifier = path.node.key;
var callName = identifier.name;
if (opts.promise.indexOf(callName) === -1) return;
path.replaceWith(ast);
}), _defineProperty(_visitor, 'FunctionDeclaration', function FunctionDeclaration(path, _ref2) {
var opts = _ref2.opts;
var uid = path.scope.generateUidIdentifier(callName);
var ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: t.functionExpression(null, [], path.node.body),
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
var parentIdentifier = path.node.id;
var callName = parentIdentifier.name;
if (opts.promise.indexOf(callName) === -1) return;
path.replaceWith(t.objectProperty(t.identifier(callName), ast.expression));
}), _visitor)
};
};
var uid = path.scope.generateUidIdentifier(callName);
var ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: t.functionExpression(null, [], path.node.body),
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
path.replaceWith(t.variableDeclaration('var', [t.variableDeclarator(parentIdentifier, ast.expression)]));
}), _defineProperty(_visitor, 'ObjectMethod', function ObjectMethod(path, _ref3) {
var opts = _ref3.opts;
var template = require('babel-template');
var fs = require('fs');
var path = require('path');
if (!t.isObjectMethod(path.node, { kind: 'method' })) return;
/* ------------------------------------------------------------
* plugins
* ------------------------------------------------------------ */
var identifier = path.node.key;
var callName = identifier.name;
if (opts.promise.indexOf(callName) === -1) return;
var timeData = fs.readFileSync(path.join(__dirname, 'time.js'));
var buildTimeFunction = template('\n\t (function (FUNCTION_KEY) {\n\t\t ' + timeData.toString() + '\n\t\t return promiseTime(REPORT, FUNCTION, NAME)\n })()\n ');
var uid = path.scope.generateUidIdentifier(callName);
var ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: t.functionExpression(null, [], path.node.body),
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
var buildReportFunction = template('\n\t (function (FUNCTION_KEY) {\n\t\t return REPORT_FUNCTION\n })()\n ');
path.replaceWith(t.objectProperty(t.identifier(callName), ast.expression));
}), _visitor);
module.exports = exports['default'];
{
"name": "babel-plugin-function-time",
"version": "1.0.2",
"version": "1.0.3",
"description": "",

@@ -13,3 +13,3 @@ "main": "lib/index.js",

"dependencies": {
"babel-template": "^6.16.0"
"babel- itemplate": "^6.16.0"
},

@@ -16,0 +16,0 @@ "devDependencies": {

const template = require('babel-template');
const fs = require('fs');
const path = require('path');

@@ -8,3 +9,3 @@ /* ------------------------------------------------------------

const timeData = fs.readFileSync('./time.js');
const timeData = fs.readFileSync(path.join(__dirname, 'time.js'));
const buildTimeFunction = template(`

@@ -23,95 +24,88 @@ (function (FUNCTION_KEY) {

const visitor = {
// 如果是函数变量声明
['FunctionExpression|ArrowFunctionExpression'](path, { opts }) {
const parentIdentifier = (() => {
let left = path.parent.left;
if (path.parent.id)
return path.parent.id;
else if (path.parent.key)
return path.parent.key;
else if (left && left.type === 'Identifier')
return left;
else if (left && left.type === 'MemberExpression')
return left.property;
}
)();
export default function({types: t}) {
return {
visitor: {
// 如果是函数变量声明
['FunctionExpression|ArrowFunctionExpression'](path, {opts}) {
const parentIdentifier = (() => {
let left = path.parent.left;
if (path.parent.id)
return path.parent.id;
else if (path.parent.key)
return path.parent.key;
else if (left && left.type === 'Identifier')
return left;
else if (left && left.type === 'MemberExpression')
return left.property;
}
)();
if (!parentIdentifier)
return;
if (!parentIdentifier)
return;
const callName = parentIdentifier.name;
if (opts.promise.indexOf(callName) === -1)
return;
const callName = parentIdentifier.name;
if (opts.promise.indexOf(callName) === -1)
return;
const uid = path.scope.generateUidIdentifier(callName);
const uid = path.scope.generateUidIdentifier(callName);
const ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: path.node,
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
const ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: path.node,
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
path.replaceWith(ast);
},
path.replaceWith(ast);
},
// 如果是函数声明
FunctionDeclaration(path, { opts }) {
const parentIdentifier = path.node.id;
const callName = parentIdentifier.name;
if (opts.promise.indexOf(callName) === -1)
return;
// 如果是函数声明
FunctionDeclaration(path, {opts}) {
const parentIdentifier = path.node.id;
const callName = parentIdentifier.name;
if (opts.promise.indexOf(callName) === -1)
return;
const uid = path.scope.generateUidIdentifier(callName);
const ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: t.functionExpression(null, [], path.node.body),
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
const uid = path.scope.generateUidIdentifier(callName);
const ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: t.functionExpression(null, [], path.node.body),
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
path.replaceWith(
t.variableDeclaration('var', [t.variableDeclarator(parentIdentifier, ast.expression)])
);
},
path.replaceWith(t.variableDeclaration('var', [t.variableDeclarator(parentIdentifier, ast.expression)]));
},
// 如果是对象的属性方法
ObjectMethod(path, { opts }) {
if (!t.isObjectMethod(path.node, { kind: 'method' }))
return;
// 如果是对象的属性方法
ObjectMethod(path, {opts}) {
if (!t.isObjectMethod(path.node, {kind: 'method'}))
return;
const identifier = path.node.key;
const callName = identifier.name;
if (opts.promise.indexOf(callName) === -1)
return;
const identifier = path.node.key;
const callName = identifier.name;
if (opts.promise.indexOf(callName) === -1)
return;
const uid = path.scope.generateUidIdentifier(callName);
const ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: t.functionExpression(null, [], path.node.body),
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
const uid = path.scope.generateUidIdentifier(callName);
const ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: t.functionExpression(null, [], path.node.body),
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
path.replaceWith(
t.objectProperty(t.identifier(callName), ast.expression)
);
},
};
export default function({ types: t }) {
return {
visitor: visitor
}
path.replaceWith(t.objectProperty(t.identifier(callName), ast.expression));
}
}
}
}
const babel = require('babel-core');
const template = require('babel-template');
const t = require('babel-types');
const fs = require('fs');
const plugin = require('./lib');

@@ -17,3 +16,2 @@ const code = `

}
`;

@@ -25,112 +23,8 @@

const timeData = fs.readFileSync('./time.js');
const buildTimeFunction = template(`
(function (FUNCTION_KEY) {
${timeData.toString()}
return promiseTime(REPORT, FUNCTION, NAME)
})()
`);
const buildReportFunction = template(`
(function (FUNCTION_KEY) {
return REPORT_FUNCTION
})()
`);
const visitor = {
// 如果是函数变量声明
['FunctionExpression|ArrowFunctionExpression'](path, { opts }) {
const parentIdentifier = (() => {
let left = path.parent.left;
if (path.parent.id)
return path.parent.id;
else if (path.parent.key)
return path.parent.key;
else if (left && left.type === 'Identifier')
return left;
else if (left && left.type === 'MemberExpression')
return left.property;
}
)();
if (!parentIdentifier)
return;
const callName = parentIdentifier.name;
if (opts.promise.indexOf(callName) === -1)
return;
const uid = path.scope.generateUidIdentifier(callName);
const ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: path.node,
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
path.replaceWith(ast);
},
// 如果是函数声明
FunctionDeclaration(path, { opts }) {
const parentIdentifier = path.node.id;
const callName = parentIdentifier.name;
if (opts.promise.indexOf(callName) === -1)
return;
const uid = path.scope.generateUidIdentifier(callName);
const ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: t.functionExpression(null, [], path.node.body),
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
path.replaceWith(
t.variableDeclaration('var', [t.variableDeclarator(parentIdentifier, ast.expression)])
);
},
// 如果是对象的属性方法
ObjectMethod(path, { opts }) {
if (!t.isObjectMethod(path.node, { kind: 'method' }))
return;
const identifier = path.node.key;
const callName = identifier.name;
if (opts.promise.indexOf(callName) === -1)
return;
const uid = path.scope.generateUidIdentifier(callName);
const ast = buildTimeFunction({
REPORT: buildReportFunction({
FUNCTION_KEY: uid,
REPORT_FUNCTION: t.identifier(opts.report.toString())
}),
FUNCTION: t.functionExpression(null, [], path.node.body),
FUNCTION_KEY: uid,
NAME: t.stringLiteral(callName)
});
path.replaceWith(
t.objectProperty(t.identifier(callName), ast.expression)
);
},
};
const result = babel.transform(code, {
plugins: [
[
{
visitor: visitor
}, {
report: (durning, args) => console.log('%sms', durning),
promise: ['a', 'b']
plugin, {
"report": "(durning) => console.log('%sms', durning)",
"promise": ["a"]
}

@@ -137,0 +31,0 @@ ]

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