Files
sdk/pkg/analyzer/lib/src/string_source.dart
T
Sam Rawlins 73ab31b98a analyzer: Move the Source class to be public API.
Deprecate accessing LineInfo, Source, or SourceRange via the
old library (pkg/analyzer/lib/src/generated/source.dart).
Migrate all SDK code to the new library.

Fixes https://github.com/dart-lang/sdk/issues/46420

Change-Id: Ic7c98a5820415c92a457f9fa2756351b05520cd1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335382
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-11-14 17:10:37 +00:00

61 lines
1.6 KiB
Dart

// Copyright (c) 2013, 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:analyzer/source/source.dart';
import 'package:analyzer/src/generated/engine.dart' show TimestampedData;
import 'package:path/path.dart' as pkg_path;
/// An implementation of [Source] that's based on an in-memory Dart string.
class StringSource extends Source {
/// The content of the source.
final String _contents;
@override
final String fullName;
@override
final Uri uri;
StringSource(this._contents, String? fullName, {Uri? uri})
: fullName = fullName ?? '/test.dart',
uri = _computeUri(uri, fullName);
@override
TimestampedData<String> get contents => TimestampedData(0, _contents);
@override
int get hashCode => _contents.hashCode ^ fullName.hashCode;
@override
String get shortName => fullName;
/// Return `true` if the given [object] is a string source that is equal to
/// this source.
@override
bool operator ==(Object object) {
return object is StringSource &&
object._contents == _contents &&
object.fullName == fullName;
}
@override
bool exists() => true;
@override
String toString() => 'StringSource ($fullName)';
static Uri _computeUri(Uri? uri, String? fullName) {
if (uri != null) {
return uri;
}
var isWindows = pkg_path.Style.platform == pkg_path.Style.windows;
if (isWindows) {
return pkg_path.toUri(r'C:\test.dart');
} else {
return pkg_path.toUri(r'/test.dart');
}
}
}