Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
diagnostics.push(
generateCompilerDiagnostic(ModuleResolutionErrors.RELATIVE_DYNAMIC_IMPORT)
);
}
const result = output[0];
code = result.code;
map = result.map;
} catch (e) {
// Rollup may have clobbered error.code with its own data
if (e instanceof CompilerError && (e as any).pluginCode) {
e.code = (e as any).pluginCode;
}
const diagnostic = normalizeToDiagnostic(ModuleResolutionErrors.MODULE_RESOLUTION_ERROR, e);
diagnostic.level = DiagnosticLevel.Fatal;
diagnostics.push(diagnostic);
}
return {
diagnostics,
code,
map,
};
}
test('compiler should report fatal diagnostic when invalid entry path is specified', async () => {
const COMPILER_CONFIG_BASEDIR = {
name: 'modules/foo',
namespace: 'x',
files: {
'foo.js': ``,
},
};
const { diagnostics, success } = await compile(COMPILER_CONFIG_BASEDIR);
expect(success).toBe(false);
expect(diagnostics[0].level).toBe(DiagnosticLevel.Fatal);
});
test('compiler returns diagnostic errors when module resolution encounters an error', async () => {
const config = {
name: 'foo',
namespace: 'x',
files: {
'foo.js': `import something from './nothing';`,
},
};
const { success, diagnostics } = await compile(config);
expect(success).toBe(false);
expect(diagnostics.length).toBe(1);
const { level, message, code } = diagnostics[0];
expect(level).toBe(DiagnosticLevel.Fatal);
expect(message).toContain(
'Failed to resolve import "./nothing" from "foo.js". Please add "nothing" file to the component folder.'
);
expect(code).toBe(1011);
});
test('compiler returns diagnostic errors when transformation encounters an error in javascript', async () => {
const config = {
name: 'foo',
namespace: 'x',
files: {
'foo.js': `throw`,
},
};
const { success, diagnostics } = await compile(config);
expect(success).toBe(false);
expect(diagnostics.length).toBe(1);
const { level, message } = diagnostics[0];
expect(level).toBe(DiagnosticLevel.Fatal);
expect(message).toContain('Unexpected token (1:5)');
});
name: 'foo',
namespace: 'x',
files: {
'foo.js': `import { LightningElement } from 'lwc';
export default class Test extends LightningElement {}
`,
'foo.html': `<template></template>`,
'foo.css': `a {`,
},
};
const { success, diagnostics } = await compile(config);
expect(success).toBe(false);
expect(diagnostics).toMatchObject([
{
level: DiagnosticLevel.Fatal,
message: expect.stringContaining('Unclosed block'),
},
]);
});
return diagnostics.some(d => {
return d.level === DiagnosticLevel.Error || d.level === DiagnosticLevel.Fatal;
});
}
return diagnostics.some(d => {
return d.level === DiagnosticLevel.Error || d.level === DiagnosticLevel.Fatal;
});
}
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,
};
});