Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
141 changes: 52 additions & 89 deletions src/rules/no-missing-atx-heading-space.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,9 @@
// Helpers
//-----------------------------------------------------------------------------

const leadingAtxHeadingHashPattern = /^(#{1,6})(?:[^# \t]|$)/u;
const leadingAtxHeadingHashPattern = /^(?<hashes>#{1,6})(?:[^# \t]|$)/gmu;
const trailingAtxHeadingHashPattern =
/(?<![ \t])([ \t]*)(?<=(?<!\\)(?:\\{2})*)(#+)([ \t]*)$/u;
const newLinePattern = /\r?\n/u;

/**
* Finds missing space before the closing hashes in an ATX heading.
* @param {string} text The input text to check.
* @returns {{ closingHashIdx: number, beforeHashIdx: number, endIdx: number } | null} The positions of the closing hashes in the heading, or null if no missing space is found.
*/
function findMissingSpaceBeforeClosingHash(text) {
const match = trailingAtxHeadingHashPattern.exec(text);

if (match) {
const [, closingSequenceSpaces, closingSequence, trailingSpaces] =
match;

if (closingSequenceSpaces.length === 0) {
const closingHashIdx =
text.length - (trailingSpaces.length + closingSequence.length);
const beforeHashIdx = closingHashIdx - 1;
const endIdx = closingHashIdx + closingSequence.length;
return {
closingHashIdx,
beforeHashIdx,
endIdx,
};
}
}

return null;
}
/(?<![ \t])(?<spaces>[ \t]*)(?<=(?<!\\)(?:\\{2})*)(?<hashes>#+)[ \t]*$/gu;

//-----------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -90,6 +61,7 @@ export default {
},

create(context) {
const { sourceCode } = context;
const [{ checkClosedHeadings }] = context.options;

return {
Expand All @@ -98,79 +70,70 @@ export default {
return;
}

const text = context.sourceCode.getText(node);
const lineNum = node.position.start.line;
const startColumn = node.position.start.column;

const missingSpace = findMissingSpaceBeforeClosingHash(text);
if (missingSpace) {
context.report({
loc: {
start: {
line: lineNum,
column:
startColumn + missingSpace.beforeHashIdx,
},
end: {
line: lineNum,
column: startColumn + missingSpace.endIdx,
},
},
messageId: "missingSpace",
data: { position: "before" },
fix(fixer) {
return fixer.insertTextBeforeRange(
[
node.position.start.offset +
missingSpace.closingHashIdx,
node.position.start.offset +
missingSpace.closingHashIdx +
1,
],
" ",
);
},
});
}
},
const text = sourceCode.getText(node);

paragraph(node) {
const text = context.sourceCode.getText(node);
const lines = text.split(newLinePattern);
const startColumn = node.position.start.column;
let offset = node.position.start.offset;
/** @type {RegExpExecArray | null} */
let match;

lines.forEach((line, idx) => {
const match = leadingAtxHeadingHashPattern.exec(line);
const lineNum = node.position.start.line + idx;
while (
(match = trailingAtxHeadingHashPattern.exec(text)) !== null
) {
const { spaces, hashes } = match.groups;

if (match) {
const hashes = match[1];
if (spaces.length === 0) {
const startOffset =
node.position.start.offset + match.index;
const endOffset = startOffset + hashes.length;

context.report({
loc: {
start: { line: lineNum, column: startColumn },
end: {
line: lineNum,
column: startColumn + hashes.length + 1,
},
start: sourceCode.getLocFromIndex(
startOffset - 1,
),
end: sourceCode.getLocFromIndex(endOffset),
},
messageId: "missingSpace",
data: { position: "after" },
data: { position: "before" },
fix(fixer) {
return fixer.insertTextAfterRange(
[
offset + hashes.length - 1,
offset + hashes.length,
],
return fixer.insertTextBeforeRange(
[startOffset, startOffset + 1],
" ",
);
},
});
}
}
},

paragraph(node) {
const text = sourceCode.getText(node);

offset += line.length + 1;
});
/** @type {RegExpExecArray | null} */
let match;

while (
(match = leadingAtxHeadingHashPattern.exec(text)) !== null
) {
const { hashes } = match.groups;
const startOffset =
node.position.start.offset + match.index;
const endOffset = startOffset + hashes.length;

context.report({
loc: {
start: sourceCode.getLocFromIndex(startOffset),
end: sourceCode.getLocFromIndex(endOffset + 1),
},
messageId: "missingSpace",
data: { position: "after" },
fix(fixer) {
return fixer.insertTextAfterRange(
[endOffset - 1, endOffset],
" ",
);
},
});
}
},
};
},
Expand Down
28 changes: 28 additions & 0 deletions tests/rules/no-missing-atx-heading-space.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,34 @@ const invalidTests = [
},
],
},
{
code: "Text before\r\n#Heading with ``` code markers\r\nText after",
output: "Text before\r\n# Heading with ``` code markers\r\nText after",
errors: [
{
messageId: "missingSpace",
data: { position: "after" },
line: 2,
column: 1,
endLine: 2,
endColumn: 3,
},
],
},
{
code: "Text before\r#Heading with ``` code markers\rText after",
output: "Text before\r# Heading with ``` code markers\rText after",
errors: [
{
messageId: "missingSpace",
data: { position: "after" },
line: 2,
column: 1,
endLine: 2,
endColumn: 3,
},
],
},
Comment on lines +481 to +494
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the tag to feat because this change can produce more lint errors. For example, the code from this test case was valid before this change.


{
code: " ##Heading 2",
Expand Down