Skip to content

feat(getJsdocProcessorPlugin): add allowedLanguagesToProcess option #1392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 1, 2025
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
10 changes: 10 additions & 0 deletions .README/processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ same name.
See the [`check-examples`](./rules/check-examples.md#readme) option of the
same name.

#### `allowedLanguagesToProcess`

This is an array which will narrow the allowable languages of fenced blocks
down to those within the array.

Set to `false` to ensure all present languages (not excluded by
any `exampleCodeRegex` and `rejectExampleCodeRegex` options) will be processed.

Defaults to `['js', 'ts', 'javascript', 'typescript']`.

#### `sourceType`

Whether to use "script" or "module" with the parser. Defaults to `"module"`.
Expand Down
12 changes: 12 additions & 0 deletions docs/processors.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ same name.
See the [`check-examples`](./rules/check-examples.md#readme) option of the
same name.

<a name="user-content-processors-options-allowedlanguagestoprocess"></a>
<a name="processors-options-allowedlanguagestoprocess"></a>
#### <code>allowedLanguagesToProcess</code>

This is an array which will narrow the allowable languages of fenced blocks
down to those within the array.

Set to `false` to ensure all present languages (not excluded by
any `exampleCodeRegex` and `rejectExampleCodeRegex` options) will be processed.

Defaults to `['js', 'ts', 'javascript', 'typescript']`.

<a name="user-content-processors-options-sourcetype"></a>
<a name="processors-options-sourcetype"></a>
#### <code>sourceType</code>
Expand Down
2 changes: 1 addition & 1 deletion src/bin/generateRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
} from 'fs';
import fs from 'fs/promises';
/**
*
* @example
* ```shell
* npm run create-rule my-new-rule -- --recommended
* ```
Expand All @@ -16,14 +16,14 @@
dirname as getDirname,
resolve,
} from 'path';
// Todo: Add back `@example` when reject other langs from processing

Check warning on line 19 in src/bin/generateRule.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'Todo: Add back `@example` when reject...'
import {
fileURLToPath,
} from 'url';

const dirname = getDirname(fileURLToPath(import.meta.url));

// Todo: Would ideally have prompts, e.g., to ask for whether

Check warning on line 26 in src/bin/generateRule.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'Todo: Would ideally have prompts, e.g.,...'
// type was problem/layout, etc.

const [
Expand Down
16 changes: 14 additions & 2 deletions src/getJsdocProcessorPlugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Todo: Support TS by fenced block type

import {
forEachPreferredTag,
getPreferredTagName,
Expand Down Expand Up @@ -94,6 +92,7 @@
* @property {string} [matchingFileNameProperties] See docs
* @property {string} [exampleCodeRegex] See docs
* @property {string} [rejectExampleCodeRegex] See docs
* @property {string[]} [allowedLanguagesToProcess] See docs
* @property {"script"|"module"} [sourceType] See docs
* @property {import('eslint').Linter.ESTreeParser|import('eslint').Linter.NonESTreeParser} [parser] See docs
*/
Expand All @@ -105,6 +104,9 @@
*/
export const getJsdocProcessorPlugin = (options = {}) => {
const {
allowedLanguagesToProcess = [
'js', 'ts', 'javascript', 'typescript',
],
captionRequired = false,
checkDefaults = false,
checkExamples = true,
Expand Down Expand Up @@ -374,6 +376,16 @@
return;
}

// If `allowedLanguagesToProcess` is falsy, all languages should be processed.
if (allowedLanguagesToProcess) {
const matches = (/^\s*```(?<language>\S+)([\s\S]*)```\s*$/u).exec(source);
if (matches?.groups && !allowedLanguagesToProcess.includes(
matches.groups.language.toLowerCase(),
)) {
return;
}
}

const sources = [];
let skipInit = false;
if (exampleCodeRegex) {
Expand Down Expand Up @@ -516,7 +528,7 @@
ruleId,
severity,

// Todo: Make fixable

Check warning on line 531 in src/getJsdocProcessorPlugin.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'Todo: Make fixable'
// fix
// fix: {range: [number, number], text: string}
// suggestions: {desc: , messageId:, fix: }[],
Expand Down
74 changes: 74 additions & 0 deletions test/getJsdocProcessPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,4 +749,78 @@ describe('`getJsdocProcessorPlugin`', () => {
text,
});
});

it('ignores language not present in default `allowedLanguagesToProcess`', () => {
const options = {};
const filename = 'something.js';
const text = `
/**
* @example
* \`\`\`shell
* node doSth.js
* \`\`\`
*/
function doSth () {}
`;
check({
filename,
options,
result: [
text,
],
text,
});
});

it('ignores language not present in supplied `allowedLanguagesToProcess`', () => {
const options = {
allowedLanguagesToProcess: [
'javascript',
],
};
const filename = 'something.js';
const text = `
/**
* @example
* \`\`\`js
* doSth();
* \`\`\`
*/
function doSth () {}
`;
check({
filename,
options,
result: [
text,
],
text,
});
});

it('checks language present in default `allowedLanguagesToProcess`', () => {
const options = {};
const filename = 'something.js';
const text = `
/**
* @example
* \`\`\`js
* doSth();
* \`\`\`
*/
function doSth () {}
`;
check({
filename,
options,
result: [
text,
{
filename: 'something.md/*.js',
text: '\n```js\ndoSth();\n```',
},
],
text,
});
});
});
Loading