babel-plugin-macros
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -74,13 +74,27 @@ 'use strict'; | ||
var id = node.arguments[0]; | ||
var name = id && id.name; | ||
var macroBody = node.arguments[1]; | ||
var subScope = path.get('arguments')[1].scope; | ||
var location = node.loc ? ', at Line: ' + node.loc.start.line + ' Column: ' + node.loc.start.column : ''; | ||
if (!(id && t.isIdentifier(id))) { | ||
// @todo add test | ||
throw new Error('First argument to DEFINE_MACRO must be an identifier.'); | ||
throw new Error("First argument to DEFINE_MACRO must be an identifier" + location); | ||
} | ||
var name = id.name; | ||
var macroBody = node.arguments[1]; | ||
var subScope = path.get('arguments')[1].scope; | ||
if (!t.isFunction(macroBody)) { | ||
throw new Error("Second argument to DEFINE_MACRO must be a FunctionExpression or ArrowFunctionExpression" + location); | ||
} | ||
scope[$registeredMacros] = scope[$registeredMacros] || {}; | ||
scope[$registeredMacros][name] = new Macro({ name: name, macroBody: macroBody, scope: subScope, state: state }); | ||
traverse(macroBody, visitors, subScope, state); | ||
traverse(macroBody, traverse.visitors.merge([visitors, { | ||
ThisExpression: function ThisExpression() { | ||
throw new Error("Can not use `this` in macro" + location); | ||
}, | ||
Identifier: function Identifier(_ref5) { | ||
var node = _ref5.node; | ||
if ("arguments" === node.name) { | ||
throw new Error("Can not use `arguments` in macro" + location); | ||
} | ||
} | ||
}]), subScope, state); | ||
path.remove(); | ||
@@ -134,5 +148,2 @@ }; | ||
}, scope); | ||
} else { | ||
// @todo add test | ||
throw new Error('Second argument to DEFINE_MACRO must be a FunctionExpression or ArrowFunctionExpression.'); | ||
} | ||
@@ -142,7 +153,7 @@ return _ref2(function (path, scope, state) { | ||
var _cloned$params$reduce = cloned.params.reduce(function (_ref7, id, index) { | ||
var _ref8 = _slicedToArray(_ref7, 2); | ||
var _cloned$params$reduce = cloned.params.reduce(function (_ref8, id, index) { | ||
var _ref9 = _slicedToArray(_ref8, 2); | ||
var params = _ref8[0]; | ||
var seen = _ref8[1]; | ||
var params = _ref9[0]; | ||
var seen = _ref9[1]; | ||
@@ -221,7 +232,7 @@ params[id.name] = { | ||
parentBlock.insertBefore([t.variableDeclaration('let', [t.variableDeclarator(uid)])]); | ||
returnStatements.forEach(function (_ref5) { | ||
var _ref6 = _slicedToArray(_ref5, 2); | ||
returnStatements.forEach(function (_ref6) { | ||
var _ref7 = _slicedToArray(_ref6, 2); | ||
var path = _ref6[0]; | ||
var child = _ref6[1]; | ||
var path = _ref7[0]; | ||
var child = _ref7[1]; | ||
@@ -327,3 +338,2 @@ var isLast = child === cloned.body.body[cloned.body.body.length - 1]; | ||
if (t.isMemberExpression(node.callee)) { | ||
// TODO temp locked for refactoring. need another idea for chaining more than 2 macros | ||
if (!node.callee.computed && getMacro(node.callee.object, path.scope, state) && getMacro(node.callee.property, path.scope, state)) { | ||
@@ -330,0 +340,0 @@ var head = node.callee.object; |
{ | ||
"name": "babel-plugin-macros", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Macros for JavaScript via a babel plugin.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -109,2 +109,3 @@ # Babel Macros | ||
- **1.0.7** add checking types in runtime | ||
- **1.0.8** block using `this` and `arguments` in macros | ||
@@ -111,0 +112,0 @@ # License |
@@ -34,13 +34,33 @@ import _ from 'lodash'; | ||
const id = node.arguments[0]; | ||
const name = id && id.name; | ||
const macroBody = node.arguments[1]; | ||
const subScope = path.get('arguments')[1].scope; | ||
const location = node.loc ? `, at Line: ${node.loc.start.line} Column: ${node.loc.start.column}`: ''; | ||
if (!(id && t.isIdentifier(id))) { | ||
// @todo add test | ||
throw new Error(`First argument to DEFINE_MACRO must be an identifier.`); | ||
throw new Error("First argument to DEFINE_MACRO must be an identifier" + location); | ||
} | ||
const name = id.name; | ||
const macroBody = node.arguments[1]; | ||
const subScope = path.get('arguments')[1].scope; | ||
if (!t.isFunction(macroBody)) { | ||
throw new Error("Second argument to DEFINE_MACRO must be a FunctionExpression or ArrowFunctionExpression" + location); | ||
} | ||
scope[$registeredMacros] = scope[$registeredMacros] || {}; | ||
scope[$registeredMacros][name] = new Macro({name: name, macroBody, scope: subScope, state}); | ||
traverse(macroBody, visitors, subScope, state); | ||
traverse( | ||
macroBody, | ||
traverse.visitors.merge([ | ||
visitors, | ||
{ | ||
ThisExpression() { | ||
throw new Error("Can not use `this` in macro" + location); | ||
}, | ||
Identifier({node}) { | ||
if("arguments" === node.name) { | ||
throw new Error("Can not use `arguments` in macro" + location); | ||
} | ||
} | ||
} | ||
]), | ||
subScope, | ||
state | ||
); | ||
path.remove(); | ||
@@ -67,5 +87,2 @@ }; | ||
}, scope); | ||
} else { | ||
// @todo add test | ||
throw new Error('Second argument to DEFINE_MACRO must be a FunctionExpression or ArrowFunctionExpression.'); | ||
} | ||
@@ -245,3 +262,2 @@ return function (path, scope, state) { | ||
if (t.isMemberExpression(node.callee)) { | ||
// TODO temp locked for refactoring. need another idea for chaining more than 2 macros | ||
if ( | ||
@@ -248,0 +264,0 @@ !node.callee.computed && |
@@ -91,3 +91,7 @@ import Plugin from '../src'; | ||
//run("execution-order-in-expression", [[1, 2], [1, 2], [1, 2]]); | ||
run("wrong-name", new Error("unknown: First argument to DEFINE_MACRO must be an identifier, at Line: 1 Column: 0")); | ||
run("wrong-function", new Error("unknown: Second argument to DEFINE_MACRO must be a FunctionExpression or ArrowFunctionExpression, at Line: 4 Column: 0")); | ||
run("this-in-macro", new Error("unknown: Can not use `this` in macro, at Line: 1 Column: 0")); | ||
run("arguments-in-macro", new Error("unknown: Can not use `arguments` in macro, at Line: 1 Column: 0")); | ||
}); | ||
Sorry, the diff of this file is not supported yet
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
117563
52
115
1066