Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function compileToFunction(source: string): Function {
const options = mergeConfig({});
options.format = 'function';
const state = new State(source, options);
const parsingResults = parseTemplate(source, state);
for (const warning of parsingResults.warnings) {
if (warning.level === DiagnosticLevel.Error) {
throw CompilerError.from(warning);
} else if (warning.level === DiagnosticLevel.Warning) {
/* eslint-disable-next-line no-console */
console.warn(warning.message);
} else {
/* eslint-disable-next-line no-console */
console.log(warning.message);
}
}
if (!parsingResults.root) {
throw generateCompilerError(TemplateErrors.INVALID_TEMPLATE);
}
const { code } = generate(parsingResults.root, state);
return new Function(TEMPLATE_MODULES_PARAMETER, code);
}
import { DiagnosticLevel } from '@lwc/errors';
import templateCompiler from '@lwc/template-compiler';
import { TextDocument, Diagnostic, DiagnosticSeverity, Range } from 'vscode-languageserver';
import { DIAGNOSTIC_SOURCE } from '../constants';
const LEVEL_MAPPING: Map = new Map([
[DiagnosticLevel.Log, DiagnosticSeverity.Information],
[DiagnosticLevel.Warning, DiagnosticSeverity.Warning],
[DiagnosticLevel.Error, DiagnosticSeverity.Error],
[DiagnosticLevel.Fatal, DiagnosticSeverity.Error],
]);
export default function lint(document: TextDocument): Diagnostic[] {
const source = document.getText();
const { warnings } = templateCompiler(source, {});
return warnings.map(warning => {
const { start = 0, length = 0 } = warning.location || { start: 0, length: 0 };
return {
range: toRange(document, start, length),
message: warning.message,
severity: LEVEL_MAPPING.get(warning.level),
source: DIAGNOSTIC_SOURCE,