Skip to content

allow console.* calls in event handlers #854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/generators/dom/visitors/Element/EventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import deindent from '../../../../utils/deindent';
import flattenReference from '../../../../utils/flattenReference';
import validCalleeObjects from '../../../../utils/validCalleeObjects';
import { DomGenerator } from '../../index';
import Block from '../../Block';
import { Node } from '../../../../interfaces';
Expand All @@ -23,7 +24,7 @@ export default function visitEventHandler(
generator.addSourcemapLocations(attribute.expression);

const flattened = flattenReference(attribute.expression.callee);
if (flattened.name !== 'event' && flattened.name !== 'this') {
if (!validCalleeObjects.has(flattened.name)) {
// allow event.stopPropagation(), this.select() etc
// TODO verify that it's a valid callee (i.e. built-in or declared method)
generator.code.prependRight(
Expand Down
3 changes: 3 additions & 0 deletions src/utils/validCalleeObjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const validCalleeObjects = new Set(['this', 'event', 'console']);

export default validCalleeObjects;
5 changes: 3 additions & 2 deletions src/validate/html/validateEventHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import flattenReference from '../../utils/flattenReference';
import list from '../utils/list';
import { Validator } from '../index';
import validCalleeObjects from '../../utils/validCalleeObjects';
import { Node } from '../../interfaces';

const validBuiltins = new Set(['set', 'fire', 'destroy']);
Expand All @@ -20,7 +21,7 @@ export default function validateEventHandlerCallee(

const { name } = flattenReference(callee);

if (name === 'this' || name === 'event') return;
if (validCalleeObjects.has(name)) return;

if (name === 'refs') {
refCallees.push(callee);
Expand All @@ -33,7 +34,7 @@ export default function validateEventHandlerCallee(
)
return;

const validCallees = ['this.*', 'event.*'].concat(
const validCallees = ['this.*', 'event.*', 'console.*'].concat(
Array.from(validBuiltins),
Array.from(validator.methods.keys())
);
Expand Down
23 changes: 23 additions & 0 deletions test/runtime/samples/event-handler-console-log/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default {
data: {
foo: 42
},

html: `
<button>click me</button>
`,

test (assert, component, target, window) {
const button = target.querySelector('button');
const event = new window.MouseEvent('click');

const messages = [];

const log = console.log;
console.log = msg => messages.push(msg);
button.dispatchEvent(event);
console.log = log;

assert.deepEqual(messages, [42]);
}
};
1 change: 1 addition & 0 deletions test/runtime/samples/event-handler-console-log/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<button on:click='console.log(foo)'>click me</button>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[{
"message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire, destroy or bar). 'foo' exists on 'helpers', did you put it in the wrong place?",
"message": "'foo' is an invalid callee (should be one of this.*, event.*, console.*, set, fire, destroy or bar). 'foo' exists on 'helpers', did you put it in the wrong place?",
"pos": 18,
"loc": {
"line": 1,
Expand Down
2 changes: 1 addition & 1 deletion test/validator/samples/method-nonexistent/warnings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[{
"message": "'foo' is an invalid callee (should be one of this.*, event.*, set, fire, destroy or bar)",
"message": "'foo' is an invalid callee (should be one of this.*, event.*, console.*, set, fire, destroy or bar)",
"pos": 18,
"loc": {
"line": 1,
Expand Down
2 changes: 1 addition & 1 deletion test/validator/samples/window-event-invalid/warnings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[{
"message": "'resize' is an invalid callee (should be one of this.*, event.*, set, fire or destroy)",
"message": "'resize' is an invalid callee (should be one of this.*, event.*, console.*, set, fire or destroy)",
"loc": {
"line": 1,
"column": 20
Expand Down