-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Show unused public properties and methods #29293
Comments
We currently assume that exported members form a public API and therefore are always used. We could try to detect that a certain set of files are an API entrypoints, and then mark unused internal exports. Not sure if there are any issue already tracking this |
Customer demand for this feature mentioned in https://dev.to/mokkapps/why-i-switched-from-visual-studio-code-to-jetbrains-webstorm-939. |
what about exported but unused
|
is there any news about this issue? |
Update: much faster after adding dummy My naive solution below. const fs = require("fs");
const ts = require("typescript");
const path = require("path");
// Copied from https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services
function getLanguageService(rootFileNames, options) {
const files = {};
// initialize the list of files
rootFileNames.forEach(fileName => {
files[fileName] = { version: 0 };
});
// Create the language service host to allow the LS to communicate with the host
const servicesHost = {
getScriptFileNames: () => rootFileNames,
getScriptVersion: fileName => files[fileName] && files[fileName].version.toString(),
getScriptSnapshot: fileName => {
if (!fs.existsSync(fileName)) {
return undefined;
}
return ts.ScriptSnapshot.fromString(fs.readFileSync(fileName).toString());
},
getCurrentDirectory: () => process.cwd(),
getCompilationSettings: () => options,
getDefaultLibFileName: options => ts.getDefaultLibFilePath(options),
getProjectVersion: () => 1,
fileExists: ts.sys.fileExists,
readFile: ts.sys.readFile,
readDirectory: ts.sys.readDirectory,
};
// Create the language service files
const services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry());
return services;
}
const tsconfigPath = "tsconfig.gen.json";
const basePath = path.resolve(path.dirname(tsconfigPath));
const parseJsonResult = ts.parseConfigFileTextToJson(tsconfigPath, fs.readFileSync(tsconfigPath, { encoding: "utf8" }));
const tsConfig = ts.parseJsonConfigFileContent(parseJsonResult.config, ts.sys, basePath);
const services = getLanguageService(tsConfig.fileNames, tsConfig.options);
// For each non-typings file
tsConfig.fileNames
.filter(f => !f.endsWith(".d.ts"))
.forEach(file => {
const source = ts.createSourceFile(file, fs.readFileSync(file, { encoding: "utf8" }));
ts.forEachChild(source, node => {
if (ts.isClassDeclaration(node)) {
// For each class member
node.members.forEach(member => {
// If member is marked as public or protected and not a constructor
if (
(ts.getCombinedModifierFlags(member) & ts.ModifierFlags.Public ||
ts.getCombinedModifierFlags(member) & ts.ModifierFlags.Protected) &&
member.kind !== ts.SyntaxKind.Constructor
) {
const references = services.findReferences(file, member.name.pos + 1);
// Fail if every reference is a definition and not in a typings file
if (
references.every(
reference =>
reference.references.length === 1 &&
reference.references[0].isDefinition &&
!reference.definition.fileName.endsWith(".d.ts")
)
) {
console.error(`File: ${file} , Member: ${member.name.text}`);
}
}
});
}
});
}); |
I was wondering if we could use custom rules to detect and fix this kind of problem |
I would like this because using top-level exports, and destructuring imports can clutter your files quite a lot. E.g. I would like to make a Or would namespaces be a solution to this problem? (does unused code detection work on namespaces?) |
I am also interested to remove the kind of dead code mentioned by zpdDG4gta8XKpMCd . Maybe Patricio Zavolinsky (pzavolinsky), creator of https://www.npmjs.com/package/ts-unused-exports, can help the Visual Code team to implement it? |
Another vote for this feature, first priority IMO would be some kind of indication for unused variables - grey/red squiggles, don't care much. |
No updates about this feature yet? This is present is most modern IDEs. It is a shame that VSCode doesn't support it. |
This would be tremendously useful. Detecting and eliminating unused public methods, exports, etc. is something I feel Code is really missing at this point. Any updates on this feature? |
Until there is an official version, I created my own extension to find unused exports. |
Any ideas how do the same for composed types transformed with Pick? For example: export type SessionStreamConfigurationFragment = (
{ __typename?: 'Session' }
& Pick<Session, 'showRecording'>
& { oneWayStreamConfiguration?: Maybe<(
{ __typename?: 'OneWayStreamConfiguration' }
& OneWayStreamConfigurationFragment
)> }
); |
I had a similar query about unused exports which ive raised here: #30517 |
this would be huge. I have an angular project with loads of classes with static methods that are probably not used anymore. It's too much to audit method by method. |
It would be very interesting to see this feature. I'm currently working on NestJS projects that would benefit from something like this. |
Ah having a huge #Angular repo, would definitely need this in vscode 🥺 |
Did you get any solution by any chance ? |
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment was marked as off-topic.
This comment was marked as off-topic.
One of my engineering peers keeps pointing out stupid errors I'm making in PR reviews because he's got way better static code analysis tools in WebStorm than I have in VSCode which I've been a loyal user of for years. This is embarrassing and making me look like an even worse programmer than I already am - please fix it! |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment has been minimized.
This comment has been minimized.
We have lots of dead public properties on our shared classes, it's painstaking to audit them, I have to use 'find all references' for each property in vscode. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
knip is a pretty good solution to this. Please refrain from posting low-value comments in this thread. |
Would knip find an unused public function on a component though? Those kinds of things are what I'm really looking to root out. |
I'm not sure, but it's not even clear that TypeScript could do a good job of that. If you have something like export class Foo {
bar() { }
baz() { }
}
interface HasBarAndBaz {
bar(): void;
baz(): void;
}
const p: HasBarAndBaz = new Foo();
p.baz(); It's not clear how you really figure out that |
In my opinion it is very clear that the desired behavior is to do what webstorm already does. I'm torn between whether this should be a feature of the language itself or a feature of a separate tool, but in the end, all I really care about is that VSCode can tell me which public methods are unused by the rest of the code (for library code that's not consumed within the same codebase, it is also a good indicator of spotting untested methods) |
I don't know what Webstorm's algorithm is or what its false positives/negatives are. Can you go into more detail on it? |
So it's 2024 and I had to touch Typescript for the first time today and within a few minutes wished for there to be a visual indicator for an unused method and landed here, in this Issue from 2019. Am I understanding correctly that even after 5 years this is still not possible and people are still being recommended Webstorm instead? |
You are absolutely correct. This is a shame! |
It would be nice to have unused detection expanded, capable of detecting unused methods, exports, etc.
Code Example
Shape.js
Circle.js
VSCode Screen
WebStorm Screen
The text was updated successfully, but these errors were encountered: