Skip to content

Commit 285a03c

Browse files
authored
feat(getJsdocProcessorPlugin): allow exampleCodeRegex and rejectExampleCodeRegex to be RegExp objects (#1395)
Also: - fix(getJsdocProcessorPlugin): avoid auto-fix being triggered for example text while we still have no fixers applied
1 parent 99cb131 commit 285a03c

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

src/getJsdocProcessorPlugin.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ const getLinesCols = (text) => {
9090
* @property {string} [matchingFileNameDefaults] See docs
9191
* @property {string} [matchingFileNameParams] See docs
9292
* @property {string} [matchingFileNameProperties] See docs
93-
* @property {string} [exampleCodeRegex] See docs
94-
* @property {string} [rejectExampleCodeRegex] See docs
93+
* @property {string|RegExp} [exampleCodeRegex] See docs
94+
* @property {string|RegExp} [rejectExampleCodeRegex] See docs
9595
* @property {string[]} [allowedLanguagesToProcess] See docs
9696
* @property {"script"|"module"} [sourceType] See docs
9797
* @property {import('eslint').Linter.ESTreeParser|import('eslint').Linter.NonESTreeParser} [parser] See docs
@@ -129,11 +129,15 @@ export const getJsdocProcessorPlugin = (options = {}) => {
129129
let rejectExampleCodeRegExp;
130130

131131
if (exampleCodeRegex) {
132-
exampleCodeRegExp = getRegexFromString(exampleCodeRegex);
132+
exampleCodeRegExp = typeof exampleCodeRegex === 'string' ?
133+
getRegexFromString(exampleCodeRegex) :
134+
exampleCodeRegex;
133135
}
134136

135137
if (rejectExampleCodeRegex) {
136-
rejectExampleCodeRegExp = getRegexFromString(rejectExampleCodeRegex);
138+
rejectExampleCodeRegExp = typeof rejectExampleCodeRegex === 'string' ?
139+
getRegexFromString(rejectExampleCodeRegex) :
140+
rejectExampleCodeRegex;
137141
}
138142

139143
/**
@@ -646,7 +650,8 @@ export const getJsdocProcessorPlugin = (options = {}) => {
646650

647651
return [];
648652
},
649-
supportsAutofix: true,
653+
// Todo: Reenable
654+
supportsAutofix: false,
650655
},
651656
},
652657
};

test/getJsdocProcessPlugin.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,34 @@ describe('`getJsdocProcessorPlugin`', () => {
9292
});
9393
});
9494

95+
it('returns text and files with `exampleCodeRegex` (as RegExp)', () => {
96+
const options = {
97+
exampleCodeRegex: /```js([\s\S]*)```/u,
98+
};
99+
const filename = 'something.js';
100+
const text = `
101+
/**
102+
* @example
103+
* \`\`\`js
104+
* doSth('a');
105+
* \`\`\`
106+
*/
107+
function doSth () {}
108+
`;
109+
check({
110+
filename,
111+
options,
112+
result: [
113+
text,
114+
{
115+
filename: 'something.md/*.js',
116+
text: '\ndoSth(\'a\');\n',
117+
},
118+
],
119+
text,
120+
});
121+
});
122+
95123
it('returns text and files with `exampleCodeRegex` (no parentheses)', () => {
96124
const options = {
97125
exampleCodeRegex: '// begin[\\s\\S]*// end',
@@ -246,6 +274,39 @@ describe('`getJsdocProcessorPlugin`', () => {
246274
});
247275
});
248276

277+
it('returns text and files (with `rejectExampleCodeRegex`) as RegExp', () => {
278+
const options = {
279+
rejectExampleCodeRegex: /^\s*<.*>\s*$/u,
280+
};
281+
const filename = 'something.js';
282+
const text = `
283+
/**
284+
* @example <b>Not JavaScript</b>
285+
*/
286+
function quux () {
287+
288+
}
289+
/**
290+
* @example quux2();
291+
*/
292+
function quux2 () {
293+
294+
}
295+
`;
296+
check({
297+
filename,
298+
options,
299+
result: [
300+
text,
301+
{
302+
filename: 'something.md/*.js',
303+
text: 'quux2();',
304+
},
305+
],
306+
text,
307+
});
308+
});
309+
249310
it('returns text and files (with `matchingFileName`)', () => {
250311
const options = {
251312
matchingFileName: '../../jsdocUtils.js',

0 commit comments

Comments
 (0)