Skip to content

Valid types #591

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 4 commits into from
Jun 23, 2020
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
7 changes: 6 additions & 1 deletion .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ how many line breaks to add when a block is missing.

### Mode

- `settings.jsdoc.mode` - Set to `jsdoc` (the default), `typescript`, or `closure`.
- `settings.jsdoc.mode` - Set to `typescript`, `closure`, or `jsdoc` (the
default unless the `@typescript-eslint` parser is in use in which case
`typescript` will be the default).
Note that if you do not wish to use separate `.eslintrc.*` files for a project
containing both JavaScript and TypeScript, you can also use [`overrides`](https://eslint.org/docs/user-guide/configuring). You may also set to `"permissive"` to
try to be as accommodating to any of the styles, but this is not recommended.
Expand All @@ -139,6 +141,9 @@ how many line breaks to add when a block is missing.
- `no-undefined-types`: Only check `@template` for types in "closure" and
"typescript" modes
- `check-syntax`: determines aspects that may be enforced
- `valid-types`: in non-Closure mode, `@extends`, `@package` and access tags
(e.g., `@private`) with a bracketed type are reported as are missing
names with `@typedef`
- For type/namepath-checking rules, determine which tags will be checked for
types/namepaths (Closure allows types on some tags which the others do not,
so these tags will additionally be checked in "closure" mode)
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ before_script: >
fi
notifications:
email: false
sudo: false
script:
- npm run test
- 'if [ -n "${LINT-}" ]; then npm run lint; fi'
Expand Down
52 changes: 46 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ how many line breaks to add when a block is missing.
<a name="eslint-plugin-jsdoc-settings-mode"></a>
### Mode

- `settings.jsdoc.mode` - Set to `jsdoc` (the default), `typescript`, or `closure`.
- `settings.jsdoc.mode` - Set to `typescript`, `closure`, or `jsdoc` (the
default unless the `@typescript-eslint` parser is in use in which case
`typescript` will be the default).
Note that if you do not wish to use separate `.eslintrc.*` files for a project
containing both JavaScript and TypeScript, you can also use [`overrides`](https://eslint.org/docs/user-guide/configuring). You may also set to `"permissive"` to
try to be as accommodating to any of the styles, but this is not recommended.
Expand All @@ -197,6 +199,9 @@ how many line breaks to add when a block is missing.
- `no-undefined-types`: Only check `@template` for types in "closure" and
"typescript" modes
- `check-syntax`: determines aspects that may be enforced
- `valid-types`: in non-Closure mode, `@extends`, `@package` and access tags
(e.g., `@private`) with a bracketed type are reported as are missing
names with `@typedef`
- For type/namepath-checking rules, determine which tags will be checked for
types/namepaths (Closure allows types on some tags which the others do not,
so these tags will additionally be checked in "closure" mode)
Expand Down Expand Up @@ -13489,6 +13494,20 @@ function foo(bar) {}
*/
// Settings: {"jsdoc":{"mode":"closure"}}
// Message: @interface should not have a name in "closure" mode.

/**
* @typedef {SomeType}
*/
function quux () {}
// Settings: {"jsdoc":{"mode":"jsdoc"}}
// Message: @typedef must have a name in "jsdoc" mode.

/**
* @private {SomeType}
*/
function quux () {}
// Settings: {"jsdoc":{"mode":"jsdoc"}}
// Message: @private should not have a bracketed type in "jsdoc" mode.
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -13617,10 +13636,16 @@ function quux() {
*/
class Bar {};

/**
* @extends Foo<String>
*/
class Bar {};

/**
* @extends {Foo<String>}
*/
class Bar {};
// Settings: {"jsdoc":{"mode":"closure"}}

/**
* @typedef {number|string} UserDefinedType
Expand All @@ -13630,17 +13655,13 @@ function quux() {
* @typedef {number|string}
*/
let UserDefinedGCCType;
// Settings: {"jsdoc":{"mode":"closure"}}

/**
* @modifies {foo|bar}
*/
function quux (foo, bar, baz) {}

/**
* @private {BadTypeNotCheckedInJsdoc<}
*/
function quux () {}

/**
* @this {Navigator}
*/
Expand Down Expand Up @@ -13672,13 +13693,32 @@ function quux () {}
function foo(bar) {}
// Settings: {"jsdoc":{"mode":"typescript"}}

/**
* Foo function.
*
* @param {[number, string]} bar - The bar array.
*/
function foo(bar) {}

/**
* Foo function.
*
* @param {[number, string]} bar - The bar array.
*/
function foo(bar) {}
// Settings: {"jsdoc":{"mode":"permissive"}}

/**
* @typedef {SomeType}
*/
function quux () {}
// Settings: {"jsdoc":{"mode":"closure"}}

/**
* @private {SomeType}
*/
function quux () {}
// Settings: {"jsdoc":{"mode":"closure"}}
````


6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
"@babel/plugin-transform-flow-strip-types": "^7.10.1",
"@babel/preset-env": "^7.10.3",
"@babel/register": "^7.10.3",
"@typescript-eslint/parser": "^3.3.0",
"@typescript-eslint/parser": "^3.4.0",
"babel-eslint": "^10.1.0",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-plugin-istanbul": "^6.0.0",
"chai": "^4.2.0",
"cross-env": "^7.0.2",
"eslint": "7.3.0",
"eslint": "7.3.1",
"eslint-config-canonical": "^20.0.6",
"gitdown": "^3.1.3",
"glob": "^7.1.6",
"husky": "^4.2.5",
"mocha": "^8.0.1",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"semantic-release": "^17.0.8",
"semantic-release": "^17.1.0",
"typescript": "^3.9.5"
},
"engines": {
Expand Down
3 changes: 2 additions & 1 deletion src/iterateJsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ const getSettings = (context) => {
augmentsExtendsReplacesDocs: context.settings.jsdoc?.augmentsExtendsReplacesDocs,

// Many rules, e.g., `check-tag-names`
mode: context.settings.jsdoc?.mode ?? 'jsdoc',
mode: context.settings.jsdoc?.mode ??
(context.parserPath.includes('@typescript-eslint') ? 'typescript' : 'jsdoc'),
};
/* eslint-enable sort-keys-fix/sort-keys-fix */

Expand Down
22 changes: 21 additions & 1 deletion src/rules/validTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default iterateJsdoc(({
const hasEither = utils.tagMightHaveEitherTypeOrNamePosition(tag.tag) && (hasTypePosition || hasNameOrNamepathPosition);
const mustHaveEither = utils.tagMustHaveEitherTypeOrNamePosition(tag.tag);

let skip;
switch (tag.tag) {
case 'borrows': {
const thisNamepath = tag.description.replace(asExpression, '');
Expand All @@ -98,8 +99,27 @@ export default iterateJsdoc(({
}
break;
}
case 'extends':
case 'package': case 'private': case 'protected': case 'public': case 'static': {
if (mode !== 'closure' && mode !== 'permissive' && tag.type) {
report(`@${tag.tag} should not have a bracketed type in "${mode}" mode.`, null, tag);
break;
}
skip = true;
}

// Fallthrough
case 'typedef': {
if (!skip && mode !== 'closure' && mode !== 'permissive' && !tag.name) {
report(`@typedef must have a name in "${mode}" mode.`, null, tag);
break;
}
skip = true;
}

// Fallthrough
case 'interface': {
if (mode === 'closure' && tag.name) {
if (!skip && mode === 'closure' && tag.name) {
report('@interface should not have a name in "closure" mode.', null, tag);
break;
}
Expand Down
104 changes: 95 additions & 9 deletions test/rules/assertions/validTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,44 @@ export default {
},
},
},
{
code: `
/**
* @typedef {SomeType}
*/
function quux () {}

`,
errors: [
{
message: '@typedef must have a name in "jsdoc" mode.',
},
],
settings: {
jsdoc: {
mode: 'jsdoc',
},
},
},
{
code: `
/**
* @private {SomeType}
*/
function quux () {}

`,
errors: [
{
message: '@private should not have a bracketed type in "jsdoc" mode.',
},
],
settings: {
jsdoc: {
mode: 'jsdoc',
},
},
},
],
valid: [
{
Expand Down Expand Up @@ -593,13 +631,26 @@ export default {
class Bar {};
`,
},
{
code: `
/**
* @extends Foo<String>
*/
class Bar {};
`,
},
{
code: `
/**
* @extends {Foo<String>}
*/
class Bar {};
`,
settings: {
jsdoc: {
mode: 'closure',
},
},
},
{
code: `
Expand All @@ -615,6 +666,11 @@ export default {
*/
let UserDefinedGCCType;
`,
settings: {
jsdoc: {
mode: 'closure',
},
},
},
{
code: `
Expand All @@ -624,15 +680,6 @@ export default {
function quux (foo, bar, baz) {}
`,
},
{
code: `
/**
* @private {BadTypeNotCheckedInJsdoc<}
*/
function quux () {}

`,
},
{
code: `
/**
Expand Down Expand Up @@ -695,6 +742,17 @@ export default {
},
},
},
{
code: `
/**
* Foo function.
*
* @param {[number, string]} bar - The bar array.
*/
function foo(bar) {}
`,
parser: require.resolve('@typescript-eslint/parser'),
},
{
code: `
/**
Expand All @@ -710,5 +768,33 @@ export default {
},
},
},
{
code: `
/**
* @typedef {SomeType}
*/
function quux () {}

`,
settings: {
jsdoc: {
mode: 'closure',
},
},
},
{
code: `
/**
* @private {SomeType}
*/
function quux () {}

`,
settings: {
jsdoc: {
mode: 'closure',
},
},
},
],
};