Skip to content

Commit

Permalink
add additional matchingFileName* options to allow distinct rules
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Oct 19, 2020
1 parent 262a474 commit 865cd65
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .README/rules/check-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ by decreasing precedence:
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).
follow one's Markdown rules). For `@example` only.
* `matchingFileNameDefaults` - As with `matchingFileName` but for use with `checkDefaults`.
* `matchingFileNameParams` - As with `matchingFileName` but for use with `checkParams`.
* `matchingFileNameProperties` As with `matchingFileName` but for use with `checkProperties`.
* `checkEslintrc` - Defaults to `true` in adding rules
based on an `.eslintrc.*` file. Setting to `false` corresponds to
ESLint's [`--no-eslintrc`](https://eslint.org/docs/user-guide/command-line-interface#--no-eslintrc).
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,10 @@ by decreasing precedence:
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).
follow one's Markdown rules). For `@example` only.
* `matchingFileNameDefaults` - As with `matchingFileName` but for use with `checkDefaults`.
* `matchingFileNameParams` - As with `matchingFileName` but for use with `checkParams`.
* `matchingFileNameProperties` As with `matchingFileName` but for use with `checkProperties`.
* `checkEslintrc` - Defaults to `true` in adding rules
based on an `.eslintrc.*` file. Setting to `false` corresponds to
ESLint's [`--no-eslintrc`](https://eslint.org/docs/user-guide/command-line-interface#--no-eslintrc).
Expand Down Expand Up @@ -1509,20 +1512,20 @@ const obj = {};
* @default 'abc'
*/
const str = 'abc';
// Options: [{"checkDefaults":false}]
// Options: [{"checkDefaults":false,"matchingFileNameDefaults":"dummy.js"}]

/**
* @param {myType} [name='abc']
*/
function quux () {
}
// Options: [{"checkParams":false}]
// Options: [{"checkParams":false,"matchingFileNameParams":"dummy.js"}]

/**
* @property {myType} [name='abc']
*/
const obj = {};
// Options: [{"checkProperties":false}]
// Options: [{"checkProperties":false,"matchingFileNameProperties":"dummy.js"}]
````


Expand Down
52 changes: 41 additions & 11 deletions src/rules/checkExamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export default iterateJsdoc(({
checkProperties = true,
noDefaultExampleRules = false,
checkEslintrc = true,
matchingFileName: filename = null,
matchingFileName = null,
matchingFileNameDefaults = null,
matchingFileNameParams = null,
matchingFileNameProperties = null,
paddedIndent = 0,
baseConfig = {},
configFile,
Expand All @@ -69,16 +72,6 @@ export default iterateJsdoc(({
captionRequired = false,
} = options;

let defaultFileName;
if (!filename) {
const jsFileName = context.getFilename();
if (typeof jsFileName === 'string' && jsFileName.includes('.')) {
defaultFileName = jsFileName.replace(/\..*?$/, '.md');
} else {
defaultFileName = 'dummy.md';
}
}

// Make this configurable?
const rulePaths = [];

Expand Down Expand Up @@ -126,6 +119,7 @@ export default iterateJsdoc(({
}

const checkSource = ({
filename, defaultFileName,
skipInit, source, targetTagName, sources = [], tag = {line: 0},
}) => {
if (!skipInit) {
Expand Down Expand Up @@ -222,27 +216,50 @@ export default iterateJsdoc(({
sources.forEach(checkRules);
};

const getFilenameInfo = (filename) => {
let defaultFileName;
if (!filename) {
const jsFileName = context.getFilename();
if (typeof jsFileName === 'string' && jsFileName.includes('.')) {
defaultFileName = jsFileName.replace(/\..*?$/, '.md');
} else {
defaultFileName = 'dummy.md';
}
}

return {
defaultFileName,
filename,
};
};

if (checkDefaults) {
const filenameInfo = getFilenameInfo(matchingFileNameDefaults);
utils.forEachPreferredTag('default', (tag, targetTagName) => {
checkSource({
source: tag.description,
targetTagName,
...filenameInfo,
});
});
}
if (checkParams) {
const filenameInfo = getFilenameInfo(matchingFileNameParams);
utils.forEachPreferredTag('param', (tag, targetTagName) => {
checkSource({
source: tag.default,
targetTagName,
...filenameInfo,
});
});
}
if (checkProperties) {
const filenameInfo = getFilenameInfo(matchingFileNameProperties);
utils.forEachPreferredTag('property', (tag, targetTagName) => {
checkSource({
source: tag.default,
targetTagName,
...filenameInfo,
});
});
}
Expand All @@ -252,6 +269,8 @@ export default iterateJsdoc(({
return;
}

const matchingFilenameInfo = getFilenameInfo(matchingFileName);

utils.forEachPreferredTag('example', (tag, targetTagName) => {
let source = tag.description;
const match = source.match(hasCaptionRegex);
Expand Down Expand Up @@ -328,12 +347,14 @@ export default iterateJsdoc(({
}
skipInit = true;
}

checkSource({
skipInit,
source,
sources,
tag,
targetTagName,
...matchingFilenameInfo,
});
});
}, {
Expand Down Expand Up @@ -383,6 +404,15 @@ export default iterateJsdoc(({
matchingFileName: {
type: 'string',
},
matchingFileNameDefaults: {
type: 'string',
},
matchingFileNameParams: {
type: 'string',
},
matchingFileNameProperties: {
type: 'string',
},
noDefaultExampleRules: {
default: false,
type: 'boolean',
Expand Down
3 changes: 3 additions & 0 deletions test/rules/assertions/checkExamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,7 @@ export default {
options: [
{
checkDefaults: false,
matchingFileNameDefaults: 'dummy.js',
},
],
},
Expand All @@ -1107,6 +1108,7 @@ export default {
options: [
{
checkParams: false,
matchingFileNameParams: 'dummy.js',
},
],
},
Expand All @@ -1120,6 +1122,7 @@ export default {
options: [
{
checkProperties: false,
matchingFileNameProperties: 'dummy.js',
},
],
},
Expand Down

0 comments on commit 865cd65

Please sign in to comment.