Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NTI] Add a ToStringContext for adapting how JSTypes are stringified.
This addresses two problems at once: 1. Issue #2499 where type variables are ambiguated, making warnings difficult to understand 2. Migrating `TypedCodeGenerator` to NTI requires printing types in a "for annotations" mode Rather than add various booleans, the context object allows plumbing any configuration all the way through the recursive stringify operation, and is extensible if further configuration is needed later. Moreover, context instances may be passed to multiple stringify operations (i.e. to ensure typevars are printed consistently throughout an entire message). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=156936000
- Loading branch information
Showing
7 changed files
with
101 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/com/google/javascript/jscomp/newtypes/ToStringContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,47 @@ | |||
/* | |||
* Copyright 2017 The Closure Compiler Authors. | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
|
|||
package com.google.javascript.jscomp.newtypes; | |||
|
|||
/** | |||
* Configuration options for {@link JSType#toString}. This object is | |||
* passed around to all internal types. | |||
*/ | |||
public class ToStringContext { | |||
|
|||
/** The default context for toString(). */ | |||
static final ToStringContext TO_STRING = new ToStringContext(); | |||
|
|||
/** The default context for toAnnotationString(). */ | |||
static final ToStringContext FOR_ANNOTATION = | |||
new ToStringContext() { | |||
@Override | |||
boolean forAnnotation() { | |||
return true; | |||
} | |||
}; | |||
|
|||
// Nobody else should be making instances. | |||
private ToStringContext() {} | |||
|
|||
String formatTypeVar(String typeVar) { | |||
return UniqueNameGenerator.getOriginalName(typeVar); | |||
} | |||
|
|||
boolean forAnnotation() { | |||
return false; | |||
} | |||
} |