-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
feat(remark-lint): add strict rules #8106
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
Draft
avivkeller
wants to merge
7
commits into
main
Choose a base branch
from
stricter-lnt-rules
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a5b4e3a
feat(remark-lint): add strict rules
avivkeller 907706d
fixup!
avivkeller 843ae46
add more checks
avivkeller 64dbee7
Update packages/remark-lint/README.md
avivkeller 6e74603
add auto fixing
avivkeller 1657f6e
fix typo
avivkeller 66398ed
fixup!
avivkeller 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
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
40 changes: 40 additions & 0 deletions
40
packages/remark-lint/src/rules/__tests__/invalid-type-reference.test.mjs
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,40 @@ | ||
import { describe, it } from 'node:test'; | ||
|
||
import { testRule } from './utils.mjs'; | ||
import invalidTypeReference from '../invalid-type-reference.mjs'; | ||
|
||
const testCases = [ | ||
{ | ||
name: 'no references', | ||
input: 'Just some text.', | ||
expected: [], | ||
}, | ||
{ | ||
name: 'single reference', | ||
input: 'Just a {number}.', | ||
expected: [], | ||
}, | ||
{ | ||
name: 'miswrapped reference', | ||
input: 'First a {string}, then a \\<number>.', | ||
expected: ['Type reference must be wrapped in "{}"; saw "<number>"'], | ||
}, | ||
{ | ||
name: 'multiple references', | ||
input: 'Psst, are you a {string | boolean}', | ||
expected: [ | ||
'Type reference should be separated by "|", without spaces; saw "{string | boolean}"', | ||
], | ||
}, | ||
{ | ||
name: 'invalid references', | ||
input: 'This is {invalid}.', | ||
expected: ['Invalid type reference: {invalid}'], | ||
}, | ||
]; | ||
|
||
describe('invalid-type-reference', () => { | ||
for (const { name, input, expected } of testCases) { | ||
it(name, () => testRule(invalidTypeReference, input, expected)); | ||
} | ||
}); |
66 changes: 31 additions & 35 deletions
66
packages/remark-lint/src/rules/duplicate-stability-nodes.mjs
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
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,49 @@ | ||
import { transformTypeToReferenceLink } from '@nodejs/doc-kit/src/utils/parser/index.mjs'; | ||
import createQueries from '@nodejs/doc-kit/src/utils/queries/index.mjs'; | ||
import { lintRule } from 'unified-lint-rule'; | ||
import { visit } from 'unist-util-visit'; | ||
|
||
const MATCH_RE = /\s\||\|\s/g; | ||
const REPLACE_RE = /\s*\|\s*/g; | ||
|
||
/** | ||
* Ensures that all type references are valid | ||
* @type {import('unified-lint-rule').Rule} | ||
*/ | ||
const invalidTypeReference = (tree, vfile) => { | ||
visit(tree, createQueries.UNIST.isTextWithType, node => { | ||
const types = node.value.match(createQueries.QUERIES.normalizeTypes); | ||
|
||
types.forEach(type => { | ||
// Ensure wrapped in {} | ||
if (type[0] !== '{' || type[type.length - 1] !== '}') { | ||
vfile.message( | ||
`Type reference must be wrapped in "{}"; saw "${type}"`, | ||
node | ||
); | ||
|
||
node.value = node.value.replace(type, `{${type.slice(1, -1)}}`); | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Fix spaces around | | ||
if (MATCH_RE.test(type)) { | ||
vfile.message( | ||
`Type reference should be separated by "|", without spaces; saw "${type}"`, | ||
node | ||
); | ||
|
||
const normalized = type.replace(REPLACE_RE, '|'); | ||
node.value = node.value.replace(type, normalized); | ||
} | ||
|
||
if (transformTypeToReferenceLink(type) === type) { | ||
vfile.message(`Invalid type reference: ${type}`, node); | ||
} | ||
}); | ||
avivkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}; | ||
|
||
export default lintRule( | ||
'node-core:invalid-type-reference', | ||
invalidTypeReference | ||
); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.