|
| 1 | +/* |
| 2 | + * Copyright 2025 The Closure Compiler Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.javascript.jscomp.parsing.parser; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | +import static com.google.common.base.Preconditions.checkState; |
| 21 | + |
| 22 | +import com.google.javascript.jscomp.parsing.parser.trees.ParseTree; |
| 23 | +import com.google.javascript.jscomp.parsing.parser.util.SourcePosition; |
| 24 | + |
| 25 | +/** |
| 26 | + * Gathers information for ES6 class / Object literal methods, getters, setters and fields. |
| 27 | + * |
| 28 | + * <p>Gathers: the start position, whether it is a class element (false for object literal), whether |
| 29 | + * it is static (only for class elements), and, after construction, the name IdentifierToken or |
| 30 | + * ParseTree. |
| 31 | + * |
| 32 | + * <p>Used by {@link Parser} but separated out to enforce visibility. |
| 33 | + */ |
| 34 | +class ClassOrObjectElementInfo { |
| 35 | + /** The start position of the element. */ |
| 36 | + final SourcePosition start; |
| 37 | + |
| 38 | + /** Whether this is a class member. False for object literal elements. */ |
| 39 | + final boolean isClassMember; |
| 40 | + |
| 41 | + /** Whether this class element is static. Can only be true if {@link #isClassMember} is true. */ |
| 42 | + final boolean isStatic; |
| 43 | + |
| 44 | + private IdentifierToken name; |
| 45 | + private ParseTree nameExpr; |
| 46 | + |
| 47 | + private ClassOrObjectElementInfo(SourcePosition start, boolean isClassMember, boolean isStatic) { |
| 48 | + this.start = start; |
| 49 | + this.isClassMember = isClassMember; |
| 50 | + this.isStatic = isStatic; |
| 51 | + } |
| 52 | + |
| 53 | + static ClassOrObjectElementInfo createClassMemberInfo(SourcePosition start, boolean isStatic) { |
| 54 | + return new ClassOrObjectElementInfo(start, /* isClassMember= */ true, isStatic); |
| 55 | + } |
| 56 | + |
| 57 | + static ClassOrObjectElementInfo createObjectLiteralElementInfo(SourcePosition start) { |
| 58 | + return new ClassOrObjectElementInfo(start, /* isClassMember= */ false, /* isStatic= */ false); |
| 59 | + } |
| 60 | + |
| 61 | + void setName(IdentifierToken name) { |
| 62 | + checkState(nameExpr == null); |
| 63 | + this.name = name; |
| 64 | + } |
| 65 | + |
| 66 | + boolean hasName() { |
| 67 | + return name != null; |
| 68 | + } |
| 69 | + |
| 70 | + IdentifierToken getName() { |
| 71 | + return checkNotNull(name); |
| 72 | + } |
| 73 | + |
| 74 | + void setNameExpr(ParseTree nameExpr) { |
| 75 | + checkState(name == null); |
| 76 | + this.nameExpr = nameExpr; |
| 77 | + } |
| 78 | + |
| 79 | + boolean hasNameExpr() { |
| 80 | + return nameExpr != null; |
| 81 | + } |
| 82 | + |
| 83 | + ParseTree getNameExpr() { |
| 84 | + return checkNotNull(nameExpr); |
| 85 | + } |
| 86 | +} |
0 commit comments