Skip to content

Commit

Permalink
Merge pull request #264 from Galooshi/fix-263
Browse files Browse the repository at this point in the history
Fix using absolute paths for path to current file
  • Loading branch information
trotzig committed May 11, 2016
2 parents 2a48a61 + 599a684 commit bb39769
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/Importer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

import escapeRegExp from 'lodash.escaperegexp';
import eslint from 'eslint';

import CommandLineEditor from './CommandLineEditor';
Expand Down Expand Up @@ -27,7 +28,12 @@ export default class Importer {
constructor(lines, pathToCurrentFile) {
this.editor = new CommandLineEditor(lines);
this.config = new Configuration(pathToCurrentFile);
this.pathToCurrentFile = pathToCurrentFile;

// Normalize the path to the current file so that we only have to deal with
// local paths.
this.pathToCurrentFile = pathToCurrentFile && pathToCurrentFile.replace(
RegExp(`^${escapeRegExp(process.cwd())}/`), '');

this.messages = Array.from(this.config.messages);
this.unresolvedImports = {};
}
Expand Down
7 changes: 2 additions & 5 deletions lib/JsModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,13 @@ export default class JsModule {
return;
}

// First, strip out any absolute path up until the current directory
let makeRelativeTo = makeRelativeToPath.replace(`^${process.cwd()}/`, '');

// Ignore if the file to relate to is part of a different lookupPath
if (!makeRelativeTo.startsWith(this.lookupPath)) {
if (!makeRelativeToPath.startsWith(this.lookupPath)) {
return;
}

// Strip out the lookupPath
makeRelativeTo = makeRelativeTo.replace(
const makeRelativeTo = makeRelativeToPath.replace(
RegExp(`^${escapeRegExp(this.lookupPath)}/`), '');

let importPath = path.relative(
Expand Down
14 changes: 14 additions & 0 deletions lib/__tests__/Importer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,20 @@ foo
expect(subject()).toEqual(`
import foo from 'foo';
foo
`.trim());
});
});

describe('when the current file is an absolute path in the same lookupPath', () => {
beforeEach(() => {
pathToCurrentFile = `${process.cwd()}/bar/test.js`;
});

it('uses a relative import path', () => {
expect(subject()).toEqual(`
import foo from './foo';
foo
`.trim());
});
Expand Down

0 comments on commit bb39769

Please sign in to comment.