Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit a5827d7

Browse files
reduce member visibility in gents where possible (#919)
1 parent 0a5eadd commit a5827d7

13 files changed

+47
-48
lines changed

src/main/java/com/google/javascript/gents/CollectModuleMetadata.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public final class CollectModuleMetadata extends AbstractTopLevelCallback implem
2828
private final AbstractCompiler compiler;
2929
private final NameUtil nameUtil;
3030

31-
final Set<String> filesToConvert;
32-
final Map<String, FileModule> fileToModule = new LinkedHashMap<>();
33-
final Map<String, FileModule> namespaceToModule = new LinkedHashMap<>();
31+
private final Set<String> filesToConvert;
32+
private final Map<String, FileModule> fileToModule = new LinkedHashMap<>();
33+
private final Map<String, FileModule> namespaceToModule = new LinkedHashMap<>();
3434

3535
Map<String, FileModule> getFileMap() {
3636
return fileToModule;
@@ -57,8 +57,7 @@ Map<String, FileModule> getSymbolMap() {
5757
return out;
5858
}
5959

60-
public CollectModuleMetadata(
61-
AbstractCompiler compiler, NameUtil nameUtil, Set<String> filesToConvert) {
60+
CollectModuleMetadata(AbstractCompiler compiler, NameUtil nameUtil, Set<String> filesToConvert) {
6261
this.compiler = compiler;
6362
this.nameUtil = nameUtil;
6463
this.filesToConvert = filesToConvert;
@@ -152,7 +151,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
152151
}
153152

154153
/** Registers a goog.module namespace for future lookup. */
155-
void registerGoogModule(Node n, String file, String namespace) {
154+
private void registerGoogModule(Node n, String file, String namespace) {
156155
if (fileToModule.containsKey(file)) {
157156
compiler.report(
158157
JSError.make(

src/main/java/com/google/javascript/gents/CommentLinkingPass.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public final class CommentLinkingPass implements CompilerPass {
7777
private final Compiler compiler;
7878
private final NodeComments nodeComments;
7979

80-
public CommentLinkingPass(Compiler compiler) {
80+
CommentLinkingPass(Compiler compiler) {
8181
this.compiler = compiler;
8282
this.nodeComments = new NodeComments();
8383
}

src/main/java/com/google/javascript/gents/GentsCodeGenerator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class GentsCodeGenerator extends CodeGenerator {
1616
private final NodeComments nodeComments;
1717
private final Map<String, String> externsMap;
1818

19-
protected GentsCodeGenerator(
19+
GentsCodeGenerator(
2020
CodeConsumer consumer,
2121
CompilerOptions options,
2222
NodeComments nodeComments,
@@ -108,7 +108,7 @@ private boolean isPreviousEmptyAndHasComment(Node n) {
108108
*
109109
* @return true if no further code generation on this node is needed.
110110
*/
111-
boolean maybeOverrideCodeGen(Node n) {
111+
private boolean maybeOverrideCodeGen(Node n) {
112112
@Nullable Node parent = n.getParent();
113113
switch (n.getToken()) {
114114
case INDEX_SIGNATURE:

src/main/java/com/google/javascript/gents/ModuleConversionPass.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ public final class ModuleConversionPass implements CompilerPass {
6565
// full module imports.
6666
private final Table<String, String, Node> destructuringAssignments = HashBasedTable.create();
6767

68-
public Table<String, String, String> getTypeRewrite() {
68+
Table<String, String, String> getTypeRewrite() {
6969
return typeRewrite;
7070
}
7171

72-
public ModuleConversionPass(
72+
ModuleConversionPass(
7373
AbstractCompiler compiler,
7474
PathUtil pathUtil,
7575
NameUtil nameUtil,
@@ -331,7 +331,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
331331
}
332332

333333
/** A single statement containing {@code goog.require(...)}. */
334-
class ModuleImport {
334+
private class ModuleImport {
335335
/** require statement Node. */
336336
private Node originalImportNode;
337337

@@ -451,7 +451,7 @@ private boolean isFullModuleImport() {
451451
* import "./sideEffectsOnly"
452452
* </pre>
453453
*/
454-
void convertNonDestructuringRequireToImportStatements(Node n, ModuleImport moduleImport) {
454+
private void convertNonDestructuringRequireToImportStatements(Node n, ModuleImport moduleImport) {
455455
// The imported file is already in TS
456456
if (moduleImport.isAlreadyConverted()) {
457457
convertRequireForAlreadyConverted(moduleImport);
@@ -538,7 +538,7 @@ void convertNonDestructuringRequireToImportStatements(Node n, ModuleImport modul
538538
* import {A as localName, B} from "./valueExports";
539539
* </pre>
540540
*/
541-
void convertDestructuringRequireToImportStatements(Node n, ModuleImport moduleImport) {
541+
private void convertDestructuringRequireToImportStatements(Node n, ModuleImport moduleImport) {
542542
// The imported file is already in TS
543543
if (moduleImport.isAlreadyConverted()) {
544544
convertRequireForAlreadyConverted(moduleImport);
@@ -673,7 +673,7 @@ private void convertRequireForSideEffectOnlyImport(ModuleImport moduleImport) {
673673
* convertExportAssignment(pre.fix = ..., "pre.fix", "name") <-> export const name = ...
674674
* convertExportAssignment(pre.fix.foo = ..., "pre.fix", "name") <-> name.foo = ...
675675
*/
676-
void convertExportAssignment(
676+
private void convertExportAssignment(
677677
Node assign, String exportedNamespace, String exportedSymbol, String fileName) {
678678
checkState(assign.isAssign());
679679
checkState(assign.getParent().isExprResult());

src/main/java/com/google/javascript/gents/NameUtil.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import javax.annotation.Nullable;
1010

1111
/** Utility methods for variable naming. */
12-
public class NameUtil {
12+
class NameUtil {
1313
private AbstractCompiler compiler;
1414

15-
public NameUtil(AbstractCompiler compiler) {
15+
NameUtil(AbstractCompiler compiler) {
1616
this.compiler = compiler;
1717
}
1818

src/main/java/com/google/javascript/gents/NodeComments.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import java.util.Map;
66

77
/** Represents the mapping from an AST Node to its corresponding comment. */
8-
public class NodeComments {
9-
final Map<Node, String> nodeToComment = new HashMap<>();
8+
class NodeComments {
9+
private final Map<Node, String> nodeToComment = new HashMap<>();
1010

1111
void addComment(Node n, String comment) {
1212
if (hasComment(n)) {

src/main/java/com/google/javascript/gents/Options.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class Options {
5858
+ "Passing dependency files is disallowed when \"--dependenciesManifest\" option is used",
5959
metaVar = "DEPENDENCIES_MANIFEST"
6060
)
61-
String dependenciesManifest = null;
61+
private String dependenciesManifest = null;
6262

6363
@Option(
6464
name = "--sourcesManifest",
@@ -67,7 +67,7 @@ public class Options {
6767
+ "\"--convert\" option is disallowed when \"--sourcesManifest\" option is used",
6868
metaVar = "SOURCES_MANIFEST"
6969
)
70-
String sourcesManifest = null;
70+
private String sourcesManifest = null;
7171

7272
@Option(
7373
name = "--convert",
@@ -115,7 +115,7 @@ public class Options {
115115
Set<String> srcFiles = new LinkedHashSet<>();
116116
Map<String, String> externsMap = null;
117117

118-
public CompilerOptions getCompilerOptions() {
118+
CompilerOptions getCompilerOptions() {
119119
final CompilerOptions options = new CompilerOptions();
120120
options.setClosurePass(true);
121121

src/main/java/com/google/javascript/gents/PathUtil.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class PathUtil {
99
private final String rootpath;
1010
private final String absolutePrefix;
1111

12-
public PathUtil(String root, String absolutePrefix) {
12+
PathUtil(String root, String absolutePrefix) {
1313
this.rootpath = root;
1414
this.absolutePrefix = absolutePrefix;
1515
}

src/main/java/com/google/javascript/gents/RemoveGoogScopePass.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class RemoveGoogScopePass extends AbstractTopLevelCallback implemen
2626
private final Set<String> providedNamespaces = new HashSet<>();
2727
private final Map<String, String> aliasToProvidedNamespace = new HashMap<>();
2828

29-
public RemoveGoogScopePass(AbstractCompiler compiler) {
29+
RemoveGoogScopePass(AbstractCompiler compiler) {
3030
this.compiler = compiler;
3131
}
3232

src/main/java/com/google/javascript/gents/StyleFixPass.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public final class StyleFixPass extends AbstractPostOrderCallback implements Com
1818
private final AbstractCompiler compiler;
1919
private final NodeComments nodeComments;
2020

21-
public StyleFixPass(AbstractCompiler compiler, NodeComments nodeComments) {
21+
StyleFixPass(AbstractCompiler compiler, NodeComments nodeComments) {
2222
this.compiler = compiler;
2323
this.nodeComments = nodeComments;
2424
}
@@ -128,7 +128,7 @@ private boolean hasGrandchildren(Node n) {
128128
* Attempts to lift class or functions declarations of the form 'var/let/const x = class/function
129129
* {...}' into 'class/function x {...}'
130130
*/
131-
void liftClassOrFunctionDefinition(Node n) {
131+
private void liftClassOrFunctionDefinition(Node n) {
132132
Node rhs = n.getFirstFirstChild();
133133
Node oldName = rhs.getFirstChild();
134134
Node newName = n.getFirstChild();

src/main/java/com/google/javascript/gents/TypeAnnotationPass.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public final class TypeAnnotationPass implements CompilerPass {
6666
/** extern -> typing map for when extern and TS typing names differ */
6767
private final Map<String, String> externsMap;
6868

69-
public TypeAnnotationPass(
69+
TypeAnnotationPass(
7070
AbstractCompiler compiler,
7171
PathUtil pathUtil,
7272
NameUtil nameUtil,
@@ -345,7 +345,7 @@ private void setTypeExpression(Node n, @Nullable TypeDeclarationNode type) {
345345
}
346346

347347
@Nullable
348-
public TypeDeclarationNode convert(@Nullable JSTypeExpression typeExpr, boolean isReturnType) {
348+
private TypeDeclarationNode convert(@Nullable JSTypeExpression typeExpr, boolean isReturnType) {
349349
if (typeExpr == null) {
350350
return null;
351351
}
@@ -360,12 +360,12 @@ public TypeDeclarationNode convert(@Nullable JSTypeExpression typeExpr, boolean
360360
* @return the root node of a TypeDeclaration AST, or null if no type is available for the node.
361361
*/
362362
@Nullable
363-
public TypeDeclarationNode convertTypeNodeAST(Node n) {
363+
private TypeDeclarationNode convertTypeNodeAST(Node n) {
364364
return convertTypeNodeAST(n, false);
365365
}
366366

367367
@Nullable
368-
public TypeDeclarationNode convertTypeNodeAST(Node n, boolean isReturnType) {
368+
private TypeDeclarationNode convertTypeNodeAST(Node n, boolean isReturnType) {
369369
switch (n.getToken()) {
370370
// for function types that don't declare a return type
371371
// ex. /** @return */ var f = function() {};
@@ -523,7 +523,7 @@ public TypeDeclarationNode convertTypeNodeAST(Node n, boolean isReturnType) {
523523
}
524524

525525
/** Returns a new node representing an index signature type. */
526-
TypeDeclarationNode indexSignatureType(
526+
private TypeDeclarationNode indexSignatureType(
527527
TypeDeclarationNode keyType, TypeDeclarationNode valueType) {
528528
TypeDeclarationNode node = new TypeDeclarationNode(Token.INDEX_SIGNATURE);
529529
TypeDeclarationNode first = null;
@@ -535,7 +535,7 @@ TypeDeclarationNode indexSignatureType(
535535
}
536536

537537
/** Converts the global type name to the local type name. */
538-
String convertTypeName(String sourceFile, String typeName) {
538+
private String convertTypeName(String sourceFile, String typeName) {
539539
Map<String, String> rewriteMap =
540540
typeRewrite.containsRow(sourceFile)
541541
? typeRewrite.rowMap().get(sourceFile)
@@ -591,7 +591,7 @@ String convertTypeName(String sourceFile, String typeName) {
591591
* If an extern-to-typing map is provided, try to look up the extern type name and replace it with
592592
* the TypeScript version.
593593
*/
594-
String convertExternNameToTypingName(String externTypeName) {
594+
private String convertExternNameToTypingName(String externTypeName) {
595595
String typingName = this.externsMap.get(externTypeName);
596596
if (typingName != null) {
597597
return typingName;
@@ -601,7 +601,7 @@ String convertExternNameToTypingName(String externTypeName) {
601601
}
602602

603603
/** Helper function to recursively flatten union types. */
604-
void flatten(
604+
private void flatten(
605605
Iterable<TypeDeclarationNode> types, List<TypeDeclarationNode> result, boolean hasNull) {
606606
for (TypeDeclarationNode t : types) {
607607
switch (t.getToken()) {
@@ -632,7 +632,7 @@ void flatten(
632632
*
633633
* @return A flattened union type with at most 1 null.
634634
*/
635-
TypeDeclarationNode flatUnionType(Iterable<TypeDeclarationNode> types) {
635+
private TypeDeclarationNode flatUnionType(Iterable<TypeDeclarationNode> types) {
636636
List<TypeDeclarationNode> flatTypes = new ArrayList<>();
637637
flatten(types, flatTypes, false);
638638
return unionType(flatTypes);

src/main/java/com/google/javascript/gents/TypeConversionPass.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class TypeConversionPass implements CompilerPass {
4141

4242
private Map<String, String> typesToFilename;
4343

44-
public TypeConversionPass(
44+
TypeConversionPass(
4545
AbstractCompiler compiler, CollectModuleMetadata modulePrepass, NodeComments nodeComments) {
4646
this.compiler = compiler;
4747
this.modulePrepass = modulePrepass;
@@ -514,7 +514,7 @@ private void convertTypeAlias() {
514514
}
515515

516516
/** Converts @constructor annotated functions into class definitions. */
517-
void convertConstructorToClass(Node n, JSDocInfo jsDoc) {
517+
private void convertConstructorToClass(Node n, JSDocInfo jsDoc) {
518518
Preconditions.checkState(n.isFunction());
519519
Preconditions.checkState(n.getFirstChild().isName());
520520
Preconditions.checkState(n.getSecondChild().isParamList());
@@ -580,7 +580,7 @@ void convertConstructorToClass(Node n, JSDocInfo jsDoc) {
580580
}
581581

582582
/** Converts goog.defineClass calls into class definitions. */
583-
void convertDefineClassToClass(Node n) {
583+
private void convertDefineClassToClass(Node n) {
584584
Preconditions.checkState(n.isCall());
585585
Node superClass = n.getSecondChild();
586586
if (superClass.isNull()) {
@@ -614,7 +614,7 @@ void convertDefineClassToClass(Node n) {
614614
}
615615

616616
/** return if node n is a @constructor annotated function inside goog.defineClass */
617-
boolean isConstructorInGoogDefineClass(Node n) {
617+
private boolean isConstructorInGoogDefineClass(Node n) {
618618
// CALL
619619
// GETPROP
620620
// NAME goog
@@ -650,7 +650,7 @@ boolean isConstructorInGoogDefineClass(Node n) {
650650
* Converts functions and variables declared in object literals into member method and field
651651
* definitions
652652
*/
653-
void convertObjectLiteral(Node classMembers, Node objectLiteralMember, boolean isStatic) {
653+
private void convertObjectLiteral(Node classMembers, Node objectLiteralMember, boolean isStatic) {
654654
Preconditions.checkState(
655655
objectLiteralMember.isStringKey() || objectLiteralMember.isMemberFunctionDef());
656656

@@ -679,7 +679,7 @@ void convertObjectLiteral(Node classMembers, Node objectLiteralMember, boolean i
679679
* Attempts to move a method declaration into a class definition. This generates a new
680680
* MEMBER_FUNCTION_DEF Node while removing the old function node from the AST.
681681
*/
682-
void moveMethodsIntoClasses(ClassMemberDeclaration declaration) {
682+
private void moveMethodsIntoClasses(ClassMemberDeclaration declaration) {
683683
Node classMembers = declaration.classNode.getLastChild();
684684
String fieldName = declaration.memberName;
685685

@@ -709,7 +709,7 @@ void moveMethodsIntoClasses(ClassMemberDeclaration declaration) {
709709
compiler.reportChangeToEnclosingScope(memberFunc);
710710
}
711711

712-
Node createMemberVariableDef(ClassMemberDeclaration declaration) {
712+
private Node createMemberVariableDef(ClassMemberDeclaration declaration) {
713713
Node fieldNode = Node.newString(Token.MEMBER_VARIABLE_DEF, declaration.memberName);
714714
fieldNode.setJSDocInfo(declaration.jsDoc);
715715
fieldNode.setStaticMember(declaration.isStatic);
@@ -721,7 +721,7 @@ Node createMemberVariableDef(ClassMemberDeclaration declaration) {
721721
* Attempts to move a field declaration into a class definition. This generates a new
722722
* MEMBER_VARIABLE_DEF Node while persisting the old node in the AST.
723723
*/
724-
void moveFieldsIntoClasses(ClassMemberDeclaration declaration) {
724+
private void moveFieldsIntoClasses(ClassMemberDeclaration declaration) {
725725
Node classMembers = declaration.classNode.getLastChild();
726726

727727
Node fieldNode = createMemberVariableDef(declaration);
@@ -766,7 +766,7 @@ private boolean canPromoteFieldInitializer(ClassMemberDeclaration declaration) {
766766
* <p>This returns without any modification if the node is not an inheritance statement. This
767767
* fails by reporting an error when the node is an invalid inheritance statement.
768768
*/
769-
void maybeRemoveInherits(Node exprNode) {
769+
private void maybeRemoveInherits(Node exprNode) {
770770
Preconditions.checkState(exprNode.isExprResult());
771771
if (exprNode.getFirstChild().isCall()) {
772772
Node callNode = exprNode.getFirstChild();
@@ -831,7 +831,7 @@ void maybeRemoveInherits(Node exprNode) {
831831
*
832832
* <p>This returns without any modification if the node is not an superclass call statement.
833833
*/
834-
void maybeReplaceSuperCall(Node callNode) {
834+
private void maybeReplaceSuperCall(Node callNode) {
835835
Preconditions.checkState(callNode.isCall());
836836
String callName = callNode.getFirstChild().getQualifiedName();
837837

@@ -903,7 +903,7 @@ void maybeReplaceSuperCall(Node callNode) {
903903
}
904904

905905
/** Adds a field node before the first method node in classMembers */
906-
void addFieldToClassMembers(Node classMembers, Node field) {
906+
private void addFieldToClassMembers(Node classMembers, Node field) {
907907
for (Node n : classMembers.children()) {
908908
if (n.isMemberFunctionDef()) {
909909
classMembers.addChildBefore(field, n);
@@ -918,7 +918,7 @@ void addFieldToClassMembers(Node classMembers, Node field) {
918918
*
919919
* <p>This determines the classname using the nearest available name node.
920920
*/
921-
void addClassToScope(Node n) {
921+
private void addClassToScope(Node n) {
922922
Preconditions.checkState(n.isClass());
923923
String className = NodeUtil.getName(n);
924924
if (className == null) {

src/main/java/com/google/javascript/gents/TypeScriptGenerator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static void main(String[] args) {
9090
private final Compiler compiler;
9191

9292
final PathUtil pathUtil;
93-
final NameUtil nameUtil;
93+
private final NameUtil nameUtil;
9494
private GentsErrorManager errorManager;
9595

9696
TypeScriptGenerator(Options opts) {

0 commit comments

Comments
 (0)