forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (42 loc) · 1.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import validateJs from './js/index.js';
import { getLocator } from 'locate-character';
import getCodeFrame from '../utils/getCodeFrame.js';
export default function validate ( parsed, source, options ) {
const locator = getLocator( source );
const validator = {
error: ( message, pos ) => {
const { line, column } = locator( pos );
const error = new Error( message );
error.frame = getCodeFrame( source, line, column );
error.loc = { line: line + 1, column };
error.pos = pos;
error.toString = () => `${error.message} (${error.loc.line}:${error.loc.column})\n${error.frame}`;
if ( options.onerror ) {
options.onerror( error );
} else {
throw error;
}
},
warn: ( message, pos ) => {
const { line, column } = locator( pos );
const frame = getCodeFrame( source, line, column );
options.onwarn({
message,
frame,
loc: { line: line + 1, column },
pos,
toString: () => `${message} (${line + 1}:${column})\n${frame}`
});
},
templateProperties: {},
errors: [],
warnings: []
};
if ( parsed.js ) {
validateJs( validator, parsed.js );
}
return {
errors: validator.errors,
warnings: validator.warnings
};
}