amdextract
Advanced tools
Comparing version 2.1.5 to 2.1.6
68
index.js
@@ -80,3 +80,5 @@ /* | ||
if (paths && paths.type === 'ArrayExpression') { | ||
module.paths = paths.elements; | ||
module.paths = paths.elements.map(function(element) { | ||
return estraverse.attachComments(element, comments, tokens); | ||
}); | ||
} | ||
@@ -88,3 +90,5 @@ | ||
if (callback && callback.type === 'FunctionExpression') { | ||
module.dependencies = callback.params; | ||
module.dependencies = callback.params.map(function(param) { | ||
return estraverse.attachComments(param, comments, tokens); | ||
}); | ||
module.body = callback.body; | ||
@@ -134,11 +138,8 @@ } | ||
for (var char = source[start]; regEx.test(char); char = source[--start]) { | ||
if (char === ',') { | ||
commaVisited = true; | ||
} | ||
} | ||
if (!commaVisited) { | ||
if (range.isForFirstElem) { | ||
for (var char = source[end]; regEx.test(char); char = source[++end]) | ||
; | ||
} else { | ||
for (var char = source[start]; regEx.test(char); char = source[--start]) | ||
; | ||
} | ||
@@ -149,10 +150,35 @@ | ||
function optimizeContent(content, rangesToRemove) { | ||
if (!rangesToRemove) { | ||
return content; | ||
} | ||
function optimizeContent(content, modules) { | ||
var rangesToRemove = [], | ||
output = '', | ||
start = 0, | ||
firstPath, | ||
firstDependency; | ||
var output = '', | ||
start = 0; | ||
modules.forEach(function(module) { | ||
if (module.paths) { firstPath = module.paths[0]; } | ||
if (module.dependencies) { firstDependency = module.dependencies[0]; } | ||
module.unusedPaths.concat(module.unusedDependencies).forEach(function(item) { | ||
var isFirstElem = item === firstPath || item === firstDependency; | ||
if (item.leadingComments) { | ||
item.leadingComments.forEach(function(comment) { | ||
comment.range.isForFirstElem = isFirstElem; | ||
rangesToRemove.push(comment.range); | ||
}); | ||
} | ||
item.range.isForFirstElem = isFirstElem; | ||
rangesToRemove.push(item.range); | ||
if (item.trailingComments) { | ||
item.trailingComments.forEach(function(comment) { | ||
comment.range.isForFirstElem = isFirstElem; | ||
rangesToRemove.push(comment.range); | ||
}); | ||
} | ||
}); | ||
}); | ||
rangesToRemove.forEach(function(range) { | ||
@@ -203,4 +229,3 @@ range = extendRange(range, content); | ||
modules = getModules(parsedCode), | ||
result = {}, | ||
ranges; | ||
result = {}; | ||
@@ -228,7 +253,4 @@ result.results = modules.map(function(module) { | ||
if (options.removeUnusedDependencies) { | ||
ranges = []; | ||
ArrayProto.push.apply(ranges, unusedPaths.map(function(path) { return path.range; })); | ||
ArrayProto.push.apply(ranges, unusedDependencies.map(function(dependency) { return dependency.range; })); | ||
} | ||
module.unusedDependencies = unusedDependencies; | ||
module.unusedPaths = unusedPaths; | ||
@@ -245,3 +267,3 @@ return { | ||
if (options.removeUnusedDependencies) { | ||
result.optimizedContent = optimizeContent(content, ranges); | ||
result.optimizedContent = optimizeContent(content, modules); | ||
} | ||
@@ -248,0 +270,0 @@ |
{ | ||
"name": "amdextract", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"description": "Uses AST to extract AMD modules, their parts and an optimized output without unused dependencies.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -120,2 +120,3 @@ # amdextract [![Build Status](https://travis-ci.org/mehdishojaei/amdextract.png)](https://travis-ci.org/mehdishojaei/amdextract) | ||
## Release History | ||
* 2014-08-22 v2.1.6 Fix a bug when there are comments between paths and dependencies. | ||
* 2014-08-16 v2.1.0 Entirely uses AST. | ||
@@ -122,0 +123,0 @@ * 2014-07-21 v2.0.3 Fix an issue related to comment detection. |
// General test for all cases. | ||
/* exceptsPaths: p3 */ | ||
define('name', ["p2" | ||
define('name', ["p2" // This paths is used. | ||
, 'p3', 'p4', "t1" , 'm1' | ||
// inline comment between paths. | ||
, | ||
// inline comment between paths. | ||
/** | ||
* Block comment between dependencies. | ||
*/ | ||
'm2'], function (b | ||
// inline comment between dependencies. | ||
, c, d) { | ||
@@ -13,0 +18,0 @@ /** |
// General test for all cases. | ||
/* exceptsPaths: p3 */ | ||
define('name', ["p1", | ||
"p2" | ||
define('name', [/* This path is unused. */ "p1", | ||
"p2" // This paths is used. | ||
, 'p3', 'p4', | ||
"p5", "t1" , 'm1' | ||
// inline comment between paths. | ||
, | ||
// inline comment between paths. | ||
/** | ||
* Block comment between dependencies. | ||
*/ | ||
'm2'], function (a , b | ||
// inline comment between dependencies. | ||
, c, d) { | ||
@@ -15,0 +20,0 @@ /** |
@@ -158,2 +158,18 @@ var amdextract = require('..'); | ||
describe('comments between paths or dependencies', function() { | ||
var output = amdextract.parse( | ||
"define('name', ['p1', /**/ 'p2', 'p3' /**/, /**/ 'p4' /**/, /**/ 'p5' /**/], function(a, b, c, d) { return a.concat(d); })", | ||
{ removeUnusedDependencies: true } | ||
); | ||
var result = output.results[0]; | ||
it('.moduleId', function() { should(result.moduleId).equal('name'); }); | ||
it('.paths', function() { result.paths.should.be.eql(['p1', 'p2', 'p3', 'p4', 'p5']); }); | ||
it('.dependencies', function() { result.dependencies.should.be.eql(['a', 'b', 'c', 'd']); }); | ||
it('.unusedPaths', function() { result.unusedPaths.should.be.eql(['p2', 'p3', 'p5']); }); | ||
it('.unusedDependencies', function() { result.unusedDependencies.should.be.eql(['b', 'c']); }); | ||
it('.optimizedContent', function() { should(output.optimizedContent).be.equal( | ||
"define('name', ['p1', /**/ 'p4' /**/], function(a, d) { return a.concat(d); })" | ||
); }); | ||
}); | ||
describe('general test', function() { | ||
@@ -160,0 +176,0 @@ var output = parse('sample', { removeUnusedDependencies: true, exceptsPaths: ['t1', /^m/] }); |
25221
470
129