1212 */
1313
1414/**
15+ * @typedef {{
16+ * line: number;
17+ * column: number;
18+ * offset: number;
19+ * }} Location
20+ *
21+ *
1522 * @typedef {PrettierOptions & {
1623 * onDiskFilepath: string;
1724 * parserMeta?: ESLint.ObjectMetaProperties['meta'];
2532 * options: Options,
2633 * fileInfoOptions: FileInfoOptions,
2734 * ) => string} PrettierFormat
35+ *
36+ *
37+ * @typedef {Parameters<Exclude<ESLint.Plugin['rules'], undefined>[string]['create']>[0] } RuleContext
2838 */
2939
3040'use strict' ;
@@ -57,10 +67,27 @@ let prettierFormat;
5767// Rule Definition
5868// ------------------------------------------------------------------------------
5969
70+ /**
71+ * @param {number[] } lineIndexes
72+ * @param {number } offset
73+ * @returns {Location }
74+ */
75+ function getLocFromOffset ( lineIndexes , offset ) {
76+ const withSentinel = [ - 1 , 0 , ...lineIndexes ] ;
77+
78+ let line = 0 ;
79+ while ( line + 1 < withSentinel . length && withSentinel [ line + 1 ] < offset ) {
80+ line ++ ;
81+ }
82+
83+ const column = offset - withSentinel [ line ] ;
84+ return { line, column, offset } ;
85+ }
86+
6087/**
6188 * Reports a difference.
6289 *
63- * @param {Rule. RuleContext } context - The ESLint rule context.
90+ * @param {RuleContext } context - The ESLint rule context.
6491 * @param {Difference } difference - The difference object.
6592 * @returns {void }
6693 */
@@ -71,8 +98,14 @@ function reportDifference(context, difference) {
7198 // `context.getSourceCode()` was deprecated in ESLint v8.40.0 and replaced
7299 // with the `sourceCode` property.
73100 // TODO: Only use property when our eslint peerDependency is >=8.40.0.
101+ const sourceCode = context . sourceCode ?? context . getSourceCode ( ) ;
102+ if ( ! ( 'text' in sourceCode ) ) {
103+ throw new Error ( 'prettier only supports textual source code' ) ;
104+ }
105+
106+ const lineIndexes = [ ...sourceCode . text . matchAll ( / \n / g) ] . map ( match => match . index ) ;
74107 const [ start , end ] = range . map ( index =>
75- ( context . sourceCode ?? context . getSourceCode ( ) ) . getLocFromIndex ( index ) ,
108+ getLocFromOffset ( lineIndexes , index )
76109 ) ;
77110
78111 context . report ( {
@@ -90,7 +123,7 @@ function reportDifference(context, difference) {
90123// Module Definition
91124// ------------------------------------------------------------------------------
92125
93- /** @type {ESLint.Plugin } */
126+ /** @satisfies {ESLint.Plugin } */
94127const eslintPluginPrettier = {
95128 meta : { name, version } ,
96129 configs : {
@@ -168,7 +201,7 @@ const eslintPluginPrettier = {
168201 const source = sourceCode . text ;
169202
170203 return {
171- Program ( node ) {
204+ [ sourceCode . ast . type ] ( node ) {
172205 if ( ! prettierFormat ) {
173206 // Prettier is expensive to load, so only load it if needed.
174207 prettierFormat = /** @type {PrettierFormat } */ (
@@ -251,7 +284,7 @@ const eslintPluginPrettier = {
251284
252285 for ( const difference of differences ) {
253286 reportDifference (
254- /** @type {Rule.RuleContext } */ ( context ) ,
287+ /** @type {Rule.RuleContext } */ ( context ) ,
255288 difference ,
256289 ) ;
257290 }
0 commit comments