Skip to content

Commit

Permalink
fix(check-examples): change default extension for simulated file name…
Browse files Browse the repository at this point in the history
… 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)
  • Loading branch information
brettz9 committed Feb 15, 2021
1 parent bc391f3 commit 2cc1227
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
19 changes: 11 additions & 8 deletions .README/rules/check-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ by decreasing precedence:
can be useful for enabling reuse of the same rules within `@example` as
with JavaScript Markdown lintable by
[other plugins](https://github.com/eslint/eslint-plugin-markdown), e.g.,
if one sets `matchingFileName` to `dummy.md` so that `@example` rules will
follow one's Markdown rules). For `@example` only.
if one sets `matchingFileName` to `dummy.md/*.js` so that `@example`
rules will follow rules for fenced JavaScript blocks within one's Markdown
rules). (In ESLint 6's process API and `eslint-plugin-markdown` < 2, one
would instead use `dummy.md`.) For `@example` only.
* `matchingFileNameDefaults` - As with `matchingFileName` but for use with
`checkDefaults` and defaulting to `.jsdoc-defaults` as extension.
* `matchingFileNameParams` - As with `matchingFileName` but for use with
Expand All @@ -116,12 +118,13 @@ by decreasing precedence:
not be checked. If `matchingFileName` is not set, and this is unset or
set to `true`, the `.eslintrc.*` configs will be checked as though the file
name were the same as the file containing the example, with any file
extension changed to ".md" (and if there is no file extension, "dummy.md"
will be used). This allows convenient sharing of similar rules with often
also context-free Markdown as well as use of `overrides` as described under
`matchingFileName`. Note that this option (whether set by `matchingFileName`
or set manually to `true`) may come at somewhat of a performance penalty
as the file's existence is checked by eslint.
extension changed to `".md/*.js"` (and if there is no file extension,
`"dummy.md/*.js"` will be the result). This allows convenient sharing of
similar rules with often also context-free Markdown as well as use of
`overrides` as described under `matchingFileName`. Note that this option
(whether set by `matchingFileName` or set manually to `true`) may come at
somewhat of a performance penalty as the file's existence is checked by
eslint.
* `baseConfig` - Set to an object of rules with the same schema
as `.eslintrc.*` for defaults.

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

Expand Down Expand Up @@ -1294,7 +1297,7 @@ function quux () {

/**
* @example
* const test = something?.find((_) => {
* const test = something.find((_) => {
* return _
* });
*/
Expand Down
10 changes: 9 additions & 1 deletion src/rules/checkExamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,15 @@ export default iterateJsdoc(({
sources.forEach(checkRules);
};

const getFilenameInfo = (filename, ext = 'md') => {
/**
*
* @param {string} filename
* @param {string} [ext="md/*.js"] Since `eslint-plugin-markdown` v2, and
* ESLint 7, this is the default which other JS-fenced rules will used.
* Formerly "md" was the default.
* @returns {{defaultFileName: string, fileName: string}}
*/
const getFilenameInfo = (filename, ext = 'md/*.js') => {
let defaultFileName;
if (!filename) {
const jsFileName = context.getFilename();
Expand Down
2 changes: 1 addition & 1 deletion test/rules/assertions/checkExamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ export default {
code: `
/**
* @example
* const test = something?.find((_) => {
* const test = something.find((_) => {
* return _
* });
*/
Expand Down

0 comments on commit 2cc1227

Please sign in to comment.