From 39a3109e6511a3eded114770fdbd50663d6f4f24 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Sun, 14 Sep 2025 15:27:56 +0800 Subject: [PATCH] fix: CJS export regression; fixes #1478 --- src/buildForbidRuleDefinition.js | 86 ++++++++++++++++++++++++++++++ src/index-cjs.js | 89 ++------------------------------ src/index-esm.js | 5 +- src/index.js | 89 ++------------------------------ test/index.js | 4 +- 5 files changed, 98 insertions(+), 175 deletions(-) create mode 100644 src/buildForbidRuleDefinition.js diff --git a/src/buildForbidRuleDefinition.js b/src/buildForbidRuleDefinition.js new file mode 100644 index 000000000..652042e94 --- /dev/null +++ b/src/buildForbidRuleDefinition.js @@ -0,0 +1,86 @@ +import iterateJsdoc from './iterateJsdoc.js'; + +/** + * @param {{ + * contexts: (string|{ + * comment: string, + * context: string, + * message: string + * })[], + * description?: string, + * contextName?: string + * url?: string, + * }} cfg + * @returns {import('@eslint/core').RuleDefinition< + * import('@eslint/core').RuleDefinitionTypeOptions + * >} + */ +export const buildForbidRuleDefinition = ({ + contextName, + contexts, + description, + url, +}) => { + return iterateJsdoc(({ + // context, + info: { + comment, + }, + report, + utils, + }) => { + const { + contextStr, + foundContext, + } = utils.findContext(contexts, comment); + + // We are not on the *particular* matching context/comment, so don't assume + // we need reporting + if (!foundContext) { + return; + } + + const message = /** @type {import('./iterateJsdoc.js').ContextObject} */ ( + foundContext + )?.message ?? + 'Syntax is restricted: {{context}}' + + (comment ? ' with {{comment}}' : ''); + + report(message, null, null, comment ? { + comment, + context: contextStr, + } : { + context: contextStr, + }); + }, { + contextSelected: true, + meta: { + docs: { + description: description ?? contextName ?? 'Reports when certain comment structures are present.', + url: url ?? 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/advanced.md#user-content-advanced-creating-your-own-rules', + }, + fixable: 'code', + schema: [], + type: 'suggestion', + }, + modifyContext: (context) => { + // Reproduce context object with our own `contexts` + const propertyDescriptors = Object.getOwnPropertyDescriptors(context); + return Object.create( + Object.getPrototypeOf(context), + { + ...propertyDescriptors, + options: { + ...propertyDescriptors.options, + value: [ + { + contexts, + }, + ], + }, + }, + ); + }, + nonGlobalSettings: true, + }); +}; diff --git a/src/index-cjs.js b/src/index-cjs.js index fa253093f..c3a62e188 100644 --- a/src/index-cjs.js +++ b/src/index-cjs.js @@ -1,7 +1,9 @@ +import { + buildForbidRuleDefinition, +} from './buildForbidRuleDefinition.js'; import { getJsdocProcessorPlugin, } from './getJsdocProcessorPlugin.js'; -import iterateJsdoc from './iterateJsdoc.js'; import checkAccess from './rules/checkAccess.js'; import checkAlignment from './rules/checkAlignment.js'; import checkExamples from './rules/checkExamples.js'; @@ -61,91 +63,6 @@ import textEscaping from './rules/textEscaping.js'; import typeFormatting from './rules/typeFormatting.js'; import validTypes from './rules/validTypes.js'; -/** - * @param {{ - * contexts: (string|{ - * comment: string, - * context: string, - * message: string - * })[], - * description?: string, - * contextName?: string - * url?: string, - * }} cfg - * @returns {import('@eslint/core').RuleDefinition< - * import('@eslint/core').RuleDefinitionTypeOptions - * >} - */ -export const buildForbidRuleDefinition = ({ - contextName, - contexts, - description, - url, -}) => { - return iterateJsdoc(({ - // context, - info: { - comment, - }, - report, - utils, - }) => { - const { - contextStr, - foundContext, - } = utils.findContext(contexts, comment); - - // We are not on the *particular* matching context/comment, so don't assume - // we need reporting - if (!foundContext) { - return; - } - - const message = /** @type {import('./iterateJsdoc.js').ContextObject} */ ( - foundContext - )?.message ?? - 'Syntax is restricted: {{context}}' + - (comment ? ' with {{comment}}' : ''); - - report(message, null, null, comment ? { - comment, - context: contextStr, - } : { - context: contextStr, - }); - }, { - contextSelected: true, - meta: { - docs: { - description: description ?? contextName ?? 'Reports when certain comment structures are present.', - url: url ?? 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/advanced.md#user-content-advanced-creating-your-own-rules', - }, - fixable: 'code', - schema: [], - type: 'suggestion', - }, - modifyContext: (context) => { - // Reproduce context object with our own `contexts` - const propertyDescriptors = Object.getOwnPropertyDescriptors(context); - return Object.create( - Object.getPrototypeOf(context), - { - ...propertyDescriptors, - options: { - ...propertyDescriptors.options, - value: [ - { - contexts, - }, - ], - }, - }, - ); - }, - nonGlobalSettings: true, - }); -}; - /* eslint-disable jsdoc/valid-types -- Bug */ /** * @typedef {"recommended" | "stylistic" | "contents" | "logical" | "requirements"} ConfigGroups diff --git a/src/index-esm.js b/src/index-esm.js index 081453a2c..9ed80fb38 100644 --- a/src/index-esm.js +++ b/src/index-esm.js @@ -4,9 +4,10 @@ import { } from 'object-deep-merge'; // BEGIN REPLACE -import index, { +import index from './index-cjs.js'; +import { buildForbidRuleDefinition, -} from './index-cjs.js'; +} from './buildForbidRuleDefinition.js'; // eslint-disable-next-line unicorn/prefer-export-from --- Reusing `index` export default index; diff --git a/src/index.js b/src/index.js index 796968c7f..1b96ee9f6 100644 --- a/src/index.js +++ b/src/index.js @@ -4,10 +4,12 @@ import { merge, } from 'object-deep-merge'; +import { + buildForbidRuleDefinition, +} from './buildForbidRuleDefinition.js'; import { getJsdocProcessorPlugin, } from './getJsdocProcessorPlugin.js'; -import iterateJsdoc from './iterateJsdoc.js'; import checkAccess from './rules/checkAccess.js'; import checkAlignment from './rules/checkAlignment.js'; import checkExamples from './rules/checkExamples.js'; @@ -67,91 +69,6 @@ import textEscaping from './rules/textEscaping.js'; import typeFormatting from './rules/typeFormatting.js'; import validTypes from './rules/validTypes.js'; -/** - * @param {{ - * contexts: (string|{ - * comment: string, - * context: string, - * message: string - * })[], - * description?: string, - * contextName?: string - * url?: string, - * }} cfg - * @returns {import('@eslint/core').RuleDefinition< - * import('@eslint/core').RuleDefinitionTypeOptions - * >} - */ -export const buildForbidRuleDefinition = ({ - contextName, - contexts, - description, - url, -}) => { - return iterateJsdoc(({ - // context, - info: { - comment, - }, - report, - utils, - }) => { - const { - contextStr, - foundContext, - } = utils.findContext(contexts, comment); - - // We are not on the *particular* matching context/comment, so don't assume - // we need reporting - if (!foundContext) { - return; - } - - const message = /** @type {import('./iterateJsdoc.js').ContextObject} */ ( - foundContext - )?.message ?? - 'Syntax is restricted: {{context}}' + - (comment ? ' with {{comment}}' : ''); - - report(message, null, null, comment ? { - comment, - context: contextStr, - } : { - context: contextStr, - }); - }, { - contextSelected: true, - meta: { - docs: { - description: description ?? contextName ?? 'Reports when certain comment structures are present.', - url: url ?? 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/advanced.md#user-content-advanced-creating-your-own-rules', - }, - fixable: 'code', - schema: [], - type: 'suggestion', - }, - modifyContext: (context) => { - // Reproduce context object with our own `contexts` - const propertyDescriptors = Object.getOwnPropertyDescriptors(context); - return Object.create( - Object.getPrototypeOf(context), - { - ...propertyDescriptors, - options: { - ...propertyDescriptors.options, - value: [ - { - contexts, - }, - ], - }, - }, - ); - }, - nonGlobalSettings: true, - }); -}; - /* eslint-disable jsdoc/valid-types -- Bug */ /** * @typedef {"recommended" | "stylistic" | "contents" | "logical" | "requirements"} ConfigGroups diff --git a/test/index.js b/test/index.js index 2d71d0b43..d701b6ca0 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,7 @@ -import jsdocDefault, { +import { buildForbidRuleDefinition, +} from '../src/buildForbidRuleDefinition.js'; +import jsdocDefault, { jsdoc, } from '../src/index.js'; import {