Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: lib/src/js/js_types.dart

Issue 1676463002: Type annotations instead of closure comments. (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: merged master Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/js/js_ast.dart ('k') | lib/src/js/nodes.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of js_ast;
6
7 final _any = new AnyTypeRef._();
8 final _unknown = new UnknownTypeRef._();
9 final _null = new NullTypeRef();
10
11 /// JavaScript type reference, designed to support a subset of the type systems
12 /// of the Closure Compiler and TypeScript:
13 /// - https://developers.google.com/closure/compiler/docs/js-for-compiler#types
14 /// - https://github.com/Microsoft/TypeScript/blob/v1.8.0-beta/doc/spec.md#3
15 ///
16 /// Note that some subtleties like "nullability" or "optionality" are handled
17 /// using unions (with a [NullTypeRef] or with an "undefined" named typeref).
18 /// Also, primitives aren't modeled differently than named / qualified types,
19 /// as it brings little value for now. Primitive-specific type formatting is
20 /// handled by the type printers (for instance, the knowledge that
21 /// `number|null` is just `number` in TypeScript, and is `number?` in Closure).
22 abstract class TypeRef extends Expression {
23
24 int get precedenceLevel => PRIMARY;
25
26 TypeRef();
27
28 factory TypeRef.any() => _any;
29
30 factory TypeRef.void_() => new TypeRef.named('void');
31
32 factory TypeRef.unknown() => _unknown;
33
34 factory TypeRef.generic(TypeRef rawType, Iterable<TypeRef> typeArgs) {
35 if (typeArgs.isEmpty) {
36 throw new ArgumentError.value(typeArgs, "typeArgs", "is empty");
37 }
38 return new GenericTypeRef(rawType, typeArgs.toList());
39 }
40
41 factory TypeRef.array([TypeRef elementType]) =>
42 new ArrayTypeRef(elementType);
43
44 factory TypeRef.object([TypeRef keyType, TypeRef valueType]) {
45 // TODO(ochafik): Roll out a dedicated ObjectTypeRef?
46 var rawType = new TypeRef.named('Object');
47 return keyType == null && valueType == null
48 ? rawType
49 : new GenericTypeRef(rawType, [keyType ?? _any, valueType ?? _any]);
50 }
51
52 factory TypeRef.function(
53 [TypeRef returnType, Map<Identifier, TypeRef> paramTypes]) =>
54 new FunctionTypeRef(returnType, paramTypes);
55
56 factory TypeRef.record(Map<Identifier, TypeRef> types) =>
57 new RecordTypeRef(types);
58
59 factory TypeRef.string() => new TypeRef.named('string');
60
61 factory TypeRef.number() => new TypeRef.named('number');
62
63 factory TypeRef.undefined() => new TypeRef.named('undefined');
64
65 factory TypeRef.boolean() => new TypeRef.named('boolean');
66
67 factory TypeRef.qualified(List<Identifier> path) =>
68 new QualifiedTypeRef(path);
69
70 factory TypeRef.named(String name) =>
71 new TypeRef.qualified(<Identifier>[new Identifier(name)]);
72
73 bool get isAny => this is AnyTypeRef;
74 bool get isUnknown => this is UnknownTypeRef;
75 bool get isNull => this is NullTypeRef;
76
77 TypeRef or(TypeRef other) => new UnionTypeRef([this, other]);
78
79 TypeRef orUndefined() => or(new TypeRef.undefined());
80 TypeRef orNull() => or(_null);
81
82 TypeRef toOptional() =>
83 new OptionalTypeRef(this);
84 }
85
86 class AnyTypeRef extends TypeRef {
87 AnyTypeRef._() : super();
88
89 factory AnyTypeRef() => _any;
90 accept(NodeVisitor visitor) => visitor.visitAnyTypeRef(this);
91 void visitChildren(NodeVisitor visitor) {}
92 _clone() => new AnyTypeRef();
93 }
94
95 class NullTypeRef extends QualifiedTypeRef {
96 NullTypeRef() : super([new Identifier("null")]);
97 _clone() => new NullTypeRef();
98 }
99
100 class UnknownTypeRef extends TypeRef {
101 UnknownTypeRef._() : super();
102
103 factory UnknownTypeRef() => _unknown;
104 accept(NodeVisitor visitor) => visitor.visitUnknownTypeRef(this);
105 void visitChildren(NodeVisitor visitor) {}
106 _clone() => new UnknownTypeRef();
107 }
108
109 class QualifiedTypeRef extends TypeRef {
110 final List<Identifier> path;
111 QualifiedTypeRef(this.path);
112
113 accept(NodeVisitor visitor) => visitor.visitQualifiedTypeRef(this);
114 void visitChildren(NodeVisitor visitor) =>
115 path.forEach((p) => p.accept(visitor));
116 _clone() => new QualifiedTypeRef(path);
117 }
118
119 class ArrayTypeRef extends TypeRef {
120 final TypeRef elementType;
121 ArrayTypeRef(this.elementType);
122 accept(NodeVisitor visitor) => visitor.visitArrayTypeRef(this);
123 void visitChildren(NodeVisitor visitor) {
124 elementType.accept(visitor);
125 }
126 _clone() => new ArrayTypeRef(elementType);
127 }
128
129 class GenericTypeRef extends TypeRef {
130 final TypeRef rawType;
131 final List<TypeRef> typeArgs;
132 GenericTypeRef(this.rawType, this.typeArgs);
133
134 accept(NodeVisitor visitor) => visitor.visitGenericTypeRef(this);
135 void visitChildren(NodeVisitor visitor) {
136 rawType.accept(visitor);
137 typeArgs.forEach((p) => p.accept(visitor));
138 }
139 _clone() => new GenericTypeRef(rawType, typeArgs);
140 }
141
142 class UnionTypeRef extends TypeRef {
143 final List<TypeRef> types;
144 UnionTypeRef(this.types);
145
146 accept(NodeVisitor visitor) => visitor.visitUnionTypeRef(this);
147 void visitChildren(NodeVisitor visitor) {
148 types.forEach((p) => p.accept(visitor));
149 }
150 _clone() => new UnionTypeRef(types);
151
152 @override
153 TypeRef or(TypeRef other) {
154 if (types.contains(other)) return this;
155 return new UnionTypeRef([]..addAll(types)..add(other));
156 }
157 }
158
159 class OptionalTypeRef extends TypeRef {
160 final TypeRef type;
161 OptionalTypeRef(this.type);
162
163 accept(NodeVisitor visitor) => visitor.visitOptionalTypeRef(this);
164 void visitChildren(NodeVisitor visitor) {
165 type.accept(visitor);
166 }
167 _clone() => new OptionalTypeRef(type);
168
169 @override
170 TypeRef orUndefined() => this;
171 }
172
173 class RecordTypeRef extends TypeRef {
174 final Map<Identifier, TypeRef> types;
175 RecordTypeRef(this.types);
176
177 accept(NodeVisitor visitor) => visitor.visitRecordTypeRef(this);
178 void visitChildren(NodeVisitor visitor) {
179 types.values.forEach((p) => p.accept(visitor));
180 }
181 _clone() => new RecordTypeRef(types);
182 }
183
184 class FunctionTypeRef extends TypeRef {
185 final TypeRef returnType;
186 final Map<Identifier, TypeRef> paramTypes;
187 FunctionTypeRef(this.returnType, this.paramTypes);
188
189 accept(NodeVisitor visitor) => visitor.visitFunctionTypeRef(this);
190 void visitChildren(NodeVisitor visitor) {
191 returnType.accept(visitor);
192 paramTypes.forEach((n, t) {
193 n.accept(visitor);
194 t.accept(visitor);
195 });
196 }
197 _clone() => new FunctionTypeRef(returnType, paramTypes);
198 }
OLDNEW
« no previous file with comments | « lib/src/js/js_ast.dart ('k') | lib/src/js/nodes.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698