Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 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
*
* @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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' );
var format = require( '@stdlib/string/format' );
var aliases = require( '@stdlib/namespace/aliases' );


// VARIABLES //

var RE_IDENTIFIER = /\b(?:var|let|const|(?:async\s+)?function\*?) +([a-zA-Z_$][a-zA-Z0-9_$]*)/g;
var GLOBALS = buildLookup();


// FUNCTIONS //

/**
* Builds a lookup table of global identifiers from the stdlib namespace aliases.
*
* @private
* @returns {Object} lookup table
*/
function buildLookup() {
var lookup;
var list;
var idx;
var i;

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;
}


// MAIN //

/**
* 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 ) {
/**
* Checks an examples section for identifiers 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_IDENTIFIER.lastIndex = 0;
match = RE_IDENTIFIER.exec( examples[ i ].code );
while ( match ) {
if ( hasOwnProp( GLOBALS, match[ 1 ] ) ) {
context.report( format( '%s collides with a global identifier', match[ 1 ] ), section );
}
match = RE_IDENTIFIER.exec( examples[ i ].code );
}
}
}

return {
'examples': noGlobalCollision
};
}


// EXPORTS //

module.exports = main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"name": "@stdlib/_tools/repl-txt/rules/no-global-collision",
"version": "0.0.0",
"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",
"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"
]
}
Loading