Skip to content

Commit

Permalink
fix: provide namespaces from deps.js
Browse files Browse the repository at this point in the history
  • Loading branch information
teppeis committed Jun 6, 2019
1 parent 5c6f4a6 commit d19e1e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
45 changes: 24 additions & 21 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,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 @@ -409,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 Down Expand Up @@ -493,7 +493,7 @@ Parser.prototype.getRequiredPackageName_ = function(name) {
* @return {?string} .
* @private
*/
Parser.prototype.getPackageName_ = function(name) {
Parser.prototype.getProvidedPackageName_ = function(name) {
name = this.replaceMethod_(name);
let names = name.split('.');
let lastname = names[names.length - 1];
Expand All @@ -511,29 +511,32 @@ Parser.prototype.getPackageName_ = function(name) {
return prev;
}
}, []);
lastname = names[names.length - 1];
if (/^[a-z$]/.test(lastname)) {
// Remove the last method name.
names.pop();
}

while (names.length > 0) {
if (!this.isProvidedNamespace_(name)) {
lastname = names[names.length - 1];
if (/^[A-Z][_0-9A-Z]+$/.test(lastname)) {
// Remove the last constant name.
if (/^[a-z$]/.test(lastname)) {
// Remove the last method name.
names.pop();
} else {
break;
}
}

// Remove the static property.
const PARENT_CLASS_INDEX_FROM_LAST = 2;
if (names.length > PARENT_CLASS_INDEX_FROM_LAST) {
lastname = names[names.length - 1];
const parentClass = names[names.length - PARENT_CLASS_INDEX_FROM_LAST];
if (/^[a-z]/.test(lastname) && /^[A-Z]/.test(parentClass)) {
names.pop();
while (names.length > 0) {
lastname = names[names.length - 1];
if (/^[A-Z][_0-9A-Z]+$/.test(lastname)) {
// Remove the last constant name.
names.pop();
} else {
break;
}
}

// Remove the static property.
const PARENT_CLASS_INDEX_FROM_LAST = 2;
if (names.length > PARENT_CLASS_INDEX_FROM_LAST) {
lastname = names[names.length - 1];
const parentClass = names[names.length - PARENT_CLASS_INDEX_FROM_LAST];
if (/^[a-z]/.test(lastname) && /^[A-Z]/.test(parentClass)) {
names.pop();
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/parse/provide-namespaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
goog.foo = function() {};
goog.aaa = function() {};

// toProvide: goog.foo

0 comments on commit d19e1e0

Please sign in to comment.