Skip to content

Commit c738417

Browse files
authored
Merge pull request #281 from htmlhint/dev/coliff/replace-dprecated-apis
fix: Replace deprecated APIs
2 parents c81126c + 46e98ac commit c738417

File tree

5 files changed

+86
-78
lines changed

5 files changed

+86
-78
lines changed

.vscode/extensions.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"recommendations": [
33
"dbaeumer.vscode-eslint",
44
"editorconfig.editorconfig",
5+
"esbenp.prettier-vscode",
56
"streetsidesoftware.code-spell-checker"
67
]
78
}

.vscode/settings.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
"editor.codeActionsOnSave": {
33
"source.fixAll.eslint": "explicit"
44
},
5-
"editor.defaultFormatter": "esbenp.prettier-vscode",
6-
"editor.formatOnSave": true,
5+
"editor.defaultFormatter": "EditorConfig.EditorConfig",
6+
"[javascript][typescript][json][jsonc][markdown][yaml]": {
7+
"editor.defaultFormatter": "esbenp.prettier-vscode",
8+
"editor.formatOnSave": true
9+
},
710
"editor.renderWhitespace": "all",
811
"editor.trimAutoWhitespace": true,
912
"files.trimTrailingWhitespace": true

htmlhint-server/src/server.ts

Lines changed: 40 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -19,107 +19,63 @@
1919
*/
2020

2121
import * as path from "path";
22+
import * as fs from "fs";
2223
import {
2324
createConnection,
2425
TextDocuments,
2526
Diagnostic,
2627
DiagnosticSeverity,
27-
ProposedFeatures,
2828
InitializeParams,
2929
TextDocumentSyncKind,
3030
InitializeResult,
3131
Connection,
32-
ErrorMessageTracker,
3332
CancellationToken,
3433
CodeActionParams,
35-
Command,
3634
CodeAction,
3735
CodeActionKind,
3836
TextEdit,
3937
WorkspaceEdit,
40-
CodeDescription,
4138
} from "vscode-languageserver/node";
4239
import { TextDocument } from "vscode-languageserver-textdocument";
4340
import * as htmlhint from "htmlhint";
44-
import fs = require("fs");
4541
import { URI } from "vscode-uri";
4642
import ignore from "ignore";
47-
let stripJsonComments: any = require("strip-json-comments");
43+
import stripJsonComments from "strip-json-comments";
4844

4945
// Cache for gitignore patterns to avoid repeatedly parsing .gitignore files
50-
let gitignoreCache: Map<string, any> = new Map();
46+
let gitignoreCache: Map<string, ReturnType<typeof ignore>> = new Map();
5147

5248
// Cache for workspace root detection to avoid repeated filesystem calls
5349
let workspaceRootCache: Map<string, string | null> = new Map();
5450

51+
interface HtmlHintSettings {
52+
configFile: string;
53+
enable: boolean;
54+
options: Record<string, unknown>;
55+
optionsFile: string;
56+
ignoreGitignore: boolean;
57+
}
58+
5559
interface Settings {
56-
htmlhint: {
57-
configFile: string;
58-
enable: boolean;
59-
options: any;
60-
optionsFile: string;
61-
ignoreGitignore: boolean;
62-
};
63-
[key: string]: any;
60+
htmlhint: HtmlHintSettings;
61+
[key: string]: unknown;
62+
}
63+
64+
interface HtmlHintConfig {
65+
[key: string]: unknown;
6466
}
6567

6668
let settings: Settings | null = null;
67-
let linter: any = null;
69+
let linter: {
70+
verify: (text: string, config?: HtmlHintConfig) => htmlhint.Error[];
71+
} | null = null;
6872

6973
/**
7074
* This variable is used to cache loaded htmlhintrc objects. It is a dictionary from path -> config object.
7175
* A value of null means a .htmlhintrc object didn't exist at the given path.
7276
* A value of undefined means the file at this path hasn't been loaded yet, or should be reloaded because it changed
7377
*/
74-
let htmlhintrcOptions: any = {};
75-
76-
/**
77-
* Given an htmlhint Error object, approximate the text range highlight
78-
*/
79-
function getRange(error: htmlhint.Error, lines: string[]): any {
80-
let line = lines[error.line - 1];
81-
if (!line) {
82-
// Fallback if line doesn't exist
83-
return {
84-
start: {
85-
line: error.line - 1,
86-
character: error.col - 1,
87-
},
88-
end: {
89-
line: error.line - 1,
90-
character: error.col - 1,
91-
},
92-
};
93-
}
94-
95-
let isWhitespace = false;
96-
let curr = error.col;
97-
while (curr < line.length && !isWhitespace) {
98-
let char = line[curr];
99-
isWhitespace =
100-
char === " " ||
101-
char === "\t" ||
102-
char === "\n" ||
103-
char === "\r" ||
104-
char === "<";
105-
++curr;
106-
}
107-
108-
if (isWhitespace) {
109-
--curr;
110-
}
111-
112-
return {
113-
start: {
114-
line: error.line - 1, // Html-hint line numbers are 1-based.
115-
character: error.col - 1,
116-
},
117-
end: {
118-
line: error.line - 1,
119-
character: curr,
120-
},
121-
};
122-
}
78+
let htmlhintrcOptions: Record<string, HtmlHintConfig | null | undefined> = {};
12379

12480
/**
12581
* Given an htmlhint.Error type return a VS Code server Diagnostic object
@@ -157,8 +113,8 @@ function makeDiagnostic(
157113
* Get the HTMLHint configuration settings for the given HTML file. This method will take care of whether to use
158114
* VS Code settings, or to use a .htmlhintrc file.
159115
*/
160-
function getConfiguration(filePath: string): any {
161-
let options: any;
116+
function getConfiguration(filePath: string): HtmlHintConfig {
117+
let options: HtmlHintConfig | undefined;
162118

163119
trace(`[HTMLHint Debug] Getting configuration for file: ${filePath}`);
164120
trace(`[HTMLHint Debug] Current settings: ${JSON.stringify(settings)}`);
@@ -228,8 +184,8 @@ function getConfiguration(filePath: string): any {
228184
* Given the path of an HTML file, this function will look in current directory & parent directories
229185
* to find a .htmlhintrc file to use as the linter configuration. The settings are
230186
*/
231-
function findConfigForHtmlFile(base: string) {
232-
let options: any;
187+
function findConfigForHtmlFile(base: string): HtmlHintConfig | undefined {
188+
let options: HtmlHintConfig | undefined;
233189
trace(`[HTMLHint Debug] Looking for config starting from: ${base}`);
234190

235191
if (fs.existsSync(base)) {
@@ -285,8 +241,8 @@ function findConfigForHtmlFile(base: string) {
285241
/**
286242
* Given a path to a .htmlhintrc file, load it into a javascript object and return it.
287243
*/
288-
function loadConfigurationFile(configFile: string): any {
289-
let ruleset: any = null;
244+
function loadConfigurationFile(configFile: string): HtmlHintConfig | null {
245+
let ruleset: HtmlHintConfig | null = null;
290246
trace(`[HTMLHint Debug] Attempting to load config file: ${configFile}`);
291247
if (fs.existsSync(configFile)) {
292248
trace(`[HTMLHint Debug] Config file exists, reading: ${configFile}`);
@@ -339,16 +295,22 @@ function validateAllTextDocuments(
339295
return;
340296
}
341297

342-
let tracker = new ErrorMessageTracker();
298+
// Collect all errors and send them together instead of using ErrorMessageTracker
299+
const errors: string[] = [];
343300
documents.forEach((document) => {
344301
try {
345302
trace(`[DEBUG] Revalidating document: ${document.uri}`);
346303
validateTextDocument(connection, document);
347304
} catch (err) {
348-
tracker.add(getErrorMessage(err, document));
305+
errors.push(getErrorMessage(err, document));
349306
}
350307
});
351-
tracker.sendErrors(connection);
308+
309+
// Send all errors at once if there are any
310+
if (errors.length > 0) {
311+
connection.window.showErrorMessage(errors.join("\n"));
312+
}
313+
352314
trace(`[DEBUG] validateAllTextDocuments completed`);
353315
}
354316

@@ -363,7 +325,7 @@ function validateTextDocument(
363325
}
364326
}
365327

366-
let connection: Connection = createConnection(ProposedFeatures.all);
328+
let connection: Connection = createConnection();
367329
let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
368330
documents.listen(connection);
369331

@@ -1914,7 +1876,9 @@ connection.onInitialize(
19141876
: undefined;
19151877

19161878
// Since Files API is no longer available, we'll use embedded htmlhint directly
1917-
linter = htmlhint.default || htmlhint.HTMLHint || htmlhint;
1879+
linter = (htmlhint.default ||
1880+
htmlhint.HTMLHint ||
1881+
htmlhint) as typeof linter;
19181882

19191883
let result: InitializeResult = {
19201884
capabilities: {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta name="description" content="Tag No Obsolete Test" />
7+
<title>Tag No Obsolete Test</title>
8+
</head>
9+
<body>
10+
<!-- Test case 1: Obsolete acronym tag (should be converted to abbr) -->
11+
<acronym title="HyperText Markup Language">HTML</acronym>
12+
13+
<!-- Test case 2: Multiple acronym tags -->
14+
<p>This is an <acronym title="World Wide Web">WWW</acronym> example with <acronym title="Uniform Resource Locator">URL</acronym>.</p>
15+
16+
<!-- Test case 3: Acronym with attributes -->
17+
<acronym title="Cascading Style Sheets" class="highlight">CSS</acronym>
18+
19+
<!-- Test case 4: Already correct abbr tag - should not trigger -->
20+
<abbr title="JavaScript">JS</abbr>
21+
22+
<!-- Test case 5: Mixed case acronym -->
23+
<ACRONYM title="Extensible Markup Language">XML</ACRONYM>
24+
</body>
25+
</html>

test/autofix/test-autofixes.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@
4848

4949
<!-- More void elements -->
5050
<img src="footer.jpg">
51+
52+
<!-- Test case 1: Obsolete acronym tag (should be converted to abbr) -->
53+
<acronym title="HyperText Markup Language">HTML</acronym>
54+
55+
<!-- Test case 2: Multiple acronym tags -->
56+
<p>This is an <acronym title="World Wide Web">WWW</acronym> example with <acronym title="Uniform Resource Locator">URL</acronym>.</p>
57+
58+
<!-- Test case 3: Acronym with attributes -->
59+
<acronym title="Cascading Style Sheets" class="highlight">CSS</acronym>
60+
61+
<!-- Test case 4: Already correct abbr tag - should not trigger -->
62+
<abbr title="JavaScript">JS</abbr>
63+
64+
<!-- Test case 5: Mixed case acronym -->
65+
<ACRONYM title="Extensible Markup Language">XML</ACRONYM>
5166
<br />
5267
</body>
5368
</html>

0 commit comments

Comments
 (0)