/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { suite, test } from 'node:test'; import assert from 'node:assert/strict'; import { ClientCapabilities, Diagnostic, getLanguageModes, TextDocument } from '../modes/languageModes.js'; import { getNodeFileFS } from '../node/nodeFs.js'; const testUri = 'test://test/test.html'; async function getJavaScriptDiagnostics(value: string): Promise { const workspace = { settings: {}, folders: [{ name: 'x', uri: testUri.substr(0, testUri.lastIndexOf('/')) }] }; const document = TextDocument.create(testUri, 'html', 0, value); const languageModes = getLanguageModes({ css: true, javascript: true }, workspace, ClientCapabilities.LATEST, getNodeFileFS()); try { return await languageModes.getMode('javascript')!.doValidation!(document); } finally { languageModes.dispose(); } } suite('HTML JavaScript Validation', () => { test('isolates classic and module script declarations', async () => { const diagnostics = await getJavaScriptDiagnostics(''); assert.deepStrictEqual(diagnostics, []); }); test('isolates module var declarations from classic scripts', async () => { const diagnostics = await getJavaScriptDiagnostics(''); assert.deepStrictEqual(diagnostics, []); }); test('isolates declarations in separate module scripts', async () => { const diagnostics = await getJavaScriptDiagnostics(''); assert.deepStrictEqual(diagnostics, []); }); test('reports redeclarations in classic scripts', async () => { const diagnostics = await getJavaScriptDiagnostics(''); assert.deepStrictEqual(diagnostics.map(diagnostic => diagnostic.message), [ 'Cannot redeclare block-scoped variable \'value\'.', 'Cannot redeclare block-scoped variable \'value\'.' ]); }); test('accepts module syntax and top-level await', async () => { const diagnostics = await getJavaScriptDiagnostics(''); assert.deepStrictEqual(diagnostics, []); }); test('maps module diagnostics to the HTML document', async () => { const diagnostics = await getJavaScriptDiagnostics(''); assert.deepStrictEqual(diagnostics.map(diagnostic => ({ range: diagnostic.range, message: diagnostic.message })), [{ range: { start: { line: 1, character: 4 }, end: { line: 1, character: 9 } }, message: 'Cannot redeclare block-scoped variable \'value\'.' }, { range: { start: { line: 2, character: 4 }, end: { line: 2, character: 9 } }, message: 'Cannot redeclare block-scoped variable \'value\'.' }]); }); });