dart-decimate 0.0.40 → 0.0.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +1 -1
- package/Cargo.toml +1 -1
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/dart_parser.rs +510 -47
- package/src/extract/references.rs +134 -1
- package/src/extract/tests.rs +71 -0
- package/src/extract.rs +4 -1
- package/src/symbols/index.rs +9 -0
- package/src/symbols/tests.rs +34 -0
- package/src/symbols.rs +2 -1
- package/src/widgets/unrendered.rs +325 -17
package/src/dart_parser.rs
CHANGED
|
@@ -53,8 +53,24 @@ pub(crate) fn parse_dart_source_strict<'source>(
|
|
|
53
53
|
path: &Path,
|
|
54
54
|
source: &'source str,
|
|
55
55
|
) -> Result<ParsedDart<'source>, DartParseError> {
|
|
56
|
+
if has_malformed_concise_constructor(source) {
|
|
57
|
+
return Err(DartParseError::Syntax {
|
|
58
|
+
path: path.to_path_buf(),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
56
62
|
let original = parse_raw(path, source)?;
|
|
57
63
|
if !original.root_node().has_error() {
|
|
64
|
+
let mut normalized = source.to_owned();
|
|
65
|
+
if normalize_concise_constructors(&mut normalized) {
|
|
66
|
+
let tree = parse_raw(path, &normalized)?;
|
|
67
|
+
if !tree.root_node().has_error() {
|
|
68
|
+
return Ok(ParsedDart {
|
|
69
|
+
tree,
|
|
70
|
+
source: Cow::Owned(normalized),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
58
74
|
return Ok(ParsedDart {
|
|
59
75
|
tree: original,
|
|
60
76
|
source: Cow::Borrowed(source),
|
|
@@ -86,6 +102,16 @@ pub(crate) fn parse_dart_source_lossy<'source>(
|
|
|
86
102
|
) -> Result<ParsedDart<'source>, DartParseError> {
|
|
87
103
|
let original = parse_raw(path, source)?;
|
|
88
104
|
if !original.root_node().has_error() {
|
|
105
|
+
let mut normalized = source.to_owned();
|
|
106
|
+
if normalize_concise_constructors(&mut normalized) {
|
|
107
|
+
let tree = parse_raw(path, &normalized)?;
|
|
108
|
+
if !tree.root_node().has_error() {
|
|
109
|
+
return Ok(ParsedDart {
|
|
110
|
+
tree,
|
|
111
|
+
source: Cow::Owned(normalized),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
89
115
|
return Ok(ParsedDart {
|
|
90
116
|
tree: original,
|
|
91
117
|
source: Cow::Borrowed(source),
|
|
@@ -135,9 +161,9 @@ fn normalize_concise_constructors(source: &mut String) -> bool {
|
|
|
135
161
|
let mut replacements = Vec::new();
|
|
136
162
|
let mut cursor = 0;
|
|
137
163
|
|
|
138
|
-
while let Some((keyword_start, keyword)) =
|
|
164
|
+
while let Some((keyword_start, keyword)) = find_next_class_like_header(source, cursor) {
|
|
139
165
|
let Some((class_name, body_start, body_end)) =
|
|
140
|
-
|
|
166
|
+
class_like_body(source, keyword_start + keyword.len(), keyword)
|
|
141
167
|
else {
|
|
142
168
|
cursor = keyword_start + keyword.len();
|
|
143
169
|
continue;
|
|
@@ -155,7 +181,32 @@ fn normalize_concise_constructors(source: &mut String) -> bool {
|
|
|
155
181
|
apply_text_replacements(source, replacements)
|
|
156
182
|
}
|
|
157
183
|
|
|
158
|
-
fn
|
|
184
|
+
fn has_malformed_concise_constructor(source: &str) -> bool {
|
|
185
|
+
let mut cursor = 0;
|
|
186
|
+
|
|
187
|
+
while let Some((keyword_start, keyword)) = find_next_class_like_header(source, cursor) {
|
|
188
|
+
let Some((_, body_start, body_end)) =
|
|
189
|
+
class_like_body(source, keyword_start + keyword.len(), keyword)
|
|
190
|
+
else {
|
|
191
|
+
cursor = keyword_start + keyword.len();
|
|
192
|
+
continue;
|
|
193
|
+
};
|
|
194
|
+
let mut member_cursor = body_start + 1;
|
|
195
|
+
while let Some((member_start, member_end)) =
|
|
196
|
+
next_class_like_member(source, member_cursor, body_end)
|
|
197
|
+
{
|
|
198
|
+
if malformed_concise_constructor_member(source, member_start, member_end) {
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
member_cursor = member_end;
|
|
202
|
+
}
|
|
203
|
+
cursor = body_end + 1;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
false
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
fn class_like_body(
|
|
159
210
|
source: &str,
|
|
160
211
|
cursor: usize,
|
|
161
212
|
keyword: &'static str,
|
|
@@ -174,6 +225,30 @@ fn class_body(
|
|
|
174
225
|
Some((name, body_start, body_end))
|
|
175
226
|
}
|
|
176
227
|
|
|
228
|
+
fn find_next_class_like_header(source: &str, start: usize) -> Option<(usize, &'static str)> {
|
|
229
|
+
let mut cursor = start;
|
|
230
|
+
while cursor < source.len() {
|
|
231
|
+
if let Some(after) = skip_non_code(source, cursor) {
|
|
232
|
+
cursor = after;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if starts_keyword(source, cursor, "class") {
|
|
236
|
+
return Some((cursor, "class"));
|
|
237
|
+
}
|
|
238
|
+
if starts_keyword(source, cursor, "enum") {
|
|
239
|
+
return Some((cursor, "enum"));
|
|
240
|
+
}
|
|
241
|
+
if starts_keyword(source, cursor, "extension") {
|
|
242
|
+
let type_start = skip_whitespace(source, cursor + "extension".len())?;
|
|
243
|
+
if starts_keyword(source, type_start, "type") {
|
|
244
|
+
return Some((type_start, "type"));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
cursor = next_char_boundary(source, cursor)?;
|
|
248
|
+
}
|
|
249
|
+
None
|
|
250
|
+
}
|
|
251
|
+
|
|
177
252
|
fn push_concise_constructor_replacements(
|
|
178
253
|
source: &str,
|
|
179
254
|
start: usize,
|
|
@@ -181,62 +256,244 @@ fn push_concise_constructor_replacements(
|
|
|
181
256
|
class_name: &str,
|
|
182
257
|
replacements: &mut Vec<(usize, usize, String)>,
|
|
183
258
|
) {
|
|
184
|
-
let bytes = source.as_bytes();
|
|
185
259
|
let mut cursor = start;
|
|
260
|
+
while let Some((member_start, member_end)) = next_class_like_member(source, cursor, end) {
|
|
261
|
+
if let Some((replacement_start, replacement_end, replacement)) =
|
|
262
|
+
concise_constructor_replacement(source, member_start, member_end, class_name)
|
|
263
|
+
{
|
|
264
|
+
replacements.push((replacement_start, replacement_end, replacement));
|
|
265
|
+
}
|
|
266
|
+
cursor = member_end;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
fn next_class_like_member(source: &str, start: usize, end: usize) -> Option<(usize, usize)> {
|
|
271
|
+
let member_start = skip_member_trivia(source, start, end);
|
|
272
|
+
if member_start >= end {
|
|
273
|
+
return None;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
let bytes = source.as_bytes();
|
|
277
|
+
let mut cursor = member_start;
|
|
278
|
+
let mut delimiters = Vec::new();
|
|
186
279
|
while cursor < end {
|
|
187
280
|
if let Some(after) = skip_non_code(source, cursor) {
|
|
188
281
|
cursor = after;
|
|
189
282
|
continue;
|
|
190
283
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
284
|
+
match bytes[cursor] {
|
|
285
|
+
b'{' => delimiters.push(b'}'),
|
|
286
|
+
b'(' => delimiters.push(b')'),
|
|
287
|
+
b'[' => delimiters.push(b']'),
|
|
288
|
+
b'}' | b')' | b']' => {
|
|
289
|
+
if delimiters.last().copied() == Some(bytes[cursor]) {
|
|
290
|
+
delimiters.pop();
|
|
291
|
+
if delimiters.is_empty() && bytes[cursor] == b'}' {
|
|
292
|
+
return Some((member_start, cursor + 1));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
200
295
|
}
|
|
296
|
+
b';' if delimiters.is_empty() => return Some((member_start, cursor + 1)),
|
|
297
|
+
_ => {}
|
|
201
298
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
299
|
+
cursor += 1;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
Some((member_start, end))
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
fn concise_constructor_replacement(
|
|
306
|
+
source: &str,
|
|
307
|
+
start: usize,
|
|
308
|
+
end: usize,
|
|
309
|
+
class_name: &str,
|
|
310
|
+
) -> Option<(usize, usize, String)> {
|
|
311
|
+
let mut cursor = skip_member_trivia(source, start, end);
|
|
312
|
+
cursor = skip_member_annotations(source, cursor, end)?;
|
|
313
|
+
|
|
314
|
+
while let Some(after) = skip_member_modifier(source, cursor, end) {
|
|
315
|
+
cursor = after;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
let (keyword_end, factory) = if starts_keyword(source, cursor, "factory") {
|
|
319
|
+
(cursor + "factory".len(), true)
|
|
320
|
+
} else if starts_keyword(source, cursor, "new") {
|
|
321
|
+
(cursor + "new".len(), false)
|
|
322
|
+
} else {
|
|
323
|
+
return None;
|
|
324
|
+
};
|
|
325
|
+
let (suffix, after_suffix) = concise_constructor_suffix(source, keyword_end, end)?;
|
|
326
|
+
let replacement = if factory {
|
|
327
|
+
format!("factory {class_name}{suffix}")
|
|
328
|
+
} else {
|
|
329
|
+
format!("{class_name}{suffix}")
|
|
330
|
+
};
|
|
331
|
+
Some((cursor, after_suffix, replacement))
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
fn skip_member_modifier(source: &str, cursor: usize, end: usize) -> Option<usize> {
|
|
335
|
+
for modifier in ["augment", "const", "external"] {
|
|
336
|
+
if starts_keyword(source, cursor, modifier) {
|
|
337
|
+
let after = cursor + modifier.len();
|
|
338
|
+
return (after <= end).then(|| skip_member_trivia(source, after, end));
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
None
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
fn malformed_concise_constructor_member(source: &str, start: usize, end: usize) -> bool {
|
|
345
|
+
let mut cursor = skip_member_trivia(source, start, end);
|
|
346
|
+
let Some(after_annotations) = skip_member_annotations(source, cursor, end) else {
|
|
347
|
+
return false;
|
|
348
|
+
};
|
|
349
|
+
cursor = after_annotations;
|
|
350
|
+
|
|
351
|
+
while let Some(after) = skip_member_modifier(source, cursor, end) {
|
|
352
|
+
cursor = after;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
let keyword_end = if starts_keyword(source, cursor, "factory") {
|
|
356
|
+
cursor + "factory".len()
|
|
357
|
+
} else if starts_keyword(source, cursor, "new") {
|
|
358
|
+
cursor + "new".len()
|
|
359
|
+
} else {
|
|
360
|
+
return false;
|
|
361
|
+
};
|
|
362
|
+
let suffix_start = skip_member_trivia(source, keyword_end, end);
|
|
363
|
+
if suffix_start >= end || source.as_bytes().get(suffix_start) == Some(&b'(') {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
let Some(suffix_end) = identifier_end(source, suffix_start) else {
|
|
367
|
+
return false;
|
|
368
|
+
};
|
|
369
|
+
let after_suffix = skip_member_trivia(source, suffix_end, end);
|
|
370
|
+
|
|
371
|
+
after_suffix < end
|
|
372
|
+
&& source.as_bytes().get(after_suffix) == Some(&b'(')
|
|
373
|
+
&& is_reserved_dart_word(&source[suffix_start..suffix_end])
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
fn is_reserved_dart_word(word: &str) -> bool {
|
|
377
|
+
matches!(
|
|
378
|
+
word,
|
|
379
|
+
"assert"
|
|
380
|
+
| "break"
|
|
381
|
+
| "case"
|
|
382
|
+
| "catch"
|
|
383
|
+
| "class"
|
|
384
|
+
| "const"
|
|
385
|
+
| "continue"
|
|
386
|
+
| "default"
|
|
387
|
+
| "do"
|
|
388
|
+
| "else"
|
|
389
|
+
| "enum"
|
|
390
|
+
| "extends"
|
|
391
|
+
| "false"
|
|
392
|
+
| "final"
|
|
393
|
+
| "finally"
|
|
394
|
+
| "for"
|
|
395
|
+
| "if"
|
|
396
|
+
| "in"
|
|
397
|
+
| "is"
|
|
398
|
+
| "new"
|
|
399
|
+
| "null"
|
|
400
|
+
| "rethrow"
|
|
401
|
+
| "return"
|
|
402
|
+
| "super"
|
|
403
|
+
| "switch"
|
|
404
|
+
| "this"
|
|
405
|
+
| "throw"
|
|
406
|
+
| "true"
|
|
407
|
+
| "try"
|
|
408
|
+
| "var"
|
|
409
|
+
| "void"
|
|
410
|
+
| "while"
|
|
411
|
+
| "with"
|
|
412
|
+
)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
fn skip_member_annotations(source: &str, mut cursor: usize, end: usize) -> Option<usize> {
|
|
416
|
+
loop {
|
|
417
|
+
cursor = skip_member_trivia(source, cursor, end);
|
|
418
|
+
if source.as_bytes().get(cursor).copied() != Some(b'@') {
|
|
419
|
+
return Some(cursor);
|
|
420
|
+
}
|
|
421
|
+
cursor += 1;
|
|
422
|
+
cursor = skip_member_trivia(source, cursor, end);
|
|
423
|
+
cursor = identifier_end(source, cursor)?;
|
|
424
|
+
loop {
|
|
425
|
+
cursor = skip_member_trivia(source, cursor, end);
|
|
426
|
+
if source.as_bytes().get(cursor).copied() != Some(b'.') {
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
cursor += 1;
|
|
430
|
+
cursor = skip_member_trivia(source, cursor, end);
|
|
431
|
+
cursor = identifier_end(source, cursor)?;
|
|
432
|
+
}
|
|
433
|
+
cursor = skip_member_trivia(source, cursor, end);
|
|
434
|
+
if source.as_bytes().get(cursor).copied() == Some(b'(') {
|
|
435
|
+
cursor = matching_delimiter(source, cursor, b'(', b')')? + 1;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
fn skip_member_trivia(source: &str, mut cursor: usize, end: usize) -> usize {
|
|
441
|
+
while cursor < end {
|
|
442
|
+
if source.as_bytes()[cursor].is_ascii_whitespace() {
|
|
443
|
+
cursor += 1;
|
|
207
444
|
continue;
|
|
208
445
|
}
|
|
209
|
-
if
|
|
210
|
-
|
|
446
|
+
if (source.as_bytes().get(cursor..cursor + 2) == Some(b"//")
|
|
447
|
+
|| source.as_bytes().get(cursor..cursor + 2) == Some(b"/*"))
|
|
448
|
+
&& let Some(after) = skip_non_code(source, cursor)
|
|
211
449
|
{
|
|
212
|
-
|
|
213
|
-
cursor,
|
|
214
|
-
after_suffix,
|
|
215
|
-
format!("factory {class_name}{suffix}"),
|
|
216
|
-
));
|
|
217
|
-
cursor = after_suffix;
|
|
450
|
+
cursor = after;
|
|
218
451
|
continue;
|
|
219
452
|
}
|
|
220
|
-
|
|
453
|
+
break;
|
|
221
454
|
}
|
|
455
|
+
cursor.min(end)
|
|
222
456
|
}
|
|
223
457
|
|
|
224
|
-
fn concise_constructor_suffix(source: &str, cursor: usize) -> Option<(String, usize)> {
|
|
225
|
-
let suffix_start =
|
|
458
|
+
fn concise_constructor_suffix(source: &str, cursor: usize, end: usize) -> Option<(String, usize)> {
|
|
459
|
+
let suffix_start = skip_member_trivia(source, cursor, end);
|
|
460
|
+
if suffix_start >= end {
|
|
461
|
+
return None;
|
|
462
|
+
}
|
|
226
463
|
if source.as_bytes().get(suffix_start).copied() == Some(b'(') {
|
|
227
|
-
return Some((
|
|
464
|
+
return Some((
|
|
465
|
+
preserved_line_breaks(&source[cursor..suffix_start]),
|
|
466
|
+
suffix_start,
|
|
467
|
+
));
|
|
228
468
|
}
|
|
229
469
|
let suffix_end = identifier_end(source, suffix_start)?;
|
|
230
|
-
|
|
470
|
+
if suffix_end > end {
|
|
471
|
+
return None;
|
|
472
|
+
}
|
|
473
|
+
let after_suffix = skip_member_trivia(source, suffix_end, end);
|
|
474
|
+
if after_suffix >= end {
|
|
475
|
+
return None;
|
|
476
|
+
}
|
|
231
477
|
if source.as_bytes().get(after_suffix).copied() != Some(b'(') {
|
|
232
478
|
return None;
|
|
233
479
|
}
|
|
234
480
|
Some((
|
|
235
|
-
format!(
|
|
481
|
+
format!(
|
|
482
|
+
"{}.{}{}",
|
|
483
|
+
preserved_line_breaks(&source[cursor..suffix_start]),
|
|
484
|
+
&source[suffix_start..suffix_end],
|
|
485
|
+
preserved_line_breaks(&source[suffix_end..after_suffix]),
|
|
486
|
+
),
|
|
236
487
|
after_suffix,
|
|
237
488
|
))
|
|
238
489
|
}
|
|
239
490
|
|
|
491
|
+
fn preserved_line_breaks(span: &str) -> String {
|
|
492
|
+
span.chars()
|
|
493
|
+
.filter(|character| matches!(character, '\n' | '\r'))
|
|
494
|
+
.collect()
|
|
495
|
+
}
|
|
496
|
+
|
|
240
497
|
fn normalize_primary_constructors(source: &str) -> Option<String> {
|
|
241
498
|
let mut replacements = Vec::new();
|
|
242
499
|
let mut cursor = 0;
|
|
@@ -455,7 +712,7 @@ fn normalize_dot_shorthands(source: &mut String) -> bool {
|
|
|
455
712
|
apply_text_replacements(source, replacements)
|
|
456
713
|
}
|
|
457
714
|
|
|
458
|
-
fn is_dot_shorthand_start(bytes: &[u8], cursor: usize) -> bool {
|
|
715
|
+
pub(crate) fn is_dot_shorthand_start(bytes: &[u8], cursor: usize) -> bool {
|
|
459
716
|
if cursor > 0 && bytes[cursor - 1] == b'?' {
|
|
460
717
|
return false;
|
|
461
718
|
}
|
|
@@ -474,7 +731,24 @@ fn is_dot_shorthand_start(bytes: &[u8], cursor: usize) -> bool {
|
|
|
474
731
|
matches!(
|
|
475
732
|
previous,
|
|
476
733
|
b'=' | b'(' | b'[' | b'{' | b',' | b':' | b'?' | b'!' | b'>' | b'|'
|
|
477
|
-
)
|
|
734
|
+
) || preceding_keyword(bytes, cursor).is_some_and(|keyword| {
|
|
735
|
+
matches!(
|
|
736
|
+
keyword,
|
|
737
|
+
b"await" | b"case" | b"const" | b"return" | b"throw" | b"yield"
|
|
738
|
+
)
|
|
739
|
+
})
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
fn preceding_keyword(bytes: &[u8], cursor: usize) -> Option<&[u8]> {
|
|
743
|
+
let mut end = cursor;
|
|
744
|
+
while end > 0 && bytes[end - 1].is_ascii_whitespace() {
|
|
745
|
+
end -= 1;
|
|
746
|
+
}
|
|
747
|
+
let mut start = end;
|
|
748
|
+
while start > 0 && (bytes[start - 1].is_ascii_alphanumeric() || bytes[start - 1] == b'_') {
|
|
749
|
+
start -= 1;
|
|
750
|
+
}
|
|
751
|
+
(start < end).then(|| &bytes[start..end])
|
|
478
752
|
}
|
|
479
753
|
|
|
480
754
|
fn normalize_null_aware_collection_elements(source: &mut String) -> bool {
|
|
@@ -790,7 +1064,9 @@ fn is_identifier_char(ch: char) -> bool {
|
|
|
790
1064
|
mod tests {
|
|
791
1065
|
use std::path::Path;
|
|
792
1066
|
|
|
793
|
-
use super::{
|
|
1067
|
+
use super::{
|
|
1068
|
+
DartParseError, normalize_modern_dart_compatibility, parse_dart_source_strict, parse_raw,
|
|
1069
|
+
};
|
|
794
1070
|
|
|
795
1071
|
#[test]
|
|
796
1072
|
fn strict_parse_normalizes_primary_constructor_headers() -> Result<(), DartParseError> {
|
|
@@ -847,30 +1123,97 @@ Widget build(Banner? banner, List<Widget>? extras) {
|
|
|
847
1123
|
#[test]
|
|
848
1124
|
fn strict_parse_normalizes_concise_constructor_forms() -> Result<(), DartParseError> {
|
|
849
1125
|
let source = r"
|
|
850
|
-
class
|
|
1126
|
+
class BodyDefault {
|
|
851
1127
|
new() {}
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
class BodyNamed {
|
|
1131
|
+
new named() {}
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
class ConstDefault {
|
|
1135
|
+
const new();
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
class ConstNamed {
|
|
1139
|
+
const new named();
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
class InitializerDefault {
|
|
1143
|
+
new() : this.other();
|
|
1144
|
+
InitializerDefault.other();
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
class InitializerNamed {
|
|
852
1148
|
new named() : this();
|
|
853
|
-
|
|
1149
|
+
InitializerNamed();
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
class ConstInitializerDefault {
|
|
1153
|
+
const new() : this.other();
|
|
1154
|
+
const ConstInitializerDefault.other();
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
class ConstInitializerNamed {
|
|
1158
|
+
const new named() : this();
|
|
1159
|
+
const ConstInitializerNamed();
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
class FactoryBodyDefault {
|
|
1163
|
+
factory() { return FactoryBodyDefault._(); }
|
|
1164
|
+
FactoryBodyDefault._();
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
class FactoryBodyNamed {
|
|
1168
|
+
factory named() { return FactoryBodyNamed._(); }
|
|
1169
|
+
FactoryBodyNamed._();
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
class FactoryRedirectDefault {
|
|
1173
|
+
factory() = FactoryRedirectDefault._;
|
|
1174
|
+
FactoryRedirectDefault._();
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
class FactoryRedirectNamed {
|
|
1178
|
+
factory named() = FactoryRedirectNamed._;
|
|
1179
|
+
FactoryRedirectNamed._();
|
|
854
1180
|
}
|
|
855
1181
|
|
|
856
|
-
class
|
|
857
|
-
factory()
|
|
858
|
-
|
|
859
|
-
factory redirect() = Child;
|
|
860
|
-
const factory cached() = Child.cached;
|
|
1182
|
+
class ConstFactoryRedirectDefault {
|
|
1183
|
+
const factory() = ConstFactoryRedirectDefault._;
|
|
1184
|
+
const ConstFactoryRedirectDefault._();
|
|
861
1185
|
}
|
|
862
1186
|
|
|
863
|
-
class
|
|
864
|
-
|
|
865
|
-
const
|
|
1187
|
+
class ConstFactoryRedirectNamed {
|
|
1188
|
+
const factory named() = ConstFactoryRedirectNamed._;
|
|
1189
|
+
const ConstFactoryRedirectNamed._();
|
|
866
1190
|
}
|
|
867
1191
|
";
|
|
868
1192
|
|
|
869
1193
|
let parsed = parse_dart_source_strict(Path::new("lib/constructors.dart"), source)?;
|
|
870
1194
|
|
|
871
1195
|
assert!(!parsed.tree().root_node().has_error());
|
|
872
|
-
|
|
873
|
-
|
|
1196
|
+
for expected in [
|
|
1197
|
+
"BodyDefault() {}",
|
|
1198
|
+
"BodyNamed.named() {}",
|
|
1199
|
+
"const ConstDefault();",
|
|
1200
|
+
"const ConstNamed.named();",
|
|
1201
|
+
"InitializerDefault() : this.other();",
|
|
1202
|
+
"InitializerNamed.named() : this();",
|
|
1203
|
+
"const ConstInitializerDefault() : this.other();",
|
|
1204
|
+
"const ConstInitializerNamed.named() : this();",
|
|
1205
|
+
"factory FactoryBodyDefault()",
|
|
1206
|
+
"factory FactoryBodyNamed.named()",
|
|
1207
|
+
"factory FactoryRedirectDefault() =",
|
|
1208
|
+
"factory FactoryRedirectNamed.named() =",
|
|
1209
|
+
"const factory ConstFactoryRedirectDefault() =",
|
|
1210
|
+
"const factory ConstFactoryRedirectNamed.named() =",
|
|
1211
|
+
] {
|
|
1212
|
+
assert!(
|
|
1213
|
+
parsed.source().contains(expected),
|
|
1214
|
+
"missing normalized constructor {expected:?}"
|
|
1215
|
+
);
|
|
1216
|
+
}
|
|
874
1217
|
|
|
875
1218
|
Ok(())
|
|
876
1219
|
}
|
|
@@ -898,6 +1241,31 @@ class Example {
|
|
|
898
1241
|
assert!(normalized.contains("Example() {}"));
|
|
899
1242
|
}
|
|
900
1243
|
|
|
1244
|
+
#[test]
|
|
1245
|
+
fn concise_constructor_normalization_accepts_intertoken_comments() {
|
|
1246
|
+
let source = r"
|
|
1247
|
+
class Commented {
|
|
1248
|
+
@deprecated
|
|
1249
|
+
const new /* before name */
|
|
1250
|
+
named // before parameters
|
|
1251
|
+
();
|
|
1252
|
+
factory /* before name */ create
|
|
1253
|
+
/* before parameters */ () => Commented.named();
|
|
1254
|
+
}
|
|
1255
|
+
";
|
|
1256
|
+
let Some(normalized) = normalize_modern_dart_compatibility(source) else {
|
|
1257
|
+
panic!("commented constructors were not normalized");
|
|
1258
|
+
};
|
|
1259
|
+
let Ok(tree) = parse_raw(Path::new("commented.dart"), &normalized) else {
|
|
1260
|
+
panic!("normalized commented constructors did not parse");
|
|
1261
|
+
};
|
|
1262
|
+
|
|
1263
|
+
assert!(!tree.root_node().has_error());
|
|
1264
|
+
assert!(normalized.contains("const Commented\n.named\n();"));
|
|
1265
|
+
assert!(normalized.contains("factory Commented.create\n()"));
|
|
1266
|
+
assert_eq!(normalized.lines().count(), source.lines().count());
|
|
1267
|
+
}
|
|
1268
|
+
|
|
901
1269
|
#[test]
|
|
902
1270
|
fn strict_parse_keeps_unrecoverable_syntax_errors() {
|
|
903
1271
|
let error = parse_dart_source_strict(Path::new("lib/bad.dart"), "class {")
|
|
@@ -909,4 +1277,99 @@ class Example {
|
|
|
909
1277
|
Some("Dart syntax errors found in lib/bad.dart")
|
|
910
1278
|
);
|
|
911
1279
|
}
|
|
1280
|
+
|
|
1281
|
+
#[test]
|
|
1282
|
+
fn strict_parse_normalizes_concise_constructors_even_when_raw_parse_is_clean()
|
|
1283
|
+
-> Result<(), DartParseError> {
|
|
1284
|
+
let source = "class Clean { new() {} }";
|
|
1285
|
+
let raw = parse_raw(Path::new("clean.dart"), source)?;
|
|
1286
|
+
assert!(!raw.root_node().has_error());
|
|
1287
|
+
|
|
1288
|
+
let strict = parse_dart_source_strict(Path::new("clean.dart"), source)?;
|
|
1289
|
+
assert!(!strict.tree().root_node().has_error());
|
|
1290
|
+
assert!(strict.source().contains("Clean()"));
|
|
1291
|
+
|
|
1292
|
+
let lossy = super::parse_dart_source_lossy(Path::new("clean.dart"), source)?;
|
|
1293
|
+
assert!(!lossy.tree().root_node().has_error());
|
|
1294
|
+
assert!(lossy.source().contains("Clean()"));
|
|
1295
|
+
Ok(())
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
#[test]
|
|
1299
|
+
fn concise_constructor_normalization_is_limited_to_top_level_members() {
|
|
1300
|
+
let source = r"
|
|
1301
|
+
class Owner {
|
|
1302
|
+
new() {}
|
|
1303
|
+
new named() {}
|
|
1304
|
+
const new cached();
|
|
1305
|
+
factory() {}
|
|
1306
|
+
factory namedFactory() {}
|
|
1307
|
+
const factory constFactory() = Target.cached;
|
|
1308
|
+
|
|
1309
|
+
final field = new Target();
|
|
1310
|
+
static final qualified = Target.new();
|
|
1311
|
+
void method() {
|
|
1312
|
+
final local = new Target();
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
mixin class MixinOwner {
|
|
1317
|
+
new() {}
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
enum Tone {
|
|
1321
|
+
quiet;
|
|
1322
|
+
new() {}
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
extension type UserId(int value) {
|
|
1326
|
+
new() : this(0);
|
|
1327
|
+
}
|
|
1328
|
+
";
|
|
1329
|
+
let Some(normalized) = normalize_modern_dart_compatibility(source) else {
|
|
1330
|
+
panic!("constructor source was not normalized");
|
|
1331
|
+
};
|
|
1332
|
+
let Ok(tree) = parse_raw(Path::new("normalized.dart"), &normalized) else {
|
|
1333
|
+
panic!("normalized source did not parse");
|
|
1334
|
+
};
|
|
1335
|
+
assert!(!tree.root_node().has_error());
|
|
1336
|
+
|
|
1337
|
+
for expected in [
|
|
1338
|
+
"Owner() {}",
|
|
1339
|
+
"Owner.named() {}",
|
|
1340
|
+
"const Owner.cached();",
|
|
1341
|
+
"factory Owner() {}",
|
|
1342
|
+
"factory Owner.namedFactory() {}",
|
|
1343
|
+
"const factory Owner.constFactory() = Target.cached;",
|
|
1344
|
+
"MixinOwner() {}",
|
|
1345
|
+
"Tone() {}",
|
|
1346
|
+
"UserId() : this(0);",
|
|
1347
|
+
] {
|
|
1348
|
+
assert!(
|
|
1349
|
+
normalized.contains(expected),
|
|
1350
|
+
"missing normalized {expected:?}"
|
|
1351
|
+
);
|
|
1352
|
+
}
|
|
1353
|
+
for untouched in [
|
|
1354
|
+
"final field = new Target();",
|
|
1355
|
+
"static final qualified = Target.new();",
|
|
1356
|
+
"final local = new Target();",
|
|
1357
|
+
] {
|
|
1358
|
+
assert!(normalized.contains(untouched), "changed {untouched:?}");
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
#[test]
|
|
1363
|
+
fn strict_parse_rejects_reserved_concise_constructor_names() {
|
|
1364
|
+
for source in [
|
|
1365
|
+
"class Invalid { new new() {} }",
|
|
1366
|
+
"class Invalid { factory class() = Invalid._; Invalid._(); }",
|
|
1367
|
+
"class Invalid { const new void(); }",
|
|
1368
|
+
] {
|
|
1369
|
+
assert!(matches!(
|
|
1370
|
+
parse_dart_source_strict(Path::new("lib/invalid.dart"), source),
|
|
1371
|
+
Err(DartParseError::Syntax { .. })
|
|
1372
|
+
));
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
912
1375
|
}
|