Skip to content

Commit 66ddee1

Browse files
committed
build: use built-ins, resolve via package.json, and avoid rootDir
--- 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: 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 2b5611b commit 66ddee1

File tree

18 files changed

+671
-77
lines changed

18 files changed

+671
-77
lines changed

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

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525
var vm = require( 'vm' );
2626
var dirname = require( 'path' ).dirname;
2727
var resolve = require( 'path' ).resolve;
28+
var join = require( 'path' ).join;
2829
var existsSync = require( 'fs' ).existsSync; // eslint-disable-line node/no-sync
30+
var readFileSync = require( 'fs' ).readFileSync; // eslint-disable-line node/no-sync
2931
var logger = require( 'debug' );
3032
var isNull = require( '@stdlib/assert/is-null' );
3133
var isNumber = require( '@stdlib/assert/is-number' );
3234
var contains = require( '@stdlib/assert/contains' );
3335
var replace = require( '@stdlib/string/replace' );
36+
var endsWith = require( '@stdlib/string/ends-with' );
37+
var trim = require( '@stdlib/string/trim' );
3438
var objectKeys = require( '@stdlib/utils/keys' );
3539
var compareValues = require( '@stdlib/_tools/doctest/compare-values' );
3640
var createAnnotationValue = require( '@stdlib/_tools/doctest/create-annotation-value' );
@@ -63,7 +67,7 @@ var rule;
6367
* @returns {string} escaped string
6468
*/
6569
function escapeRegex( str ) {
66-
return str.replace( RE_SPECIAL_CHARS, '\\$&' );
70+
return replace( str, RE_SPECIAL_CHARS, '\\$&' );
6771
}
6872

6973
/**
@@ -133,22 +137,73 @@ function findName( scope, expected ) {
133137
}
134138

135139
/**
136-
* Resolves the implementation path relative to a TypeScript declaration file.
140+
* Finds the nearest package.json file by traversing up the directory tree.
141+
*
142+
* @private
143+
* @param {string} filepath - starting file path
144+
* @returns {string|null} path to package.json or null if not found
145+
*/
146+
function findNearestPackageJSON( filepath ) {
147+
var pkgPath;
148+
var dir;
149+
150+
dir = dirname( resolve( filepath ) );
151+
while ( dir !== dirname( dir ) ) { // Stop at root
152+
pkgPath = join( dir, 'package.json' );
153+
if ( existsSync( pkgPath ) ) {
154+
return pkgPath;
155+
}
156+
dir = dirname( dir );
157+
}
158+
return null;
159+
}
160+
161+
/**
162+
* Resolves the implementation path from package.json.
137163
*
138164
* @private
139165
* @param {string} filepath - TypeScript declaration file path
140-
* @param {string} implementationPath - relative path to implementation
141166
* @returns {string|null} resolved implementation path or null if not found
142167
*/
143-
function resolveImplementationPath( filepath, implementationPath ) {
168+
function resolveImplementationPath( filepath ) {
144169
var implPath;
145-
var baseDir;
170+
var mainPath;
171+
var pkgJSON;
172+
var pkgPath;
173+
var pkgDir;
174+
175+
// Find the nearest package.json:
176+
pkgPath = findNearestPackageJSON( filepath );
177+
if ( !pkgPath ) {
178+
debug( 'Could not find package.json for: ' + filepath );
179+
return null;
180+
}
181+
182+
// Read and parse package.json:
183+
try {
184+
pkgJSON = JSON.parse( readFileSync( pkgPath, 'utf8' ) );
185+
} catch ( err ) {
186+
debug( 'Could not read/parse package.json: ' + pkgPath + '. Error: ' + err.message );
187+
return null;
188+
}
189+
190+
// Get the main entry point:
191+
mainPath = pkgJSON.main || './lib';
192+
pkgDir = dirname( pkgPath );
193+
implPath = resolve( pkgDir, mainPath );
146194

147-
baseDir = dirname( resolve( filepath ) );
148-
implPath = resolve( baseDir, implementationPath );
195+
// Check if it's a directory or file:
149196
if ( existsSync( implPath ) ) {
150197
return implPath;
151198
}
199+
// Try with index.js if it's a directory:
200+
if ( existsSync( join( implPath, 'index.js' ) ) ) {
201+
return join( implPath, 'index.js' );
202+
}
203+
// Try adding .js extension:
204+
if ( existsSync( implPath + '.js' ) ) {
205+
return implPath + '.js';
206+
}
152207
return null;
153208
}
154209

@@ -161,7 +216,7 @@ function resolveImplementationPath( filepath, implementationPath ) {
161216
*/
162217
function cleanTSDocComment( comment ) {
163218
// Remove opening /** and closing */
164-
var cleaned = comment.replace( /^\/\*\*/, '' ).replace( /\*\/$/, '' );
219+
var cleaned = replace( replace( comment, /^\/\*\*/, '' ), /\*\/$/, '' );
165220

166221
// Remove * at the beginning of lines
167222
cleaned = replace( cleaned, RE_COMMENT_PREFIX, '' );
@@ -329,15 +384,13 @@ function processExampleCode( code, commentIdx, comments, scope, report, opts, so
329384
function main( context ) {
330385
var sourceCode;
331386
var filename;
332-
var options;
333387
var dir;
334388

335389
sourceCode = context.getSourceCode();
336390
filename = context.getFilename();
337-
options = context.options[ 0 ] || {};
338391

339392
// Only process TypeScript declaration files:
340-
if ( !filename.endsWith( '.d.ts' ) ) {
393+
if ( !endsWith( filename, '.d.ts' ) ) {
341394
return {};
342395
}
343396

@@ -379,7 +432,6 @@ function main( context ) {
379432
* @private
380433
*/
381434
function validate() {
382-
var implementationPath;
383435
var implPath;
384436
var examples;
385437
var comments;
@@ -397,11 +449,10 @@ function main( context ) {
397449
regexCache = {};
398450
lineCountCache = {};
399451

400-
// Resolve implementation path relative to TypeScript declaration file path:
401-
implementationPath = options.implementationPath || '../../lib';
402-
implPath = resolveImplementationPath( filename, implementationPath );
452+
// Resolve implementation path from nearest package.json:
453+
implPath = resolveImplementationPath( filename );
403454
if ( !implPath ) {
404-
debug( 'Could not resolve implementation path: ' + implementationPath + ' from ' + filename );
455+
debug( 'Could not resolve implementation path from package.json for: ' + filename );
405456
return;
406457
}
407458

@@ -442,7 +493,7 @@ function main( context ) {
442493
}
443494

444495
for ( j = 0; j < examples.length; j++ ) {
445-
code = examples[ j ].trim();
496+
code = trim( examples[ j ] );
446497

447498
// Process the example code and validate annotations (reusing VM context):
448499
processExampleCode( code, i, comments, scope, report, opts, sourceCode );
@@ -465,18 +516,7 @@ rule = {
465516
'description': 'ensure return annotations in TSDoc examples match the actual output'
466517
},
467518
'fixable': 'code',
468-
'schema': [
469-
{
470-
'type': 'object',
471-
'properties': {
472-
'implementationPath': {
473-
'type': 'string',
474-
'default': '../../lib'
475-
}
476-
},
477-
'additionalProperties': false
478-
}
479-
]
519+
'schema': []
480520
},
481521
'create': main
482522
};

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
// MODULES //
2222

2323
var join = require( 'path' ).join;
24-
var rootDir = require( '@stdlib/_tools/utils/root-dir' );
2524

2625

2726
// VARIABLES //
2827

29-
var ROOT_DIR = rootDir();
3028
var invalid = [];
3129
var test;
3230

@@ -49,7 +47,7 @@ test = {
4947
'',
5048
'export = abs;'
5149
].join( '\n' ),
52-
'filename': join( ROOT_DIR, 'lib/node_modules/@stdlib/math/base/special/abs/docs/types/index.d.ts' ),
50+
'filename': join( __dirname, 'packages/@stdlib/math/base/special/abs/docs/types/index.d.ts' ),
5351
'output': [
5452
'/**',
5553
'* Returns the absolute value.',
@@ -92,7 +90,7 @@ test = {
9290
'',
9391
'export = ctor;'
9492
].join( '\n' ),
95-
'filename': join( ROOT_DIR, 'lib/node_modules/@stdlib/ndarray/vector/int32/docs/types/index.d.ts' ),
93+
'filename': join( __dirname, 'packages/@stdlib/ndarray/vector/int32/docs/types/index.d.ts' ),
9694
'output': [
9795
'/**',
9896
'* Abbreviated Int32Vector example (invalid).',
@@ -139,7 +137,7 @@ test = {
139137
'',
140138
'export = sqrt;'
141139
].join( '\n' ),
142-
'filename': join( ROOT_DIR, 'lib/node_modules/@stdlib/math/base/special/sqrt/docs/types/index.d.ts' ),
140+
'filename': join( __dirname, 'packages/@stdlib/math/base/special/sqrt/docs/types/index.d.ts' ),
143141
'output': [
144142
'/**',
145143
'* Returns the square root.',
@@ -181,7 +179,7 @@ test = {
181179
'',
182180
'export = PI;'
183181
].join( '\n' ),
184-
'filename': join( ROOT_DIR, 'lib/node_modules/@stdlib/constants/float64/pi/docs/types/index.d.ts' ),
182+
'filename': join( __dirname, 'packages/@stdlib/constants/float64/pi/docs/types/index.d.ts' ),
185183
'output': [
186184
'/**',
187185
'* The mathematical constant pi.',
Lines changed: 30 additions & 0 deletions
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 Complex64Array = require( '@stdlib/array/complex64' );
26+
27+
28+
// EXPORTS //
29+
30+
module.exports = Complex64Array;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "@stdlib/array/complex64",
3+
"version": "0.0.0",
4+
"description": "64-bit complex number array.",
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+
"scripts": {},
17+
"homepage": "https://github.com/stdlib-js/stdlib",
18+
"repository": {
19+
"type": "git",
20+
"url": "git://github.com/stdlib-js/stdlib.git"
21+
},
22+
"bugs": {
23+
"url": "https://github.com/stdlib-js/stdlib/issues"
24+
},
25+
"dependencies": {},
26+
"devDependencies": {},
27+
"engines": {
28+
"node": ">=0.10.0",
29+
"npm": ">2.7.0"
30+
},
31+
"main": "./lib",
32+
"directories": {
33+
"doc": "./docs",
34+
"example": "./examples",
35+
"lib": "./lib",
36+
"test": "./test"
37+
},
38+
"types": "./docs/types",
39+
"keywords": [
40+
"stdlib",
41+
"stdtypes",
42+
"types",
43+
"data",
44+
"structure",
45+
"array",
46+
"typed",
47+
"typed array",
48+
"typed-array",
49+
"complex64",
50+
"complex",
51+
"cmplx",
52+
"float32",
53+
"single",
54+
"single-precision",
55+
"float",
56+
"floating",
57+
"floating-point"
58+
]
59+
}
Lines changed: 30 additions & 0 deletions
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 PI = require( '@stdlib/constants/float64/pi' );
26+
27+
28+
// EXPORTS //
29+
30+
module.exports = PI;

0 commit comments

Comments
 (0)