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
86 changes: 86 additions & 0 deletions src/buildForbidRuleDefinition.js
Original file line number Diff line number Diff line change
@@ -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,
});
};
89 changes: 3 additions & 86 deletions src/index-cjs.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/index-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
89 changes: 3 additions & 86 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import jsdocDefault, {
import {
buildForbidRuleDefinition,
} from '../src/buildForbidRuleDefinition.js';
import jsdocDefault, {
jsdoc,
} from '../src/index.js';
import {
Expand Down
Loading