pahen / madge

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

Fails silently when analyzing files containing reserved words #343

Open Jemt opened 1 year ago

Jemt commented 1 year ago

Hi.

I had a source file that declared a local variable like this:

var interface =
{
    Something: function() { ... }
}

This results in Madge skipping the file, failing to detect any dependencies: precinct could not parse content: Unexpected reserved word 'interface'. (366:8) +32ms

Complete debug output:

MBP:AppKit jemt$ node_modules/madge/bin/cli.js --debug Client/AppKit/Component.js 
⠋ Finding files  madge using src paths [ '/Users/jemt/www/AppKit/Client/AppKit/Component.js' ] +0ms
  madge using config { baseDir: null, excludeRegExp: false, fileExtensions: [ 'js' ], 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)], debug: true, version: [Function: version] } +3ms
  madge using base directory /Users/jemt/www/AppKit/Client/AppKit +2ms
  tree given filename: /Users/jemt/www/AppKit/Client/AppKit/Component.js +0ms
  tree resolved filename: /Users/jemt/www/AppKit/Client/AppKit/Component.js +0ms
  tree visited:  {} +1ms
  tree traversing /Users/jemt/www/AppKit/Client/AppKit/Component.js +0ms
  precinct paperwork: invoking precinct +0ms
  precinct options given: { includeCore: false } +0ms
  precinct we assume this is JS +1ms
  precinct could not parse content: Unexpected reserved word 'interface'. (366:8) +32ms
  tree extracted 0 dependencies:  [] +33ms
  tree cabinet-resolved all dependencies:  [] +0ms
  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 {} +0ms
  tree deduped list of nonExistent partials:  [] +0ms
  tree object form of results requested +0ms
  tree final tree { '/Users/jemt/www/AppKit/Client/AppKit/Component.js': {} } +0ms
Processed 1 file (481ms) 

I suggest that the check fails with an exit code or at least issues a warning (even if --warning is not specified), rather than giving the impression that no dependencies are present.

-- Thanks :-)

PabloLION commented 1 year ago

I see two problems here:

  1. we should split two types of warning: "user skipped the file"/ "cannot parse file", and the latter should not be affected by --warning cli flag
  2. handle files with reserved word like 'interface' correctly ( I dug a little here and found that interface was actually a JS reserved word: MDN, DOM