Skip to content

Commit

Permalink
Merge d19e1e0 into acc065d
Browse files Browse the repository at this point in the history
  • Loading branch information
teppeis committed Jun 6, 2019
2 parents acc065d + d19e1e0 commit 93bb340
Show file tree
Hide file tree
Showing 42 changed files with 208 additions and 244 deletions.
11 changes: 3 additions & 8 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ function setCommandOptions(command) {
.usage('[options] files...')
.option('-f, --fix-in-place', 'Fix the file in-place.')
.option('--provideRoots <roots>', 'Root namespaces to provide separated by comma.', list)
.option('--requireRoots <roots>', 'Root namespaces to require separated by comma.', list)
.option(
'--namespaceMethods <methods>',
'Methods or properties which are also namespaces separated by comma.',
list
)
.option('--namespaceMethods <methods>', 'DEPRECATED: Use --namespaces', list)
.option('--namespaces <methods>', 'Provided namespaces separated by comma.', list)
.option(
'--replaceMap <map>',
'Methods or properties to namespaces mapping like "before1:after1,before2:after2".',
Expand Down Expand Up @@ -148,8 +144,7 @@ async function main(argv, stdout, stderr, exit) {
const src = await promisify(fs.readFile)(file, 'utf8');
const options = {
provideRoots: program.provideRoots,
requireRoots: program.requireRoots,
namespaceMethods: symbols.concat(program.namespaceMethods),
providedNamespace: symbols.concat(program.namespaceMethods).concat(program.namespaces),
replaceMap: program.replaceMap,
};
const parser = new Parser(options);
Expand Down
5 changes: 3 additions & 2 deletions lib/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ const ignorePackages = ['goog'];

/**
* @type {Array<string>}
* @deprecated Use --depsJs
*/
const namespaceMethods = [
const providedNamespaces = [
'goog.color.names',
'goog.date.month',
'goog.date.weekDay',
Expand Down Expand Up @@ -66,7 +67,7 @@ exports.getReplaceMap = () => new Map(Object.entries(replaceMap));
/**
* @return {Set<string>}
*/
exports.getNamespaceMethods = () => new Set(namespaceMethods);
exports.getProvidedNamespaces = () => new Set(providedNamespaces);

/**
* @return {Set<string>}
Expand Down
75 changes: 37 additions & 38 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,17 @@ const Parser = function(opt_options) {
this.provideRoots_ = def.getRoots();
}

if (options.requireRoots) {
this.requireRoots_ = new Set(options.requireRoots);
} else {
this.requireRoots_ = def.getRoots();
this.provideRoots_.forEach(root => {
this.requireRoots_.add(root);
});
}

this.replaceMap_ = def.getReplaceMap();
if (options.replaceMap) {
options.replaceMap.forEach((value, key) => {
this.replaceMap_.set(key, value);
});
}

this.namespaceMethods_ = def.getNamespaceMethods();
if (options.namespaceMethods) {
options.namespaceMethods.forEach(method => {
this.namespaceMethods_.add(method);
this.providedNamespaces_ = def.getProvidedNamespaces();
if (options.providedNamespace) {
options.providedNamespace.forEach(method => {
this.providedNamespaces_.add(method);
});
}

Expand Down Expand Up @@ -173,7 +164,6 @@ Parser.prototype.extractToRequire_ = function(parsed, toProvide, comments, opt_r
.map(this.toRequireMapper_.bind(this))
.concat(additional)
.filter(this.isDefAndNotNull_)
.filter(this.requireRootFilter_.bind(this))
.sort()
.reduce(this.uniq_, []);
return difference(toRequire, toProvide);
Expand Down Expand Up @@ -202,7 +192,7 @@ Parser.prototype.extractToRequireTypeFromJsDoc_ = function(comments) {
});
return prev;
}, [])
.filter(this.requireRootFilter_.bind(this))
.filter(name => this.isProvidedNamespace_(name))
.sort()
.reduce(this.uniq_, []);
};
Expand Down Expand Up @@ -399,16 +389,6 @@ Parser.prototype.provideRootFilter_ = function(item) {
return this.provideRoots_.has(root);
};

/**
* @param {string} item .
* @return {boolean} True if the item has a root namespace to extract.
* @private
*/
Parser.prototype.requireRootFilter_ = function(item) {
const root = item.split('.')[0];
return this.requireRoots_.has(root);
};

/**
* @param {Object} use .
* @return {?string} Used namespace.
Expand All @@ -420,7 +400,7 @@ Parser.prototype.toProvideMapper_ = function(use) {
switch (use.node.type) {
case Syntax.AssignmentExpression:
if (use.key === 'left' && use.node.loc.start.column === 0) {
return this.getPackageName_(name);
return this.getProvidedPackageName_(name);
}
break;
case Syntax.ExpressionStatement:
Expand All @@ -429,7 +409,7 @@ Parser.prototype.toProvideMapper_ = function(use) {
}
typeDefComments = this.getTypedefComments_(use.node.leadingComments);
if (typeDefComments.length > 0) {
return this.getPackageName_(name);
return this.getProvidedPackageName_(name);
}
break;

Expand All @@ -446,7 +426,7 @@ Parser.prototype.toProvideMapper_ = function(use) {
*/
Parser.prototype.toRequireMapper_ = function(use) {
const name = use.name.join('.');
return this.getPackageName_(name);
return this.getRequiredPackageName_(name);
};

/**
Expand Down Expand Up @@ -491,17 +471,31 @@ Parser.prototype.suppressFilter_ = function(comments, use) {
return !suppressComment;
};

/**
* @param {string} name .
* @return {string|null} .
* @private
*/
Parser.prototype.getRequiredPackageName_ = function(name) {
let names = name.split('.');
do {
const name = this.replaceMethod_(names.join('.'));
if (this.providedNamespaces_.has(name) && !this.isIgnorePackage_(name)) {
return name;
}
names = names.slice(0, -1);
} while (names.length > 0);
return null;
};

/**
* @param {string} name .
* @return {?string} .
* @private
*/
Parser.prototype.getPackageName_ = function(name) {
Parser.prototype.getProvidedPackageName_ = function(name) {
name = this.replaceMethod_(name);
let names = name.split('.');
if (this.isPrivateProp_(names)) {
return null;
}
let lastname = names[names.length - 1];
// Remove calling with apply or call.
if (lastname === 'apply' || lastname === 'call') {
Expand All @@ -517,7 +511,8 @@ Parser.prototype.getPackageName_ = function(name) {
return prev;
}
}, []);
if (!this.isNamespaceMethod_(name)) {

if (!this.isProvidedNamespace_(name)) {
lastname = names[names.length - 1];
if (/^[a-z$]/.test(lastname)) {
// Remove the last method name.
Expand All @@ -526,7 +521,7 @@ Parser.prototype.getPackageName_ = function(name) {

while (names.length > 0) {
lastname = names[names.length - 1];
if (/^[A-Z][_0-9A-Z]*$/.test(lastname)) {
if (/^[A-Z][_0-9A-Z]+$/.test(lastname)) {
// Remove the last constant name.
names.pop();
} else {
Expand All @@ -545,6 +540,10 @@ Parser.prototype.getPackageName_ = function(name) {
}
}

if (this.isPrivateProp_(names)) {
return null;
}

const pkg = names.join('.');
if (pkg && !this.isIgnorePackage_(pkg)) {
return this.replaceMethod_(pkg);
Expand Down Expand Up @@ -582,12 +581,12 @@ Parser.prototype.replaceMethod_ = function(method) {
};

/**
* @param {string} method Method name.
* @return {boolean} .
* @param {string} name
* @return {boolean}
* @private
*/
Parser.prototype.isNamespaceMethod_ = function(method) {
return this.namespaceMethods_.has(method);
Parser.prototype.isProvidedNamespace_ = function(name) {
return this.providedNamespaces_.has(name);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ function registerIdentifier_(node, parents, path) {
function createMemberObject_(namespace, node, parentKey) {
return {
name: namespace,
node: node,
node,
key: parentKey,
};
}

module.exports = {
leave: leave,
leave,
};
3 changes: 1 addition & 2 deletions test/.fixclosurerc
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
--provideRoots=foo,bar
--namespaceMethods=goog.foo.namespacemethod1
--provideRoots=foo
Loading

0 comments on commit 93bb340

Please sign in to comment.