From 5c5f63e18be55319672521be87589ba5ec2e2c33 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:21:30 +0000 Subject: [PATCH] chore: enable no-explicit-any rule as error level Removes our override such that `any` is no longer allowed, and replaces usages of it. --- eslint.config.js | 1 - .../parse5-html-rewriting-stream/lib/index.ts | 2 +- packages/parse5-parser-stream/lib/index.ts | 2 +- packages/parse5-sax-parser/lib/index.ts | 2 +- .../lib/parser/parser-location-info.test.ts | 75 ++++++++++++------- packages/parse5/lib/serializer/index.test.ts | 2 +- packages/parse5/lib/tokenizer/index.test.ts | 2 +- 7 files changed, 53 insertions(+), 33 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index 84a37651..b835b09e 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -60,7 +60,6 @@ export default [ rules: { '@typescript-eslint/no-unsafe-declaration-merging': 'off', '@typescript-eslint/no-non-null-assertion': 'warn', - '@typescript-eslint/no-explicit-any': 'warn', '@typescript-eslint/explicit-function-return-type': 'error', '@typescript-eslint/consistent-type-imports': 'error', diff --git a/packages/parse5-html-rewriting-stream/lib/index.ts b/packages/parse5-html-rewriting-stream/lib/index.ts index 9397849f..e052edbe 100644 --- a/packages/parse5-html-rewriting-stream/lib/index.ts +++ b/packages/parse5-html-rewriting-stream/lib/index.ts @@ -173,5 +173,5 @@ export interface RewritingStream { * @param event Name of the event * @param handler Event handler */ - on(event: string, handler: (...args: any[]) => void): this; + on(event: string, handler: (...args: never[]) => void): this; } diff --git a/packages/parse5-parser-stream/lib/index.ts b/packages/parse5-parser-stream/lib/index.ts index 36006eaf..33941488 100644 --- a/packages/parse5-parser-stream/lib/index.ts +++ b/packages/parse5-parser-stream/lib/index.ts @@ -144,5 +144,5 @@ export interface ParserStream void): this; + on(event: string, handler: (...args: never[]) => void): this; } diff --git a/packages/parse5-sax-parser/lib/index.ts b/packages/parse5-sax-parser/lib/index.ts index da95dec7..4de6f591 100644 --- a/packages/parse5-sax-parser/lib/index.ts +++ b/packages/parse5-sax-parser/lib/index.ts @@ -291,5 +291,5 @@ export interface SAXParser { * @param event Name of the event * @param handler Event handler */ - on(event: string, handler: (...args: any[]) => void): this; + on(event: string, handler: (...args: never[]) => void): this; } diff --git a/packages/parse5/lib/parser/parser-location-info.test.ts b/packages/parse5/lib/parser/parser-location-info.test.ts index 718f5210..2325fa61 100644 --- a/packages/parse5/lib/parser/parser-location-info.test.ts +++ b/packages/parse5/lib/parser/parser-location-info.test.ts @@ -1,6 +1,13 @@ import * as assert from 'node:assert'; import { outdent } from 'outdent'; -import { type ParserOptions, type TreeAdapterTypeMap, parse, parseFragment } from 'parse5'; +import { + type ParserOptions, + type TreeAdapterTypeMap, + parse, + parseFragment, + type DefaultTreeAdapterMap, + type Token, +} from 'parse5'; import { generateLocationInfoParserTests, assertStartTagLocation, @@ -230,32 +237,34 @@ generateTestsForEachTreeAdapter('location-info-parser', (treeAdapter) => { describe('location-info-parser', () => { it('Updating node source code location (GH-314)', () => { const sourceCodeLocationSetter = { - setNodeSourceCodeLocation(node: any, location: any): void { + setNodeSourceCodeLocation( + node: DefaultTreeAdapterMap['node'], + location: Token.ElementLocation | null, + ): void { node.sourceCodeLocation = location === null ? null : { - start: { - line: location.startLine, - column: location.startCol, - offset: location.startOffset, - }, - end: { - line: location.endLine, - column: location.endCol, - offset: location.endOffset, - }, + startLine: location.startLine * 2, + startCol: location.startCol * 2, + startOffset: location.startOffset * 2, + endLine: location.endLine * 2, + endCol: location.endCol * 2, + endOffset: location.endOffset * 2, }; }, - updateNodeSourceCodeLocation(node: any, endLocation: any): void { - node.sourceCodeLocation = { - start: node.sourceCodeLocation.start, - end: { - line: endLocation.endLine, - column: endLocation.endCol, - offset: endLocation.endOffset, - }, - }; + updateNodeSourceCodeLocation( + node: DefaultTreeAdapterMap['node'], + endLocation: Token.ElementLocation, + ): void { + if (node.sourceCodeLocation) { + node.sourceCodeLocation = { + ...node.sourceCodeLocation, + endLine: endLocation.endLine * 2, + endCol: endLocation.endCol * 2, + endOffset: endLocation.endOffset * 2, + }; + } }, }; const treeAdapter = { ...treeAdapters.default, ...sourceCodeLocationSetter }; @@ -270,18 +279,30 @@ describe('location-info-parser', () => { const [text] = body.childNodes; assert.deepEqual(doctype.sourceCodeLocation, { - start: { line: 1, column: 1, offset: 0 }, - end: { line: 1, column: 11, offset: 10 }, + startLine: 2, + startCol: 2, + startOffset: 0, + endLine: 2, + endCol: 22, + endOffset: 20, }); assert.strictEqual(html.sourceCodeLocation, null); assert.strictEqual(head.sourceCodeLocation, null); assert.deepEqual(body.sourceCodeLocation, { - start: { line: 1, column: 11, offset: 10 }, - end: { line: 1, column: 40, offset: 39 }, + startLine: 2, + startCol: 22, + startOffset: 20, + endLine: 2, + endCol: 80, + endOffset: 78, }); assert.deepEqual(text.sourceCodeLocation, { - start: { line: 1, column: 17, offset: 16 }, - end: { line: 1, column: 33, offset: 32 }, + startLine: 2, + startCol: 34, + startOffset: 32, + endLine: 2, + endCol: 66, + endOffset: 64, }); }); }); diff --git a/packages/parse5/lib/serializer/index.test.ts b/packages/parse5/lib/serializer/index.test.ts index 136e18bc..48f0733a 100644 --- a/packages/parse5/lib/serializer/index.test.ts +++ b/packages/parse5/lib/serializer/index.test.ts @@ -58,7 +58,7 @@ describe('serializer', () => { }); it('serializes unknown node to empty string', () => { - const unknown: any = {}; + const unknown = {} as never; assert.strictEqual(serialize(unknown), ''); assert.strictEqual(serializeOuter(unknown), ''); }); diff --git a/packages/parse5/lib/tokenizer/index.test.ts b/packages/parse5/lib/tokenizer/index.test.ts index 7b965be9..073546f6 100644 --- a/packages/parse5/lib/tokenizer/index.test.ts +++ b/packages/parse5/lib/tokenizer/index.test.ts @@ -52,7 +52,7 @@ describe('Tokenizer methods', () => { }); it('should throw if setting the state to an unknown value', () => { - const tokenizer = new Tokenizer(tokenizerOpts, {} as any); + const tokenizer = new Tokenizer(tokenizerOpts, {} as never); tokenizer.state = -1 as never; expect(() => tokenizer.write('foo', true)).toThrow('Unknown state'); });