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,29 @@ let prettierFormat;
5767// Rule Definition
5868// ------------------------------------------------------------------------------
5969
70+ /**
71+ * Converts a byte offset to a Location.
72+ *
73+ * See also `getLocFromIndex` in `@eslint/js`.
74+ *
75+ * @param {number[] } lineIndexes
76+ * @param {number } offset
77+ * @returns {Location }
78+ */
79+ function getLocFromOffset ( lineIndexes , offset ) {
80+ let line = 0 ;
81+ while ( line + 1 < lineIndexes . length && lineIndexes [ line + 1 ] < offset ) {
82+ line += 1 ;
83+ }
84+
85+ const column = offset - lineIndexes [ line ] ;
86+ return { line : line + 1 , column, offset } ;
87+ }
88+
6089/**
6190 * Reports a difference.
6291 *
63- * @param {Rule. RuleContext } context - The ESLint rule context.
92+ * @param {RuleContext } context - The ESLint rule context.
6493 * @param {Difference } difference - The difference object.
6594 * @returns {void }
6695 */
@@ -71,8 +100,16 @@ function reportDifference(context, difference) {
71100 // `context.getSourceCode()` was deprecated in ESLint v8.40.0 and replaced
72101 // with the `sourceCode` property.
73102 // TODO: Only use property when our eslint peerDependency is >=8.40.0.
103+ const sourceCode = context . sourceCode ?? context . getSourceCode ( ) ;
104+ if ( ! ( 'text' in sourceCode ) ) {
105+ throw new Error ( 'prettier/prettier: non-textual source code is unsupported' ) ;
106+ }
107+
108+ const lineIndexes = [ ...sourceCode . text . matchAll ( / \n / g) ] . map ( match => match . index ) ;
109+ // first line in the file starts at byte offset 0
110+ lineIndexes . unshift ( 0 ) ;
74111 const [ start , end ] = range . map ( index =>
75- ( context . sourceCode ?? context . getSourceCode ( ) ) . getLocFromIndex ( index ) ,
112+ getLocFromOffset ( lineIndexes , index )
76113 ) ;
77114
78115 context . report ( {
@@ -90,7 +127,7 @@ function reportDifference(context, difference) {
90127// Module Definition
91128// ------------------------------------------------------------------------------
92129
93- /** @type {ESLint.Plugin } */
130+ /** @satisfies {ESLint.Plugin } */
94131const eslintPluginPrettier = {
95132 meta : { name, version } ,
96133 configs : {
@@ -168,7 +205,7 @@ const eslintPluginPrettier = {
168205 const source = sourceCode . text ;
169206
170207 return {
171- Program ( node ) {
208+ [ sourceCode . ast . type ] ( node ) {
172209 if ( ! prettierFormat ) {
173210 // Prettier is expensive to load, so only load it if needed.
174211 prettierFormat = /** @type {PrettierFormat } */ (
@@ -251,7 +288,7 @@ const eslintPluginPrettier = {
251288
252289 for ( const difference of differences ) {
253290 reportDifference (
254- /** @type {Rule.RuleContext } */ ( context ) ,
291+ /** @type {Rule.RuleContext } */ ( context ) ,
255292 difference ,
256293 ) ;
257294 }
0 commit comments