Skip to content

Commit 2731872

Browse files
Planeshifterkgrytestdlib-bot
authored
build: migrate custom ESLint rules to modern APIs
PR-URL: #10950 Ref: stdlib-js/metr-issue-tracker#54 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent e3d94c0 commit 2731872

111 files changed

Lines changed: 1279 additions & 1245 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/node_modules/@stdlib/_tools/eslint/rules/capitalized-comments/lib/main.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var isCapitalized = require( '@stdlib/assert/is-capitalized' );
2424
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
25+
var uppercase = require( '@stdlib/string/base/uppercase' );
2526
var ltrim = require( '@stdlib/string/left-trim' );
2627
var copy = require( '@stdlib/utils/copy' );
2728
var DEFAULTS = require( './defaults.json' );
@@ -57,7 +58,7 @@ function main( context ) {
5758
if ( hasOwnProp( options, 'whitelist' ) ) {
5859
opts.whitelist = options.whitelist.slice();
5960
}
60-
source = context.getSourceCode();
61+
source = context.sourceCode;
6162
visited = {};
6263

6364
/**
@@ -89,7 +90,7 @@ function main( context ) {
8990

9091
str = source.getText( comment );
9192
idx = str.search( /[a-zA-Z]/ );
92-
ch = str.charAt( idx ).toUpperCase();
93+
ch = uppercase( str.charAt( idx ) );
9394
replacement = str.slice( 0, idx ) + ch + str.slice( idx+1 );
9495
return fixer.replaceText( comment, replacement );
9596
}
@@ -194,7 +195,7 @@ function main( context ) {
194195
var i;
195196

196197
comments = source.getCommentsInside( node );
197-
scope = context.getScope( node );
198+
scope = source.getScope( node );
198199
for ( i = 0; i < comments.length; i++ ) {
199200
comment = comments[ i ];
200201
if ( comment.type !== 'Shebang' && !visited[ comment.range ] ) {

lib/node_modules/@stdlib/_tools/eslint/rules/doctest-annotation-spacing/lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var rule;
6666
* @returns {Object} validators
6767
*/
6868
function main( context ) {
69-
var source = context.getSourceCode();
69+
var source = context.sourceCode;
7070

7171
/**
7272
* Reports the error message.

lib/node_modules/@stdlib/_tools/eslint/rules/doctest-marker/lib/main.js

Lines changed: 91 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818

1919
'use strict';
2020

21-
// MODULES //
22-
23-
var walk = require( 'acorn-walk' );
24-
25-
2621
// VARIABLES //
2722

2823
var RE_ANNOTATION = /^\s*(?:\* ){0,1}(?:\/\/|\/\*)* *(?:e\.g\.,){0,1} (returns|=>|throws)/;
@@ -31,14 +26,101 @@ var rule;
3126

3227
// FUNCTIONS //
3328

29+
/**
30+
* Finds the nearest statement-level ancestor of a given AST node.
31+
*
32+
* @private
33+
* @param {ASTNode} node - AST node
34+
* @returns {ASTNode} statement-level ancestor
35+
*/
36+
function findStatement( node ) {
37+
while ( node ) {
38+
if (
39+
node.type === 'VariableDeclaration' ||
40+
node.type === 'ExpressionStatement' ||
41+
node.type === 'ReturnStatement' ||
42+
node.type === 'ThrowStatement' ||
43+
node.type === 'IfStatement' ||
44+
node.type === 'ForStatement' ||
45+
node.type === 'WhileStatement' ||
46+
node.type === 'TryStatement' ||
47+
node.type === 'FunctionDeclaration' ||
48+
node.type === 'Program'
49+
) {
50+
return node;
51+
}
52+
node = node.parent;
53+
}
54+
return null;
55+
}
56+
57+
/**
58+
* Checks whether a comment is a return annotation and, if so, whether it follows marker style conventions.
59+
*
60+
* @private
61+
* @param {Object} source - source code object
62+
* @param {string} comment - comment to examine
63+
* @returns {(string|null)} error message or null
64+
*/
65+
function checkComment( source, comment ) {
66+
var matches;
67+
var token;
68+
var node;
69+
var type;
70+
71+
matches = comment.value.match( RE_ANNOTATION );
72+
if ( matches ) {
73+
type = matches[ 1 ];
74+
75+
// Find the preceding code token:
76+
token = source.getTokenBefore( comment );
77+
if ( !token ) {
78+
return 'Encountered an orphaned return annotation without a preceding node';
79+
}
80+
// Check for orphaned annotation (preceding token is not on the same or previous line):
81+
if ( comment.loc.start.line - token.loc.end.line > 1 ) {
82+
return 'Encountered an orphaned return annotation without a preceding node';
83+
}
84+
node = source.getNodeByRangeIndex( token.range[ 0 ] );
85+
if ( !node || node.type === 'Program' ) {
86+
return 'Encountered an orphaned return annotation without a preceding node';
87+
}
88+
node = findStatement( node );
89+
if ( !node || node.type === 'Program' ) {
90+
return null;
91+
}
92+
switch ( type ) {
93+
case 'returns':
94+
if (
95+
node.type !== 'VariableDeclaration' &&
96+
( node.type !== 'ExpressionStatement' || node.expression.type !== 'AssignmentExpression' )
97+
) {
98+
return 'Only include `// returns` after variable declarations or assignment expressions (use `=>` after `console.log`)';
99+
}
100+
break;
101+
case '=>':
102+
if (
103+
node.type === 'VariableDeclaration' ||
104+
( node.type === 'ExpressionStatement' && node.expression.type === 'AssignmentExpression' )
105+
) {
106+
return 'Use `// returns` after variable declarations or assignment expressions instead of `=>`';
107+
}
108+
break;
109+
default:
110+
break;
111+
}
112+
}
113+
return null;
114+
}
115+
34116
/**
35117
* Rule for validating that return annotations follow marker style conventions.
36118
*
37119
* @param {Object} context - ESLint context
38120
* @returns {Object} validators
39121
*/
40122
function main( context ) {
41-
var source = context.getSourceCode();
123+
var source = context.sourceCode;
42124

43125
/**
44126
* Reports the error message.
@@ -64,85 +146,17 @@ function main( context ) {
64146
function validate( node ) {
65147
var comments;
66148
var current;
67-
var offset;
68-
var prev;
69149
var msg;
70150
var i;
71151

72152
comments = source.getAllComments( node );
73-
if ( comments.length > 0 ) {
74-
current = comments[ 0 ];
75-
msg = checkComment( current, node, 0 );
153+
for ( i = 0; i < comments.length; i++ ) {
154+
current = comments[ i ];
155+
msg = checkComment( source, current );
76156
if ( msg ) {
77157
report( current.loc, msg );
78158
}
79-
for ( i = 1; i < comments.length; i++ ) {
80-
prev = comments[ i-1 ];
81-
current = comments[ i ];
82-
offset = 0;
83-
84-
// Check whether previous comment sits one line before the current one; if so, adjust offset for finding last node:
85-
if ( current.loc.start.line === prev.loc.end.line + 1 ) {
86-
offset = prev.loc.end.column - prev.loc.start.column + 1;
87-
}
88-
msg = checkComment( current, node, offset );
89-
if ( msg ) {
90-
report( current.loc, msg );
91-
}
92-
}
93-
}
94-
}
95-
96-
/**
97-
* Checks whether a comment is a return annotation and, if so, whether it follows marker style conventions.
98-
*
99-
* @private
100-
* @param {string} comment - comment to examine
101-
* @param {ASTNode} ast - node to examine
102-
* @param {integer} offset - non-zero if previous line ends with a comment
103-
* @returns {(string|null)} error message or null
104-
*/
105-
function checkComment( comment, ast, offset ) {
106-
var matches;
107-
var node;
108-
var prev;
109-
var type;
110-
111-
matches = comment.value.match( RE_ANNOTATION );
112-
if ( matches ) {
113-
offset += 1 + comment.loc.start.column;
114-
prev = walk.findNodeAt( ast, null, comment.start-offset );
115-
type = matches[ 1 ];
116-
if ( !prev ) {
117-
// Handle case when comment refers to node on the same line:
118-
if ( walk.findNodeAt( ast, null, comment.start-1 ) ) {
119-
return null;
120-
}
121-
return 'Encountered an orphaned return annotation without a preceding node';
122-
}
123-
node = prev.node;
124-
switch ( type ) {
125-
case 'returns':
126-
if (
127-
node.type !== 'VariableDeclaration' &&
128-
( node.type !== 'ExpressionStatement' || node.expression.type !== 'AssignmentExpression' )
129-
) {
130-
return 'Only include `// returns` after variable declarations or assignment expressions (use `=>` after `console.log`)';
131-
}
132-
break;
133-
case '=>':
134-
if (
135-
node.type === 'VariableDeclaration' ||
136-
( node.type === 'ExpressionStatement' && node.expression === 'AssignmentExpression' )
137-
) {
138-
return 'Use `// returns` after variable declarations or assignment expressions instead of `=>`';
139-
}
140-
break;
141-
default:
142-
break;
143-
}
144159
}
145-
return null;
146160
}
147161

148162
return {

lib/node_modules/@stdlib/_tools/eslint/rules/doctest-marker/test/fixtures/valid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ test = {
112112
'code': [
113113
'var functionName = require( \'./../lib\' );',
114114
'',
115-
'console.log( functionName( Math.sqrt ) ); // eslint-disable-line stdlib/no-builtin-math',
115+
'console.log( functionName( Math.sqrt ) );',
116116
'// => \'sqrt\'',
117117
'',
118118
'console.log( functionName( Float64Array ) );',

lib/node_modules/@stdlib/_tools/eslint/rules/doctest-quote-props/lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ function checkComment( comment ) {
154154
* @returns {Object} validators
155155
*/
156156
function main( context ) {
157-
var source = context.getSourceCode();
157+
var source = context.sourceCode;
158158

159159
/**
160160
* Reports the error message.

lib/node_modules/@stdlib/_tools/eslint/rules/doctest/lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ function main( context ) {
122122
var scope;
123123
var dir;
124124

125-
source = context.getSourceCode();
125+
source = context.sourceCode;
126126
if ( NODE_SHEBANG.test( source.text ) ) {
127127
// Do not lint executable Node.js script files:
128128
return {};

lib/node_modules/@stdlib/_tools/eslint/rules/empty-line-before-comment/lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var rule;
4040
* @returns {Object} validators
4141
*/
4242
function main( context ) {
43-
var source = context.getSourceCode();
43+
var source = context.sourceCode;
4444

4545
/**
4646
* Reports the error message.

lib/node_modules/@stdlib/_tools/eslint/rules/eol-open-bracket-spacing/lib/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var rule;
3333
* @returns {Object} validators
3434
*/
3535
function main( context ) {
36-
var source = context.getSourceCode();
36+
var source = context.sourceCode;
3737

3838
/**
3939
* Reports the error message.

lib/node_modules/@stdlib/_tools/eslint/rules/jsdoc-blockquote-indentation/lib/main.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ var rule;
4343

4444
// FUNCTIONS //
4545

46+
/**
47+
* Creates a location object.
48+
*
49+
* @private
50+
* @param {Object} options - function options
51+
* @returns {Object} location info
52+
*/
53+
function locationInfo( options ) {
54+
return {
55+
'start': {
56+
'line': options.startLine,
57+
'column': options.startColumn
58+
},
59+
'end': {
60+
'line': options.endLine,
61+
'column': options.endColumn
62+
}
63+
};
64+
}
65+
4666
/**
4767
* Rule for enforcing Markdown blockquote indentation in JSDoc descriptions.
4868
*
@@ -63,7 +83,7 @@ function main( context ) {
6383
]
6484
};
6585
lint = remark().use( config ).processSync; // eslint-disable-line node/no-sync
66-
source = context.getSourceCode();
86+
source = context.sourceCode;
6787

6888
return {
6989
'FunctionExpression:exit': validate,
@@ -121,26 +141,6 @@ function main( context ) {
121141
}
122142
}
123143

124-
/**
125-
* Creates a location object.
126-
*
127-
* @private
128-
* @param {Object} options - function options
129-
* @returns {Object} location info
130-
*/
131-
function locationInfo( options ) {
132-
return {
133-
'start': {
134-
'line': options.startLine,
135-
'column': options.startColumn
136-
},
137-
'end': {
138-
'line': options.endLine,
139-
'column': options.endColumn
140-
}
141-
};
142-
}
143-
144144
/**
145145
* Reports an error message.
146146
*

0 commit comments

Comments
 (0)