Skip to content

Commit 0adbf43

Browse files
brettz9Copilot
andauthored
feat(getJsdocProcessorPlugin): add allowedLanguagesToProcess option (#1392)
* feat(`getJsdocProcessorPlugin`): add `allowedLanguagesToProcess` option * Update src/getJsdocProcessorPlugin.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/getJsdocProcessorPlugin.js Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 228c568 commit 0adbf43

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

.README/processors.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ same name.
134134
See the [`check-examples`](./rules/check-examples.md#readme) option of the
135135
same name.
136136

137+
#### `allowedLanguagesToProcess`
138+
139+
This is an array which will narrow the allowable languages of fenced blocks
140+
down to those within the array.
141+
142+
Set to `false` to ensure all present languages (not excluded by
143+
any `exampleCodeRegex` and `rejectExampleCodeRegex` options) will be processed.
144+
145+
Defaults to `['js', 'ts', 'javascript', 'typescript']`.
146+
137147
#### `sourceType`
138148

139149
Whether to use "script" or "module" with the parser. Defaults to `"module"`.

docs/processors.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ same name.
160160
See the [`check-examples`](./rules/check-examples.md#readme) option of the
161161
same name.
162162

163+
<a name="user-content-processors-options-allowedlanguagestoprocess"></a>
164+
<a name="processors-options-allowedlanguagestoprocess"></a>
165+
#### <code>allowedLanguagesToProcess</code>
166+
167+
This is an array which will narrow the allowable languages of fenced blocks
168+
down to those within the array.
169+
170+
Set to `false` to ensure all present languages (not excluded by
171+
any `exampleCodeRegex` and `rejectExampleCodeRegex` options) will be processed.
172+
173+
Defaults to `['js', 'ts', 'javascript', 'typescript']`.
174+
163175
<a name="user-content-processors-options-sourcetype"></a>
164176
<a name="processors-options-sourcetype"></a>
165177
#### <code>sourceType</code>

src/bin/generateRule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from 'fs';
77
import fs from 'fs/promises';
88
/**
9-
*
9+
* @example
1010
* ```shell
1111
* npm run create-rule my-new-rule -- --recommended
1212
* ```

src/getJsdocProcessorPlugin.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Todo: Support TS by fenced block type
2-
31
import {
42
forEachPreferredTag,
53
getPreferredTagName,
@@ -94,6 +92,7 @@ const getLinesCols = (text) => {
9492
* @property {string} [matchingFileNameProperties] See docs
9593
* @property {string} [exampleCodeRegex] See docs
9694
* @property {string} [rejectExampleCodeRegex] See docs
95+
* @property {string[]} [allowedLanguagesToProcess] See docs
9796
* @property {"script"|"module"} [sourceType] See docs
9897
* @property {import('eslint').Linter.ESTreeParser|import('eslint').Linter.NonESTreeParser} [parser] See docs
9998
*/
@@ -105,6 +104,9 @@ const getLinesCols = (text) => {
105104
*/
106105
export const getJsdocProcessorPlugin = (options = {}) => {
107106
const {
107+
allowedLanguagesToProcess = [
108+
'js', 'ts', 'javascript', 'typescript',
109+
],
108110
captionRequired = false,
109111
checkDefaults = false,
110112
checkExamples = true,
@@ -374,6 +376,16 @@ export const getJsdocProcessorPlugin = (options = {}) => {
374376
return;
375377
}
376378

379+
// If `allowedLanguagesToProcess` is falsy, all languages should be processed.
380+
if (allowedLanguagesToProcess) {
381+
const matches = (/^\s*```(?<language>\S+)([\s\S]*)```\s*$/u).exec(source);
382+
if (matches?.groups && !allowedLanguagesToProcess.includes(
383+
matches.groups.language.toLowerCase(),
384+
)) {
385+
return;
386+
}
387+
}
388+
377389
const sources = [];
378390
let skipInit = false;
379391
if (exampleCodeRegex) {

test/getJsdocProcessPlugin.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,4 +749,78 @@ describe('`getJsdocProcessorPlugin`', () => {
749749
text,
750750
});
751751
});
752+
753+
it('ignores language not present in default `allowedLanguagesToProcess`', () => {
754+
const options = {};
755+
const filename = 'something.js';
756+
const text = `
757+
/**
758+
* @example
759+
* \`\`\`shell
760+
* node doSth.js
761+
* \`\`\`
762+
*/
763+
function doSth () {}
764+
`;
765+
check({
766+
filename,
767+
options,
768+
result: [
769+
text,
770+
],
771+
text,
772+
});
773+
});
774+
775+
it('ignores language not present in supplied `allowedLanguagesToProcess`', () => {
776+
const options = {
777+
allowedLanguagesToProcess: [
778+
'javascript',
779+
],
780+
};
781+
const filename = 'something.js';
782+
const text = `
783+
/**
784+
* @example
785+
* \`\`\`js
786+
* doSth();
787+
* \`\`\`
788+
*/
789+
function doSth () {}
790+
`;
791+
check({
792+
filename,
793+
options,
794+
result: [
795+
text,
796+
],
797+
text,
798+
});
799+
});
800+
801+
it('checks language present in default `allowedLanguagesToProcess`', () => {
802+
const options = {};
803+
const filename = 'something.js';
804+
const text = `
805+
/**
806+
* @example
807+
* \`\`\`js
808+
* doSth();
809+
* \`\`\`
810+
*/
811+
function doSth () {}
812+
`;
813+
check({
814+
filename,
815+
options,
816+
result: [
817+
text,
818+
{
819+
filename: 'something.md/*.js',
820+
text: '\n```js\ndoSth();\n```',
821+
},
822+
],
823+
text,
824+
});
825+
});
752826
});

0 commit comments

Comments
 (0)