From 563b039da592213f27fd00e46e7a45ca62223b24 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Sat, 20 Dec 2025 13:52:49 +0000 Subject: [PATCH] feat(linter/plugins): add `SourceCode#getLines` method (#17177) ESLint has an undocumented `SourceCode#getLines` method. Implement it. Discovered the existence of this method from conformance tests. --- apps/oxlint/conformance/snapshot.md | 334 +----------------- apps/oxlint/src-js/plugins/source_code.ts | 8 + .../oxlint/test/fixtures/sourceCode/plugin.ts | 1 + 3 files changed, 14 insertions(+), 329 deletions(-) diff --git a/apps/oxlint/conformance/snapshot.md b/apps/oxlint/conformance/snapshot.md index f1bd122144c16..08b5873d3bc58 100644 --- a/apps/oxlint/conformance/snapshot.md +++ b/apps/oxlint/conformance/snapshot.md @@ -7,9 +7,9 @@ | Status | Count | % | | ----------------- | ----- | ------ | | Total rules | 277 | 100.0% | -| Fully passing | 218 | 78.7% | +| Fully passing | 219 | 79.1% | | Partially passing | 58 | 20.9% | -| Fully failing | 1 | 0.4% | +| Fully failing | 0 | 0.0% | | Load errors | 0 | 0.0% | | No tests run | 0 | 0.0% | @@ -18,8 +18,8 @@ | Status | Count | % | | ----------- | ----- | ------ | | Total tests | 29765 | 100.0% | -| Passing | 28673 | 96.3% | -| Failing | 985 | 3.3% | +| Passing | 28684 | 96.4% | +| Failing | 974 | 3.3% | | Skipped | 107 | 0.4% | ## Fully Passing Rules @@ -172,6 +172,7 @@ - `no-sequences` (42 tests) - `no-spaced-func` (28 tests) - `no-sparse-arrays` (6 tests) +- `no-tabs` (11 tests) - `no-template-curly-in-string` (22 tests) - `no-ternary` (4 tests) - `no-throw-literal` (41 tests) @@ -282,7 +283,6 @@ - `no-shadow-restricted-names` - 43 / 44 (97.7%) - `no-shadow` - 300 / 308 (97.4%) - `no-sync` - 8 / 10 (80.0%) -- `no-tabs` - 0 / 11 (0.0%) - `no-trailing-spaces` - 53 / 54 (98.1%) - `no-undef` - 54 / 94 (57.4%) - `no-undefined` - 37 / 53 (69.8%) @@ -81432,330 +81432,6 @@ AssertionError [ERR_ASSERTION]: Should have 1 error but had 0: [] at apps/oxlint/dist/index.js -### `no-tabs` - -Pass: 0 / 11 (0.0%) -Fail: 11 / 11 (100.0%) -Skip: 0 / 11 (0.0%) - -#### no-tabs > valid - -```js -function test(){ -} -``` - -```json -{} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - -#### no-tabs > valid - -```js -function test(){ - // sdfdsf -} -``` - -```json -{} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - -#### no-tabs > valid - -```js - doSomething(); -``` - -```json -{ - "options": [ - { - "allowIndentationTabs": true - } - ] -} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - -#### no-tabs > valid - -```js - // comment -``` - -```json -{ - "options": [ - { - "allowIndentationTabs": true - } - ] -} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - -#### no-tabs > invalid - -```js -function test(){ } -``` - -```json -{ - "errors": [ - { - "messageId": "unexpectedTab", - "line": 1, - "column": 17, - "endLine": 1, - "endColumn": 18 - } - ] -} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - -#### no-tabs > invalid - -```js -/** comment test */ -``` - -```json -{ - "errors": [ - { - "messageId": "unexpectedTab", - "line": 1, - "column": 5, - "endLine": 1, - "endColumn": 6 - } - ] -} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - -#### no-tabs > invalid - -```js -function test(){ - // sdfdsf -} -``` - -```json -{ - "errors": [ - { - "messageId": "unexpectedTab", - "line": 2, - "column": 5, - "endLine": 2, - "endColumn": 6 - } - ] -} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - -#### no-tabs > invalid - -```js -function test(){ - //sdfdsf -} -``` - -```json -{ - "errors": [ - { - "messageId": "unexpectedTab", - "line": 1, - "column": 9, - "endLine": 1, - "endColumn": 10 - } - ] -} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - -#### no-tabs > invalid - -```js -function test(){ - // sdfdsf - } -``` - -```json -{ - "errors": [ - { - "messageId": "unexpectedTab", - "line": 2, - "column": 5, - "endLine": 2, - "endColumn": 6 - }, - { - "messageId": "unexpectedTab", - "line": 3, - "column": 1, - "endLine": 3, - "endColumn": 2 - } - ] -} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - -#### no-tabs > invalid - -```js - // Comment with leading tab and inline tab -``` - -```json -{ - "options": [ - { - "allowIndentationTabs": true - } - ], - "errors": [ - { - "messageId": "unexpectedTab", - "line": 1, - "column": 30, - "endLine": 1, - "endColumn": 31 - } - ] -} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - -#### no-tabs > invalid - -```js - a = b + c ; -``` - -```json -{ - "errors": [ - { - "messageId": "unexpectedTab", - "line": 1, - "column": 1, - "endLine": 1, - "endColumn": 3 - }, - { - "messageId": "unexpectedTab", - "line": 1, - "column": 6, - "endLine": 1, - "endColumn": 9 - }, - { - "messageId": "unexpectedTab", - "line": 1, - "column": 12, - "endLine": 1, - "endColumn": 13 - }, - { - "messageId": "unexpectedTab", - "line": 1, - "column": 14, - "endLine": 1, - "endColumn": 16 - }, - { - "messageId": "unexpectedTab", - "line": 1, - "column": 17, - "endLine": 1, - "endColumn": 19 - } - ] -} -``` - -TypeError: sourceCode.getLines is not a function - at Program (apps/oxlint/conformance/submodules/eslint/lib/rules/no-tabs.js:78:16) - at walkProgram (apps/oxlint/dist/lint.js) - at lintFileImpl (apps/oxlint/dist/lint.js) - at lint (apps/oxlint/dist/index.js) - - ### `no-trailing-spaces` Pass: 53 / 54 (98.1%) diff --git a/apps/oxlint/src-js/plugins/source_code.ts b/apps/oxlint/src-js/plugins/source_code.ts index e274109af159e..fa93e5efcf182 100644 --- a/apps/oxlint/src-js/plugins/source_code.ts +++ b/apps/oxlint/src-js/plugins/source_code.ts @@ -246,6 +246,14 @@ export const SOURCE_CODE = Object.freeze({ return ancestors.reverse(); }, + /** + * Get source text as array of lines, split according to specification's definition of line breaks. + */ + getLines(): string[] { + if (lines.length === 0) initLines(); + return lines; + }, + // Location methods getNodeByRangeIndex, getLocFromIndex: getLineColumnFromOffset, diff --git a/apps/oxlint/test/fixtures/sourceCode/plugin.ts b/apps/oxlint/test/fixtures/sourceCode/plugin.ts index b81b3e4b8b32a..f59510efcecf3 100644 --- a/apps/oxlint/test/fixtures/sourceCode/plugin.ts +++ b/apps/oxlint/test/fixtures/sourceCode/plugin.ts @@ -23,6 +23,7 @@ const createRule: Rule = { const { ast, text } = sourceCode; assert(context.getSourceCode() === sourceCode); + assert(sourceCode.getLines() === lines); let locs = ""; for (let offset = 0; offset <= text.length; offset++) {