[decorators] Don't transform every AssignmentExpression (#7032)

This commit is contained in:
Nicolò Ribaudo 2017-12-15 22:17:31 +01:00 committed by Henry Zhu
parent f2437583ba
commit 9a146d01b0
4 changed files with 72 additions and 4 deletions

View File

@ -27,6 +27,8 @@ const buildGetObjectInitializer = template(`
`);
export default function() {
const WARNING_CALLS = new WeakSet();
/**
* If the decorator expressions are non-identifiers, hoist them to before the class so we can be sure
* that they are evaluated in order.
@ -151,11 +153,14 @@ export default function() {
t.blockStatement([t.returnStatement(node.value)]),
)
: t.nullLiteral();
node.value = t.callExpression(
state.addHelper("initializerWarningHelper"),
[descriptor, t.thisExpression()],
);
WARNING_CALLS.add(node.value);
acc = acc.concat([
t.assignmentExpression(
"=",
@ -255,10 +260,7 @@ export default function() {
},
AssignmentExpression(path, state) {
if (!path.get("left").isMemberExpression()) return;
if (!path.get("left.property").isIdentifier()) return;
if (!path.get("right").isCallExpression()) return;
if (!path.get("right.callee").isIdentifier()) return;
if (!WARNING_CALLS.has(path.node.right)) return;
path.replaceWith(
t.callExpression(state.addHelper("initializerDefineProperty"), [

View File

@ -0,0 +1,22 @@
function generateAsyncAction(type) {
type = type.toUpperCase()
const request = createAction(type+'_REQUEST',
undefined, requestMetaCreator
)
request.request = request // crazy
request.success = createAction(type+'_SUCCESS', undefined, metaCreator)
request.error = createAction(type+'_ERROR', undefined, metaCreator)
request.cancel = createAction(type+'_CANCEL', undefined, metaCreator)
request.progress = createAction(type+'_PROGRESS', undefined, metaCreator)
request.process = createAction(type+'_PROCESS', undefined, metaCreator)
return request
}
class A extends B {
constructor(timestamp) {
super()
this.timestamp = timestamp
this.moment = moment(timestamp)
}
}

View File

@ -0,0 +1,41 @@
function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return right[Symbol.hasInstance](left); } else { return left instanceof right; } }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function generateAsyncAction(type) {
type = type.toUpperCase();
var request = createAction(type + '_REQUEST', undefined, requestMetaCreator);
request.request = request; // crazy
request.success = createAction(type + '_SUCCESS', undefined, metaCreator);
request.error = createAction(type + '_ERROR', undefined, metaCreator);
request.cancel = createAction(type + '_CANCEL', undefined, metaCreator);
request.progress = createAction(type + '_PROGRESS', undefined, metaCreator);
request.process = createAction(type + '_PROCESS', undefined, metaCreator);
return request;
}
var A =
/*#__PURE__*/
function (_B) {
_inherits(A, _B);
function A(timestamp) {
var _this;
_classCallCheck(this, A);
_this = _possibleConstructorReturn(this, (A.__proto__ || Object.getPrototypeOf(A)).call(this));
_this.timestamp = timestamp;
_this.moment = moment(timestamp);
return _this;
}
return A;
}(B);

View File

@ -0,0 +1,3 @@
{
"presets": ["es2015", "stage-0"]
}