You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/compiler/validate/index.js

55 lines
1.2 KiB

8 years ago
import validateJs from './js/index.js';
import { getLocator } from 'locate-character';
8 years ago
import getCodeFrame from '../utils/getCodeFrame.js';
8 years ago
8 years ago
export default function validate ( parsed, source, options ) {
8 years ago
const locator = getLocator( source );
const validator = {
error: ( message, pos ) => {
const { line, column } = locator( pos );
8 years ago
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;
}
8 years ago
},
warn: ( message, pos ) => {
const { line, column } = locator( pos );
8 years ago
const frame = getCodeFrame( source, line, column );
options.onwarn({
8 years ago
message,
8 years ago
frame,
loc: { line: line + 1, column },
8 years ago
pos,
8 years ago
toString: () => `${message} (${line + 1}:${column})\n${frame}`
8 years ago
});
},
templateProperties: {},
errors: [],
warnings: []
};
if ( parsed.js ) {
8 years ago
validateJs( validator, parsed.js );
8 years ago
}
return {
errors: validator.errors,
warnings: validator.warnings
};
}