Skip to content

Commit

Permalink
fix: provide parent namespaces of private props
Browse files Browse the repository at this point in the history
  • Loading branch information
teppeis committed Jun 6, 2019
1 parent b21a949 commit 5c6f4a6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
45 changes: 22 additions & 23 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,6 @@ Parser.prototype.getRequiredPackageName_ = function(name) {
Parser.prototype.getPackageName_ = 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 @@ -514,32 +511,34 @@ Parser.prototype.getPackageName_ = function(name) {
return prev;
}
}, []);
if (!this.isProvidedNamespace_(name)) {
lastname = names[names.length - 1];
if (/^[a-z$]/.test(lastname)) {
// Remove the last method name.
names.pop();
}

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

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();
}
}

// 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();
}
}
if (this.isPrivateProp_(names)) {
return null;
}

const pkg = names.join('.');
Expand Down
32 changes: 18 additions & 14 deletions test/fixtures/parse/private_static_property.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,46 @@ goog.p1.Private_ = function() {
};

/**
* Nested private class.
* A nested private class should not be provided.
* @constructor
* @private
*/
goog.p2.Private1_.Private2_.Private3_ = function() {
goog.p2.Private1_.Private2_ = function() {
};

/**
* Method of nested private class.
* A nested private class should not be provided.
* @constructor
* @private
*/
goog.p2.Private1_.Private2_.Private3_.prototype.hello = function() {
goog.p2.Private1_.Private2_.Private3_ = function() {
};

/**
* Static method of nested private class.
* A method of nested private class should not be provided.
*/
goog.p2.Private1_.Private2_.Private3_.publicStatic = function() {
goog.p2.Private1_.Private2_.Private3_.prototype.hello = function() {
};

/**
* Private static property should not be require or provide.
* @private
* A static method of nested private class should not be provided.
*/
goog.p3.privateProp_ = goog.debug.Logger.getLogger('aaa');
goog.p2.Private1_.Private2_.Private3_.publicStatic = function() {
};

/**
* Private static method should not be require or provide.
* Private static method should not be provided,
* but the parent namespace should be provided.
* @private
*/
goog.p4.privateMethod_ = function() {
goog.p3.privateMethod_ = function() {
};

/**
* To be provided
* Normal case. The parent namespace should be provided.
*/
goog.p5.hello = function() {
goog.p4.hello = function() {
};

// toProvide: goog.p5
// toProvide: goog.p3
// toProvide: goog.p4

0 comments on commit 5c6f4a6

Please sign in to comment.