-
-
Notifications
You must be signed in to change notification settings - Fork 995
feat: add doctest-annotation-spacing ESLint rule
#8842
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
base: develop
Are you sure you want to change the base?
Conversation
This rule enforces spacing in return annotations in single-line
comments (`// returns` and `// =>`). It validates:
- Exactly 1 space between `//` and the annotation keyword
- Configurable spacing after the keyword (default: 1 space)
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
- task: lint_filenames
status: passed
- task: lint_editorconfig
status: passed
- task: lint_markdown
status: passed
- task: lint_package_json
status: passed
- task: lint_repl_help
status: na
- task: lint_javascript_src
status: passed
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: passed
- task: lint_javascript_tests
status: passed
- task: lint_javascript_benchmarks
status: na
- task: lint_python
status: na
- task: lint_r
status: na
- task: lint_c_src
status: na
- task: lint_c_examples
status: na
- task: lint_c_benchmarks
status: na
- task: lint_c_tests_fixtures
status: na
- task: lint_shell
status: na
- task: lint_typescript_declarations
status: passed
- task: lint_typescript_tests
status: na
- task: lint_license_headers
status: passed
---
Also register the rule in the ESLint plugin namespace.
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
- task: lint_filenames
status: passed
- task: lint_editorconfig
status: passed
- task: lint_markdown
status: passed
- task: lint_package_json
status: na
- task: lint_repl_help
status: na
- task: lint_javascript_src
status: passed
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: na
- task: lint_javascript_tests
status: na
- task: lint_javascript_benchmarks
status: na
- task: lint_python
status: na
- task: lint_r
status: na
- task: lint_c_src
status: na
- task: lint_c_examples
status: na
- task: lint_c_benchmarks
status: na
- task: lint_c_tests_fixtures
status: na
- task: lint_shell
status: na
- task: lint_typescript_declarations
status: passed
- task: lint_typescript_tests
status: na
- task: lint_license_headers
status: passed
---
doctest-annotation-spacing ESLint rule
| // returns 3.14 | ||
| ``` | ||
|
|
||
| Note: The spacing before the annotation keyword is always enforced to be exactly 1 space and is not configurable. |
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.
@Planeshifter What is the rationale for spacing being configurable after the keyword but not before?
lib/node_modules/@stdlib/_tools/eslint/rules/doctest-annotation-spacing/README.md
Outdated
Show resolved
Hide resolved
Signed-off-by: Athan <kgryte@gmail.com>
| { | ||
| 'ruleId': 'doctest-annotation-spacing', | ||
| 'severity': 2, | ||
| 'message': 'Return annotation `returns` should be followed by 1 space(s). Found 13 space(s).', |
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.
"Return annotation keyword returns"
IIUC, the entire comment is an "annotation".
lib/node_modules/@stdlib/_tools/eslint/rules/doctest-annotation-spacing/examples/index.js
Outdated
Show resolved
Hide resolved
Signed-off-by: Athan <kgryte@gmail.com>
| @@ -0,0 +1,3 @@ | |||
| { | |||
| "numSpaces": 1 | |||
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.
@Planeshifter Were you to support space configuration before and after keyword: beforeSpaces and afterSpaces.
| * - match a `//` literal | ||
| * | ||
| * - `(\s*)` | ||
| * - capture zero or more whitespace characters (the spacing before the keyword) |
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.
@Planeshifter I don't think this is actually what you want. \s matches multiple different types of whitespace, not just . Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes#types.
I think we should be explicit here. E.g., we should not be allowing tab characters or various Unicode whitespace characters. Just a regular space character.
| spacingAfter = matches[ 4 ].length; | ||
|
|
||
| // Check spacing before keyword (must be exactly 1 space): | ||
| if ( spacingBefore !== 1 ) { |
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.
As mentioned above, seems arbitrary to allow configuration of one but not the other.
| // Check spacing before keyword (must be exactly 1 space): | ||
| if ( spacingBefore !== 1 ) { | ||
| if ( prefix ) { | ||
| msg = 'Return annotation `' + prefix + ' ' + keyword + '` should have exactly 1 space after `//`. Found ' + spacingBefore + ' space(s).'; |
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.
We have string/format. We should use it here and below.
| // Check spacing after keyword (configurable): | ||
| expected = opts.numSpaces; | ||
| if ( spacingAfter !== expected ) { | ||
| msg = 'Return annotation `' + keyword + '` should be followed by ' + expected + ' space(s). Found ' + spacingAfter + ' space(s).'; |
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.
| msg = 'Return annotation `' + keyword + '` should be followed by ' + expected + ' space(s). Found ' + spacingAfter + ' space(s).'; | |
| msg = 'Return annotation keyword `' + keyword + '` should be followed by ' + expected + ' space(s). Found ' + spacingAfter + ' space(s).'; |
| 'properties': { | ||
| 'numSpaces': { | ||
| 'type': 'integer', | ||
| 'minimum': 1 |
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.
If we want this to be a "general" thing eventually which others can use, I'd allow the minimum to be 0.
kgryte
left a comment
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.
Left an initial round of comments.
Resolves stdlib-js/metr-issue-tracker#127.
Description
This pull request:
//)Related Issues
No.
Questions
No.
Other
No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
PR was generated by Claude Code according to my specifications.
@stdlib-js/reviewers