Skip to content
Open
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
66 changes: 65 additions & 1 deletion etc/eslint/.eslintrc.overrides.js
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm at a loss for explaining why the overrides file is being modified in this PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the actual approach which should be taken is to 1) create an .eslintrc.jsdoc.js file which uses the .eslintrc.examples.js file has the source of the defaults and 2) enables the rule in the stdlib.js file by loading in the file created in step (1).

As it is, this PR seems confused and is just creating a configuration mess.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's workaround to avoid a circular dependency as was detailed in the PR description:

The rule is configured in .eslintrc.overrides.js (rather than stdlib.js) to avoid a circular dependency: stdlib.js → .eslintrc.examples.js → .eslintrc.js → rules/index.js → stdlib.js. The overrides file loads after the base config resolves, so it can safely require the examples config.

Per Claude,

What actually happens at runtime under that cycle: when stdlib.js is being evaluated as part of resolving .eslintrc.js,
requiring .eslintrc.jsdoc.js reaches back to .eslintrc.js mid-evaluation. Node returns the partial module.exports (empty at
that point). .eslintrc.examples.js does copy(defaults) against {}, sets a handful of its own rules, returns a near-empty
config. .eslintrc.jsdoc.js filters that near-empty rules map and the jsdoc-example-eslint rule ends up with effectively no
rules to apply. Silent breakage — the linter loads, but the JSDoc snippet rule does nothing.

I ran into this problem during development of this rule. To address your desire, Claude suggests

split config into .eslintrc.jsdoc.js, but keep wiring in .eslintrc.overrides.js (one-liner:
eslint.rules['stdlib/jsdoc-example-eslint'] = ['warn', require('./.eslintrc.jsdoc.js')];). Cycle stays broken because the
overrides file resolves AFTER the base. Addresses his "config mess" complaint cleanly: the heavy lifting lives in a
dedicated, named file consistent with .eslintrc.examples.js/.eslintrc.tests.js/.eslintrc.benchmarks.js; the overrides file
just hooks it up.

Thoughts?

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,72 @@
* limitations under the License.
*/

/* eslint-disable vars-on-top, stdlib/empty-line-before-comment */

'use strict';

// MODULES //

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

Check warning on line 25 in etc/eslint/.eslintrc.overrides.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 27 in etc/eslint/.eslintrc.overrides.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Comments should begin with an uppercase character
var objectKeys = require( './../../lib/node_modules/@stdlib/utils/keys' );
var defaults = require( './.eslintrc.js' );
var examplesConfig = require( './.eslintrc.examples.js' );


// VARIABLES //

/**
* List of rules to exclude when linting JSDoc code snippets because they are incompatible with code fragments extracted from JSDoc comments.
*
* @private
* @type {Array}
*/
var JSDOC_SNIPPET_EXCLUDE = [
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer to other files such as .eslintrc.examples for how rules are overridden. In short, be consistent with what we already have.

'no-undef',
'no-unused-vars',
'strict',
'no-var',
'eol-last',
'indent',
'no-restricted-syntax'
];


// FUNCTIONS //

/**
* Extracts built-in ESLint rules from a configuration object, filtering out plugin rules and rules listed in an exclusion list.
*
* @private
* @param {Object} config - ESLint configuration object
* @param {Array} exclude - rule names to exclude
* @returns {Object} filtered rules object
*/
function extractSnippetRules( config, exclude ) {
var rules;
var keys;
var key;
var i;

rules = {};
keys = objectKeys( config.rules );
for ( i = 0; i < keys.length; i++ ) {
key = keys[ i ];

// Skip plugin rules (e.g., "stdlib/foo", "node/bar") since the Linter instance does not have them registered:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment.

if ( key.indexOf( '/' ) !== -1 ) {
continue;
}
// Skip rules explicitly excluded as incompatible with code snippets:
if ( exclude.indexOf( key ) !== -1 ) {
continue;
}
rules[ key ] = config.rules[ key ];
}
return rules;
}


// MAIN //
Expand All @@ -45,6 +102,13 @@
*/
eslint.overrides = require( './overrides' );

/**
* Configure JSDoc example linting using the examples ESLint config, with rules filtered for use on JSDoc code snippets.
*/
eslint.rules[ 'stdlib/jsdoc-example-eslint' ] = [ 'warn', {
'rules': extractSnippetRules( examplesConfig, JSDOC_SNIPPET_EXCLUDE )
}];


// EXPORTS //

Expand Down
32 changes: 32 additions & 0 deletions etc/eslint/rules/stdlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,38 @@
*/
rules[ 'stdlib/jsdoc-empty-line-before-example' ] = 'error';

/**
* Lint JavaScript code in JSDoc example blocks using ESLint.
*
* @name jsdoc-example-eslint
* @memberof rules
* @type {string}
* @default 'warn'
*
* @example
* // Bad...
*
* /**
* * Squares a number.
* *
* * @example
* * var y = square( 3.0 )
* *\/
* function square( x ) {}
*
* @example
* // Good...
*
* /**
* * Squares a number.
* *
* * @example
* * var y = square( 3.0 );
* *\/
* function square( x ) {}
*/
rules[ 'stdlib/jsdoc-example-eslint' ] = 'off';

/**
* Enforce empty lines between requires and code in JSDoc examples.
*
Expand All @@ -879,7 +911,7 @@
* // Bad...
*
* /**
* * Fréchet distribution constructor.

Check warning on line 914 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 +927,7 @@
* // Good...
*
* /**
* * Fréchet distribution constructor.

Check warning on line 930 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<!--

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

-->

# jsdoc-example-eslint

> [ESLint rule][eslint-rules] to lint JavaScript code in JSDoc example blocks.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

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

#### rule

[ESLint rule][eslint-rules] to lint JavaScript code in JSDoc example blocks. The rule extracts code from `@example` tags in JSDoc comments, lints it using a provided set of ESLint rules, and reports violations mapped back to the original source lines.

The rule accepts an options object with a `rules` property specifying which ESLint rules to apply to the extracted example code.

**Bad** (missing semicolon):

<!-- eslint stdlib/jsdoc-example-eslint: "off", stdlib/jsdoc-doctest-marker: "off", valid-jsdoc: "off" -->

```javascript
/**
* Squares a number.
*
* @example
* var y = square( 3.0 )
*/
function square( x ) {
return x * x;
}
```

**Good**:

<!-- eslint valid-jsdoc: "off" -->

```javascript
/**
* Squares a number.
*
* @example
* var y = square( 3.0 );
*/
function square( x ) {
return x * x;
}
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

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

var linter = new Linter();
var code = [
'/**',
'* Squares a number.',
'*',
'* @example',
'* var y = square( 3.0 )',
'* // returns 9.0',
'*/',
'function square( x ) {',
'\treturn x * x;',
'}'
].join( '\n' );

linter.defineRule( 'jsdoc-example-eslint', rule );

var result = linter.verify( code, {
'rules': {
'jsdoc-example-eslint': [ 'error', {
'rules': {
'semi': 'error'
}
}]
}
});
console.log( result );
/* =>
[
{
'ruleId': 'jsdoc-example-eslint',
'severity': 2,
'message': 'Missing semicolon. (semi)',
...
}
]
*/
```

</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,59 @@
/**
* @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 = [
'/**',
'* Squares a number.',
'*',
'* @example',
'* var y = square( 3.0 )',
'* // returns 9.0',
'*/',
'function square( x ) {',
'\treturn x * x;',
'}'
].join( '\n' );

linter.defineRule( 'jsdoc-example-eslint', rule );

var result = linter.verify( code, {
'rules': {
'jsdoc-example-eslint': [ 'error', {
'rules': {
'semi': 'error'
}
}]
}
});
console.log( result );
/* =>
[
{
'ruleId': 'jsdoc-example-eslint',
'severity': 2,
'message': 'Missing semicolon. (semi)',
...
}
]
*/
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 to lint JavaScript code in JSDoc example blocks.
*
* @module @stdlib/_tools/eslint/rules/jsdoc-example-eslint
*
* @example
* var main = require( '@stdlib/_tools/eslint/rules/jsdoc-example-eslint' );
*
* console.log( main );
*/

// MODULES //

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


// EXPORTS //

module.exports = main;
Loading
Loading