-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add ESLint rule disallowing string concatenation in benchmark descriptions #9031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
lib/node_modules/@stdlib/_tools/eslint/rules/no-bench-string-concat/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| <!-- | ||
|
|
||
| @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(); | ||
| var result; | ||
| var code; | ||
|
|
||
| code = 'bench( pkg+\':len=\'+len, f );'; | ||
|
|
||
| linter.defineRule( 'no-bench-string-concat', rule ); | ||
|
|
||
| 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 --> | ||
51 changes: 51 additions & 0 deletions
51
lib/node_modules/@stdlib/_tools/eslint/rules/no-bench-string-concat/examples/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /** | ||
| * @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(); | ||
| var result; | ||
| var code; | ||
|
|
||
| code = 'bench( pkg+\':len=\'+len, f );'; | ||
|
|
||
| linter.defineRule( 'no-bench-string-concat', rule ); | ||
|
|
||
| result = linter.verify( code, { | ||
kgryte marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| '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 | ||
| } | ||
| ] | ||
| */ | ||
39 changes: 39 additions & 0 deletions
39
lib/node_modules/@stdlib/_tools/eslint/rules/no-bench-string-concat/lib/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
116 changes: 116 additions & 0 deletions
116
lib/node_modules/@stdlib/_tools/eslint/rules/no-bench-string-concat/lib/main.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.