-
-
Notifications
You must be signed in to change notification settings - Fork 996
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| <!-- | ||
| @license Apache-2.0 | ||
| Copyright (c) 2025 The Stdlib Authors. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
|
|
||
| # doctest-annotation-spacing | ||
|
|
||
| > [ESLint rule][eslint-rules] to enforce spacing in return annotations in single-line comments. | ||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var rule = require( '@stdlib/_tools/eslint/rules/doctest-annotation-spacing' ); | ||
| ``` | ||
|
|
||
| #### rule | ||
|
|
||
| [ESLint rule][eslint-rules] to enforce spacing in return annotations in single-line comments. The rule checks `// returns` and `// =>` annotations (including `// e.g., returns` and `// e.g., =>` variants) and ensures: | ||
|
|
||
| - Exactly 1 space between `//` and the annotation keyword | ||
| - The configured number of spaces after the annotation keyword (default: 1) | ||
|
|
||
| **Bad** (too many spaces after `returns`): | ||
|
|
||
| <!-- eslint-disable stdlib/doctest-annotation-spacing --> | ||
|
|
||
| ```javascript | ||
| var v = 3.14; | ||
| // returns 3.14 | ||
| ``` | ||
|
|
||
| **Bad** (no space before `=>`): | ||
|
|
||
| <!-- eslint-disable stdlib/doctest-annotation-spacing, spaced-comment --> | ||
|
|
||
| ```javascript | ||
| console.log( 'beep' ); | ||
| //=> 'beep' | ||
| ``` | ||
|
|
||
| **Bad** (too many spaces before `returns`): | ||
|
|
||
| <!-- eslint-disable stdlib/doctest-annotation-spacing, spaced-comment --> | ||
|
|
||
| ```javascript | ||
| var x = true; | ||
| // returns true | ||
| ``` | ||
|
|
||
| **Good**: | ||
|
|
||
| ```javascript | ||
| var v = 3.14; | ||
| // returns 3.14 | ||
|
|
||
| console.log( 'beep' ); | ||
| // => 'beep' | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| The rule accepts an options object with the following property: | ||
|
|
||
| - **numSpaces**: number of spaces required after the annotation keyword. Default: `1`. | ||
|
|
||
| ```javascript | ||
| // With numSpaces: 2, the following would be valid: | ||
| var v = 3.14; | ||
| // returns 3.14 | ||
| ``` | ||
|
|
||
| Note: The spacing before the annotation keyword is always enforced to be exactly 1 space and is not configurable. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - This rule only applies to single-line comments (starting with `//`). Multi-line comments (`/* ... */`) are ignored. | ||
|
|
||
| - The rule matches the following patterns: | ||
|
|
||
| - `// returns` | ||
| - `// =>` | ||
| - `// e.g., returns` | ||
| - `// e.g., =>` | ||
|
|
||
| - The rule enforces two spacing requirements: | ||
|
|
||
| - Exactly 1 space after `//` (not configurable) | ||
| - Configurable spacing after the keyword (default: 1 space) | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var Linter = require( 'eslint' ).Linter; | ||
| var rule = require( '@stdlib/_tools/eslint/rules/doctest-annotation-spacing' ); | ||
|
|
||
| var linter = new Linter(); | ||
|
|
||
| var code = [ | ||
| 'var v = foo();', | ||
| '// returns \'beep\'', | ||
| '', | ||
| 'console.log( bar() );', | ||
| '// => \'boop\'' | ||
| ].join( '\n' ); | ||
|
|
||
| linter.defineRule( 'doctest-annotation-spacing', rule ); | ||
|
|
||
| var result = linter.verify( code, { | ||
| 'rules': { | ||
| 'doctest-annotation-spacing': 'error' | ||
| } | ||
| }); | ||
| /* returns | ||
| [ | ||
| { | ||
| 'ruleId': 'doctest-annotation-spacing', | ||
| 'severity': 2, | ||
| 'message': 'Return annotation `returns` should be followed by 1 space(s). Found 13 space(s).', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Return annotation keyword IIUC, the entire comment is an "annotation". |
||
| 'line': 2, | ||
| 'column': 1, | ||
| 'nodeType': null, | ||
| 'endLine': 2, | ||
| 'endColumn': 31 | ||
| } | ||
| ] | ||
| */ | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var Linter = require( 'eslint' ).Linter; | ||
| var rule = require( './../lib' ); | ||
|
|
||
| var linter = new Linter(); | ||
|
|
||
| var code = [ | ||
| 'var v = foo();', | ||
| '// returns \'beep\'', | ||
| '', | ||
| 'console.log( bar() );', | ||
| '// => \'boop\'' | ||
| ].join( '\n' ); | ||
|
|
||
| linter.defineRule( 'doctest-annotation-spacing', rule ); | ||
|
|
||
| var result = linter.verify( code, { | ||
| 'rules': { | ||
| 'doctest-annotation-spacing': 'error' | ||
| } | ||
| }); | ||
| console.log( result ); | ||
| /* => | ||
| [ | ||
| { | ||
| 'ruleId': 'doctest-annotation-spacing', | ||
| 'severity': 2, | ||
| 'message': 'Return annotation `returns` should be followed by 1 space(s). Found 13 space(s).', | ||
| 'line': 2, | ||
| 'column': 1, | ||
| 'nodeType': null, | ||
| 'endLine': 2, | ||
| 'endColumn': 31 | ||
| } | ||
| ] | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "numSpaces": 1 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Planeshifter Were you to support space configuration before and after keyword: |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2025 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * ESLint rule to enforce spacing after return annotations in single-line comments. | ||
| * | ||
| * @module @stdlib/_tools/eslint/rules/doctest-annotation-spacing | ||
| * | ||
| * @example | ||
| * var rule = require( '@stdlib/_tools/eslint/rules/doctest-annotation-spacing' ); | ||
| * | ||
| * console.log( rule ); | ||
| */ | ||
|
|
||
| // MODULES // | ||
|
|
||
| var main = require( './main.js' ); | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| module.exports = main; |
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?
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.
@kgryte I was debating this myself and we can definitely change it, but I was coming from the perspective that what constitutes a return annotation comment is that it starts with
// returnsor// =>(and thee.g.variants). If we loosen that, then at least a lot of our own tooling, which right now assumes this format, might not work as expected for external users in case they use a different number of leading spaces for their return annotations.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.
What then would be the rationale for allowing the spaces after the keyword to be configurable? I'd say either both are configurable or none.
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 fine to be more restrictive to start. And if we want to support configurability later the changes necessary are relatively straightforward.