Skip to content

Commit

Permalink
fix: default to u flag for checkTypesPattern option for `require-…
Browse files Browse the repository at this point in the history
…param`, `check-param-names`; for `check-values`, default to `u` flag and allow slashed regexes with own flags; for `match-description`, allow slashed regexs with own flags; for all of the previous (and `check-examples`) allows newlines
  • Loading branch information
brettz9 committed Feb 16, 2021
1 parent 732b088 commit 4f0dc8e
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 40 deletions.
7 changes: 6 additions & 1 deletion .README/rules/check-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ If present as an array, will be used in place of SPDX identifiers.
A string to be converted into a `RegExp` (with `u` flag) and whose first
parenthetical grouping, if present, will match the portion of the license
description to check (if no grouping is present, then the whole portion
matched will be used). Defaults to `([^\n]*)`, i.e., the SPDX expression
matched will be used). Defaults to `/([^\n]*)/gu`, i.e., the SPDX expression
is expected before any line breaks.

Note that the `/` delimiters are optional, but necessary to add flags.

Defaults to using the `u` flag, so to add your own flags, encapsulate
your expression as a string, but like a literal, e.g., `/^mit$/ui`.

|||
|---|---|
|Context|everywhere|
Expand Down
13 changes: 10 additions & 3 deletions .README/rules/match-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ by our supported Node versions):
Applies to the jsdoc block description and `@description` (or `@desc`)
by default but the `tags` option (see below) may be used to match other tags.

The default (and all regex options) defaults to using (only) the `u` flag, so
to add your own flags, encapsulate your expression as a string, but like a
literal, e.g., `/[A-Z].*\\./ui`.

Note that `/` delimiters are optional, but necessary to add flags (besides
`u`).

Also note that the default or optional regular expressions is *not*
case-insensitive unless one opts in to add the `i` flag.

#### Options

##### `matchDescription`
Expand All @@ -24,9 +34,6 @@ You can supply your own expression to override the default, passing a
}
```

As with the default, the supplied regular expression will be applied with the
Unicode (`"u"`) flag and is *not* case-insensitive.

##### `tags`

If you want different regular expressions to apply to tags, you may use
Expand Down
5 changes: 4 additions & 1 deletion .README/rules/require-param.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export const bboxToObj = function ({x, y, width, height}) {
```

By default `checkTypesPattern` is set to
`/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/`,
`/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/u`,
meaning that destructuring will be required only if the type of the `@param`
(the text between curly brackets) is a match for "Object" or "Array" (with or
without initial caps), "PlainObject", or "GenericObject", "GenericArray" (or
Expand All @@ -315,6 +315,9 @@ parameters.

Note that the `/` delimiters are optional, but necessary to add flags.

Defaults to using (only) the `u` flag, so to add your own flags, encapsulate
your expression as a string, but like a literal, e.g., `/^object$/ui`.

You could set this regular expression to a more expansive list, or you
could restrict it such that even types matching those strings would not
need destructuring.
Expand Down
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5333,9 +5333,14 @@ If present as an array, will be used in place of SPDX identifiers.
A string to be converted into a `RegExp` (with `u` flag) and whose first
parenthetical grouping, if present, will match the portion of the license
description to check (if no grouping is present, then the whole portion
matched will be used). Defaults to `([^\n]*)`, i.e., the SPDX expression
matched will be used). Defaults to `/([^\n]*)/gu`, i.e., the SPDX expression
is expected before any line breaks.

Note that the `/` delimiters are optional, but necessary to add flags.

Defaults to using the `u` flag, so to add your own flags, encapsulate
your expression as a string, but like a literal, e.g., `/^mit$/ui`.

|||
|---|---|
|Context|everywhere|
Expand Down Expand Up @@ -5944,6 +5949,16 @@ by our supported Node versions):
Applies to the jsdoc block description and `@description` (or `@desc`)
by default but the `tags` option (see below) may be used to match other tags.
The default (and all regex options) defaults to using (only) the `u` flag, so
to add your own flags, encapsulate your expression as a string, but like a
literal, e.g., `/[A-Z].*\\./ui`.
Note that `/` delimiters are optional, but necessary to add flags (besides
`u`).
Also note that the default or optional regular expressions is *not*
case-insensitive unless one opts in to add the `i` flag.
<a name="eslint-plugin-jsdoc-rules-match-description-options-11"></a>
#### Options
Expand All @@ -5959,9 +5974,6 @@ You can supply your own expression to override the default, passing a
}
```
As with the default, the supplied regular expression will be applied with the
Unicode (`"u"`) flag and is *not* case-insensitive.
<a name="eslint-plugin-jsdoc-rules-match-description-options-11-tags-2"></a>
##### <code>tags</code>
Expand Down Expand Up @@ -12181,7 +12193,7 @@ export const bboxToObj = function ({x, y, width, height}) {
```

By default `checkTypesPattern` is set to
`/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/`,
`/^(?:[oO]bject|[aA]rray|PlainObject|Generic(?:Object|Array))$/u`,
meaning that destructuring will be required only if the type of the `@param`
(the text between curly brackets) is a match for "Object" or "Array" (with or
without initial caps), "PlainObject", or "GenericObject", "GenericArray" (or
Expand All @@ -12191,6 +12203,9 @@ parameters.

Note that the `/` delimiters are optional, but necessary to add flags.

Defaults to using (only) the `u` flag, so to add your own flags, encapsulate
your expression as a string, but like a literal, e.g., `/^object$/ui`.

You could set this regular expression to a more expansive list, or you
could restrict it such that even types matching those strings would not
need destructuring.
Expand Down
4 changes: 4 additions & 0 deletions src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ const getUtils = (
} : null, tag);
};

utils.getRegexFromString = (str, requiredFlags) => {
return jsdocUtils.getRegexFromString(str, requiredFlags);
};

utils.getDescription = () => {
const descriptions = [];
let lastDescriptionLine;
Expand Down
18 changes: 18 additions & 0 deletions src/jsdocUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,23 @@ const comparePaths = (name) => {
};
};

const getRegexFromString = (regexString, requiredFlags) => {
const match = regexString.match(/^\/(.*)\/([gimyus]*)$/us);
let flags = 'u';
let regex = regexString;
if (match) {
[, regex, flags] = match;
if (!flags) {
flags = 'u';
}
}

const uniqueFlags = [...new Set(flags + (requiredFlags || ''))];
flags = uniqueFlags.join('');

return new RegExp(regex, flags);
};

export default {
comparePaths,
dropPathSegmentQuotes,
Expand All @@ -1191,6 +1208,7 @@ export default {
getIndent,
getJsdocTagsDeep,
getPreferredTagName,
getRegexFromString,
getTagsByType,
getTagStructureForMode,
hasATag,
Expand Down
20 changes: 2 additions & 18 deletions src/rules/checkExamples.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,6 @@ const countChars = (str, ch) => {
return (str.match(new RegExp(escapeStringRegexp(ch), 'gu')) || []).length;
};

const getRegexFromString = (regexString) => {
const match = regexString.match(/^\/(.*)\/([gimyus]*)$/u);
let flags = 'u';
let regex = regexString;
if (match) {
[, regex, flags] = match;
if (!flags) {
flags = 'u';
}
const uniqueFlags = [...new Set(flags)];
flags = uniqueFlags.join('');
}

return new RegExp(regex, flags);
};

const defaultMdRules = {
// "always" newline rule at end unlikely in sample code
'eol-last': 0,
Expand Down Expand Up @@ -135,10 +119,10 @@ export default iterateJsdoc(({
const expressionRules = noDefaultExampleRules ? undefined : defaultExpressionRules;

if (exampleCodeRegex) {
exampleCodeRegex = getRegexFromString(exampleCodeRegex);
exampleCodeRegex = utils.getRegexFromString(exampleCodeRegex);
}
if (rejectExampleCodeRegex) {
rejectExampleCodeRegex = getRegexFromString(rejectExampleCodeRegex);
rejectExampleCodeRegex = utils.getRegexFromString(rejectExampleCodeRegex);
}

const checkSource = ({
Expand Down
5 changes: 1 addition & 4 deletions src/rules/checkParamNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,7 @@ export default iterateJsdoc(({
disableExtraPropertyReporting = false,
} = context.options[0] || {};

const lastSlashPos = checkTypesPattern.lastIndexOf('/');
const checkTypesRegex = lastSlashPos === -1 ?
new RegExp(checkTypesPattern) :
new RegExp(checkTypesPattern.slice(1, lastSlashPos), checkTypesPattern.slice(lastSlashPos + 1));
const checkTypesRegex = utils.getRegexFromString(checkTypesPattern);

const jsdocParameterNamesDeep = utils.getJsdocTagsDeep('param');
if (!jsdocParameterNamesDeep.length) {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/checkValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default iterateJsdoc(({
const {
allowedLicenses = null,
allowedAuthors = null,
licensePattern = '([^\n]*)',
licensePattern = '/([^\n]*)/gu',
} = options;

utils.forEachPreferredTag('version', (jsdocParameter, targetTagName) => {
Expand Down Expand Up @@ -47,7 +47,7 @@ export default iterateJsdoc(({
}
});
utils.forEachPreferredTag('license', (jsdocParameter, targetTagName) => {
const licenseRegex = new RegExp(licensePattern, 'g');
const licenseRegex = utils.getRegexFromString(licensePattern, 'g');
const match = jsdocParameter.description.match(licenseRegex);
const license = match && match[1] || match[0];
if (!license.trim()) {
Expand Down
3 changes: 1 addition & 2 deletions src/rules/matchDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ export default iterateJsdoc(({
tagValue = options.tags[tagName];
}

const regex = new RegExp(
const regex = utils.getRegexFromString(
stringOrDefault(tagValue, options.matchDescription),
'u',
);

if (!regex.test(description)) {
Expand Down
5 changes: 1 addition & 4 deletions src/rules/requireParam.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ export default iterateJsdoc(({
useDefaultObjectProperties = false,
} = context.options[0] || {};

const lastSlashPos = checkTypesPattern.lastIndexOf('/');
const checkTypesRegex = lastSlashPos === -1 ?
new RegExp(checkTypesPattern) :
new RegExp(checkTypesPattern.slice(1, lastSlashPos), checkTypesPattern.slice(lastSlashPos + 1));
const checkTypesRegex = utils.getRegexFromString(checkTypesPattern);

const missingTags = [];
const functionParameterNames = utils.getFunctionParameterNames(useDefaultObjectProperties);
Expand Down

0 comments on commit 4f0dc8e

Please sign in to comment.