Skip to content

Commit d6621a7

Browse files
authored
fix: correct the return type of applyInlineConfig (#548)
* fix: correct the return type of `applyInlineConfig` * wip: add type test
1 parent 2862546 commit d6621a7

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/language/markdown-source-code.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import { findOffsets } from "../util.js";
2020
//-----------------------------------------------------------------------------
2121

2222
/**
23+
* @import { Position } from "unist";
2324
* @import { Root, Node, Html } from "mdast";
24-
* @import { TraversalStep, SourceLocation, FileProblem, DirectiveType, RulesConfig } from "@eslint/core";
25+
* @import { TraversalStep, FileProblem, DirectiveType, RulesConfig } from "@eslint/core";
2526
* @import { MarkdownLanguageOptions } from "../types.js";
2627
*/
2728

@@ -46,15 +47,15 @@ class InlineConfigComment {
4647

4748
/**
4849
* The position of the comment in the source code.
49-
* @type {SourceLocation}
50+
* @type {Position}
5051
*/
5152
position;
5253

5354
/**
5455
* Creates a new instance.
5556
* @param {Object} options The options for the instance.
5657
* @param {string} options.value The comment text.
57-
* @param {SourceLocation} options.position The position of the comment in the source code.
58+
* @param {Position} options.position The position of the comment in the source code.
5859
*/
5960
constructor({ value, position }) {
6061
this.value = value.trim();
@@ -127,7 +128,7 @@ function extractInlineConfigCommentsFromHTML(node) {
127128

128129
/**
129130
* Markdown Source Code Object
130-
* @extends {TextSourceCodeBase<{LangOptions: MarkdownLanguageOptions, RootNode: Root, SyntaxElementWithLoc: Node, ConfigNode: { value: string; position: SourceLocation }}>}
131+
* @extends {TextSourceCodeBase<{LangOptions: MarkdownLanguageOptions, RootNode: Root, SyntaxElementWithLoc: Node, ConfigNode: { value: string; position: Position }}>}
131132
*/
132133
export class MarkdownSourceCode extends TextSourceCodeBase {
133134
/**
@@ -261,13 +262,13 @@ export class MarkdownSourceCode extends TextSourceCodeBase {
261262
/**
262263
* Returns inline rule configurations along with any problems
263264
* encountered while parsing the configurations.
264-
* @returns {{problems:Array<FileProblem>,configs:Array<{config:{rules:RulesConfig},loc:SourceLocation}>}} Information
265+
* @returns {{problems:Array<FileProblem>,configs:Array<{config:{rules:RulesConfig},loc:Position}>}} Information
265266
* that ESLint needs to further process the rule configurations.
266267
*/
267268
applyInlineConfig() {
268269
/** @type {Array<FileProblem>} */
269270
const problems = [];
270-
/** @type {Array<{config:{rules:RulesConfig},loc:SourceLocation}>} */
271+
/** @type {Array<{config:{rules:RulesConfig},loc:Position}>} */
271272
const configs = [];
272273

273274
this.getInlineConfigNodes().forEach(comment => {

tests/types/types.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ typeof processorPlugins satisfies {};
124124
null as AssertAllNamesIn<RecommendedRuleName, RuleName>;
125125
}
126126

127+
{
128+
type ApplyInlineConfigLoc = ReturnType<
129+
MarkdownSourceCode["applyInlineConfig"]
130+
>["configs"][0]["loc"];
131+
132+
// Check that `applyInlineConfig`'s return type includes correct `loc` structure.
133+
const loc: ApplyInlineConfigLoc = {
134+
start: { line: 1, column: 1, offset: 0 },
135+
end: { line: 1, column: 1, offset: 0 },
136+
};
137+
}
138+
127139
(): MarkdownRuleDefinition => ({
128140
create({ sourceCode }): MarkdownRuleVisitor {
129141
sourceCode satisfies MarkdownSourceCode;
@@ -145,6 +157,10 @@ typeof processorPlugins satisfies {};
145157
sourceCode.getParent(node) satisfies Node | undefined;
146158
sourceCode.getAncestors(node) satisfies Node[];
147159
sourceCode.getText(node) satisfies string;
160+
sourceCode.applyInlineConfig().configs[0].loc.start
161+
.offset satisfies Position["start"]["offset"];
162+
sourceCode.applyInlineConfig().configs[0].loc.end
163+
.offset satisfies Position["end"]["offset"];
148164
}
149165

150166
return {

0 commit comments

Comments
 (0)