1
1
import * as namespaces from '../../utils/namespaces' ;
2
2
import validateElement from './validateElement' ;
3
3
import validateWindow from './validateWindow' ;
4
+ import fuzzymatch from '../utils/fuzzymatch'
5
+ import flattenReference from '../../utils/flattenReference' ;
4
6
import { Validator } from '../index' ;
5
7
import { Node } from '../../interfaces' ;
6
8
@@ -11,6 +13,9 @@ const meta = new Map([[':Window', validateWindow]]);
11
13
export default function validateHtml ( validator : Validator , html : Node ) {
12
14
let elementDepth = 0 ;
13
15
16
+ const refs = new Map ( ) ;
17
+ const refCallees : Node [ ] = [ ] ;
18
+
14
19
function visit ( node : Node ) {
15
20
if ( node . type === 'Element' ) {
16
21
if (
@@ -25,12 +30,12 @@ export default function validateHtml(validator: Validator, html: Node) {
25
30
}
26
31
27
32
if ( meta . has ( node . name ) ) {
28
- return meta . get ( node . name ) ( validator , node ) ;
33
+ return meta . get ( node . name ) ( validator , node , refs , refCallees ) ;
29
34
}
30
35
31
36
elementDepth += 1 ;
32
37
33
- validateElement ( validator , node ) ;
38
+ validateElement ( validator , node , refs , refCallees ) ;
34
39
} else if ( node . type === 'EachBlock' ) {
35
40
if ( validator . helpers . has ( node . context ) ) {
36
41
let c = node . expression . end ;
@@ -61,4 +66,20 @@ export default function validateHtml(validator: Validator, html: Node) {
61
66
}
62
67
63
68
html . children . forEach ( visit ) ;
69
+
70
+ refCallees . forEach ( callee => {
71
+ const { parts } = flattenReference ( callee ) ;
72
+ const ref = parts [ 1 ] ;
73
+
74
+ if ( refs . has ( ref ) ) {
75
+ // TODO check method is valid, e.g. `audio.stop()` should be `audio.pause()`
76
+ } else {
77
+ const match = fuzzymatch ( ref , Array . from ( refs . keys ( ) ) ) ;
78
+
79
+ let message = `'refs.${ ref } ' does not exist` ;
80
+ if ( match ) message += ` (did you mean 'refs.${ match } '?)` ;
81
+
82
+ validator . error ( message , callee . start ) ;
83
+ }
84
+ } ) ;
64
85
}
0 commit comments