-
Notifications
You must be signed in to change notification settings - Fork 91
feat: add no-heading-like-paragraph rule #716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gaic4o
wants to merge
3
commits into
eslint:main
Choose a base branch
from
Gaic4o:feat/no-heading-like-paragraph
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # no-heading-like-paragraph | ||
|
|
||
| Disallow paragraphs that look like ATX headings. | ||
|
|
||
| ## Background | ||
|
|
||
| In Markdown, an ATX heading opens with one to six hash (`#`) characters followed by a space, a tab, or a line ending, so `###### Installation` is a level 6 heading. Seven or more hash characters aren't heading syntax at all, so Markdown reads `####### Installation` as paragraph text that begins with seven literal hash characters. | ||
|
|
||
| This is almost always a typo, and it's easy to miss in review because the source still reads like a heading. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule flags a line of a paragraph that begins with seven or more hash characters followed by a space, a tab, a line ending, or the end of the paragraph. It checks continuation lines as well as the first line, because six or fewer hash characters in the same position would open a real heading. Block quote markers and up to three spaces of indentation may precede the hash characters, the same positions where an ATX heading is allowed to start. | ||
|
|
||
| This rule ignores anything that can't open an ATX heading. `#######Installation` has no whitespace to delimit the hash characters, `\####### Installation` and `####### Installation` escape their leading hash character on purpose, and four or more spaces of indentation are too many for a heading. | ||
|
|
||
| This rule provides suggestions rather than an automatic fix, because the number of hash characters alone doesn't reveal which correction the author intended: | ||
|
|
||
| * Replace the leading hash characters with `######`, which makes the paragraph a level 6 heading. `####### Installation` becomes `###### Installation`. | ||
| * Escape the leading hash character, which leaves the rendered output unchanged. `####### Installation` becomes `\####### Installation`. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```markdown | ||
| <!-- eslint markdown/no-heading-like-paragraph: "error" --> | ||
|
|
||
| ####### Installation | ||
|
|
||
| ######## Configuration | ||
|
|
||
| > ####### Usage | ||
|
|
||
| - ####### Options | ||
|
|
||
| Install the package first. | ||
| ####### Installation | ||
|
|
||
| > foo | ||
| > ####### hi | ||
| > bar | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```markdown | ||
| <!-- eslint markdown/no-heading-like-paragraph: "error" --> | ||
|
|
||
| ###### Installation | ||
|
|
||
| > ###### Usage | ||
|
|
||
| - ###### Options | ||
|
|
||
| #######Configuration | ||
|
|
||
| \####### Not a heading | ||
|
|
||
| Seven ####### characters in the middle of a paragraph. | ||
|
|
||
| Install the package first. | ||
| ###### Installation | ||
|
|
||
| > foo | ||
| > ###### hi | ||
| > bar | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| This rule has no options. | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| If you intentionally write paragraphs that begin with seven or more hash characters, you can safely disable this rule. | ||
|
|
||
| ## Prior Art | ||
|
|
||
| * [remark-lint-no-heading-like-paragraph](https://github.com/remarkjs/remark-lint/tree/main/packages/remark-lint-no-heading-like-paragraph) | ||
|
|
||
| ## Further Reading | ||
|
|
||
| * [CommonMark Spec: ATX Headings](https://spec.commonmark.org/0.31.2/#atx-headings) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /** | ||
| * @fileoverview Rule to disallow paragraphs that look like ATX headings in Markdown. | ||
| * @author Gaic4o | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Type Definitions | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * @import { MarkdownRuleDefinition } from "../types.js"; | ||
| * @typedef {"headingLikeParagraph" | "useMaxDepthHashes" | "escapeLeadingHash"} NoHeadingLikeParagraphMessageIds | ||
| * @typedef {[]} NoHeadingLikeParagraphOptions | ||
| * @typedef {MarkdownRuleDefinition<{ RuleOptions: NoHeadingLikeParagraphOptions, MessageIds: NoHeadingLikeParagraphMessageIds }>} NoHeadingLikeParagraphRuleDefinition | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Helpers | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Matches seven or more hash characters at the start of a line within a paragraph, | ||
| * followed by a space, a tab, a line ending, or the end of the paragraph. This mirrors | ||
| * the way CommonMark delimits the opening sequence of an ATX heading, so a no-break | ||
| * space doesn't count as a delimiter. | ||
| * | ||
| * This pattern avoids the `m` flag, which would also treat U+2028 and U+2029 as line | ||
| * boundaries even though Markdown doesn't. `(?:^|(?<=[\r\n]))` starts a new line only | ||
| * after an actual carriage return or line feed. | ||
| * | ||
| * Block quote markers and up to three spaces of indentation may precede the hash | ||
| * characters, because a heading with six or fewer hash characters would still open in | ||
| * that position. | ||
| */ | ||
| const headingLikeParagraphPattern = | ||
| /(?:^|(?<=[\r\n]))(?: {0,3}>[ \t]?)* {0,3}(?<hashes>#{7,})(?=[ \t\r\n]|$)/gu; | ||
|
|
||
| /** The longest opening sequence an ATX heading allows. */ | ||
| const maxDepthHashes = "######"; | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Rule Definition | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| export default /** @satisfies {NoHeadingLikeParagraphRuleDefinition} */ ({ | ||
| meta: { | ||
| type: "problem", | ||
|
|
||
| docs: { | ||
| description: "Disallow paragraphs that look like ATX headings", | ||
| url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-heading-like-paragraph.md", | ||
| }, | ||
|
|
||
| hasSuggestions: true, | ||
|
|
||
| messages: { | ||
| headingLikeParagraph: | ||
| "Unexpected paragraph starting with {{count}} hash characters. ATX headings support at most 6.", | ||
| useMaxDepthHashes: | ||
| 'Replace "{{hashes}}" with "{{maxDepthHashes}}".', | ||
| escapeLeadingHash: "Escape the leading hash character.", | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| const { sourceCode } = context; | ||
|
|
||
| return { | ||
| paragraph(node) { | ||
| /* | ||
| * Read the raw source text instead of the `value` of the first `text` | ||
| * child, because `value` already resolves character escapes and character | ||
| * references. Both `\####### Foo` and `####### Foo` render as a | ||
| * paragraph whose text starts with seven hash characters, but in each case | ||
| * the author escaped the leading hash on purpose. | ||
| */ | ||
| const text = sourceCode.getText(node); | ||
|
|
||
| /** @type {RegExpExecArray | null} */ | ||
| let match; | ||
|
|
||
| while ( | ||
| (match = headingLikeParagraphPattern.exec(text)) !== null | ||
| ) { | ||
| const { hashes } = match.groups; | ||
| const startOffset = | ||
| node.position.start.offset + | ||
| match.index + | ||
| match[0].length - | ||
| hashes.length; | ||
| const endOffset = startOffset + hashes.length; | ||
|
|
||
| context.report({ | ||
| loc: { | ||
| start: sourceCode.getLocFromIndex(startOffset), | ||
| end: sourceCode.getLocFromIndex(endOffset), | ||
| }, | ||
| messageId: "headingLikeParagraph", | ||
| data: { count: hashes.length }, | ||
| suggest: [ | ||
| { | ||
| messageId: "useMaxDepthHashes", | ||
| data: { hashes, maxDepthHashes }, | ||
| fix(fixer) { | ||
| return fixer.replaceTextRange( | ||
| [startOffset, endOffset], | ||
| maxDepthHashes, | ||
| ); | ||
| }, | ||
| }, | ||
| { | ||
| messageId: "escapeLeadingHash", | ||
| fix(fixer) { | ||
| return fixer.insertTextBeforeRange( | ||
| [startOffset, startOffset + 1], | ||
| "\\", | ||
| ); | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to add a dedicated
## Optionssection explaining that this rule has no options, since there was a prior request in ESLint to always include an options section even when none are available: eslint/eslint#20196There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me. I added a
## Optionssection to keep the documentation structure consistent.