Skip to content

Conversation

lumirlumir
Copy link
Member

@lumirlumir lumirlumir commented Oct 1, 2025

Prerequisites checklist

What is the purpose of this pull request?

Which language are you using?

JSONC and JSON5

What did you do?

I expected the return type of applyInlineConfig().configs — specifically loc.start and loc.end — to include an offset property, but they don't.

What did you expect to happen?

I expect the types of loc.start and loc.end to include an offset property.

Link to minimal reproducible Example

import { JSONSourceCode, JSONLanguage } from "./src/index.js";

const file = {
	body: '/* eslint json/no-duplicate-keys: "error" */ {}',
	path: "test.jsonc",
};
const language = new JSONLanguage({ mode: "jsonc" });
const parseResult = language.parse(file);
const jsonSourceCode = new JSONSourceCode({
	text: file.body,
	ast: parseResult.ast,
});

console.log(jsonSourceCode.applyInlineConfig().configs[0].loc);

If you run the code above, you'll see that jsonSourceCode.applyInlineConfig().configs[0].loc.start and jsonSourceCode.applyInlineConfig().configs[0].loc.end include an offset property. However, the return type doesn't reflect that offset property.

# Output
{
  start: { line: 1, column: 1, offset: 0 },
  end: { line: 1, column: 45, offset: 44 }
}

What changes did you make? (Give an overview)

Looking at the origin of the loc property returned by applyInlineConfig, you'll find that it directly uses the comment.loc.

loc: comment.loc,

The comment.loc property uses the JSONSyntaxElement['loc'] (LocationRange) type from @humanwhocodes/momoa as shown below, so technically it doesn’t match the SourceLocation type.

  • JSONSyntaxElement['loc'] (LocationRange)
interface LocationRange {
    start: {
        line: number;
        column: number;
        offset: number;
    }
    end: {
        line: number;
        column: number;
        offset: number;
    }
}
  • SourceLocation
interface SourceLocation {
    start: {
        line: number;
        column: number;
    }
    end: {
        line: number;
        column: number;
    }
}

To provide a more accurate return type, I've replaced the SourceLocation type with the JSONSyntaxElement['loc'] (LocationRange) type from @humanwhocodes/momoa.

Related Issues

Ref: eslint/markdown#548, eslint/css#281

Is there anything you'd like reviewers to focus on?

N/A

@eslint-github-bot eslint-github-bot bot added the bug Something isn't working label Oct 1, 2025
@eslintbot eslintbot added this to Triage Oct 1, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in Triage Oct 1, 2025
* Returns inline rule configurations along with any problems
* encountered while parsing the configurations.
* @returns {{problems:Array<FileProblem>,configs:Array<{config:{rules:RulesConfig},loc:SourceLocation}>}} Information
* @returns {{problems:Array<FileProblem>,configs:Array<{config:{rules:RulesConfig},loc:JSONSyntaxElement['loc']}>}} Information
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use LocationRange directly from @humanwhocodes/momoa, but that type isn't exported.

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably an oversight. You could open a PR at the momoa repo to fix that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted bug Something isn't working
Projects
Status: Implementing
Development

Successfully merging this pull request may close these issues.

2 participants