Skip to content

Commit aaab685

Browse files
committed
support $method(...) calls, and warn on store.method(...)
1 parent 6ff02b2 commit aaab685

File tree

9 files changed

+58
-17
lines changed

9 files changed

+58
-17
lines changed

src/generators/Generator.ts

+7
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export default class Generator {
9393
helpers: Set<string>;
9494
components: Set<string>;
9595
events: Set<string>;
96+
methods: Set<string>;
9697
transitions: Set<string>;
9798
actions: Set<string>;
9899
importedComponents: Map<string, string>;
@@ -141,6 +142,7 @@ export default class Generator {
141142
this.helpers = new Set();
142143
this.components = new Set();
143144
this.events = new Set();
145+
this.methods = new Set();
144146
this.transitions = new Set();
145147
this.actions = new Set();
146148
this.importedComponents = new Map();
@@ -419,6 +421,7 @@ export default class Generator {
419421
code,
420422
source,
421423
computations,
424+
methods,
422425
templateProperties,
423426
imports
424427
} = this;
@@ -608,6 +611,10 @@ export default class Generator {
608611

609612
if (templateProperties.methods && dom) {
610613
addDeclaration('methods', templateProperties.methods.value);
614+
615+
templateProperties.methods.value.properties.forEach(prop => {
616+
this.methods.add(prop.key.name);
617+
});
611618
}
612619

613620
if (templateProperties.namespace) {

src/generators/nodes/Element.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,19 @@ export default class Element extends Node {
519519
if (!validCalleeObjects.has(flattened.name)) {
520520
// allow event.stopPropagation(), this.select() etc
521521
// TODO verify that it's a valid callee (i.e. built-in or declared method)
522-
generator.code.prependRight(
523-
attribute.expression.start,
524-
`${block.alias('component')}.`
525-
);
522+
if (flattened.name[0] === '$' && !generator.methods.has(flattened.name)) {
523+
generator.code.overwrite(
524+
attribute.expression.start,
525+
attribute.expression.start + 1,
526+
`${block.alias('component')}.store.`
527+
);
528+
} else {
529+
generator.code.prependRight(
530+
attribute.expression.start,
531+
`${block.alias('component')}.`
532+
);
533+
}
534+
526535
if (shouldHoist) eventHandlerUsesComponent = true; // this feels a bit hacky but it works!
527536
}
528537

src/validate/html/validateEventHandler.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default function validateEventHandlerCallee(
2424

2525
const { name } = flattenReference(callee);
2626

27-
if (validCalleeObjects.has(name) || name === 'options' || name === 'store') return;
27+
if (validCalleeObjects.has(name) || name === 'options') return;
2828

2929
if (name === 'refs') {
3030
refCallees.push(callee);
@@ -34,21 +34,30 @@ export default function validateEventHandlerCallee(
3434
if (
3535
(callee.type === 'Identifier' && validBuiltins.has(callee.name)) ||
3636
validator.methods.has(callee.name)
37-
)
37+
) {
3838
return;
39+
}
3940

40-
const validCallees = ['this.*', 'event.*', 'options.*', 'console.*', 'store.*'].concat(
41+
if (name[0] === '$') {
42+
// assume it's a store method
43+
return;
44+
}
45+
46+
const validCallees = ['this.*', 'event.*', 'options.*', 'console.*'].concat(
4147
Array.from(validBuiltins),
4248
Array.from(validator.methods.keys())
4349
);
4450

45-
let message = `'${validator.source.slice(
46-
callee.start,
47-
callee.end
48-
)}' is an invalid callee (should be one of ${list(validCallees)})`;
51+
let message = `'${validator.source.slice(callee.start, callee.end)}' is an invalid callee ` ;
52+
53+
if (name === 'store') {
54+
message += `(did you mean '$${validator.source.slice(callee.start + 6, callee.end)}(...)'?)`;
55+
} else {
56+
message += `(should be one of ${list(validCallees)})`;
4957

50-
if (callee.type === 'Identifier' && validator.helpers.has(callee.name)) {
51-
message += `. '${callee.name}' exists on 'helpers', did you put it in the wrong place?`;
58+
if (callee.type === 'Identifier' && validator.helpers.has(callee.name)) {
59+
message += `. '${callee.name}' exists on 'helpers', did you put it in the wrong place?`;
60+
}
5261
}
5362

5463
validator.warn(attribute.expression, {
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<input on:input='store.setName(this.value)'>
1+
<input on:input='$setName(this.value)'>

test/validator/samples/method-nonexistent-helper/warnings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[{
22
"code": "invalid-callee",
3-
"message": "'foo' is an invalid callee (should be one of this.*, event.*, options.*, console.*, store.*, set, fire, destroy or bar). 'foo' exists on 'helpers', did you put it in the wrong place?",
3+
"message": "'foo' is an invalid callee (should be one of this.*, event.*, options.*, console.*, set, fire, destroy or bar). 'foo' exists on 'helpers', did you put it in the wrong place?",
44
"pos": 18,
55
"start": {
66
"line": 1,

test/validator/samples/method-nonexistent/warnings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[{
22
"code": "invalid-callee",
3-
"message": "'foo' is an invalid callee (should be one of this.*, event.*, options.*, console.*, store.*, set, fire, destroy or bar)",
3+
"message": "'foo' is an invalid callee (should be one of this.*, event.*, options.*, console.*, set, fire, destroy or bar)",
44
"pos": 18,
55
"start": {
66
"line": 1,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<button on:click="store.set({ foo: 'bar' })">click me</button>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[{
2+
"code": "invalid-callee",
3+
"message": "'store.set' is an invalid callee (did you mean '$set(...)'?)",
4+
"pos": 18,
5+
"start": {
6+
"line": 1,
7+
"column": 18,
8+
"character": 18
9+
},
10+
"end": {
11+
"line": 1,
12+
"column": 43,
13+
"character": 43
14+
}
15+
}]

test/validator/samples/window-event-invalid/warnings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[{
22
"code": "invalid-callee",
3-
"message": "'resize' is an invalid callee (should be one of this.*, event.*, options.*, console.*, store.*, set, fire or destroy)",
3+
"message": "'resize' is an invalid callee (should be one of this.*, event.*, options.*, console.*, set, fire or destroy)",
44
"start": {
55
"line": 1,
66
"column": 26,

0 commit comments

Comments
 (0)