Skip to content

Commit b70db1b

Browse files
committed
build: fix example code replacements in namespace TypeScript declarations
--- 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: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 01e7bd4 commit b70db1b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/node_modules/@stdlib/_tools/scripts/create_namespace_types.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,14 @@ function create( fullPath ) {
367367
/**
368368
* Replaces variable names inside of example code to use exported namespace methods.
369369
*
370+
* ## Notes
371+
*
372+
* - Thus function adds `ns.` prefix to all usages of `name` EXCEPT:
373+
374+
* - Inside type annotations: `<TypeName>`
375+
* - Inside require paths: `/name/`
376+
* - Variable declarations: `var name` (handled by excluding `var ` prefix)
377+
*
370378
* @private
371379
* @param {string} match - example code
372380
* @returns {string} example code using namespace exports
@@ -378,8 +386,12 @@ function create( fullPath ) {
378386
RE = new RegExp( '(^|[^a-zA-Z0-9_@])' + mainExport[ 1 ] + '(?![a-zA-Z0-9_])', 'g' );
379387
match = replace( match, RE, '$1' + name );
380388

381-
// Only match the standalone identifier `name`:
382-
RE = new RegExp( '(^|[^a-zA-Z0-9_@])' + name + '(?![a-zA-Z0-9_])', 'g' );
389+
// Handle `new Name` constructor calls:
390+
RE = new RegExp( '(new )' + name + '(?![a-zA-Z0-9_])', 'g' );
391+
match = replace( match, RE, '$1ns.' + name );
392+
393+
// Handle all other usages: preceded by non-identifier chars, but NOT `<`, `/`, or after `var `:
394+
RE = new RegExp( '([^a-zA-Z0-9_@<./])' + name + '(?![a-zA-Z0-9_>])', 'g' );
383395
return replace( match, RE, '$1ns.' + name );
384396
}
385397
}

0 commit comments

Comments
 (0)