Skip to content

Commit 8fba5da

Browse files
authored
feat: Pass file path to getContext (#820)
The `getContext` function now accepts a `path` argument, which is stored in the `Context` object. This path is then used in `updateRulesList` to correctly determine the plugin root when generating error messages related to missing rule list markers. Previously, this was done using `pathPlugin`, which was removed. The `pathPlugin` argument has been removed from `generateRuleHeaderLines` and `updateRulesList`. (This commit message was automatically generated by an LLM.)
1 parent 5b331cf commit 8fba5da

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

lib/context.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import { getEndOfLine } from './eol.js';
66
*/
77
export interface Context {
88
endOfLine: string;
9+
path: string;
910
}
1011

11-
export async function getContext(): Promise<Context> {
12+
export async function getContext(path: string): Promise<Context> {
1213
return {
1314
endOfLine: await getEndOfLine(),
15+
path,
1416
};
1517
}

lib/generator.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function stringOrArrayToArrayWithFallback(
6363

6464
// eslint-disable-next-line complexity
6565
export async function generate(path: string, options?: GenerateOptions) {
66-
const context = await getContext();
66+
const context = await getContext(path);
6767
const { endOfLine } = context;
6868

6969
const plugin = await loadPlugin(path);
@@ -200,7 +200,6 @@ export async function generate(path: string, options?: GenerateOptions) {
200200
plugin,
201201
configsToRules,
202202
pluginPrefix,
203-
path,
204203
pathRuleDoc,
205204
configEmojis,
206205
configFormat,
@@ -317,7 +316,6 @@ export async function generate(path: string, options?: GenerateOptions) {
317316
pluginPrefix,
318317
pathRuleDoc,
319318
pathToFile,
320-
path,
321319
configEmojis,
322320
configFormat,
323321
ignoreConfig,

lib/rule-doc-notices.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const RULE_NOTICES: {
105105
replacedBy: readonly string[] | undefined;
106106
plugin: Plugin;
107107
pluginPrefix: string;
108-
pathPlugin: string;
108+
path: string;
109109
pathRuleDoc: string | PathRuleDocFunction;
110110
type?: `${RULE_TYPE}`;
111111
urlRuleDoc?: string | UrlRuleDocFunction;
@@ -190,7 +190,7 @@ const RULE_NOTICES: {
190190
replacedBy,
191191
plugin,
192192
pluginPrefix,
193-
pathPlugin,
193+
path,
194194
pathRuleDoc,
195195
ruleName,
196196
urlRuleDoc,
@@ -200,7 +200,7 @@ const RULE_NOTICES: {
200200
replacementRuleName,
201201
plugin,
202202
pluginPrefix,
203-
pathPlugin,
203+
path,
204204
pathRuleDoc,
205205
replaceRulePlaceholder(pathRuleDoc, ruleName),
206206
true,
@@ -301,11 +301,11 @@ function getNoticesForRule(
301301
* Get the lines for the notice section at the top of a rule doc.
302302
*/
303303
function getRuleNoticeLines(
304+
context: Context,
304305
ruleName: string,
305306
plugin: Plugin,
306307
configsToRules: ConfigsToRules,
307308
pluginPrefix: string,
308-
pathPlugin: string,
309309
pathRuleDoc: string | PathRuleDocFunction,
310310
configEmojis: ConfigEmojis,
311311
configFormat: ConfigFormat,
@@ -314,6 +314,8 @@ function getRuleNoticeLines(
314314
urlConfigs?: string,
315315
urlRuleDoc?: string | UrlRuleDocFunction,
316316
) {
317+
const { path } = context;
318+
317319
const lines: string[] = [];
318320

319321
const rule = plugin.rules?.[ruleName];
@@ -393,7 +395,7 @@ function getRuleNoticeLines(
393395
replacedBy: rule.meta?.replacedBy,
394396
plugin,
395397
pluginPrefix,
396-
pathPlugin,
398+
path,
397399
pathRuleDoc,
398400
type: rule.meta?.type,
399401
urlRuleDoc,
@@ -498,7 +500,6 @@ export function generateRuleHeaderLines(
498500
plugin: Plugin,
499501
configsToRules: ConfigsToRules,
500502
pluginPrefix: string,
501-
pathPlugin: string,
502503
pathRuleDoc: string | PathRuleDocFunction,
503504
configEmojis: ConfigEmojis,
504505
configFormat: ConfigFormat,
@@ -513,11 +514,11 @@ export function generateRuleHeaderLines(
513514
return [
514515
makeRuleDocTitle(name, description, pluginPrefix, ruleDocTitleFormat),
515516
...getRuleNoticeLines(
517+
context,
516518
name,
517519
plugin,
518520
configsToRules,
519521
pluginPrefix,
520-
pathPlugin,
521522
pathRuleDoc,
522523
configEmojis,
523524
configFormat,

lib/rule-list.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ export function updateRulesList(
412412
pluginPrefix: string,
413413
pathRuleDoc: string | PathRuleDocFunction,
414414
pathRuleList: string,
415-
pathPlugin: string,
416415
configEmojis: ConfigEmojis,
417416
configFormat: ConfigFormat,
418417
ignoreConfig: readonly string[],
@@ -421,7 +420,7 @@ export function updateRulesList(
421420
urlConfigs?: string,
422421
urlRuleDoc?: string | UrlRuleDocFunction,
423422
): string {
424-
const { endOfLine } = context;
423+
const { endOfLine, path } = context;
425424

426425
let listStartIndex = markdown.indexOf(BEGIN_RULE_LIST_MARKER);
427426
let listEndIndex = markdown.indexOf(END_RULE_LIST_MARKER);
@@ -449,7 +448,7 @@ export function updateRulesList(
449448
if (listStartIndex === -1 || listEndIndex === -1) {
450449
throw new Error(
451450
`${relative(
452-
getPluginRoot(pathPlugin),
451+
getPluginRoot(path),
453452
pathRuleList,
454453
)} is missing rules list markers: ${BEGIN_RULE_LIST_MARKER}${END_RULE_LIST_MARKER}`,
455454
);
@@ -555,7 +554,7 @@ export function updateRulesList(
555554
configsToRules,
556555
plugin,
557556
pluginPrefix,
558-
pathPlugin,
557+
path,
559558
pathRuleDoc,
560559
pathRuleList,
561560
configEmojis,

test/lib/markdown-test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,36 @@ import { outdent } from 'outdent';
22
import { findSectionHeader } from '../../lib/markdown.js';
33
import { getContext } from '../../lib/context.js';
44

5+
const cwd = process.cwd();
6+
57
describe('markdown', function () {
68
describe('#findSectionHeader', function () {
79
it('handles standard section title', async function () {
8-
const context = await getContext();
10+
const context = await getContext(cwd);
911
const title = '## Rules\n';
1012
expect(findSectionHeader(context, title, 'rules')).toBe(title);
1113
});
1214

1315
it('handles section title with leading emoji', async function () {
14-
const context = await getContext();
16+
const context = await getContext(cwd);
1517
const title = '## 🍟 Rules\n';
1618
expect(findSectionHeader(context, title, 'rules')).toBe(title);
1719
});
1820

1921
it('handles section title with html', async function () {
20-
const context = await getContext();
22+
const context = await getContext(cwd);
2123
const title = "## <a name='Rules'></a>Rules\n";
2224
expect(findSectionHeader(context, title, 'rules')).toBe(title);
2325
});
2426

2527
it('handles sentential section title', async function () {
26-
const context = await getContext();
28+
const context = await getContext(cwd);
2729
const title = '## List of supported rules\n';
2830
expect(findSectionHeader(context, title, 'rules')).toBe(title);
2931
});
3032

3133
it('handles doc with multiple sections', async function () {
32-
const context = await getContext();
34+
const context = await getContext(cwd);
3335
expect(
3436
findSectionHeader(
3537
context,
@@ -47,7 +49,7 @@ describe('markdown', function () {
4749
});
4850

4951
it('handles doc with multiple rules-related sections', async function () {
50-
const context = await getContext();
52+
const context = await getContext(cwd);
5153
expect(
5254
findSectionHeader(
5355
context,

0 commit comments

Comments
 (0)