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
7 changes: 7 additions & 0 deletions etc/eslint/.eslintrc.benchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

// MODULES //

// FIXME: remove the next line and uncomment the subsequent line once all remark JSDoc ESLint rules are completed

Check warning on line 23 in etc/eslint/.eslintrc.benchmarks.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: remove the next line and...'
var copy = require( './../../lib/node_modules/@stdlib/utils/copy' );

// var copy = require( './utils/copy.js' );

Check warning on line 26 in etc/eslint/.eslintrc.benchmarks.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Comments should begin with an uppercase character
var defaults = require( './.eslintrc.js' );


Expand Down Expand Up @@ -126,6 +126,13 @@
*/
eslint.rules[ 'stdlib/no-unnecessary-nested-functions' ] = 'off';

/**
* Warn when using string concatenation in benchmark descriptions.
*
* @private
*/
eslint.rules[ 'stdlib/no-bench-string-concat' ] = 'warn';


// EXPORTS //

Expand Down
9 changes: 9 additions & 0 deletions lib/node_modules/@stdlib/_tools/eslint/rules/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,15 @@ setReadOnly( rules, 'new-cap-error', require( '@stdlib/_tools/eslint/rules/new-c
*/
setReadOnly( rules, 'new-cap-regexp', require( '@stdlib/_tools/eslint/rules/new-cap-regexp' ) );

/**
* @name no-bench-string-concat
* @memberof rules
* @readonly
* @type {Function}
* @see {@link module:@stdlib/_tools/eslint/rules/no-bench-string-concat}
*/
setReadOnly( rules, 'no-bench-string-concat', require( '@stdlib/_tools/eslint/rules/no-bench-string-concat' ) );

/**
* @name no-builtin-big-int
* @memberof rules
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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.

-->

# no-bench-string-concat

> [ESLint rule][eslint-rules] enforcing that `@stdlib/string/format` is used instead of string concatenation in benchmark descriptions.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var rule = require( '@stdlib/_tools/eslint/rules/no-bench-string-concat' );
```

#### rule

[ESLint rule][eslint-rules] enforcing that `@stdlib/string/format` is used instead of string concatenation in benchmark descriptions.

**Bad**:

<!-- run-disable -->

<!-- eslint-disable stdlib/no-bench-string-concat, no-restricted-syntax -->

```javascript
bench( pkg+':len='+len, function benchmark( b ) {
// ...
});
```

**Good**:

<!-- run-disable -->

<!-- eslint-disable no-restricted-syntax -->

```javascript
var format = require( '@stdlib/string/format' );

bench( format( '%s:len=%d', pkg, len ), function benchmark( b ) {
// ...
});
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var Linter = require( 'eslint' ).Linter;
var rule = require( '@stdlib/_tools/eslint/rules/no-bench-string-concat' );

var linter = new Linter();
linter.defineRule( 'no-bench-string-concat', rule );

var code = 'bench( pkg+\':len=\'+len, f );';
var result = linter.verify( code, {
'rules': {
'no-bench-string-concat': 'error'
}
});
/* returns
[
{
'ruleId': 'no-bench-string-concat',
'severity': 2,
'message': 'Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions.',
'line': 1,
'column': 8,
'nodeType': 'BinaryExpression',
'endLine': 1,
'endColumn': 23
}
]
*/
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[eslint-rules]: https://eslint.org/docs/developer-guide/working-with-rules

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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';

var Linter = require( 'eslint' ).Linter;
var rule = require( './../lib' );

var linter = new Linter();
linter.defineRule( 'no-bench-string-concat', rule );

var code = 'bench( pkg+\':len=\'+len, f );';
var result = linter.verify( code, {
'rules': {
'no-bench-string-concat': 'error'
}
});
console.log( result );
/* =>
[
{
'ruleId': 'no-bench-string-concat',
'severity': 2,
'message': 'Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions.',
'line': 1,
'column': 8,
'nodeType': 'BinaryExpression',
'endLine': 1,
'endColumn': 23
}
]
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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';

/**
* ESLint rule enforcing that string concatenation is not used in benchmark descriptions.
*
* @module @stdlib/_tools/eslint/rules/no-bench-string-concat
*
* @example
* var rule = require( '@stdlib/_tools/eslint/rules/no-bench-string-concat' );
*
* 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,116 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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';

// VARIABLES //

var rule;


// FUNCTIONS //

/**
* Checks whether a node is a BinaryExpression with a '+' operator.
*
* @private
* @param {ASTNode} node - AST node
* @returns {boolean} boolean indicating whether the node is a string concatenation
*/
function isStringConcatenation( node ) {
if ( node.type !== 'BinaryExpression' ) {
return false;
}
if ( node.operator !== '+' ) {
return false;
}
return true;
}

/**
* Rule for validating that `@stdlib/string/format` is used instead of string concatenation in benchmark descriptions.
*
* @param {Object} context - ESLint context
* @returns {Object} validators
*/
function main( context ) {
/**
* Reports the error message.
*
* @private
* @param {ASTNode} node - node to report
*/
function report( node ) {
context.report({
'node': node,
'message': 'Use `@stdlib/string/format` instead of string concatenation for benchmark descriptions.'
});
}

/**
* Checks whether a `bench()` call uses string concatenation in its first argument.
*
* @private
* @param {ASTNode} node - CallExpression node to examine
*/
function validate( node ) {
var firstArg;
var callee;

callee = node.callee;

// Check if this is a call to `bench`:
if ( callee.type !== 'Identifier' || callee.name !== 'bench' ) {
return;
}

// Check if there is at least one argument:
if ( node.arguments.length === 0 ) {
return;
}

firstArg = node.arguments[ 0 ];

// Check if the first argument is string concatenation:
if ( isStringConcatenation( firstArg ) ) {
report( firstArg );
}
}

return {
'CallExpression': validate
};
}


// MAIN //

rule = {
'meta': {
'docs': {
'description': 'enforce that `@stdlib/string/format` is used instead of string concatenation in benchmark descriptions'
},
'schema': []
},
'create': main
};


// EXPORTS //

module.exports = rule;
Loading