fix: address doctest regex span and stale eslint-disable in utils/constructor-name example#11893
fix: address doctest regex span and stale eslint-disable in utils/constructor-name example#11893Planeshifter wants to merge 3 commits into
utils/constructor-name example#11893Conversation
…nstructor-name` example The CI JavaScript lint job (run https://github.com/stdlib-js/stdlib/actions/runs/25195861572, issue #11867) failed with two errors in `utils/constructor-name/examples/index.js`: 1. `stdlib/doctest` at line 39: "Displayed return value is `'String'`, but expected `undefined` instead". Root cause: the doctest rule's `RE_ANNOTATION` regex uses `[^;]*;` which matches any non-semicolon characters including newlines. The original `noop()` body contained only `// Do nothing...` with no semicolon, so the greedy `[^;]*` spanned from `function noop` across the function body and blank lines to `console.log( constructorName( 'a' ) );`, misattributing the `// => 'String'` annotation to the `function` identifier. Adding `return;` to the body provides a semicolon that terminates the greedy match inside the function, so the regex no longer spans past it. 2. Unused `eslint-disable-line no-buffer-constructor` at line 151. The `no-buffer-constructor` rule no longer fires for `new Buffer()` in this project, making the directive stale. Removing it eliminates the unused-directive error. Failing run: https://github.com/stdlib-js/stdlib/actions/runs/25195861572
…ntion Reviewer C (style) flagged that removing the `// Do nothing...` comment deviated from the established stdlib noop convention in example files. Restore the comment and keep `return;` below it so the function body preserves convention while still providing the semicolon that terminates the doctest regex's greedy `[^;]*` match inside the function.
|
Hello! Thank you for your contribution to stdlib. We noticed that the contributing guidelines acknowledgment is missing from your pull request. Here's what you need to do:
This acknowledgment confirms that you've read the guidelines, which include:
We can't review or accept contributions without this acknowledgment. Thank you for your understanding and cooperation. We look forward to reviewing your contribution! |
Coverage Report
The above coverage report was generated for the changes in this PR. |
The `return;` added to break the `stdlib/doctest` regex's greedy span triggers the `no-useless-return` rule (configured as error). Add an inline eslint-disable-line comment to suppress it. The `RE_ESLINT_INLINE` preprocessing in the doctest rule strips `eslint-disable-line` comments before running `RE_ANNOTATION`, so the doctest fix is unaffected.
|
This PR is a duplicate of another PR. The lint error was already resolved, but we are still in need of a general solution for when the doctest return annotation is matched incorrectly. |
|
Ref: #11906 |
Failing run: https://github.com/stdlib-js/stdlib/actions/runs/25195861572 (issue #11867, 2026-05-01)
Symptom:
Root cause:
Error 1 (
stdlib/doctest): The rule'sRE_ANNOTATIONregex uses[^;]*;\nto locate annotated expressions.[^;]*is greedy and matches any non-semicolon character, including newlines. The originalnoop()body contained only// Do nothing...— no semicolon — so the regex spanned fromfunction noopacross the entire function body and the trailing blank line toconsole.log( constructorName( 'a' ) );on line 44. The rule then associated// => 'String'with thefunctionidentifier, evaluated the combined expression in a VM sandbox, and gotundefined(not'String').Error 2 (unused
eslint-disable-line): Theno-buffer-constructorrule is no longer active in the project's ESLint configuration, making the inline directive on line 151 stale.Fix:
Two minimal edits to
lib/node_modules/@stdlib/utils/constructor-name/examples/index.js:Add
return; // eslint-disable-line no-useless-returnafter// Do nothing...innoop(). This inserts a semicolon inside the function body, causingRE_ANNOTATION's[^;]*to stop inside the function instead of spanning across it. The// Do nothing...comment is preserved to match stdlib noop conventions. Theno-useless-returndisable is required because ESLint flags a barereturn;at the end of a void function. TheRE_ESLINT_INLINEpreprocessor in thestdlib/doctestrule stripseslint-disable-linecomments before runningRE_ANNOTATION, so the doctest fix is unaffected by the suppression comment.Remove
// eslint-disable-line no-buffer-constructorfrom line 151. The rule is not present inetc/eslint/.eslintrc.js,.eslintrc.examples.js, or.eslintrc.overrides.js, confirming the directive is stale.Neither change alters the runtime behavior of the example.
noopis only consumed byconstructorName( noop )to demonstrate a'Function'result; its return value is never used.Validation:
return;in the body (comment stripped byRE_ESLINT_INLINE),[^;]*matchesnoop() {\n\t// Do nothing...\n\treturnand stops at;. The following\n}does not match\n//, so the wrong match attempt fails. The regex then correctly matchesconsole.log( constructorName( 'a' ) );\n// => 'String'as intended.no-buffer-constructorabsent from all project ESLint configs; removing the directive will not introduce a new error.utils/constructor-name100% (statements, branches, functions, lines).Reviewer notes:
lib/node_modules/@stdlib/utils/constructor-name/README.md) still shows the original// Do nothing...-only noop body. The README is not processed bystdlib/doctest(the rule is disabled for markdown), so this is not a lint issue. Aligning the README is out of scope but could be done as a follow-up.// eslint-disable-line no-buffer-constructordirectives exist elsewhere in the repo (e.g.,buffer/from-buffer,buffer/from-array,assert/is-gzip-buffer). Those are separate packages and out of scope here, but a repo-wide cleanup may be warranted.Related: issue #11867 (clusters 1 and 2 of that issue —
symbol/async-iteratorandndarray/fancy— are addressed separately in draft PR #11882)Contributing Guidelines