Skip to content

Commit

Permalink
fix(cli): fixed v-on compiler logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Mar 21, 2020
1 parent f54ed95 commit 39f829c
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 102 deletions.
50 changes: 25 additions & 25 deletions packages/cli/core/plugins/template/directives/v-on.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ const parseHandlerProxy = (expr, scope) => {
let injectParams = [];
let handlerExpr = expr;
let eventInArg = false;
let wxInArg = false;
let needDeclareWx = false;
let argumentsInArg = false;

let parsedHandler;
// eslint-disable-next-line
if (/^[\w\.]+$/.test(expr)) {
// @tap="doSomething" or @tap="m.doSomething"
wxInArg = true;
needDeclareWx = true;
argumentsInArg = true;
eventInArg = true;

parsedHandler = {
callee: { name: handlerExpr },
params: []
};
handlerExpr = `${expr}.apply(vm, $args || [$event])`;
handlerExpr = `${expr}.apply(_vm, $args || [$event])`;
} else {
try {
parsedHandler = paramsDetect(handlerExpr);
Expand All @@ -46,51 +46,51 @@ const parseHandlerProxy = (expr, scope) => {
}

if (parsedHandler.identifiers.$event) {
wxInArg = true;
needDeclareWx = true;
eventInArg = true;
}

if (parsedHandler.identifiers.$wx) {
wxInArg = true;
needDeclareWx = true;
}

if (parsedHandler.identifiers.arguments) {
wxInArg = true;
needDeclareWx = true;
argumentsInArg = true;

handlerExpr = handlerExpr.replace('arguments', '$args');
}
}

const functionCode = handlerExpr;
let declareCode = wxInArg ? 'const $wx = arguments[arguments.length - 1].$wx;' : '';

let declaredCodes = [];

declaredCodes.push('const _vm=this;');
if (needDeclareWx) {
declaredCodes.push('const $wx = arguments[arguments.length - 1].$wx;');
}
if (eventInArg) {
declareCode +=
'const $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];';
declaredCodes.push(
' const $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length -1];'
);
}
if (argumentsInArg) {
// for use define component event, use detail arguments instead;
declareCode += 'const $args = $wx.detail && $wx.detail.arguments;';
}

if (wxInArg) {
declareCode +=
'if ($wx.detail && $wx.detail.arguments) $wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];';
declaredCodes.push(' const $args = $wx.detail && $wx.detail.arguments;');
}

let proxy = `function proxy (${injectParams.join(', ')}) {
const vm = this;
${declareCode}
with (this) {
return (function () {
${functionCode};
})();
}
}`;
${declaredCodes.join('\n')}
with (this) {
return (function () {
${functionCode};
})();}
}`;

proxy = vueWithTransform(proxy); // transform this
proxy = proxy.replace('var _h=_vm.$createElement;var _c=_vm._self._c||_h;', ''); // removed unused vue code;
if (proxy.indexOf('_vm=this') !== proxy.lastIndexOf('_vm=this')) {
proxy = proxy.replace('var _vm=this;\n', ''); // _vm will decalred twice;
}
return {
proxy: proxy,
params: injectParams,
Expand Down
10 changes: 3 additions & 7 deletions packages/cli/test/core/fixtures/template/assert/v-on/0-0.tap.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length -1];
var $args = $wx.detail && $wx.detail.arguments;
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
var _vm=this;
return (function () {
_vm.myclick.apply(vm, $args || [$event]);
_vm.myclick.apply(_vm, $args || [$event]);
})();
}
10 changes: 3 additions & 7 deletions packages/cli/test/core/fixtures/template/assert/v-on/0-1.tap.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length -1];
var $args = $wx.detail && $wx.detail.arguments;
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
var _vm=this;
return (function () {
_vm.myclickStop.apply(vm, $args || [$event]);
_vm.myclickStop.apply(_vm, $args || [$event]);
})();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $args = $wx.detail && $wx.detail.arguments;
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
return (function () {
_vm.myclick(1, $args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
return (function () {
_vm.myclick(1, $event);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
return (function () {
_vm.myclick(1, $wx);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];
var $args = $wx.detail && $wx.detail.arguments;
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
return (function () {
_vm.myclick(1, $wx, $event, $args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function proxy () {
var vm = this;
var _vm = this;
return (function () {
_vm.myclick();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function proxy () {
var vm = this;
var _vm = this;
return (function () {
_vm.myclick(1);
Expand Down
10 changes: 3 additions & 7 deletions packages/cli/test/core/fixtures/template/assert/v-on/0-2.tap.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length -1];
var $args = $wx.detail && $wx.detail.arguments;
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
var _vm=this;
return (function () {
_vm.myclickCapture.apply(vm, $args || [$event]);
_vm.myclickCapture.apply(_vm, $args || [$event]);
})();
}
10 changes: 3 additions & 7 deletions packages/cli/test/core/fixtures/template/assert/v-on/0-3.tap.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length -1];
var $args = $wx.detail && $wx.detail.arguments;
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
var _vm=this;
return (function () {
_vm.myclickCaptureStop.apply(vm, $args || [$event]);
_vm.myclickCaptureStop.apply(_vm, $args || [$event]);
})();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function proxy () {
var vm = this;
var _vm = this;
return (function () {
_vm.myClickCaptureStopWithParams(1, { a: 1 }, [1, 2]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
return (function () {
_vm.myclick($event);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function proxy (item) {
var vm = this;
var _vm = this;
return (function () {
_vm.myClickInFor(item);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];
var $args = $wx.detail && $wx.detail.arguments;
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
return (function () {
_vm.handleCaptureBindTap.apply(vm, $args || [$event]);
_vm.handleCaptureBindTap.apply(_vm, $args || [$event]);
})();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];
var $args = $wx.detail && $wx.detail.arguments;
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
return (function () {
_vm.handleCaptureCatchTap.apply(vm, $args || [$event]);
_vm.handleCaptureCatchTap.apply(_vm, $args || [$event]);
})();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
function proxy () {
var vm = this;
var $wx = arguments[arguments.length - 1].$wx;
var $event = ($wx.detail && $wx.detail.arguments) ? $wx.detail.arguments[0] : arguments[arguments.length - 1];
var $args = $wx.detail && $wx.detail.arguments;
if ($wx.detail && $wx.detail.arguments) {
$wx.detail = $wx.detail.arguments.length > 1 ? $wx.detail.arguments : $wx.detail.arguments[0];
}
var _vm = this;
return (function () {
_vm.rightNavbar.apply(vm, $args || [$event]);
_vm.rightNavbar.apply(_vm, $args || [$event]);
})();
}
13 changes: 3 additions & 10 deletions packages/core/weapp/init/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,19 @@ const proxyHandler = function(e) {
if (isFunc(fn)) {
const $event = new Event(e);
const paramsWithEvent = params.concat($event);
let args = e.detail && e.detail.arguments;

if (args) {
e.detail = args.length > 1 ? args : args[0];
}
let args = (e.detail && e.detail.arguments) || [];

const hookRes = callUserHook(vm, 'before-event', {
event: $event,
params: paramsWithEvent
params: paramsWithEvent,
args: args
});

if (hookRes === false) {
// Event cancelled.
return;
}

if (args) {
e.detail = { arguments: args };
}

return fn.apply(vm, paramsWithEvent);
} else if (!model) {
throw new Error('Unrecognized event');
Expand Down

0 comments on commit 39f829c

Please sign in to comment.