Skip to content

Commit f723627

Browse files
committed
build: minor refactoring
--- 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: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent d4211dd commit f723627

File tree

1 file changed

+55
-7
lines changed
  • lib/node_modules/@stdlib/_tools/eslint/rules/tsdoc-doctest/lib

1 file changed

+55
-7
lines changed

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

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19+
/* eslint-disable max-len, max-lines */
20+
1921
'use strict';
2022

2123
// MODULES //
@@ -43,14 +45,51 @@ var debug = logger( 'tsdoc-doctest' );
4345
var RE_TSDOC = /\/\*\*[\s\S]+?\*\//g;
4446
var RE_EXAMPLE = /@example\s*([\s\S]*?)(?=\n\s*@\w|\*\/|$)/g;
4547
var RE_NEWLINE = /\r?\n/g;
46-
var RE_ANNOTATION = /(?:\n|^)(?:var|let|const)? ?([a-zA-Z0-9._]+) ?=[^;]*?;\n\/\/ ?(returns|([A-Za-z][A-Za-z_0-9]*)? ?=>|throws) {0,1}([\s\S]*?)(\n|$)/g;
48+
var RE_ANNOTATION = /(?:\n|^)(?:var|let|const)? ?([a-zA-Z0-9._]+) ?=[^;]*;\n\/\/ ?(returns|([A-Za-z][A-Za-z_0-9]*)? ?=>|throws) ?([\s\S]*?)(\n|$)/g;
4749
var RE_CONSOLE = /console\.(?:dir|error|log)/;
4850
var RE_COMMENT_PREFIX = /^\s*\*\s?/gm;
51+
var RE_SPECIAL_CHARS = /[.*+?^${}()|[\]\\]/g;
52+
var regexCache = {};
53+
var lineCountCache = {};
4954
var rule;
5055

5156

5257
// FUNCTIONS //
5358

59+
/**
60+
* Escapes special regex characters in a string.
61+
*
62+
* @private
63+
* @param {string} str - string to escape
64+
* @returns {string} escaped string
65+
*/
66+
function escapeRegex( str ) {
67+
return str.replace( RE_SPECIAL_CHARS, '\\$&' );
68+
}
69+
70+
/**
71+
* Gets or creates a cached regex for finding return annotations.
72+
*
73+
* @private
74+
* @param {string} annotationType - type of annotation (returns, throws, etc.)
75+
* @param {string} value - expected value
76+
* @returns {RegExp} compiled regular expression
77+
*/
78+
function getAnnotationRegex( annotationType, value ) {
79+
var escapedValue;
80+
var pattern;
81+
var key;
82+
83+
escapedValue = escapeRegex( value );
84+
key = [ annotationType, escapedValue ].join( '::' );
85+
86+
if ( !regexCache[ key ] ) {
87+
pattern = '// ' + annotationType + '\\s+' + escapedValue;
88+
regexCache[ key ] = new RegExp( pattern );
89+
}
90+
return regexCache[ key ];
91+
}
92+
5493
/**
5594
* Counts the number of lines in the given string.
5695
*
@@ -59,7 +98,15 @@ var rule;
5998
* @returns {number} number of lines
6099
*/
61100
function countLines( str ) {
62-
return ( str.match( RE_NEWLINE ) || '' ).length;
101+
var matches;
102+
103+
// Use cached result if available
104+
if ( lineCountCache[ str ] ) {
105+
return lineCountCache[ str ];
106+
}
107+
matches = str.match( RE_NEWLINE );
108+
lineCountCache[ str ] = ( matches ) ? matches.length : 0;
109+
return lineCountCache[ str ];
63110
}
64111

65112
/**
@@ -78,7 +125,7 @@ function findName( scope, expected ) {
78125
key = keys[ i ];
79126
if (
80127
!contains( SCOPE_DEFAULTS, key ) &&
81-
!compareValues( scope[ key ], expected )
128+
compareValues( scope[ key ], expected ) === null
82129
) {
83130
return key;
84131
}
@@ -175,7 +222,7 @@ function processExampleCode( code, commentIdx, comments, scope, report, opts, so
175222
commentStartIdx = sourceCode.text.indexOf( comments[ commentIdx ] );
176223

177224
// Find the annotation in the original comment
178-
returnAnnotationPattern = new RegExp( '// ' + arr[ 2 ] + '\\s+' + arr[ 4 ].replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ) );
225+
returnAnnotationPattern = getAnnotationRegex( arr[ 2 ], arr[ 4 ] );
179226
annotationMatch = comments[ commentIdx ].match( returnAnnotationPattern );
180227

181228
if ( !annotationMatch ) {
@@ -281,9 +328,6 @@ function processExampleCode( code, commentIdx, comments, scope, report, opts, so
281328
}
282329
}
283330

284-
285-
// MAIN //
286-
287331
/**
288332
* Rule for validating that return annotations in TypeScript declaration examples match the actual output.
289333
*
@@ -357,6 +401,10 @@ function main( context ) {
357401
var i;
358402
var j;
359403

404+
// Clear caches to prevent memory leaks
405+
regexCache = {};
406+
lineCountCache = {};
407+
360408
// Resolve implementation path relative to TypeScript declaration file path:
361409
implementationPath = options.implementationPath || '../../lib';
362410
implPath = resolveImplementationPath( filename, implementationPath );

0 commit comments

Comments
 (0)