Skip to content
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

Use babel's loadPartialConfig function #44

Closed
0xdevalias opened this issue Nov 22, 2019 · 3 comments · Fixed by #106
Closed

Use babel's loadPartialConfig function #44

0xdevalias opened this issue Nov 22, 2019 · 3 comments · Fixed by #106

Comments

@0xdevalias
Copy link

0xdevalias commented Nov 22, 2019

TypeError: Cannot read property 'loc' of undefined
    at CallExpression (./node_modules/eslint-plugin-module-resolver/dist/rules/use-alias.js:112:36)
    at listeners.(anonymous function).forEach.listener (./node_modules/eslint/lib/util/safe-emitter.js:47:58)
    at Array.forEach (<anonymous>)
    at Object.emit (./node_modules/eslint/lib/util/safe-emitter.js:47:38)
    at NodeEventGenerator.applySelector (./node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (./node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode (./node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode (./node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:608:23)
    at Traverser.enter [as _enter] (./node_modules/eslint/lib/linter.js:865:28)
    at Traverser._traverse (./node_modules/eslint/lib/util/traverser.js:132:14)
    at Traverser._traverse (./node_modules/eslint/lib/util/traverser.js:147:30)
    at Traverser._traverse (./node_modules/eslint/lib/util/traverser.js:144:34)
    at Traverser._traverse (./node_modules/eslint/lib/util/traverser.js:144:34)
    at Traverser._traverse (./node_modules/eslint/lib/util/traverser.js:147:30)
    at Traverser._traverse (./node_modules/eslint/lib/util/traverser.js:144:34)
    at Traverser._traverse (./node_modules/eslint/lib/util/traverser.js:147:30)

Hacking in some debug logging just before it dies:

CallExpression: function CallExpression(node) {
  console.warn('CallExpression2:', { node })
  context.report({
    node: node,
    message: message,
    loc: node.arguments[0].loc
  });
}

It's trying to access node.arguments[0].loc, but there are no arguments, which is why it crashes:

{ node:
   Node {
     type: 'CallExpression',
     start: 598,
     end: 621,
     loc: SourceLocation { start: [Object], end: [Object] },
     callee:
      Node {
        type: 'Identifier',
        start: 598,
        end: 619,
        loc: [Object],
        name: '_makeShallowComponent',
        range: [Array],
        _babelType: 'Identifier' },
     arguments: [],
     range: [ 598, 621 ],
     _babelType: 'CallExpression',
     parent:
      Node {
        type: 'VariableDeclarator',
        start: 575,
        end: 621,
        loc: [Object],
        id: [Object],
        init: [Circular],
        range: [Array],
        _babelType: 'VariableDeclarator',
        parent: [Object] } } }

The snippet of code it appears to be failing on for me is this, nested relatively deep in my jest test:

const { wrappedComponent } = _makeShallowComponent()

It doesn't seem to matter what it is/where it's placed though, as the following as the only thing in the file also causes a failure:

const { foo } = bar()

Hacking that line of code to use a proper check lets the error complete, and shows it as:

  • 'Unable to find config for babel-plugin-module-resolver'

So I'm thinking maybe this has gotten itself into a weird/undesirable edgecase due to the config being unable to be found, and then it's dying based on an unexpected case there.

Getting super hacky with things, I put the following code in place as per the babel docs:

// var _findBabelConfig$sync = findBabelConfig.sync(babelDir),
//     config = _findBabelConfig$sync.config;
var ___babel = require("@babel/core");
var config = ___babel.loadPartialConfig()
console.warn('babelconfig', config)

And it seemed to find my config file (though obviously it's not correctly wired into this rule/plugin properly, just enough to proof of concept):

babelconfig PartialConfig {
// ..snip ..
 config: 'my-project-dir/babel.config.js'
}
@HeroProtagonist
Copy link
Owner

@0xdevalias based on the discussion in your issue and the input from the maintainer we should move off of using find-babel-config in favor of the function you mention. I am changing the issue title to reflect this.

@HeroProtagonist HeroProtagonist changed the title TypeError: Cannot read property 'loc' of undefined Use babel's loadPartialConfig function Dec 19, 2019
@juzerzarif
Copy link
Contributor

@HeroProtagonist Has there been any updates on this? I ran into an issue with the api argument to the babel config not being defined by find-babel-config and crashing the rule. I was able to get it to pass using loadPartialConfig as mentioned above:

    // var _findBabelConfig$sync = findBabelConfig.sync(babelDir),
    //     config = _findBabelConfig$sync.config;
    var partialConfig = __babel.loadPartialConfig();
    var config = partialConfig.options;

    try {
      var validPluginNames = new Set(['babel-plugin-module-resolver', 'module-resolver']);

      var _config$plugins$filte = config.plugins.filter(function (plugin) {
        if (validPluginNames.has(plugin.file.request)) {
          return plugin;
        }
      }),
          _config$plugins$filte2 = _slicedToArray(_config$plugins$filte, 1),
          moduleResolver = _config$plugins$filte2[0];

      alias = moduleResolver.options.alias || {};

I'd be willing to put up a PR if you're down with what I'm doing here

@HeroProtagonist
Copy link
Owner

PR's are definitely welcome. See contributing.md for useful information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants