Skip to content

Commit 2cc1227

Browse files
committed
fix(check-examples): change default extension for simulated file name of @example from md to md/*.js. Only applies when matchingFileName is not used.
BREAKING CHANGE: Per update to `eslint-plugin-markdown` v2 and its support of ESLint 7's new processor API, fenced blocks can (and must) be targeted separately from the Markdown parent file as a whole, so in order to allow the same `overrides` config to be reusable between Markdown fenced blocks and `@example` tags (since one often wishes to disable the same kind of rules for each, being as that each may deliberately lack full context, e.g., undefined or unused variables), we update the default simulated extension set for `@example` tags by `check-examples` to simulate an expression which will now be a suitable choice for `overrides` in targeting Markdown fenced blocks using the new parser API, as with eslint-plugin-markdown@2 (i.e., "md/*.js" instead of "md"). If you need to use ESLint 6 (or eslint-plugin-markdown < 2, for example) with `check-examples`, you should be able to manually supply the `matchingFileName` option targeting "md", e.g., set to `'dummy.md`. This commit also simplifies a testing example to avoid it throwing with changed config not having proper babel support (not meaningful to test anyways)
1 parent bc391f3 commit 2cc1227

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

.README/rules/check-examples.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ by decreasing precedence:
9999
can be useful for enabling reuse of the same rules within `@example` as
100100
with JavaScript Markdown lintable by
101101
[other plugins](https://github.com/eslint/eslint-plugin-markdown), e.g.,
102-
if one sets `matchingFileName` to `dummy.md` so that `@example` rules will
103-
follow one's Markdown rules). For `@example` only.
102+
if one sets `matchingFileName` to `dummy.md/*.js` so that `@example`
103+
rules will follow rules for fenced JavaScript blocks within one's Markdown
104+
rules). (In ESLint 6's process API and `eslint-plugin-markdown` < 2, one
105+
would instead use `dummy.md`.) For `@example` only.
104106
* `matchingFileNameDefaults` - As with `matchingFileName` but for use with
105107
`checkDefaults` and defaulting to `.jsdoc-defaults` as extension.
106108
* `matchingFileNameParams` - As with `matchingFileName` but for use with
@@ -116,12 +118,13 @@ by decreasing precedence:
116118
not be checked. If `matchingFileName` is not set, and this is unset or
117119
set to `true`, the `.eslintrc.*` configs will be checked as though the file
118120
name were the same as the file containing the example, with any file
119-
extension changed to ".md" (and if there is no file extension, "dummy.md"
120-
will be used). This allows convenient sharing of similar rules with often
121-
also context-free Markdown as well as use of `overrides` as described under
122-
`matchingFileName`. Note that this option (whether set by `matchingFileName`
123-
or set manually to `true`) may come at somewhat of a performance penalty
124-
as the file's existence is checked by eslint.
121+
extension changed to `".md/*.js"` (and if there is no file extension,
122+
`"dummy.md/*.js"` will be the result). This allows convenient sharing of
123+
similar rules with often also context-free Markdown as well as use of
124+
`overrides` as described under `matchingFileName`. Note that this option
125+
(whether set by `matchingFileName` or set manually to `true`) may come at
126+
somewhat of a performance penalty as the file's existence is checked by
127+
eslint.
125128
* `baseConfig` - Set to an object of rules with the same schema
126129
as `.eslintrc.*` for defaults.
127130

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -992,8 +992,10 @@ by decreasing precedence:
992992
can be useful for enabling reuse of the same rules within `@example` as
993993
with JavaScript Markdown lintable by
994994
[other plugins](https://github.com/eslint/eslint-plugin-markdown), e.g.,
995-
if one sets `matchingFileName` to `dummy.md` so that `@example` rules will
996-
follow one's Markdown rules). For `@example` only.
995+
if one sets `matchingFileName` to `dummy.md/*.js` so that `@example`
996+
rules will follow rules for fenced JavaScript blocks within one's Markdown
997+
rules). (In ESLint 6's process API and `eslint-plugin-markdown` < 2, one
998+
would instead use `dummy.md`.) For `@example` only.
997999
* `matchingFileNameDefaults` - As with `matchingFileName` but for use with
9981000
`checkDefaults` and defaulting to `.jsdoc-defaults` as extension.
9991001
* `matchingFileNameParams` - As with `matchingFileName` but for use with
@@ -1009,12 +1011,13 @@ by decreasing precedence:
10091011
not be checked. If `matchingFileName` is not set, and this is unset or
10101012
set to `true`, the `.eslintrc.*` configs will be checked as though the file
10111013
name were the same as the file containing the example, with any file
1012-
extension changed to ".md" (and if there is no file extension, "dummy.md"
1013-
will be used). This allows convenient sharing of similar rules with often
1014-
also context-free Markdown as well as use of `overrides` as described under
1015-
`matchingFileName`. Note that this option (whether set by `matchingFileName`
1016-
or set manually to `true`) may come at somewhat of a performance penalty
1017-
as the file's existence is checked by eslint.
1014+
extension changed to `".md/*.js"` (and if there is no file extension,
1015+
`"dummy.md/*.js"` will be the result). This allows convenient sharing of
1016+
similar rules with often also context-free Markdown as well as use of
1017+
`overrides` as described under `matchingFileName`. Note that this option
1018+
(whether set by `matchingFileName` or set manually to `true`) may come at
1019+
somewhat of a performance penalty as the file's existence is checked by
1020+
eslint.
10181021
* `baseConfig` - Set to an object of rules with the same schema
10191022
as `.eslintrc.*` for defaults.
10201023

@@ -1294,7 +1297,7 @@ function quux () {
12941297

12951298
/**
12961299
* @example
1297-
* const test = something?.find((_) => {
1300+
* const test = something.find((_) => {
12981301
* return _
12991302
* });
13001303
*/

src/rules/checkExamples.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,15 @@ export default iterateJsdoc(({
246246
sources.forEach(checkRules);
247247
};
248248

249-
const getFilenameInfo = (filename, ext = 'md') => {
249+
/**
250+
*
251+
* @param {string} filename
252+
* @param {string} [ext="md/*.js"] Since `eslint-plugin-markdown` v2, and
253+
* ESLint 7, this is the default which other JS-fenced rules will used.
254+
* Formerly "md" was the default.
255+
* @returns {{defaultFileName: string, fileName: string}}
256+
*/
257+
const getFilenameInfo = (filename, ext = 'md/*.js') => {
250258
let defaultFileName;
251259
if (!filename) {
252260
const jsFileName = context.getFilename();

test/rules/assertions/checkExamples.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ export default {
555555
code: `
556556
/**
557557
* @example
558-
* const test = something?.find((_) => {
558+
* const test = something.find((_) => {
559559
* return _
560560
* });
561561
*/

0 commit comments

Comments
 (0)