Skip to content

Commit 5d7fc0c

Browse files
committed
refactor: address PR feedback
--- 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: passed - 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 c1c8739 commit 5d7fc0c

File tree

16 files changed

+416
-108
lines changed

16 files changed

+416
-108
lines changed

lib/node_modules/@stdlib/_tools/eslint/rules/tsdoc-doctest/README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var rule = require( '@stdlib/_tools/eslint/rules/tsdoc-doctest' );
3838

3939
#### rule
4040

41-
[ESLint rule][eslint-rules] to ensure that return annotations in TSDoc examples match the actual output. The rule validates `@example` blocks in TSDoc comments within `.d.ts` files.
41+
[ESLint rule][eslint-rules] to ensure that return annotations in TSDoc examples match the actual output. Return annotations may start with `returns`, `throws`, or `=>`. `returns` follow variable declarations or assignment expressions, whereas `=>` follow expression-only forms including `console.log` calls. The rule validates `@example` blocks in TSDoc comments within `.d.ts` files by automatically resolving the corresponding implementation via the nearest `package.json` file.
4242

4343
**Bad**:
4444

@@ -129,7 +129,7 @@ var result = linter.verify( code, {
129129
'tsdoc-doctest': 'error'
130130
}
131131
}, {
132-
'filename': 'lib/node_modules/@stdlib/math/base/special/abs/docs/types/index.d.ts'
132+
'filename': '/path/to/project/lib/node_modules/@stdlib/math/base/special/abs/docs/types/index.d.ts'
133133
});
134134
/* returns
135135
[
@@ -155,11 +155,9 @@ var result = linter.verify( code, {
155155

156156
## Notes
157157

158-
- The corresponding JavaScript package must be loadable via `require('@stdlib/<package>')`
159-
- The rule skips validation if the package cannot be loaded.
160-
- Examples are executed in a sandboxed VM context with limited globals.
161-
- The rule validates assignment-form examples (e.g., `var x = fn(...);` followed by an annotation). It does not validate console output or expression-only forms using `// =>`.
162-
- The implementation path the rule uses to load the JavaScript implementation can be overridden via the `implementationPath` rule option (default: `../../lib` relative to the declaration file).
158+
- The rule automatically resolves the implementation path by traversing up the directory tree to find the nearest `package.json` file and using its `main` field.
159+
- The rule skips validation if the `package.json` file cannot be found or if the resolved implementation cannot be loaded.
160+
- Examples are executed in a sandboxed VM context with limited globals for security.
163161

164162
</section>
165163

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

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,65 +21,83 @@
2121
// VARIABLES //
2222

2323
// Regular expressions for matching TypeScript declarations:
24+
25+
/**
26+
* Regular expression to match function declarations like "declare function abs( x: number ): number;" (captures function name).
27+
*
28+
* @type {RegExp}
29+
*/
2430
var RE_DECLARE_FUNCTION = /declare\s+function\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*[<(]/;
31+
32+
/**
33+
* Regular expression to match variable declarations like "declare var someVar: SomeType;" (captures variable name).
34+
*
35+
* @type {RegExp}
36+
*/
2537
var RE_DECLARE_VAR = /declare\s+var\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/;
38+
39+
/**
40+
* Regular expression to match class declarations like "declare class Complex64Array {" (captures class name).
41+
*
42+
* @type {RegExp}
43+
*/
2644
var RE_DECLARE_CLASS = /declare\s+class\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s/;
27-
var RE_DECLARE_VAR_NAMESPACE = /declare\s+var\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:\s*[A-Z][a-zA-Z0-9_$]*/;
45+
46+
/**
47+
* Regular expression to match const declarations like "declare const PI: number;" (captures constant name).
48+
*
49+
* @type {RegExp}
50+
*/
2851
var RE_DECLARE_CONST = /declare\s+const\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/;
52+
53+
/**
54+
* Regular expression to match variable declarations with interface types like "declare var ctor: Int32Vector;" (captures variable name and interface name).
55+
*
56+
* @type {RegExp}
57+
*/
2958
var RE_DECLARE_VAR_INTERFACE = /declare\s+var\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:\s*([A-Z][a-zA-Z0-9_$]*)/;
3059

3160

3261
// MAIN //
3362

3463
/**
35-
* Adds package export to scope based on TypeScript declarations.
64+
* Adds a package export to the scope based on TypeScript declarations.
3665
*
66+
* @private
3767
* @param {Object} scope - VM scope object to add the package export to
3868
* @param {*} pkg - package export value to be added to scope
3969
* @param {string} sourceText - TypeScript declaration source text to parse for identifier names
4070
*/
4171
function addPackageToScope( scope, pkg, sourceText ) {
4272
var interfaceMatch;
4373
var namespaceMatch;
44-
var funcMatch;
74+
var pkgType;
75+
var match;
4576

46-
if ( typeof pkg === 'function' ) {
47-
// Try to match declare function pattern (handles generics with <):
48-
funcMatch = sourceText.match( RE_DECLARE_FUNCTION );
49-
if ( !funcMatch ) {
50-
// Try to match declare var pattern:
51-
funcMatch = sourceText.match( RE_DECLARE_VAR );
52-
}
53-
if ( !funcMatch ) {
54-
// Try to match declare class pattern (for constructor functions):
55-
funcMatch = sourceText.match( RE_DECLARE_CLASS );
56-
}
57-
if ( funcMatch ) {
58-
scope[ funcMatch[1] ] = pkg;
77+
pkgType = typeof pkg;
78+
if ( pkgType === 'function' ) {
79+
match = sourceText.match( RE_DECLARE_FUNCTION ) ||
80+
sourceText.match( RE_DECLARE_VAR ) ||
81+
sourceText.match( RE_DECLARE_CLASS );
82+
if ( match ) {
83+
scope[ match[1] ] = pkg;
5984
}
60-
// Also check for declare var with interface pattern (e.g., declare var ctor: Int32Vector):
6185
interfaceMatch = sourceText.match( RE_DECLARE_VAR_INTERFACE );
6286
if ( interfaceMatch ) {
63-
// Make the function available under both the variable name and the interface name:
87+
// Make the function available under both the variable and interface names:
6488
scope[ interfaceMatch[1] ] = pkg; // e.g., ctor
6589
scope[ interfaceMatch[2] ] = pkg; // e.g., Int32Vector
6690
}
67-
} else if ( typeof pkg === 'object' && pkg !== null ) {
68-
// Handle namespace objects and other object interfaces:
69-
namespaceMatch = sourceText.match( RE_DECLARE_VAR_NAMESPACE );
70-
if ( namespaceMatch ) {
71-
scope[ namespaceMatch[1] ] = pkg;
72-
}
73-
// Also check for const declarations (e.g., Complex64/Complex128 constants):
74-
funcMatch = sourceText.match( RE_DECLARE_CONST );
75-
if ( funcMatch ) {
76-
scope[ funcMatch[1] ] = pkg;
77-
}
7891
} else {
79-
// Try to match declare const pattern:
80-
funcMatch = sourceText.match( RE_DECLARE_CONST );
81-
if ( funcMatch ) {
82-
scope[ funcMatch[1] ] = pkg;
92+
if ( pkgType === 'object' && pkg !== null ) {
93+
namespaceMatch = sourceText.match( RE_DECLARE_VAR_INTERFACE );
94+
if ( namespaceMatch ) {
95+
scope[ namespaceMatch[1] ] = pkg;
96+
}
97+
}
98+
match = sourceText.match( RE_DECLARE_CONST );
99+
if ( match ) {
100+
scope[ match[1] ] = pkg;
83101
}
84102
}
85103
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ var windowShim = require( './window.js' );
3434
*
3535
* @private
3636
* @param {*} data - data to be logged
37-
* @param {...*} args - substitution values
38-
* @returns {*} return value
37+
* @param {...*} [args] - substitution values
38+
* @returns {*} formatted string if first argument is string, otherwise first argument
3939
*/
4040
function log( data ) {
4141
if ( isString( data ) ) {
@@ -50,6 +50,7 @@ function log( data ) {
5050
/**
5151
* Creates a VM execution scope with necessary globals.
5252
*
53+
* @private
5354
* @param {string} dir - directory path
5455
* @param {string} filename - file name
5556
* @returns {Object} VM scope object

0 commit comments

Comments
 (0)