From 5f1d2b7dca079d2a5e862b000bd84aec6398485f Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 14 Oct 2026 16:23:18 +0500 Subject: [PATCH 1/3] feat: add _tools/repl-txt/rules/no-global-collision --- .../no-global-collision/lib/globals.json | 65 +++++++++++++++++ .../rules/no-global-collision/lib/index.js | 39 ++++++++++ .../rules/no-global-collision/lib/main.js | 73 +++++++++++++++++++ .../rules/no-global-collision/package.json | 65 +++++++++++++++++ 4 files changed, 242 insertions(+) create mode 100644 lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/globals.json create mode 100644 lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/index.js create mode 100644 lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js create mode 100644 lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/package.json diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/globals.json b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/globals.json new file mode 100644 index 000000000000..43fe77219bd1 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/globals.json @@ -0,0 +1,65 @@ +{ + "Array": true, + "ArrayBuffer": true, + "BigInt": true, + "BigInt64Array": true, + "BigUint64Array": true, + "Boolean": true, + "DataView": true, + "Date": true, + "Error": true, + "EvalError": true, + "Float32Array": true, + "Float64Array": true, + "Function": true, + "Int8Array": true, + "Int16Array": true, + "Int32Array": true, + "Map": true, + "Number": true, + "Object": true, + "Promise": true, + "Proxy": true, + "RangeError": true, + "ReferenceError": true, + "Reflect": true, + "RegExp": true, + "Set": true, + "SharedArrayBuffer": true, + "String": true, + "Symbol": true, + "SyntaxError": true, + "TypeError": true, + "URIError": true, + "Uint8Array": true, + "Uint8ClampedArray": true, + "Uint16Array": true, + "Uint32Array": true, + "WeakMap": true, + "WeakSet": true, + "decodeURI": true, + "decodeURIComponent": true, + "encodeURI": true, + "encodeURIComponent": true, + "eval": true, + "isFinite": true, + "isNaN": true, + "parseFloat": true, + "parseInt": true, + "Intl": true, + "JSON": true, + "Math": true, + "Infinity": true, + "NaN": true, + "undefined": true, + "clearImmediate": true, + "clearInterval": true, + "clearTimeout": true, + "setImmediate": true, + "setInterval": true, + "setTimeout": true, + "Buffer": true, + "console": true, + "global": true, + "process": true +} diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/index.js b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/index.js new file mode 100644 index 000000000000..ece394647e5c --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Lint rule to enforce that variables declared within an examples section do not collide with global identifiers. +* +* @module @stdlib/_tools/repl-txt/rules/no-global-collision +* +* @example +* var rule = require( '@stdlib/_tools/repl-txt/rules/no-global-collision' ); +* +* console.log( rule ); +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js new file mode 100644 index 000000000000..f670a665974e --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js @@ -0,0 +1,73 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var format = require( '@stdlib/string/format' ); +var GLOBALS = require( './globals.json' ); + + +// VARIABLES // + +var RE_VAR_DECLARATION = /\b(?:var|let|const) +([a-zA-Z_$][a-zA-Z0-9_$]*)/g; + + +// MAIN // + +/** +* Rule for enforcing that variables declared within an examples section do not collide with global identifiers. +* +* @param {Context} context - lint context +* @returns {Object} validators +*/ +function main( context ) { + /** + * Checks an examples section for variable declarations that collide with global identifiers. + * + * @private + * @param {Object} section - examples section + */ + function noGlobalCollision( section ) { + var examples; + var match; + var i; + + examples = section.examples; + for ( i = 0; i < examples.length; i++ ) { + RE_VAR_DECLARATION.lastIndex = 0; + match = RE_VAR_DECLARATION.exec( examples[ i ].code ); + while ( match ) { + if ( GLOBALS[ match[ 1 ] ] === true ) { + context.report( format( '%s collides with a global identifier', match[ 1 ] ), section ); + } + match = RE_VAR_DECLARATION.exec( examples[ i ].code ); + } + } + } + + return { + 'examples': noGlobalCollision + }; +} + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/package.json b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/package.json new file mode 100644 index 000000000000..6b921f448cc8 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/_tools/repl-txt/rules/no-global-collision", + "version": "0.0.0", + "description": "Lint rule to enforce that variables declared within an examples section do not collide with global identifiers.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "bin": {}, + "main": "./lib", + "directories": { + "lib": "./lib" + }, + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "tools", + "tool", + "repl.txt", + "repl", + "lint", + "custom", + "rules", + "rule", + "plugin", + "code", + "example", + "style", + "global", + "collision", + "shadow" + ] +} From 52a970651523961f5b64776a61a94b77076d28ab Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 15 Oct 2026 12:26:31 +0500 Subject: [PATCH 2/3] refactor: apply suggestions from code review --- .../no-global-collision/lib/globals.json | 65 ------------------- .../rules/no-global-collision/lib/main.js | 31 ++++++++- 2 files changed, 29 insertions(+), 67 deletions(-) delete mode 100644 lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/globals.json diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/globals.json b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/globals.json deleted file mode 100644 index 43fe77219bd1..000000000000 --- a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/globals.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "Array": true, - "ArrayBuffer": true, - "BigInt": true, - "BigInt64Array": true, - "BigUint64Array": true, - "Boolean": true, - "DataView": true, - "Date": true, - "Error": true, - "EvalError": true, - "Float32Array": true, - "Float64Array": true, - "Function": true, - "Int8Array": true, - "Int16Array": true, - "Int32Array": true, - "Map": true, - "Number": true, - "Object": true, - "Promise": true, - "Proxy": true, - "RangeError": true, - "ReferenceError": true, - "Reflect": true, - "RegExp": true, - "Set": true, - "SharedArrayBuffer": true, - "String": true, - "Symbol": true, - "SyntaxError": true, - "TypeError": true, - "URIError": true, - "Uint8Array": true, - "Uint8ClampedArray": true, - "Uint16Array": true, - "Uint32Array": true, - "WeakMap": true, - "WeakSet": true, - "decodeURI": true, - "decodeURIComponent": true, - "encodeURI": true, - "encodeURIComponent": true, - "eval": true, - "isFinite": true, - "isNaN": true, - "parseFloat": true, - "parseInt": true, - "Intl": true, - "JSON": true, - "Math": true, - "Infinity": true, - "NaN": true, - "undefined": true, - "clearImmediate": true, - "clearInterval": true, - "clearTimeout": true, - "setImmediate": true, - "setInterval": true, - "setTimeout": true, - "Buffer": true, - "console": true, - "global": true, - "process": true -} diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js index f670a665974e..91fb40ac0e3b 100644 --- a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js @@ -20,8 +20,9 @@ // MODULES // +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); var format = require( '@stdlib/string/format' ); -var GLOBALS = require( './globals.json' ); +var aliases = require( '@stdlib/namespace/aliases/data/data.json' ); // VARIABLES // @@ -29,6 +30,30 @@ var GLOBALS = require( './globals.json' ); var RE_VAR_DECLARATION = /\b(?:var|let|const) +([a-zA-Z_$][a-zA-Z0-9_$]*)/g; +// FUNCTIONS // + +/** +* Builds a lookup table of global identifiers from the stdlib namespace aliases. +* +* @private +* @returns {Object} lookup table +*/ +function buildLookup() { + var lookup = {}; + var parts; + var i; + var j; + + for ( i = 0; i < aliases.length; i++ ) { + parts = aliases[ i ].split( '.' ); + for ( j = 0; j < parts.length; j++ ) { + lookup[ parts[ j ] ] = true; + } + } + return lookup; +} + + // MAIN // /** @@ -38,6 +63,8 @@ var RE_VAR_DECLARATION = /\b(?:var|let|const) +([a-zA-Z_$][a-zA-Z0-9_$]*)/g; * @returns {Object} validators */ function main( context ) { + var globals = buildLookup(); + /** * Checks an examples section for variable declarations that collide with global identifiers. * @@ -54,7 +81,7 @@ function main( context ) { RE_VAR_DECLARATION.lastIndex = 0; match = RE_VAR_DECLARATION.exec( examples[ i ].code ); while ( match ) { - if ( GLOBALS[ match[ 1 ] ] === true ) { + if ( hasOwnProp( globals, match[ 1 ] ) ) { context.report( format( '%s collides with a global identifier', match[ 1 ] ), section ); } match = RE_VAR_DECLARATION.exec( examples[ i ].code ); From e52aac240715cde785b3e520a7bdef54d20095d5 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 15 Oct 2026 22:01:13 +0500 Subject: [PATCH 3/3] fix: apply suggestions from code review --- .../rules/no-global-collision/lib/index.js | 2 +- .../rules/no-global-collision/lib/main.js | 37 ++++++++++--------- .../rules/no-global-collision/package.json | 2 +- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/index.js b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/index.js index ece394647e5c..cd66543b09fb 100644 --- a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/index.js +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* Lint rule to enforce that variables declared within an examples section do not collide with global identifiers. +* Lint rule to enforce that identifiers declared within an examples section do not collide with global identifiers in the REPL namespace. * * @module @stdlib/_tools/repl-txt/rules/no-global-collision * diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js index 91fb40ac0e3b..5a80fa2227e8 100644 --- a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/lib/main.js @@ -22,12 +22,13 @@ var hasOwnProp = require( '@stdlib/assert/has-own-property' ); var format = require( '@stdlib/string/format' ); -var aliases = require( '@stdlib/namespace/aliases/data/data.json' ); +var aliases = require( '@stdlib/namespace/aliases' ); // VARIABLES // -var RE_VAR_DECLARATION = /\b(?:var|let|const) +([a-zA-Z_$][a-zA-Z0-9_$]*)/g; +var RE_IDENTIFIER = /\b(?:var|let|const|(?:async\s+)?function\*?) +([a-zA-Z_$][a-zA-Z0-9_$]*)/g; +var GLOBALS = buildLookup(); // FUNCTIONS // @@ -39,15 +40,19 @@ var RE_VAR_DECLARATION = /\b(?:var|let|const) +([a-zA-Z_$][a-zA-Z0-9_$]*)/g; * @returns {Object} lookup table */ function buildLookup() { - var lookup = {}; - var parts; + var lookup; + var list; + var idx; var i; - var j; - for ( i = 0; i < aliases.length; i++ ) { - parts = aliases[ i ].split( '.' ); - for ( j = 0; j < parts.length; j++ ) { - lookup[ parts[ j ] ] = true; + list = aliases(); + lookup = {}; + for ( i = 0; i < list.length; i++ ) { + idx = list[ i ].indexOf( '.' ); + if ( idx === -1 ) { + lookup[ list[ i ] ] = true; + } else { + lookup[ list[ i ].substring( 0, idx ) ] = true; } } return lookup; @@ -57,16 +62,14 @@ function buildLookup() { // MAIN // /** -* Rule for enforcing that variables declared within an examples section do not collide with global identifiers. +* Rule for enforcing that identifiers declared within an examples section do not collide with global identifiers in the REPL namespace. * * @param {Context} context - lint context * @returns {Object} validators */ function main( context ) { - var globals = buildLookup(); - /** - * Checks an examples section for variable declarations that collide with global identifiers. + * Checks an examples section for identifiers that collide with global identifiers. * * @private * @param {Object} section - examples section @@ -78,13 +81,13 @@ function main( context ) { examples = section.examples; for ( i = 0; i < examples.length; i++ ) { - RE_VAR_DECLARATION.lastIndex = 0; - match = RE_VAR_DECLARATION.exec( examples[ i ].code ); + RE_IDENTIFIER.lastIndex = 0; + match = RE_IDENTIFIER.exec( examples[ i ].code ); while ( match ) { - if ( hasOwnProp( globals, match[ 1 ] ) ) { + if ( hasOwnProp( GLOBALS, match[ 1 ] ) ) { context.report( format( '%s collides with a global identifier', match[ 1 ] ), section ); } - match = RE_VAR_DECLARATION.exec( examples[ i ].code ); + match = RE_IDENTIFIER.exec( examples[ i ].code ); } } } diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/package.json b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/package.json index 6b921f448cc8..0c0c76696d85 100644 --- a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/package.json +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-global-collision/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/_tools/repl-txt/rules/no-global-collision", "version": "0.0.0", - "description": "Lint rule to enforce that variables declared within an examples section do not collide with global identifiers.", + "description": "Lint rule to enforce that identifiers declared within an examples section do not collide with global identifiers in the REPL namespace.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors",