ts-morph icon indicating copy to clipboard operation
ts-morph copied to clipboard

How to skip resolving import declarations and quickly find default export

Open dreamerblue opened this issue 3 years ago • 0 comments

I need to parse a large number of files, but encountered some performance issues. My analysis can be roughly simplified to the following operations.

Given a ts file:

import { View as V } from 'some-lib';

// other codes... 

@V('/view')
@OtherDecorator()
export default class MyClass {}

I want to find all files which exports a class decorated with @View from specified module, and collect all necessary data from these files. But I find there are two operations cost a lot of time (usually 50ms - 400ms):

const project = new Project({
  tsConfigFilePath: path.join(opts.clientDir, 'tsconfig.json'),
  skipAddingFilesFromTsConfig: true,
  skipFileDependencyResolution: true,
  skipLoadingLibFiles: true,
});

const sourceFile = project.createSourceFile(/** file fetched dynamically */);

sourceFile.getDefaultExportSymbol(); // the first performance issue point

// then get the default exported class declaration and its decorators. traverse all decorators.
// now parse the decorators[0], get the Identifier `V`, and confirm the `V` is the alias of `View` from 'some-lib'
const def = decoratorIdentifier.getDefinitions()[0]; // it's the second point which cost a lot of time
if (def && def.getContainerName().includes(PKG_NAME) && def.getName() === 'View') {
  // ok
}

For the first point, I dont know why finding default export costs a lot of time. And for the second point, I want to skip resolving imported package (just find a way to confirm the identifier is imported from specified module's import clause in current file?), maybe its helpful to save time.

dreamerblue avatar Sep 11 '21 15:09 dreamerblue