pahen / madge

Create graphs from your CommonJS, AMD or ES6 module dependencies
MIT License
9.06k stars 317 forks source link

Module dependencies completely ignored in Typescript #366

Open crystalfp opened 1 year ago

crystalfp commented 1 year ago

Madge 6.0.0 seems finding dependencies correctly for my Node typescript application except for one module (Routes.ts) for which all dependencies are ignored. These are the dependencies, nothing strange here:

import fs from "node:fs";
import path from "node:path";
import {StatusCodes as HttpStatusCode} from "http-status-codes";
import Router from "koa-router";
import {isRootUUID} from "../common/CommonUtilities.js";
import {serverLogger} from "../common/Loggers.js";
import {listDirectoryContent} from "./Utility.js";
import {Configuration} from "./ServerConfiguration.js";
import type Koa from "koa";
import type {ConfigurationData, DocumentReferenceOnDisk,
             TagValue, ParsedMarkdownDocument, Project} from "./types.js";
import type {ServerResources} from "./ServerResources.js";

Running madge with --debug shows an error:

2023-03-16T15:00:39.596Z tree traversing D:\Projects\VUEIdeaIgniterEnvironment\server\server\Routes.ts
2023-03-16T15:00:39.596Z typescript-eslint:typescript-estree:createSourceFile Getting AST without type information in TS mode for: D:\Projects\VUEIdeaIgniterEnvironment\server\estree.ts
2023-03-16T15:00:39.602Z tree error getting dependencies: Expression expected.
2023-03-16T15:00:39.602Z tree TSError: Expression expected.
    at createError (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\node-utils.js:539:12)
    at convertError (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\convert.js:41:41)
    at astConverter (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\ast-converter.js:15:42)
    at parseWithNodeMapsInternal (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\parser.js:65:66)
    at Object.parse (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\parser.js:43:21)
    at module.exports.parse (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\node-source-walk\index.js:64:22)
    at module.exports.walk (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\node-source-walk\index.js:108:42)
    at module.exports (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\detective-typescript\index.js:33:10)
    at precinct (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\dependency-tree\node_modules\precinct\index.js:108:20)
    at precinct.paperwork (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\dependency-tree\node_modules\precinct\index.js:161:16)
2023-03-16T15:00:39.602Z tree cabinet-resolved all dependencies:  []
2023-03-16T15:00:39.602Z tree using filter function to filter out dependencies
2023-03-16T15:00:39.602Z tree unfiltered number of dependencies: 0
2023-03-16T15:00:39.602Z tree filtered number of dependencies: 0

Is this a madge bug, or I have done something strange in my module (that otherwise compiles without errors)? Thanks for your help! mario

crystalfp commented 1 year ago

Seems I found the cause. This file contains a class with a big method (~600 lines). For test I shortened it to < 200 lines and the error disappeared. Now I will try to refactor this routine, well, not only to solve this problem. Anyway, at least returning the error to the user could help a lot! Thanks mario

crystalfp commented 1 year ago

Unfortunately the refactoring changed nothing. Same error from madge --debug.

Finding files
2023-03-20T09:51:20.667Z madge using src paths [ 'D:\\Projects\\VUEIdeaIgniterEnvironment\\server\\server\\Routes.ts' ]
2023-03-20T09:51:20.669Z madge using config { baseDir: './server', excludeRegExp: false, fileExtensions: [ 'ts' ], includeNpm: false, requireConfig: null, webpackConfig: null, tsConfig: { compilerOptions: { target: 'esnext', module: 'esnext', lib: [Array], sourceMap: true, incremental: true, removeComments: true, strict: true, alwaysStrict: true, useDefineForClassFields: true, noUnusedLocals: true, noUnusedParameters: true, noImplicitReturns: true, moduleResolution: 'node', allowSyntheticDefaultImports: true, esModuleInterop: true, resolveJsonModule: true, forceConsistentCasingInFileNames: true, importsNotUsedAsValues: 'remove', strictNullChecks: true }, include: [ '*', '../common/*' ], exclude: [ './node_modules', './tests' ] }, rankdir: 'LR', layout: 'dot', fontName: 'Arial', fontSize: '14px', backgroundColor: '#111111', nodeColor: '#c6c5fe', nodeShape: 'box', nodeStyle: 'rounded', noDependencyColor: '#cfffac', cyclicNodeColor: '#ff6c60', edgeColor: '#757575', graphVizOptions: false, graphVizPath: false, dependencyFilter: [Function (anonymous)], debug: true, detectiveOptions: { ts: { skipTypeImports: false } }, version: [Function: version] }
2023-03-20T09:51:22.356Z madge using base directory D:\Projects\VUEIdeaIgniterEnvironment\server\server
2023-03-20T09:51:22.358Z tree given filename: D:\Projects\VUEIdeaIgniterEnvironment\server\server\Routes.ts
2023-03-20T09:51:22.358Z tree resolved filename: D:\Projects\VUEIdeaIgniterEnvironment\server\server\Routes.ts
2023-03-20T09:51:22.358Z tree visited:  {}
2023-03-20T09:51:22.358Z tree traversing D:\Projects\VUEIdeaIgniterEnvironment\server\server\Routes.ts
2023-03-20T09:51:22.360Z typescript-eslint:typescript-estree:createSourceFile Getting AST without type information in TS mode for: D:\Projects\VUEIdeaIgniterEnvironment\server\estree.ts
2023-03-20T09:51:22.397Z tree error getting dependencies: Expression expected.
2023-03-20T09:51:22.398Z tree TSError: Expression expected.
    at createError (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\node-utils.js:539:12)
    at convertError (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\convert.js:41:41)
    at astConverter (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\ast-converter.js:15:42)
    at parseWithNodeMapsInternal (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\parser.js:65:66)
    at Object.parse (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\parser.js:43:21)
    at module.exports.parse (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\node-source-walk\index.js:64:22)
    at module.exports.walk (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\node-source-walk\index.js:108:42)
    at module.exports (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\detective-typescript\index.js:33:10)
    at precinct (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\dependency-tree\node_modules\precinct\index.js:108:20)
    at precinct.paperwork (c:\Users\mvalle\AppData\Roaming\npm\node_modules\madge\node_modules\dependency-tree\node_modules\precinct\index.js:161:16)
2023-03-20T09:51:22.398Z tree cabinet-resolved all dependencies:  []
2023-03-20T09:51:22.398Z tree using filter function to filter out dependencies
2023-03-20T09:51:22.398Z tree unfiltered number of dependencies: 0
2023-03-20T09:51:22.398Z tree filtered number of dependencies: 0
2023-03-20T09:51:22.398Z tree traversal complete {}
2023-03-20T09:51:22.398Z tree deduped list of nonExistent partials:  []
2023-03-20T09:51:22.398Z tree object form of results requested
2023-03-20T09:51:22.398Z tree final tree {
  'D:\\Projects\\VUEIdeaIgniterEnvironment\\server\\server\\Routes.ts': {}
}

Well, at least my class now looks better...

james-pre commented 1 year ago

I'm getting a different error...

⠋ Finding files  madge using src paths [ 'D:\\blankstorm\\src\\client\\index.ts' ] +0ms
  madge using config { baseDir: null, excludeRegExp: false, fileExtensions: [ 'ts' ], includeNpm: false, requireConfig: null, webpackConfig: null, tsConfig: null, rankdir: 'LR', layout: 'dot', fontName: 'Arial', fontSize: '14px', backgroundColor: '#111111', nodeColor: '#c6c5fe', nodeShape: 'box', nodeStyle: 'rounded', noDependencyColor: '#cfffac', cyclicNodeColor: '#ff6c60', edgeColor: '#757575', graphVizOptions: false, graphVizPath: false, dependencyFilter: [Function (anonymous)], detectiveOptions: { ts: { skipTypeImports: true } }, image: 'C:/Users/preve/Pictures/Blankstorm/Deps/client.svg', extensions: 'ts', debug: true, version: [Function: version] } +2ms
  madge using base directory D:/blankstorm/src/client +2ms
  tree given filename: D:\blankstorm\src\client\index.ts +0ms
  tree resolved filename: D:\blankstorm\src\client\index.ts +0ms
  tree visited:  {} +0ms
  tree traversing D:\blankstorm\src\client\index.ts +0ms
  typescript-eslint:typescript-estree:createSourceFile Getting AST without type information in TS mode for: D:\blankstorm\estree.ts +0ms
  tree error getting dependencies: Property or signature expected. +33ms
  tree TSError: Property or signature expected.
  tree     at createError (C:\Users\preve\AppData\Roaming\nvm\v18.14.0\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\node-utils.js:529:12)
  tree     at convertError (C:\Users\preve\AppData\Roaming\nvm\v18.14.0\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\convert.js:41:41)
  tree     at astConverter (C:\Users\preve\AppData\Roaming\nvm\v18.14.0\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\ast-converter.js:15:42)
  tree     at parseWithNodeMapsInternal (C:\Users\preve\AppData\Roaming\nvm\v18.14.0\node_modules\madge\node_modules\@typescript-eslint\typescript-estree\dist\parser.js:65:66)
  tree cabinet-resolved all dependencies:  [] +2ms
  tree using filter function to filter out dependencies +0ms
  tree unfiltered number of dependencies: 0 +0ms
  tree filtered number of dependencies: 0 +0ms
  tree traversal complete {} +1ms
  tree deduped list of nonExistent partials:  [] +0ms
  tree object form of results requested +0ms
  tree final tree { 'D:\\blankstorm\\src\\client\\index.ts': {} } +0ms
✔ Image created at C:\Users\preve\Pictures\Blankstorm\Deps\client.svg

Imports (TypeScript)

import { Vector2, Vector3 } from '@babylonjs/core/Maths/math.vector';
import { Color3 } from '@babylonjs/core/Maths/math.color';
import { Engine } from '@babylonjs/core/Engines/engine';
import $ from 'jquery';
$.ajaxSetup({ timeout: 3000 });

import { GAME_URL, config, version, versions } from '../core/meta';
import { isHex, isJSON, random, xpToLevel } from '../core/utils';
import { commands, execCommandString } from '../core/commands';
import * as api from '../core/api';
import { Ship } from '../core/entities/Ship';
import type { Player } from '../core/entities/Player';
import type { Entity } from '../core/entities/Entity';
import type { ShipType } from '../core/generic/ships';
import { Level } from '../core/Level';
import { Keybind, SettingsMap } from './settings';
import { LocaleStore } from './locales';
import { upload, minimize, alert, cookies } from './utils';
import { Waypoint } from './waypoint';
import { SaveMap, Save, LiveSave } from './Save';
import { ServerMap, Server } from './Server';
import fs from './fs';
import * as ui from './ui/ui';
import { sounds, playsound } from './audio';
import * as renderer from '../renderer/index';

The file is 956 lines and worked fine when it was JavaScript.

NutchapolSal commented 1 year ago

I think it might have to do with Madge's Typescript being older than your project's Typescript, and also that the dependencies are not returned when a TSError occurs (so it thinks that the file is a leaf node)

Explicitly installing Typescript into npm -g (instead of relying on having it from peer dependencies) seems to fix the problem