Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/plugin-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ export function getConfigsThatSetARule(
export function getConfigsForRule(
ruleName: string,
configsToRules: ConfigsToRules,
pluginPrefix: string,
pluginPrefix: string | undefined,
severityType?: SEVERITY_TYPE,
) {
const severity = severityType
? SEVERITY_TYPE_TO_SET[severityType]
: undefined;
const configNames: Array<keyof typeof configsToRules> = [];
const prefix = pluginPrefix ? `${pluginPrefix}/` : '';

for (const configName in configsToRules) {
const rules = configsToRules[configName];
const value = rules[`${pluginPrefix}/${ruleName}`];
const value = rules[`${prefix}${ruleName}`];
const isSet =
((typeof value === 'string' || typeof value === 'number') &&
(!severity || severity.has(value))) ||
Expand Down
28 changes: 24 additions & 4 deletions lib/rule-doc-notices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,13 @@ function getRuleNoticeLines(
function makeRuleDocTitle(
name: string,
description: string | undefined,
pluginPrefix: string,
pluginPrefix: string | undefined,
ruleDocTitleFormat: RuleDocTitleFormat,
) {
const descriptionFormatted = description
? removeTrailingPeriod(toSentenceCase(description))
: undefined;
const prefix = pluginPrefix ? `${pluginPrefix}/` : '';

let ruleDocTitleFormatWithFallback: RuleDocTitleFormat = ruleDocTitleFormat;

Expand Down Expand Up @@ -468,13 +469,13 @@ function makeRuleDocTitle(
'Attempting to display non-existent description in rule doc title.',
);
}
return `# ${descriptionFormatted} (\`${pluginPrefix}/${name}\`)`;
return `# ${descriptionFormatted} (\`${prefix}${name}\`)`;
}
case 'name': {
return `# ${name}`;
}
case 'prefix-name': {
return `# ${pluginPrefix}/${name}`;
return `# ${prefix}${name}`;
}
/* istanbul ignore next -- this shouldn't happen */
default: {
Expand All @@ -487,6 +488,24 @@ function makeRuleDocTitle(
}
}

function detectPrefixFromConfigsToRules(
ruleName: string,
configsToRules: ConfigsToRules,
): string | undefined {
const prefixes = new Set<string>();

for (const rules of Object.values(configsToRules)) {
for (const ruleId of Object.keys(rules)) {
const [prefix, name] = ruleId.split('/');
if (name === ruleName && prefix) {
prefixes.add(prefix);
}
}
}

return prefixes.size === 1 ? [...prefixes][0] : undefined;
}

/**
* Generate a rule doc header for a particular rule.
* @returns {string} - new header including marker
Expand All @@ -508,10 +527,11 @@ export function generateRuleHeaderLines(
urlConfigs?: string,
urlRuleDoc?: string | UrlRuleDocFunction,
): string {
const prefix = detectPrefixFromConfigsToRules(name, configsToRules);
const { endOfLine } = context;

return [
makeRuleDocTitle(name, description, pluginPrefix, ruleDocTitleFormat),
makeRuleDocTitle(name, description, prefix, ruleDocTitleFormat),
...getRuleNoticeLines(
name,
plugin,
Expand Down
19 changes: 11 additions & 8 deletions lib/rule-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function pathToUrl(path: string): string {
export function getUrlToRule(
ruleName: string,
ruleSource: RULE_SOURCE,
pluginPrefix: string,
pluginPrefix: string | undefined,
pathPlugin: string,
pathRuleDoc: string | PathRuleDocFunction,
pathCurrentPage: string,
Expand All @@ -51,10 +51,12 @@ export function getUrlToRule(
}
}

const prefix = pluginPrefix ? `${pluginPrefix}/` : '';

// Ignore plugin prefix if it's included in rule name.
// While we could display the prefix if we wanted, it definitely cannot be part of the link.
const ruleNameWithoutPluginPrefix = ruleName.startsWith(`${pluginPrefix}/`)
? ruleName.slice(pluginPrefix.length + 1)
const ruleNameWithoutPluginPrefix = ruleName.startsWith(prefix)
? ruleName.slice(prefix.length + 1)
: ruleName;

// If the URL is a function, evaluate it.
Expand Down Expand Up @@ -85,16 +87,17 @@ export function getUrlToRule(
export function getLinkToRule(
ruleName: string,
plugin: Plugin,
pluginPrefix: string,
pluginPrefix: string | undefined,
pathPlugin: string,
pathRuleDoc: string | PathRuleDocFunction,
pathCurrentPage: string,
includeBackticks: boolean,
includePrefix: boolean,
urlRuleDoc?: string | UrlRuleDocFunction,
) {
const ruleNameWithoutPluginPrefix = ruleName.startsWith(`${pluginPrefix}/`)
? ruleName.slice(pluginPrefix.length + 1)
const prefix = pluginPrefix ? `${pluginPrefix}/` : '';
const ruleNameWithoutPluginPrefix = ruleName.startsWith(prefix)
? ruleName.slice(prefix.length + 1)
: ruleName;

// Determine what plugin this rule comes from.
Expand All @@ -108,10 +111,10 @@ export function getLinkToRule(
ruleSource = RULE_SOURCE.eslintCore;
}

const ruleNameWithPluginPrefix = ruleName.startsWith(`${pluginPrefix}/`)
const ruleNameWithPluginPrefix = ruleName.startsWith(prefix)
? ruleName
: ruleSource === RULE_SOURCE.self
? `${pluginPrefix}/${ruleName}`
? `${prefix}${ruleName}`
: undefined;

const urlToRule = getUrlToRule(
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/cjs/docs/rules/no-foo.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Disallow foo (`test/no-foo`)
# Disallow foo (`no-foo`)

<!-- end auto-generated rule header -->
2 changes: 1 addition & 1 deletion test/lib/generate/__snapshots__/cjs-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports[`generate (cjs) basic generates the documentation 1`] = `
`;

exports[`generate (cjs) basic generates the documentation 2`] = `
"# Disallow foo (\`test/no-foo\`)
"# Disallow foo (\`no-foo\`)

<!-- end auto-generated rule header -->
"
Expand Down
10 changes: 5 additions & 5 deletions test/lib/generate/__snapshots__/comment-markers-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ exports[`generate (comment markers) no existing comment markers - minimal doc co
`;

exports[`generate (comment markers) no existing comment markers - minimal doc content generates the documentation 2`] = `
"# Description of no-foo (\`test/no-foo\`)
"# Description of no-foo (\`no-foo\`)

🔧 This rule is automatically fixable by the [\`--fix\` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

Expand Down Expand Up @@ -79,7 +79,7 @@ Existing rules section content."
`;

exports[`generate (comment markers) no existing comment markers - with no blank lines in existing content generates the documentation 2`] = `
"# Description of no-foo (\`test/no-foo\`)
"# Description of no-foo (\`no-foo\`)

🔧 This rule is automatically fixable by the [\`--fix\` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

Expand All @@ -103,7 +103,7 @@ Existing rules section content."
`;

exports[`generate (comment markers) no existing comment markers - with one blank line around existing content generates the documentation 2`] = `
"# Description of no-foo (\`test/no-foo\`)
"# Description of no-foo (\`no-foo\`)

🔧 This rule is automatically fixable by the [\`--fix\` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

Expand Down Expand Up @@ -173,7 +173,7 @@ No blank line before this."
`;

exports[`generate (comment markers) with no blank lines around comment markers generates the documentation 2`] = `
"# Description of no-foo (\`test/no-foo\`)
"# Description of no-foo (\`no-foo\`)

🔧 This rule is automatically fixable by the [\`--fix\` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

Expand All @@ -200,7 +200,7 @@ One blank line before this."
`;

exports[`generate (comment markers) with one blank line around comment markers generates the documentation 2`] = `
"# Description of no-foo (\`test/no-foo\`)
"# Description of no-foo (\`no-foo\`)

🔧 This rule is automatically fixable by the [\`--fix\` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

Expand Down
12 changes: 6 additions & 6 deletions test/lib/generate/__snapshots__/eol-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ exports[`getEndOfLine with a ".editorconfig" file generates using the correct en
`;

exports[`getEndOfLine with a ".editorconfig" file generates using the correct end of line when ".editorconfig" exists generates using crlf end of line from ".editorconfig" 3`] = `
"# test/B
"# B

<!-- end auto-generated rule header -->
"
`;

exports[`getEndOfLine with a ".editorconfig" file generates using the correct end of line when ".editorconfig" exists generates using crlf end of line from ".editorconfig" 4`] = `
"# test/c
"# c

<!-- end auto-generated rule header -->
"
Expand Down Expand Up @@ -65,14 +65,14 @@ exports[`getEndOfLine with a ".editorconfig" file generates using the correct en
`;

exports[`getEndOfLine with a ".editorconfig" file generates using the correct end of line when ".editorconfig" exists generates using lf end of line from ".editorconfig" 3`] = `
"# test/B
"# B

<!-- end auto-generated rule header -->
"
`;

exports[`getEndOfLine with a ".editorconfig" file generates using the correct end of line when ".editorconfig" exists generates using lf end of line from ".editorconfig" 4`] = `
"# test/c
"# c

<!-- end auto-generated rule header -->
"
Expand Down Expand Up @@ -104,14 +104,14 @@ exports[`getEndOfLine with a ".editorconfig" file generates using the correct en
`;

exports[`getEndOfLine with a ".editorconfig" file generates using the correct end of line when ".editorconfig" exists generates using the end of line from ".editorconfig" while respecting the .md specific end of line setting 3`] = `
"# test/B
"# B

<!-- end auto-generated rule header -->
"
`;

exports[`getEndOfLine with a ".editorconfig" file generates using the correct end of line when ".editorconfig" exists generates using the end of line from ".editorconfig" while respecting the .md specific end of line setting 4`] = `
"# test/c
"# c

<!-- end auto-generated rule header -->
"
Expand Down
12 changes: 6 additions & 6 deletions test/lib/generate/__snapshots__/file-paths-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports[`generate (file paths) custom path to rule docs and rules list generates
`;

exports[`generate (file paths) custom path to rule docs and rules list generates the documentation 2`] = `
"# test/no-foo
"# no-foo

<!-- end auto-generated rule header -->
"
Expand All @@ -28,7 +28,7 @@ exports[`generate (file paths) custom path to rule docs and rules list generates
`;

exports[`generate (file paths) custom path to rule docs and rules list generates the documentation using a function for pathRuleDoc 2`] = `
"# test/no-foo
"# no-foo

<!-- end auto-generated rule header -->
"
Expand All @@ -55,14 +55,14 @@ exports[`generate (file paths) lowercase README file generates the documentation
`;

exports[`generate (file paths) missing rule doc when initRuleDocs is true creates the rule doc 1`] = `
"# test/no-foo
"# no-foo

<!-- end auto-generated rule header -->
"
`;

exports[`generate (file paths) missing rule doc when initRuleDocs is true creates the rule doc 2`] = `
"# test/no-bar
"# no-bar

<!-- end auto-generated rule header -->

Expand All @@ -79,7 +79,7 @@ exports[`generate (file paths) missing rule doc when initRuleDocs is true create
`;

exports[`generate (file paths) missing rule doc, initRuleDocs is true, and with ruleDocSectionInclude creates the rule doc including the mandatory section 1`] = `
"# test/no-foo
"# no-foo

<!-- end auto-generated rule header -->

Expand All @@ -88,7 +88,7 @@ exports[`generate (file paths) missing rule doc, initRuleDocs is true, and with
`;

exports[`generate (file paths) missing rule doc, initRuleDocs is true, and with ruleDocSectionInclude creates the rule doc including the mandatory section 2`] = `
"# test/no-bar
"# no-bar

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion test/lib/generate/__snapshots__/general-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ details"
`;

exports[`generate (general) basic updates the documentation 4`] = `
"# Description of no-boz (\`test/no-baz\`)
"# Description of no-boz (\`no-baz\`)

<!-- end auto-generated rule header -->
## Rule details
Expand Down
2 changes: 1 addition & 1 deletion test/lib/generate/__snapshots__/option-check-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exports[`generate (--check) basic prints the issues, exits with failure, and doe
"- Expected
+ Received

- # Description for no-foo (\`test/no-foo\`)
- # Description for no-foo (\`no-foo\`)
-
- <!-- end auto-generated rule header -->
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Located at README.md"
`;

exports[`generate (postprocess option) basic calls the postprocessor 2`] = `
"# test/no-foo
"# no-foo

<!-- end auto-generated rule header -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports[`generate (--rule-doc-notices) basic shows the right rule doc notices 1`
`;

exports[`generate (--rule-doc-notices) basic shows the right rule doc notices 2`] = `
"# Description for no-foo (\`test/no-foo\`)
"# Description for no-foo (\`no-foo\`)

💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports[`generate (--rule-doc-title-format) desc uses the right rule doc title f
`;

exports[`generate (--rule-doc-title-format) desc uses the right rule doc title format 2`] = `
"# test/no-bar
"# no-bar

<!-- end auto-generated rule header -->
"
Expand All @@ -29,14 +29,14 @@ exports[`generate (--rule-doc-title-format) desc-parens-name uses the right rule
`;

exports[`generate (--rule-doc-title-format) desc-parens-prefix-name uses the right rule doc title format, with fallback when missing description 1`] = `
"# Description for no-foo (\`test/no-foo\`)
"# Description for no-foo (\`no-foo\`)

<!-- end auto-generated rule header -->
"
`;

exports[`generate (--rule-doc-title-format) desc-parens-prefix-name uses the right rule doc title format, with fallback when missing description 2`] = `
"# test/no-bar
"# no-bar

<!-- end auto-generated rule header -->
"
Expand All @@ -50,7 +50,7 @@ exports[`generate (--rule-doc-title-format) name uses the right rule doc title f
`;

exports[`generate (--rule-doc-title-format) prefix-name uses the right rule doc title format 1`] = `
"# test/no-foo
"# no-foo

<!-- end auto-generated rule header -->
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports[`generate (--rule-list-columns) basic shows the right columns and legend
`;

exports[`generate (--rule-list-columns) basic shows the right columns and legend 2`] = `
"# Description for no-foo (\`test/no-foo\`)
"# Description for no-foo (\`no-foo\`)

❌ This rule is deprecated.

Expand Down Expand Up @@ -68,7 +68,7 @@ exports[`generate (--rule-list-columns) shows column and notice for requiresType
`;

exports[`generate (--rule-list-columns) shows column and notice for requiresTypeChecking updates the documentation 3`] = `
"# Description of no-bar (\`test/no-bar\`)
"# Description of no-bar (\`no-bar\`)

💭 This rule requires [type information](https://typescript-eslint.io/linting/typed-linting).

Expand Down
Loading
Loading