Skip to content

Commit a9c7797

Browse files
authored
build: refactor rule to disallow tabs in entire JSDoc comment
PR-URL: #5325 Closes: stdlib-js/metr-issue-tracker#36 Ref: dca9cf7 Reviewed-by: Athan Reines <[email protected]>
1 parent 4f2dc0d commit a9c7797

File tree

4 files changed

+139
-17
lines changed

4 files changed

+139
-17
lines changed

lib/node_modules/@stdlib/_tools/eslint/rules/jsdoc-no-tabs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# No Tabs
2222

23-
> [ESLint rule][eslint-rules] forbidding the use of tabs in JSDoc descriptions.
23+
> [ESLint rule][eslint-rules] forbidding the use of tabs in JSDoc comments.
2424
2525
<section class="intro">
2626

@@ -38,7 +38,7 @@ var rule = require( '@stdlib/_tools/eslint/rules/jsdoc-no-tabs' );
3838

3939
#### rule
4040

41-
[ESLint rule][eslint-rules] forbidding the use of tabs in JSDoc descriptions.
41+
[ESLint rule][eslint-rules] forbidding the use of tabs in JSDoc comments.
4242

4343
**Bad**:
4444

lib/node_modules/@stdlib/_tools/eslint/rules/jsdoc-no-tabs/lib/main.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,24 @@
2020

2121
// MODULES //
2222

23-
var parseJSDoc = require( 'doctrine' ).parse;
2423
var remark = require( 'remark' );
2524
var remarkLint = require( 'remark-lint' );
2625
var remarkPlugin = require( 'remark-lint-no-tabs' );
26+
var replace = require( '@stdlib/string/replace' );
2727
var isObject = require( '@stdlib/assert/is-object' );
2828
var findJSDoc = require( '@stdlib/_tools/eslint/utils/find-jsdoc' );
2929

3030

3131
// VARIABLES //
3232

33-
var DOPTS = {
34-
'sloppy': true,
35-
'unwrap': true
36-
};
33+
var RE_LEADING_TABS = /^\t+(\*|$)/gm;
3734
var rule;
3835

3936

4037
// FUNCTIONS //
4138

4239
/**
43-
* Rule forbidding the use of tabs in JSDoc descriptions.
40+
* Rule forbidding the use of tabs in JSDoc comments.
4441
*
4542
* @param {Object} context - ESLint context
4643
* @returns {Object} validators
@@ -67,24 +64,23 @@ function main( context ) {
6764
};
6865

6966
/**
70-
* Lints JSDoc descriptions.
67+
* Lints entire JSDoc comments for tabs.
7168
*
7269
* @private
7370
* @param {ASTNode} node - AST node
7471
*/
7572
function validate( node ) {
7673
var jsdoc;
7774
var vfile;
78-
var ast;
75+
var text;
7976

8077
jsdoc = findJSDoc( source, node );
8178
if ( isObject( jsdoc ) ) {
82-
ast = parseJSDoc( jsdoc.value, DOPTS );
83-
if ( ast.description ) {
84-
vfile = lint( ast.description );
85-
if ( vfile.messages.length ) {
86-
reportErrors( vfile.messages, jsdoc.loc );
87-
}
79+
text = jsdoc.value;
80+
text = replace( text, RE_LEADING_TABS, '*' );
81+
vfile = lint( text );
82+
if ( vfile.messages.length ) {
83+
reportErrors( vfile.messages, jsdoc.loc );
8884
}
8985
}
9086
}
@@ -154,7 +150,7 @@ function main( context ) {
154150
rule = {
155151
'meta': {
156152
'docs': {
157-
'description': 'forbid the use of tabs in JSDoc descriptions'
153+
'description': 'forbid the use of tabs in JSDoc comments'
158154
},
159155
'schema': []
160156
},

lib/node_modules/@stdlib/_tools/eslint/rules/jsdoc-no-tabs/test/fixtures/invalid.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,66 @@ test = {
7070
};
7171
invalid.push( test );
7272

73+
test = {
74+
'code': [
75+
'/**',
76+
'* Beep boop.',
77+
'*',
78+
'* [foo]: http://foo.bar/baz',
79+
'*',
80+
'* @param {string} str - input value',
81+
'* @returns {string} output value',
82+
'*',
83+
'* @example',
84+
'* var out = beep( "boop" );',
85+
'* // returns "beepboop"',
86+
'*/',
87+
'function beep( str ) {',
88+
' return "beep" + str;',
89+
'}'
90+
].join( '\n' ),
91+
'errors': [
92+
{
93+
'message': 'Use spaces instead of tabs',
94+
'type': null
95+
},
96+
{
97+
'message': 'Use spaces instead of tabs',
98+
'type': null
99+
}
100+
]
101+
};
102+
invalid.push( test );
103+
104+
test = {
105+
'code': [
106+
'/**',
107+
'* Beep boop.',
108+
'*',
109+
'* @param {string} str - input value',
110+
'* @returns {string} output value',
111+
'*',
112+
'* @example',
113+
'* var out = beep( "boop" );',
114+
'* // returns "beepboop"',
115+
'*/',
116+
'function beep( str ) {',
117+
' return "beep" + str;',
118+
'}'
119+
].join( '\n' ),
120+
'errors': [
121+
{
122+
'message': 'Use spaces instead of tabs',
123+
'type': null
124+
},
125+
{
126+
'message': 'Use spaces instead of tabs',
127+
'type': null
128+
}
129+
]
130+
};
131+
invalid.push( test );
132+
73133

74134
// EXPORTS //
75135

lib/node_modules/@stdlib/_tools/eslint/rules/jsdoc-no-tabs/test/fixtures/valid.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,72 @@ test = {
4848
};
4949
valid.push( test );
5050

51+
test = {
52+
'code': [
53+
'/**',
54+
'* @license Apache-2.0',
55+
'*',
56+
'* Copyright (c) 2018 The Stdlib Authors.',
57+
'*',
58+
'* Licensed under the Apache License, Version 2.0 (the "License");',
59+
'* you may not use this file except in compliance with the License.',
60+
'* You may obtain a copy of the License at',
61+
'*',
62+
'* http://www.apache.org/licenses/LICENSE-2.0',
63+
'*',
64+
'* Unless required by applicable law or agreed to in writing, software',
65+
'* distributed under the License is distributed on an "AS IS" BASIS,',
66+
'* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.',
67+
'* See the License for the specific language governing permissions and',
68+
'* limitations under the License.',
69+
'*/',
70+
'',
71+
'/**',
72+
'* Squares a number.',
73+
'* ',
74+
'* @param {number} x - input number',
75+
'* @returns {number} x squared',
76+
'*',
77+
'* @example',
78+
'* var y = square( 2.0 );',
79+
'* // returns 4.0',
80+
'*/',
81+
'function square( x ) {',
82+
' return x*x;',
83+
'}'
84+
].join( '\n' )
85+
};
86+
valid.push( test );
87+
88+
test = {
89+
'code': [
90+
'/**',
91+
'* Creates a function to calculate powers.',
92+
'*',
93+
'* @param {number} base - base number',
94+
'* @returns {Function} function to calculate powers',
95+
'*',
96+
'* @example',
97+
'* var powerFn = createPowerFn( 2 );',
98+
'* var result = powerFn( 3 );',
99+
'* // returns 8',
100+
'*/',
101+
'function createPowerFn( base ) {',
102+
' /**',
103+
' * Calculates power of base number.',
104+
' *',
105+
' * @private',
106+
' * @param {number} exponent - power to raise base to',
107+
' * @returns {number} result of base raised to exponent',
108+
' */',
109+
' return function power( exponent ) {',
110+
' return Math.pow( base, exponent );',
111+
' };',
112+
'}'
113+
].join( '\n' )
114+
};
115+
valid.push( test );
116+
51117
test = {
52118
'code': [
53119
'/**',

0 commit comments

Comments
 (0)