Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This rule checks that each line of the jsdoc comment starts with an asterisk. For composability with other rules, this does not check the alignment or indentation of the comment content. fix #199
- Loading branch information
Eli Skeggs
committed
Dec 14, 2019
1 parent
6014360
commit 6a2217b
Showing
7 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,8 @@ | ||
### `check-prefix` | ||
|
||
Ensures that each JSDoc line starts with an `*`. | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|N/a| |
This file contains 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 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 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,41 @@ | ||
import iterateJsdoc from '../iterateJsdoc'; | ||
|
||
const prefixMatch = /^(\s+)(?:\*( ?))?/u; | ||
const validPrefix = /^\s+\*(?:\/?$| )/u; | ||
|
||
export default iterateJsdoc(({ | ||
sourceCode, | ||
jsdocNode, | ||
report, | ||
}) => { | ||
const fix = (fixer) => { | ||
const replacement = sourceCode.getText(jsdocNode).split('\n') | ||
.map((line, index) => { | ||
return index && !validPrefix.test(line) ? line.replace(prefixMatch, (_, $1, $2) => { | ||
return `${$1}*${$2 || ' '}`; | ||
}) : line; | ||
}) | ||
.join('\n'); | ||
|
||
return fixer.replaceText(jsdocNode, replacement); | ||
}; | ||
|
||
sourceCode.getText(jsdocNode).split('\n').some((line, index) => { | ||
const lineNum = parseInt(index, 10); | ||
if (lineNum && !validPrefix.test(line)) { | ||
report('Expected JSDoc block to have the prefix.', fix, { | ||
line: lineNum, | ||
}); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
}, { | ||
iterateAllJsdocs: true, | ||
meta: { | ||
fixable: 'code', | ||
type: 'layout', | ||
}, | ||
}); |
This file contains 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,91 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: ` | ||
/** | ||
@param {Number} foo | ||
*/ | ||
function quux (foo) { | ||
// with spaces | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
line: 4, | ||
message: 'Expected JSDoc block to have the prefix.', | ||
}, | ||
], | ||
output: ` | ||
/** | ||
* @param {Number} foo | ||
*/ | ||
function quux (foo) { | ||
// with spaces | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
@param {Number} foo | ||
*/function quux (foo) { | ||
// with spaces | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
message: 'Expected JSDoc block to have the prefix.', | ||
}, | ||
], | ||
output: ` | ||
/** | ||
* @param {Number} foo | ||
*/function quux (foo) { | ||
// with spaces | ||
} | ||
`, | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: ` | ||
/** | ||
* Desc | ||
* | ||
* @param {Number} foo | ||
* This is more comment. | ||
*/ | ||
function quux (foo) { | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
* Desc | ||
* | ||
* @param {{ | ||
* foo: Bar, | ||
* bar: Baz | ||
* }} foo | ||
* | ||
*/ | ||
function quux (foo) { | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
/* <- JSDoc must start with 2 stars. | ||
So this is unchecked. | ||
*/ | ||
function quux (foo) {} | ||
`, | ||
}, | ||
], | ||
}; |
This file contains 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