OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.35k stars 6.46k forks source link

[BUG] typescript-angular api.service template contains unused locals #1880

Open gkamperis opened 5 years ago

gkamperis commented 5 years ago
Description

file https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache

contains the below comment / tslint:disable:no-unused-variable member-ordering /

attempting to avoid linting errors.

However, this prevents fixers (like prettier-tslint, etc) to actually attempt and fix the the unused local vars issue.

Also note that the specific problem of unused locals will not be avoided if the TS compiler has stricter config that does not allow unused locals.

openapi-generator version

3.3.2

OpenAPI declaration file content or url
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache
/* tslint:disable:no-unused-variable member-ordering */
Suggest a fix

Disabling the tslint rule does nobody a favor as TS can still catch you out.

The fix is to properly handle the conditionals of the template to correctly declare things only when needed.

Workaround

override the template to remove the comment and use a fixer - but this is not ideal

EDIT: override the template to disable TS(provided your TS version supports it)/ESLint/TSLint checks completely as auto-generated code should be ignored from specific codebase rules.

wing328 commented 5 years ago

@gkamperis as you've suggested, please a fixer as a workaround for the time being. You can do so in the file post-processing hook. For example, using prettier to format the code:

export TS_POST_PROCESS_FILE="/usr/local/bin/prettier --write"

and switch on post file processing via --enable-post-process-file (CLI)

rcambrj commented 5 years ago

tslint had the no-unused-variable rule which came with a fixer, but that rule has been deprecated because it is now implemented in the built-in typescript compiler. However, tsc doesn't have a fixer.

So... how does one run a post-process fixer for this?

I can't seem to find a linter that will fix no-unused-variable (eslint's one doesn't have a fixer)

clintonb commented 4 years ago

I'm new to the typescript-axios generator, and just encountered this error. Are they any programatic workarounds?

Jrubzjeknf commented 3 years ago

With Angular 12 and the increased use of TSC's noUnusedLocals, this issue becomes a lot more common. For example, in my generated client, the addToHttpParams method is unused, because the client doesn't have calls with parameters.

Currently I'm inserting a // @ts-nocheck comment (feature added in 3.7) to the top of all generated files to prevent the compiler from breaking. I reckon it would be a good solution to do this by default. It is generated code and it is practically impossible to check for unused references.

The tslint autofixer isn't a solution, since its deprecated and eslint is now common usage. The lint ignore comments should probably be updated.

For people who want a solution now, I use this powershell script to add the // @ts-nocheck comment to all files.


$outDir = 'directory/with/generated/files'

Write-Host "Adding ts-nocheck comment to all .ts files.."

$tsNoCheckCode = @"
// @ts-nocheck

"@

Get-ChildItem -Path $outDir -Recurse -Force -Filter *.ts | ForEach-Object {
    $fileContent = Get-Content -Path $_.FullName -Raw

    if ($fileContent.StartsWith($tsNoCheckCode)) { return }

    $newContent = $tsNoCheckCode + $fileContent
    Set-Content -Path $_.FullName -Value $newContent
}

Write-Host "Adding ts-nocheck complete"
gkamperis commented 3 years ago

@Jrubzjeknf you are rgiht the fixers are problematic/deprectaed and admitedly the approach of fixing the problem is not the correct one.

The best approach is to disable TS/ESLint/TSLint completely on these files. The thing is that TS check disabling was only supported after a specific TS version.

The solution/workaround I now use is to use custom templates and disable checks using the appropriate comments for TS ESLint TSLint

on the api/models templates at the top of the files.

newaeonweb commented 2 years ago

any news about this issue?

fell-lucas commented 1 year ago

Bash script to automatically add // @ts-nocheck to all .ts files. Adjust the find path accordingly.

./scripts/ts-nocheck-on-generated-apis.sh

#! /bin/bash
# The @ts-nocheck is to ignore the typescript compiler itself, otherwise the 
# language server would analyze and throw errors, mostly unused imports 
# and variables, on the generated code.
# The issue can be found on their github repo.
# https://github.com/OpenAPITools/openapi-generator/issues/1880

find ./src/lib/*/adapters/*/apiclient -regextype sed -regex ".*.ts" |
while IFS= read -r file
do
  if ! grep -q "// @ts-nocheck" $file; then
    sed -i "1i// @ts-nocheck" $file
  fi
done

then in package.json:

{
  "scripts": {
    "generate-apis": "openapi-generator-cli generate && sh scripts/ts-nocheck-on-generated-apis.sh"
  }
}
jmezach commented 1 year ago

The same applies for the typescript-fetch generator. Would be nice to just have the generator add the // @ts-nocheck comment at the top, same as the /* tslint:disable */ and /* eslint-disable */ comments that are already there.