Skip to content

Commit 03a8a69

Browse files
authored
fix: CJS export regression; fixes #1478 (#1508)
1 parent 34e5910 commit 03a8a69

File tree

5 files changed

+98
-175
lines changed

5 files changed

+98
-175
lines changed

src/buildForbidRuleDefinition.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import iterateJsdoc from './iterateJsdoc.js';
2+
3+
/**
4+
* @param {{
5+
* contexts: (string|{
6+
* comment: string,
7+
* context: string,
8+
* message: string
9+
* })[],
10+
* description?: string,
11+
* contextName?: string
12+
* url?: string,
13+
* }} cfg
14+
* @returns {import('@eslint/core').RuleDefinition<
15+
* import('@eslint/core').RuleDefinitionTypeOptions
16+
* >}
17+
*/
18+
export const buildForbidRuleDefinition = ({
19+
contextName,
20+
contexts,
21+
description,
22+
url,
23+
}) => {
24+
return iterateJsdoc(({
25+
// context,
26+
info: {
27+
comment,
28+
},
29+
report,
30+
utils,
31+
}) => {
32+
const {
33+
contextStr,
34+
foundContext,
35+
} = utils.findContext(contexts, comment);
36+
37+
// We are not on the *particular* matching context/comment, so don't assume
38+
// we need reporting
39+
if (!foundContext) {
40+
return;
41+
}
42+
43+
const message = /** @type {import('./iterateJsdoc.js').ContextObject} */ (
44+
foundContext
45+
)?.message ??
46+
'Syntax is restricted: {{context}}' +
47+
(comment ? ' with {{comment}}' : '');
48+
49+
report(message, null, null, comment ? {
50+
comment,
51+
context: contextStr,
52+
} : {
53+
context: contextStr,
54+
});
55+
}, {
56+
contextSelected: true,
57+
meta: {
58+
docs: {
59+
description: description ?? contextName ?? 'Reports when certain comment structures are present.',
60+
url: url ?? 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/advanced.md#user-content-advanced-creating-your-own-rules',
61+
},
62+
fixable: 'code',
63+
schema: [],
64+
type: 'suggestion',
65+
},
66+
modifyContext: (context) => {
67+
// Reproduce context object with our own `contexts`
68+
const propertyDescriptors = Object.getOwnPropertyDescriptors(context);
69+
return Object.create(
70+
Object.getPrototypeOf(context),
71+
{
72+
...propertyDescriptors,
73+
options: {
74+
...propertyDescriptors.options,
75+
value: [
76+
{
77+
contexts,
78+
},
79+
],
80+
},
81+
},
82+
);
83+
},
84+
nonGlobalSettings: true,
85+
});
86+
};

src/index-cjs.js

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import {
2+
buildForbidRuleDefinition,
3+
} from './buildForbidRuleDefinition.js';
14
import {
25
getJsdocProcessorPlugin,
36
} from './getJsdocProcessorPlugin.js';
4-
import iterateJsdoc from './iterateJsdoc.js';
57
import checkAccess from './rules/checkAccess.js';
68
import checkAlignment from './rules/checkAlignment.js';
79
import checkExamples from './rules/checkExamples.js';
@@ -61,91 +63,6 @@ import textEscaping from './rules/textEscaping.js';
6163
import typeFormatting from './rules/typeFormatting.js';
6264
import validTypes from './rules/validTypes.js';
6365

64-
/**
65-
* @param {{
66-
* contexts: (string|{
67-
* comment: string,
68-
* context: string,
69-
* message: string
70-
* })[],
71-
* description?: string,
72-
* contextName?: string
73-
* url?: string,
74-
* }} cfg
75-
* @returns {import('@eslint/core').RuleDefinition<
76-
* import('@eslint/core').RuleDefinitionTypeOptions
77-
* >}
78-
*/
79-
export const buildForbidRuleDefinition = ({
80-
contextName,
81-
contexts,
82-
description,
83-
url,
84-
}) => {
85-
return iterateJsdoc(({
86-
// context,
87-
info: {
88-
comment,
89-
},
90-
report,
91-
utils,
92-
}) => {
93-
const {
94-
contextStr,
95-
foundContext,
96-
} = utils.findContext(contexts, comment);
97-
98-
// We are not on the *particular* matching context/comment, so don't assume
99-
// we need reporting
100-
if (!foundContext) {
101-
return;
102-
}
103-
104-
const message = /** @type {import('./iterateJsdoc.js').ContextObject} */ (
105-
foundContext
106-
)?.message ??
107-
'Syntax is restricted: {{context}}' +
108-
(comment ? ' with {{comment}}' : '');
109-
110-
report(message, null, null, comment ? {
111-
comment,
112-
context: contextStr,
113-
} : {
114-
context: contextStr,
115-
});
116-
}, {
117-
contextSelected: true,
118-
meta: {
119-
docs: {
120-
description: description ?? contextName ?? 'Reports when certain comment structures are present.',
121-
url: url ?? 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/advanced.md#user-content-advanced-creating-your-own-rules',
122-
},
123-
fixable: 'code',
124-
schema: [],
125-
type: 'suggestion',
126-
},
127-
modifyContext: (context) => {
128-
// Reproduce context object with our own `contexts`
129-
const propertyDescriptors = Object.getOwnPropertyDescriptors(context);
130-
return Object.create(
131-
Object.getPrototypeOf(context),
132-
{
133-
...propertyDescriptors,
134-
options: {
135-
...propertyDescriptors.options,
136-
value: [
137-
{
138-
contexts,
139-
},
140-
],
141-
},
142-
},
143-
);
144-
},
145-
nonGlobalSettings: true,
146-
});
147-
};
148-
14966
/* eslint-disable jsdoc/valid-types -- Bug */
15067
/**
15168
* @typedef {"recommended" | "stylistic" | "contents" | "logical" | "requirements"} ConfigGroups

src/index-esm.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import {
44
} from 'object-deep-merge';
55

66
// BEGIN REPLACE
7-
import index, {
7+
import index from './index-cjs.js';
8+
import {
89
buildForbidRuleDefinition,
9-
} from './index-cjs.js';
10+
} from './buildForbidRuleDefinition.js';
1011

1112
// eslint-disable-next-line unicorn/prefer-export-from --- Reusing `index`
1213
export default index;

src/index.js

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import {
44
merge,
55
} from 'object-deep-merge';
66

7+
import {
8+
buildForbidRuleDefinition,
9+
} from './buildForbidRuleDefinition.js';
710
import {
811
getJsdocProcessorPlugin,
912
} from './getJsdocProcessorPlugin.js';
10-
import iterateJsdoc from './iterateJsdoc.js';
1113
import checkAccess from './rules/checkAccess.js';
1214
import checkAlignment from './rules/checkAlignment.js';
1315
import checkExamples from './rules/checkExamples.js';
@@ -67,91 +69,6 @@ import textEscaping from './rules/textEscaping.js';
6769
import typeFormatting from './rules/typeFormatting.js';
6870
import validTypes from './rules/validTypes.js';
6971

70-
/**
71-
* @param {{
72-
* contexts: (string|{
73-
* comment: string,
74-
* context: string,
75-
* message: string
76-
* })[],
77-
* description?: string,
78-
* contextName?: string
79-
* url?: string,
80-
* }} cfg
81-
* @returns {import('@eslint/core').RuleDefinition<
82-
* import('@eslint/core').RuleDefinitionTypeOptions
83-
* >}
84-
*/
85-
export const buildForbidRuleDefinition = ({
86-
contextName,
87-
contexts,
88-
description,
89-
url,
90-
}) => {
91-
return iterateJsdoc(({
92-
// context,
93-
info: {
94-
comment,
95-
},
96-
report,
97-
utils,
98-
}) => {
99-
const {
100-
contextStr,
101-
foundContext,
102-
} = utils.findContext(contexts, comment);
103-
104-
// We are not on the *particular* matching context/comment, so don't assume
105-
// we need reporting
106-
if (!foundContext) {
107-
return;
108-
}
109-
110-
const message = /** @type {import('./iterateJsdoc.js').ContextObject} */ (
111-
foundContext
112-
)?.message ??
113-
'Syntax is restricted: {{context}}' +
114-
(comment ? ' with {{comment}}' : '');
115-
116-
report(message, null, null, comment ? {
117-
comment,
118-
context: contextStr,
119-
} : {
120-
context: contextStr,
121-
});
122-
}, {
123-
contextSelected: true,
124-
meta: {
125-
docs: {
126-
description: description ?? contextName ?? 'Reports when certain comment structures are present.',
127-
url: url ?? 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/advanced.md#user-content-advanced-creating-your-own-rules',
128-
},
129-
fixable: 'code',
130-
schema: [],
131-
type: 'suggestion',
132-
},
133-
modifyContext: (context) => {
134-
// Reproduce context object with our own `contexts`
135-
const propertyDescriptors = Object.getOwnPropertyDescriptors(context);
136-
return Object.create(
137-
Object.getPrototypeOf(context),
138-
{
139-
...propertyDescriptors,
140-
options: {
141-
...propertyDescriptors.options,
142-
value: [
143-
{
144-
contexts,
145-
},
146-
],
147-
},
148-
},
149-
);
150-
},
151-
nonGlobalSettings: true,
152-
});
153-
};
154-
15572
/* eslint-disable jsdoc/valid-types -- Bug */
15673
/**
15774
* @typedef {"recommended" | "stylistic" | "contents" | "logical" | "requirements"} ConfigGroups

test/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import jsdocDefault, {
1+
import {
22
buildForbidRuleDefinition,
3+
} from '../src/buildForbidRuleDefinition.js';
4+
import jsdocDefault, {
35
jsdoc,
46
} from '../src/index.js';
57
import {

0 commit comments

Comments
 (0)