refactoring-codemods icon indicating copy to clipboard operation
refactoring-codemods copied to clipboard

Assume index.js when path resolves to a directory

Open jurassix opened this issue 7 years ago • 6 comments

import app from './app' where ./app was a folder containing an index.js

jurassix avatar Apr 10 '17 17:04 jurassix

require.resolve should handle this. Wonder if it would be as simple as switching to that from path.resolve?

ahutchings avatar Apr 10 '17 17:04 ahutchings

Giving it a shot now.

jurassix avatar Apr 11 '17 01:04 jurassix

it looks like require.resolve is a no-go. It try's to resolve the file relative to the closure e.g.https://github.com/jurassix/refactoring-codemods/blob/master/src/transformers/fileHelpers/filterMatchingPaths.js#L7 . I'll hack at it from a different vector.

jurassix avatar Apr 11 '17 02:04 jurassix

https://github.com/substack/node-resolve lets you resolve relative to a basedir. I believe this is what browserify uses.

ahutchings avatar Apr 11 '17 02:04 ahutchings

i added paths to see if that would help. Looking for more outs. I think I will also have to have a whitelist even with resolve since it defaults to just .js

jurassix avatar Apr 11 '17 03:04 jurassix

Ok was approaching it wrong. This is closer.

error:

 FAIL  src/transformers/__tests__/import-specifier-transform-test.js (1.704s)
● import-specifier-transform › it transforms correctly
  - Error: Cannot find module './bar' from '/Users/cayres/projects/refactoring-codemods/src/transformers/__testfixtures__'

code:

import resolve from 'resolve';
import { normalize } from 'path';
import removeExtension from './removeExtension';

export default function filterMatchingPaths(basedir, filePath) {
  const normalizedFilePath = normalize(filePath);
  return path => {
    const testPath = resolve.sync(path.value.value, { basedir });
    return testPath === normalizedFilePath;
  };
}

jurassix avatar Apr 11 '17 03:04 jurassix