babel-plugin-function-time
Advanced tools
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": { |
158
src/index.js
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)); | ||
} | ||
} | ||
} | ||
} |
114
test.js
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 @@ ] |
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
3
0
12009
260
+ Addedbabel- itemplate@^6.16.0
- Removedbabel-template@^6.16.0
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedbabel-code-frame@6.26.0(transitive)
- Removedbabel-messages@6.23.0(transitive)
- Removedbabel-runtime@6.26.0(transitive)
- Removedbabel-template@6.26.0(transitive)
- Removedbabel-traverse@6.26.0(transitive)
- Removedbabel-types@6.26.0(transitive)
- Removedbabylon@6.18.0(transitive)
- Removedchalk@1.1.3(transitive)
- Removedcore-js@2.6.12(transitive)
- Removeddebug@2.6.9(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesutils@2.0.3(transitive)
- Removedglobals@9.18.0(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedinvariant@2.2.4(transitive)
- Removedjs-tokens@3.0.2(transitive)
- Removedlodash@4.17.21(transitive)
- Removedloose-envify@1.4.0(transitive)
- Removedms@2.0.0(transitive)
- Removedregenerator-runtime@0.11.1(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedto-fast-properties@1.0.3(transitive)