Skip to content

Commit ee09d11

Browse files
committed
feat: support non-js languages
1 parent aa4935c commit ee09d11

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

eslint-plugin-prettier.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
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'];
@@ -25,6 +32,9 @@
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 [lineStart, line] = lineIndexes.reduce((acc, lineStart, lineNo) => lineStart < offset ? [lineStart, lineNo] : acc, [-1, -1]);
77+
if (line < 0) {
78+
const line = Math.max(0, lineIndexes.length - 1);
79+
const column = offset - Math.max(0, lineIndexes[line]);
80+
return { line, column, offset }
81+
}
82+
83+
const column = offset - lineStart;
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} */
94127
const 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

Comments
 (0)