Skip to content

Instantly share code, notes, and snippets.

@jameswomack
Created June 5, 2024 18:16
Show Gist options
  • Save jameswomack/872dcf472c6bbd1ff89fde571185a115 to your computer and use it in GitHub Desktop.
Save jameswomack/872dcf472c6bbd1ff89fde571185a115 to your computer and use it in GitHub Desktop.
Parse imports from TypeScript files
// Use like `node ts-module-lexer.js packages/extensions/foo/src/extension.ts`
const fs = require('fs');
const path = require('path');
const ts = require('typescript');
const [_processName, _scriptName, ...args] = process.argv;
const fileName = path.basename(args[0]);
const sourceText = fs.readFileSync(args[0], 'utf8');
// eslint-disable-next-line no-console
console.info(`Parsing imports for ${fileName} at path ${args[0]}`);
const node = ts.createSourceFile(fileName, sourceText, ts.ScriptTarget.Latest);
const importStatements = node.statements.filter(ts.isImportDeclaration);
const massagedImportStatements = importStatements.map(importStatement => {
const { moduleSpecifier } = importStatement;
const { importClause } = importStatement;
const retVal = {
importedModuleString: moduleSpecifier.text
};
if (importClause) {
if (importClause.namedBindings) {
if (ts.isNamedImports(importClause.namedBindings)) {
retVal.whatWasImported = importClause.namedBindings.elements.map(element => element.name.text);
} else if (ts.isNamespaceImport(importClause.namedBindings)) {
retVal.namespace = importClause.namedBindings.name.text;
}
}
}
return retVal;
});
// eslint-disable-next-line no-console
console.dir(massagedImportStatements, { depth: 3 });
@jameswomack
Copy link
Author

jameswomack commented Jun 5, 2024

Example output:

// Parsing imports for extension.ts at path packages/extensions/application/src/extension.ts
[
  {
    importedModuleString: 'vscode',
    whatWasImported: [ 'commands', 'ExtensionContext', 'window', 'workspace' ]
  },
  { importedModuleString: 'vscode', namespace: 'vscode' }, // For `import * as vscode from 'vscode';`
  {
    importedModuleString: '@foo/constants',
    whatWasImported: [ 'getSnHttp', 'FOO_COMMON', 'SnCommon' ]
  },
  {
    importedModuleString: './constants',
    whatWasImported: [
      'CREATE_RECORD_COMMAND',
      'OPEN_RECORD_IN_NEW_TAB_COMMAND',
      'RELOAD_META_DATA_COMMAND'
    ]
  },
  {
    importedModuleString: '@foo/host',
    whatWasImported: [ 'HostService' ]
  },
  {
    importedModuleString: './host',
    whatWasImported: [ 'genOpenFormCallback' ]
  },
  {
    importedModuleString: './application',
    whatWasImported: [
      'createRecordCallback',
      'EDITOR_RECORD_TYPE',
      'RecordEditorProvider',
      'APPLICATION_NAVIGATOR_TREE_VIEW_ID',
      'ApplicationNavigatorDataProvider',
      'WebviewSerializer'
    ]
  },
  {
    importedModuleString: './utils',
    whatWasImported: [
      'genReloadCallback',
      'onReady',
      'toDataReady',
      'toUri',
      'toViewReady'
    ]
  }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment