OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Code transform for @observable. The core transformation is relatively | 6 * Code transform for @observable. The core transformation is relatively |
7 * straightforward, and essentially like an editor refactoring. | 7 * straightforward, and essentially like an editor refactoring. |
8 */ | 8 */ |
9 library observe.transform; | 9 library observe.transform; |
10 | 10 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 span: _getSpan(file, cls)); | 131 span: _getSpan(file, cls)); |
132 } | 132 } |
133 | 133 |
134 // We'd like to track whether observable was declared explicitly, otherwise | 134 // We'd like to track whether observable was declared explicitly, otherwise |
135 // report a warning later below. Because we don't have type analysis (only | 135 // report a warning later below. Because we don't have type analysis (only |
136 // syntactic understanding of the code), we only report warnings that are | 136 // syntactic understanding of the code), we only report warnings that are |
137 // known to be true. | 137 // known to be true. |
138 var declaresObservable = false; | 138 var declaresObservable = false; |
139 if (cls.extendsClause != null) { | 139 if (cls.extendsClause != null) { |
140 var id = _getSimpleIdentifier(cls.extendsClause.superclass.name); | 140 var id = _getSimpleIdentifier(cls.extendsClause.superclass.name); |
141 if (id.name == 'ObservableBase') { | 141 if (id.name == 'Observable') { |
142 code.edit(id.offset, id.end, 'ChangeNotifierBase'); | 142 code.edit(id.offset, id.end, 'ChangeNotifier'); |
143 declaresObservable = true; | 143 declaresObservable = true; |
144 } else if (id.name == 'ChangeNotifierBase') { | 144 } else if (id.name == 'ChangeNotifier') { |
145 declaresObservable = true; | 145 declaresObservable = true; |
146 } else if (id.name != 'HtmlElement' && id.name != 'CustomElement' | 146 } else if (id.name != 'HtmlElement' && id.name != 'CustomElement' |
147 && id.name != 'Object') { | 147 && id.name != 'Object') { |
148 // TODO(sigmund): this is conservative, consider using type-resolution to | 148 // TODO(sigmund): this is conservative, consider using type-resolution to |
149 // improve this check. | 149 // improve this check. |
150 declaresObservable = true; | 150 declaresObservable = true; |
151 } | 151 } |
152 } | 152 } |
153 | 153 |
154 if (cls.withClause != null) { | 154 if (cls.withClause != null) { |
155 for (var type in cls.withClause.mixinTypes) { | 155 for (var type in cls.withClause.mixinTypes) { |
156 var id = _getSimpleIdentifier(type.name); | 156 var id = _getSimpleIdentifier(type.name); |
157 if (id.name == 'ObservableMixin') { | 157 if (id.name == 'Observable') { |
158 code.edit(id.offset, id.end, 'ChangeNotifierMixin'); | 158 code.edit(id.offset, id.end, 'ChangeNotifier'); |
159 declaresObservable = true; | 159 declaresObservable = true; |
160 break; | 160 break; |
161 } else if (id.name == 'ChangeNotifierMixin') { | 161 } else if (id.name == 'ChangeNotifier') { |
162 declaresObservable = true; | 162 declaresObservable = true; |
163 break; | 163 break; |
164 } else { | 164 } else { |
165 // TODO(sigmund): this is conservative, consider using type-resolution | 165 // TODO(sigmund): this is conservative, consider using type-resolution |
166 // to improve this check. | 166 // to improve this check. |
167 declaresObservable = true; | 167 declaresObservable = true; |
168 } | 168 } |
169 } | 169 } |
170 } | 170 } |
171 | 171 |
(...skipping 15 matching lines...) Expand all Loading... |
187 logger.warning('Static fields can no longer be observable. ' | 187 logger.warning('Static fields can no longer be observable. ' |
188 'Observable fields should be put in an observable objects.', | 188 'Observable fields should be put in an observable objects.', |
189 span: _getSpan(file, member)); | 189 span: _getSpan(file, member)); |
190 } | 190 } |
191 continue; | 191 continue; |
192 } | 192 } |
193 if (_hasObservable(member)) { | 193 if (_hasObservable(member)) { |
194 if (!declaresObservable) { | 194 if (!declaresObservable) { |
195 logger.warning('Observable fields should be put in an observable ' | 195 logger.warning('Observable fields should be put in an observable ' |
196 'objects. Please declare that this class extends from ' | 196 'objects. Please declare that this class extends from ' |
197 'ObservableBase, includes ObservableMixin, or implements ' | 197 'Observable, includes Observable, or implements ' |
198 'Observable.', | 198 'Observable.', |
199 span: _getSpan(file, member)); | 199 span: _getSpan(file, member)); |
200 } | 200 } |
201 _transformFields(file, member, code, logger); | 201 _transformFields(file, member, code, logger); |
202 | 202 |
203 var names = member.fields.variables.map((v) => v.name.name); | 203 var names = member.fields.variables.map((v) => v.name.name); |
204 | 204 |
205 getters.addAll(names); | 205 getters.addAll(names); |
206 if (!_isReadOnly(member.fields)) { | 206 if (!_isReadOnly(member.fields)) { |
207 setters.addAll(names); | 207 setters.addAll(names); |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 | 382 |
383 Token _findFieldSeperator(Token token) { | 383 Token _findFieldSeperator(Token token) { |
384 while (token != null) { | 384 while (token != null) { |
385 if (token.type == TokenType.COMMA || token.type == TokenType.SEMICOLON) { | 385 if (token.type == TokenType.COMMA || token.type == TokenType.SEMICOLON) { |
386 break; | 386 break; |
387 } | 387 } |
388 token = token.next; | 388 token = token.next; |
389 } | 389 } |
390 return token; | 390 return token; |
391 } | 391 } |
OLD | NEW |