This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor stripFilePaths to better detect OK paths (#729)
- Loading branch information
Showing
6 changed files
with
87 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,48 @@ | ||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:path/path.dart' as path; | ||
|
||
/// Matches any path in the string and replaces the part of the path before the | ||
/// last separator with either dart:core, package:flutter, or removes it. | ||
/// Normalizes any "paths" from [text], replacing the segments before the last | ||
/// separator with either "dart:core" or "package:flutter", or removes them, | ||
/// according to their content. | ||
/// | ||
/// ## Examples: | ||
/// | ||
/// "Unused import: '/path/foo.dart'" -> "Unused import: 'foo.dart'" | ||
/// | ||
/// "Unused import: '/path/to/dart/lib/world.dart'" -> "Unused import: | ||
/// 'dart:core/world.dart'" | ||
/// "Unused import: '/path/to/dart/lib/core/world.dart'" -> | ||
/// "Unused import: 'dart:core/world.dart'" | ||
/// | ||
/// "Unused import: 'package:flutter/material.dart'" -> "Unused import: | ||
/// 'package:flutter/material.dart'" | ||
String stripFilePaths(String str) { | ||
// Match any URI. Also match URIs that are prefixed with dart:core or | ||
// package:* | ||
final regex = RegExp(r'(?:dart:core?)?(?:package:?)?[a-z]*\/\S*'); | ||
|
||
return str.replaceAllMapped(regex, (match) { | ||
final urlString = match.group(0); | ||
final pathComponents = path.split(urlString); | ||
final isDartPath = | ||
pathComponents.contains('lib') && pathComponents.contains('core'); | ||
|
||
// matches the 'flutter' package in the SDK | ||
final isFlutterPath = pathComponents.contains('flutter'); | ||
|
||
final isPackagePath = urlString.contains('package:'); | ||
final isDartCorePath = urlString.contains('dart:core'); | ||
final basename = path.basename(urlString); | ||
|
||
if (isFlutterPath) { | ||
return path.join('package:flutter', basename); | ||
/// "Unused import: 'package:flutter/material.dart'" -> | ||
/// "Unused import: 'package:flutter/material.dart'" | ||
String normalizeFilePaths(String text) { | ||
return text.replaceAllMapped(_possiblePathPattern, (match) { | ||
final possiblePath = match.group(0); | ||
|
||
final uri = Uri.tryParse(possiblePath); | ||
if (uri != null && uri.hasScheme) { | ||
return possiblePath; | ||
} | ||
|
||
if (isDartCorePath) { | ||
return urlString; | ||
final pathComponents = path.split(possiblePath); | ||
final basename = path.basename(possiblePath); | ||
|
||
if (pathComponents.contains('flutter')) { | ||
return path.join('package:flutter', basename); | ||
} | ||
|
||
if (isDartPath) { | ||
if (pathComponents.contains('lib') && pathComponents.contains('core')) { | ||
return path.join('dart:core', basename); | ||
} | ||
|
||
if (isPackagePath) { | ||
return urlString; | ||
} | ||
return basename; | ||
}); | ||
} | ||
|
||
/// A pattern which matches a possible path. | ||
/// | ||
/// This pattern is essentially "possibly some letters and colons, followed by a | ||
/// slash, followed by non-whitespace." | ||
final _possiblePathPattern = RegExp(r'[a-zA-Z:]*\/\S*'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,61 @@ | ||
library utils.tests; | ||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:dart_services/src/utils.dart'; | ||
import 'package:expected_output/expected_output.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('utils.stripFilePaths', () { | ||
for (final dataCase in dataCasesUnder(library: #utils.tests)) { | ||
test(dataCase.testDescription, () { | ||
final actualOutput = stripFilePaths(dataCase.input); | ||
expect(actualOutput, equals(dataCase.expectedOutput)); | ||
}); | ||
} | ||
void expectNormalizeFilePaths(String input, String output) { | ||
expect(normalizeFilePaths(input), equals(output)); | ||
} | ||
|
||
test('normalizeFilePaths strips a temporary directory path', () { | ||
expectNormalizeFilePaths( | ||
'List is defined in /var/folders/4p/y54w9nqj0_n6ryqwn7lxqz6800m6cw/T/DartAnalysisWrapperintLAw/main.dart', | ||
'List is defined in main.dart', | ||
); | ||
}); | ||
|
||
test('normalizeFilePaths replaces a SDK path with "dart:core"', () { | ||
expectNormalizeFilePaths( | ||
'List is defined in /path/dart/dart/sdk/lib/core/list.dart', | ||
'List is defined in dart:core/list.dart', | ||
); | ||
}); | ||
|
||
test('normalizeFilePaths replaces a specific SDK path with "dart:core"', () { | ||
expectNormalizeFilePaths( | ||
"The argument type 'List<int> (where List is defined in /Users/username/sdk/dart/2.10.5/lib/core/list.dart)' can't be assigned to the parameter type 'List<int> (where List is defined in /var/folders/4p/tmp/T/DartAnalysisWrapperintLAw/main.dart)'.", | ||
"The argument type 'List<int> (where List is defined in dart:core/list.dart)' can't be assigned to the parameter type 'List<int> (where List is defined in main.dart)'.", | ||
); | ||
}); | ||
|
||
test('normalizeFilePaths keeps a "package:" path intact', () { | ||
expectNormalizeFilePaths( | ||
"Unused import: 'package:flutter/material.dart'.", | ||
"Unused import: 'package:flutter/material.dart'.", | ||
); | ||
}); | ||
|
||
test('normalizeFilePaths keeps a "dart:core" path intact', () { | ||
expectNormalizeFilePaths( | ||
'dart:core/foo.dart', | ||
'dart:core/foo.dart', | ||
); | ||
}); | ||
|
||
test('normalizeFilePaths keeps a web URL intact', () { | ||
expectNormalizeFilePaths( | ||
'See http://dart.dev/go/non-promo-property', | ||
'See http://dart.dev/go/non-promo-property', | ||
); | ||
}); | ||
|
||
test('normalizeFilePaths strips a Flutter SDK path', () { | ||
expectNormalizeFilePaths( | ||
"The argument type 'StatelessWidget (where StatelessWidget is defined in /Users/username/path/to/dart-services/project_templates/flutter_project/main.dart)' can't be assigned to the parameter type 'StatelessWidget (where StatelessWidget is defined in /Users/username/path/to/dart-services/flutter-sdk/packages/flutter/lib/src/widgets/framework.dart)'.", | ||
"The argument type 'StatelessWidget (where StatelessWidget is defined in main.dart)' can't be assigned to the parameter type 'StatelessWidget (where StatelessWidget is defined in package:flutter/framework.dart)'.", | ||
); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.