Skip to content

Commit 54dada5

Browse files
committed
build: support specifying alias via custom comment
--- 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: passed - 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: passed - 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: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 7f55434 commit 54dada5

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ declare function add( x: number, y: number ): number;
8989
- The rule skips validation if the `package.json` file cannot be found or if the resolved implementation cannot be loaded.
9090
- Examples are executed in a sandboxed VM context with limited globals for security.
9191
- This rule is specifically designed for TypeScript declaration files and will only process files with a `*.d.ts` filename extension.
92+
- You can specify a custom alias for the package export in the doctest scope using a comment in the format `// eslint-doctest-alias: <alias>`. This is useful when the TypeScript declaration uses a type name that differs from the exported identifier, or when you want to use a specific identifier in examples for clarity.
9293

9394
</section>
9495

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ var RE_DECLARE_CONST = /declare\s+const\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/;
3535
// Regular expression to match variable declarations with interface types such as "declare var ctor: Int32Vector;" (captures variable name and interface name):
3636
var RE_DECLARE_VAR_INTERFACE = /declare\s+(?:var|const)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:\s*([A-Z][a-zA-Z0-9_$]*)/;
3737

38+
// Regular expression to match custom alias comment "// eslint-doctest-alias: <alias>" capturing alias name:
39+
var RE_ALIAS_COMMENT = /\/\/\s*eslint-doctest-alias:\s*([a-zA-Z_$][a-zA-Z0-9_$]*)/;
40+
3841

3942
// MAIN //
4043

@@ -49,9 +52,16 @@ var RE_DECLARE_VAR_INTERFACE = /declare\s+(?:var|const)\s+([a-zA-Z_$][a-zA-Z0-9_
4952
function addPackageToScope( scope, pkg, sourceText ) {
5053
var interfaceMatch;
5154
var namespaceMatch;
55+
var aliasMatch;
5256
var pkgType;
5357
var match;
5458

59+
// Check for custom alias comment first...
60+
aliasMatch = sourceText.match( RE_ALIAS_COMMENT );
61+
if ( aliasMatch ) {
62+
scope[ aliasMatch[1] ] = pkg;
63+
}
64+
5565
pkgType = typeof pkg;
5666
if ( pkgType === 'function' ) {
5767
match = sourceText.match( RE_DECLARE_FUNCTION ) || sourceText.match( RE_DECLARE_VAR ) || sourceText.match( RE_DECLARE_CLASS ); // eslint-disable-line max-len
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "@stdlib/array/index",
3+
"version": "0.0.0",
4+
"description": "Create an array index.",
5+
"license": "Apache-2.0",
6+
"author": {
7+
"name": "The Stdlib Authors",
8+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9+
},
10+
"contributors": [
11+
{
12+
"name": "The Stdlib Authors",
13+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14+
}
15+
],
16+
"main": "./lib",
17+
"directories": {
18+
"doc": "./docs",
19+
"example": "./examples",
20+
"lib": "./lib",
21+
"test": "./test"
22+
},
23+
"types": "./docs/types",
24+
"scripts": {},
25+
"homepage": "https://github.com/stdlib-js/stdlib",
26+
"repository": {
27+
"type": "git",
28+
"url": "git://github.com/stdlib-js/stdlib.git"
29+
},
30+
"bugs": {
31+
"url": "https://github.com/stdlib-js/stdlib/issues"
32+
},
33+
"dependencies": {},
34+
"devDependencies": {},
35+
"engines": {
36+
"node": ">=0.10.0",
37+
"npm": ">2.7.0"
38+
},
39+
"keywords": [
40+
"stdlib",
41+
"array",
42+
"index",
43+
"indexing"
44+
]
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/* eslint-disable stdlib/jsdoc-main-export */
22+
23+
// MODULES //
24+
25+
var ArrayIndex = require( '@stdlib/array/index' );
26+
27+
28+
// EXPORTS //
29+
30+
module.exports = ArrayIndex;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,34 @@ test = {
185185
};
186186
valid.push( test );
187187

188+
test = {
189+
'code': [
190+
'// eslint-doctest-alias: ArrayIndex',
191+
'',
192+
'/**',
193+
'* Array index constructor.',
194+
'*',
195+
'* @param x - input array',
196+
'* @returns ArrayIndex instance',
197+
'*',
198+
'* @example',
199+
'* var x = [ 1, 2, 3, 4 ];',
200+
'*',
201+
'* var idx = new ArrayIndex( x );',
202+
'* // returns <ArrayIndex>',
203+
'*/',
204+
'interface Constructor {',
205+
' new( x: Array<number> ): any;',
206+
'}',
207+
'',
208+
'declare var ctor: Constructor;',
209+
'',
210+
'export = ctor;'
211+
].join( '\n' ),
212+
'filename': join( __dirname, 'packages/@stdlib/array/index/docs/types/index.d.ts' )
213+
};
214+
valid.push( test );
215+
188216

189217
// EXPORTS //
190218

0 commit comments

Comments
 (0)