-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
overloaded TypeScript functions accept jsdoc only on implementation #666
Comments
Within the examples for the {
contexts: [
'TSDeclareFunction:not(TSDeclareFunction + TSDeclareFunction)',
'FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)',
],
require: {
FunctionDeclaration: false,
},
} This disables the normal required reporting about all plain function declarations ( I'm not sure we should change the default here because, besides adding (TS) parser-specific complexity to the code, I'm not sure every project necessarily wants things to work this way. |
Where are the examples located? I didn't see them in the README. I agree that if this can be configured to work with TypeScript correctly, there's no need to change the default. Unfortunately, that configuration didn't work for me. For the file: /**
* Adds two things
*/
export function add(a: number, b: number): number
export function add(a: string, b: string): string
export function add(a: any, b: any): any {
return a + b
} and eslint configuration {
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"jsdoc"
],
"parserOptions": {
"ecmaVersion": 2020
},
"rules": {
"jsdoc/require-jsdoc": [
"warn",
{
"contexts": [
"TSDeclareFunction:not(TSDeclareFunction + TSDeclareFunction)",
"FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)"
],
"require": {
"FunctionDeclaration": false
}
}
]
}
} when linting I get
It looks to me like the overloads without bodies are being interpreted as FunctionDeclaration instead of TSDeclareFunction. |
For the example, search for As far as the error, my apologies, In this case, if you check https://astexplorer.net/ , you'll see that you need {
contexts: [
'TSDeclareFunction:not(TSDeclareFunction + TSDeclareFunction)',
'ExportNamedDeclaration:not(TSDeclareFunction + ExportNamedDeclaration',
],
require: {
FunctionDeclaration: false,
},
} |
That doesn't work either (I fixed the missing bracket). Now it warns on Looking at TypeScript AST Viewer I think what I need is to warn on a Can I delve into an AST node this way? Thanks for your help here. |
Actually, forget that, the example immediately after the example you gave works by the look of it.
|
Did that work for ya? I'll close in assuming it was ok if I don't hear back. |
Yeah, that one looks to be good, thanks. |
Thanks for this issue, i met this problem Last solution did not work for me because I have an export written before the function. unfortunately, I do not quite understand how I can adapt the rule setting so that it works for overloads with export. if you have the opportunity, please help. My code:
with #666 (comment) I get warnings |
Did you add require: {
FunctionDeclaration: false,
} ...in addition to |
I removed this and get the same result
I'm not sure if I want to open a separate issue for this, sorry. I think this is relevant to the current issue. For myself, I found a workround: I removed exports from function declarations and added named exports later. |
I was just asking whether you had added: require: {
FunctionDeclaration: false,
} ..as it should be there if using the {
code: `
/**
* Calculate a 32 bit FNV-1a hash
* Found here: https://gist.github.com/vaiorabbit/5657561
* Ref.: http://isthe.com/chongo/tech/comp/fnv/
*
* @param str the input value
* @param [asString=false] set to true to return the hash value as
* 8-digit hex string instead of an integer
* @param [seed] optionally pass the hash of the previous chunk
*
* @returns хэк
*/
export function hashFnv32a(str: string, asString: true, seed?: number): string;
export function hashFnv32a(str: string, asString?: false, seed?: number): number;
export function hashFnv32a(str: string, asString?: boolean, seed?: number): string | number { }`,
options: [{
contexts: [
// eslint-disable-next-line max-len
'ExportNamedDeclaration[declaration.type="TSDeclareFunction"]:not(ExportNamedDeclaration[declaration.type="TSDeclareFunction"] + ExportNamedDeclaration[declaration.type="TSDeclareFunction"])',
// eslint-disable-next-line max-len
'ExportNamedDeclaration[declaration.type="FunctionDeclaration"]:not(ExportNamedDeclaration[declaration.type="TSDeclareFunction"] + ExportNamedDeclaration[declaration.type="FunctionDeclaration"])',
],
require: {
FunctionDeclaration: false,
},
}],
parser: require.resolve('@typescript-eslint/parser'),
}, |
For classes method overloads, best I could get was the following context mimicking midgleyc's answer above: {
contexts: [
'ExportNamedDeclaration[declaration.type="TSDeclareFunction"]:not(ExportNamedDeclaration[declaration.type="TSDeclareFunction"] + ExportNamedDeclaration[declaration.type="TSDeclareFunction"])',
'ExportNamedDeclaration[declaration.type="FunctionDeclaration"]:not(ExportNamedDeclaration[declaration.type="TSDeclareFunction"] + ExportNamedDeclaration[declaration.type="FunctionDeclaration"])',
'MethodDefinition[value.type="TSEmptyBodyFunctionExpression"]:not(MethodDefinition[value.type="TSEmptyBodyFunctionExpression"] + MethodDefinition[value.type="TSEmptyBodyFunctionExpression"])',
'MethodDefinition[value.type="FunctionExpression"]:not(MethodDefinition[value.type="TSEmptyBodyFunctionExpression"] + MethodDefinition[value.type="FunctionExpression"])',
],
require: {
FunctionDeclaration: false,
},
} Note that you can omit constructor & get/set with following contexts: 'MethodDefinition[kind="method"][value.type="TSEmptyBodyFunctionExpression"]:not(MethodDefinition[value.type="TSEmptyBodyFunctionExpression"] + MethodDefinition[value.type="TSEmptyBodyFunctionExpression"])',
'MethodDefinition[kind="method"][value.type="FunctionExpression"]:not(MethodDefinition[value.type="TSEmptyBodyFunctionExpression"] + MethodDefinition[value.type="FunctionExpression"])', |
Expected behavior
Given an overloaded typescript function, a JSDoc at the top of the series of functions should count for all of them.
Actual behavior
The JSDoc comment is required to be on the implementation function.
A comment only on the implementation function is passed through to the
.js
file, but not to the.d.ts
file, and isn't picked up by e.g. VSCode as describing the function.ESLint Config
ESLint sample
Should not error, but does.
Environment
eslint-plugin-jsdoc
version: 30.7.13The text was updated successfully, but these errors were encountered: