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
24 changes: 23 additions & 1 deletion etc/eslint/rules/stdlib.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable stdlib/jsdoc-doctest-marker, stdlib/jsdoc-doctest, stdlib/jsdoc-example-require-spacing, stdlib/jsdoc-no-tabs */
/* eslint-disable stdlib/jsdoc-doctest-marker, stdlib/jsdoc-example-require-spacing, stdlib/jsdoc-no-tabs */

/**
* @license Apache-2.0
Expand Down Expand Up @@ -275,6 +275,28 @@
*/
rules[ 'stdlib/eol-open-bracket-spacing' ] = 'error';

/**
* Require that format calls are provided an expected number of arguments.
*
* @name format-args
* @memberof rules
* @type {string}
* @default 'error'
*
* @example
* // Bad...
* var format = require( '@stdlib/string/format' );
*
* var str = format( '%s %s', 'foo' );
*
* @example
* // Good...
* var format = require( '@stdlib/string/format' );
*
* var str = format( '%s %s', 'foo', 'bar' );
*/
rules[ 'stdlib/format-args' ] = 'error';

/**
* Require blockquotes to have `2` character indentation.
*
Expand Down Expand Up @@ -879,7 +901,7 @@
* // Bad...
*
* /**
* * Fréchet distribution constructor.

Check warning on line 904 in etc/eslint/rules/stdlib.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "échet"
* *
* * @module @stdlib/stats/base/dists/frechet/ctor
* *
Expand All @@ -895,7 +917,7 @@
* // Good...
*
* /**
* * Fréchet distribution constructor.

Check warning on line 920 in etc/eslint/rules/stdlib.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "échet"
* *
* * @module @stdlib/stats/base/dists/frechet/ctor
* *
Expand Down
140 changes: 140 additions & 0 deletions lib/node_modules/@stdlib/_tools/eslint/rules/format-args/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<!--

@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.

-->

# format-args

> [ESLint rule][eslint-rules] requiring that format calls are provided an expected number of arguments.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var rule = require( '@stdlib/_tools/eslint/rules/format-args' );
```

#### rule

[ESLint rule][eslint-rules] requiring that calls to the format function exported by [`@stdlib/string/format`][@stdlib/string/format] are provided a number of arguments matching the number of placeholders in the format string.

**Bad**:

<!-- eslint-disable stdlib/format-args -->

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

var str = format( '%s %s', 'foo' );
```

**Good**:

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

var str = format( '%s %s', 'foo', 'bar' );
// returns 'foo bar'
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The rule only applies when the format function is resolved from a `require( '@stdlib/string/format' )` statement, thus guarding against unrelated symbols sharing the same name and against shadowed symbols.
- The rule skips calls in which the format string is not a string literal, calls containing spread elements, calls via `#.call` and `#.apply`, and format strings containing positional placeholders (e.g., `%1$s`).

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var Linter = require( 'eslint' ).Linter;
var rule = require( '@stdlib/_tools/eslint/rules/format-args' );

var linter = new Linter();

var code = [
'var format = require( \'@stdlib/string/format\' );',
'',
'var str = format( \'%s %s\', \'foo\' );'
].join( '\n' );

linter.defineRule( 'format-args', rule );

var result = linter.verify( code, {
'rules': {
'format-args': 'error'
}
});
/* returns
[
{
'ruleId': 'format-args',
'severity': 2,
'message': 'Unexpected number of format arguments. Format string expects 2 argument(s), but 1 argument(s) were provided.',
'line': 3,
'column': 11,
'nodeType': 'CallExpression',
'endLine': 3,
'endColumn': 35
}
]
*/
```

</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

[@stdlib/string/format]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/string/format

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @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';

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

var linter = new Linter();
var code = [
'var format = require( \'@stdlib/string/format\' );',
'',
'var str = format( \'%s %s\', \'foo\' );'
].join( '\n' );

linter.defineRule( 'format-args', rule );

var results = linter.verify( code, {
'rules': {
'format-args': 'error'
}
});
console.log( results );
/* =>
[
{
'ruleId': 'format-args',
'severity': 2,
'message': 'Unexpected number of format arguments. Format string expects 2 argument(s), but 1 argument(s) were provided.',
'line': 3,
'column': 11,
'nodeType': 'CallExpression',
'endLine': 3,
'endColumn': 35
}
]
*/
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';

/**
* ESLint rule requiring that format calls are provided an expected number of arguments.
*
* @module @stdlib/_tools/eslint/rules/format-args
*
* @example
* var rule = require( '@stdlib/_tools/eslint/rules/format-args' );
*
* console.log( rule );
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Loading