diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-redeclare/lib/index.js b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-redeclare/lib/index.js new file mode 100644 index 000000000000..1cab392bfe0c --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-redeclare/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 are not redeclared within an examples section. +* +* @module @stdlib/_tools/repl-txt/rules/no-redeclare +* +* @example +* var rule = require( '@stdlib/_tools/repl-txt/rules/no-redeclare' ); +* +* 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-redeclare/lib/main.js b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-redeclare/lib/main.js new file mode 100644 index 000000000000..4f1f56bd8249 --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-redeclare/lib/main.js @@ -0,0 +1,77 @@ +/** +* @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' ); + + +// VARIABLES // + +var RE_VAR_DECLARATION = /\b(?:var|let|const) +([a-zA-Z_$][a-zA-Z0-9_$]*)/g; + + +// MAIN // + +/** +* Rule for enforcing that variables are not redeclared within an examples section. +* +* @param {Context} context - lint context +* @returns {Object} validators +*/ +function main( context ) { + /** + * Checks an examples section for variable redeclarations. + * + * @private + * @param {Object} section - examples section + */ + function noRedeclare( section ) { + var examples; + var declared; + var match; + var i; + + declared = {}; + 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 ( hasOwnProp( declared, match[ 1 ] ) ) { + context.report( format( '%s is already defined', match[ 1 ] ), section ); + } else { + declared[ match[ 1 ] ] = true; + } + match = RE_VAR_DECLARATION.exec( examples[ i ].code ); + } + } + } + + return { + 'examples': noRedeclare + }; +} + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-redeclare/package.json b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-redeclare/package.json new file mode 100644 index 000000000000..f51126593cea --- /dev/null +++ b/lib/node_modules/@stdlib/_tools/repl-txt/rules/no-redeclare/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/_tools/repl-txt/rules/no-redeclare", + "version": "0.0.0", + "description": "Lint rule to enforce that variables are not redeclared within an examples section.", + "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", + "redeclare", + "variables" + ] +}