Skip to content
Merged
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 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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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"
]
}
Loading