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

Relative path manipulation handles Windows paths and different extensions #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ var rebaseRelativePath = memoize(function(sourceFile, relativeRoot, relativePath
// join relative path to root (e.g. 'src/' + 'file.js')
var relativeRootedPath = relativeRoot ? path.join(relativeRoot, relativePath) : relativePath;

// Ultimately these will be used in URLs so normalise to URL path separators before comparison
relativeRootedPath = relativeRootedPath.replace(/\\/g, '/');
sourceFile = sourceFile.replace(/\\/g, '/');

if (sourceFile === relativeRootedPath || // same path,
path.dirname(sourceFile) === path.dirname(relativeRootedPath) || // same dir, different file (e.g. foo.ts > foo.js)
pathIsAbsolute(relativeRootedPath) || // absolute path, nor
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix the alignment of || here, they were all in the same column for improved readability.
Also cut the comment a bit shorter, (remove the e.g. part).

protocolRx.test(relativeRootedPath)) { // absolute protocol need rebasing
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Required to make existing tests pass on Windows, but probably wise anyway.

return relativeRootedPath;
}

// make relative to source file
return path.join(path.dirname(sourceFile), relativeRootedPath);
return path.join(path.dirname(sourceFile), relativeRootedPath).replace(/\\/g, '/');
}, function(a, b, c) {
return a + '::' + b + '::' + c;
});
Expand Down
61 changes: 61 additions & 0 deletions test/combine-source-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,64 @@ test('remove comments', function (t) {
})
t.end()
})

test('relative path mainipulation copes with changing file extension', function (t) {

var foo = {
version : 3,
file : 'bar/foo.js',
sourceRoot : '',
sources : [ 'bar/foo.coffee' ],
names : [],
mappings : ';AAAA;CAAA;CAAA,CAAA,CAAA,IAAO,GAAK;CAAZ',
sourcesContent : [ 'console.log(require \'./bar.js\')\n' ] };

var mapComment = convert.fromObject(foo).toComment();

var file = {
id: 'xyz'
, source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + mapComment
, sourceFile: 'bar/foo.js'
};

var lineOffset = 3
var base64 = combine.create()
.addFile(file, { line: lineOffset })
.base64()

var sm = convert.fromBase64(base64).toObject();

t.deepEqual(sm.sources, ['bar/foo.coffee'], 'includes original relative file path')
t.end()
})

test('relative path mainipulation generates paths with URL path separators', function (t) {

var foo = {
version : 3,
file : 'bar/foo.js',
sourceRoot : '',
sources : [ 'bar\\foo.coffee' ],
names : [],
mappings : ';AAAA;CAAA;CAAA,CAAA,CAAA,IAAO,GAAK;CAAZ',
sourcesContent : [ 'console.log(require \'./bar.js\')\n' ] };

var mapComment = convert.fromObject(foo).toComment();

var file = {
id: 'xyz'
, source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + mapComment
, sourceFile: 'bar/foo.js'
};

var lineOffset = 3
var base64 = combine.create()
.addFile(file, { line: lineOffset })
.base64()

var sm = convert.fromBase64(base64).toObject();
var res = checkMappings(foo, sm, lineOffset);

t.deepEqual(sm.sources, ['bar/foo.coffee'], 'includes relative file ulr path')
t.end()
})