Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/src/symbol_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class SymbolGenerator {

if (element is MethodElement) {
final className = element.enclosingElement?.name;
return '$namespace/$className#${element.name}().';
return '$namespace/$className#${_escapeIdentifier(element.name!)}().';
}

if (element is TopLevelFunctionElement || element is LocalFunctionElement) {
Expand Down Expand Up @@ -334,6 +334,13 @@ class SymbolGenerator {
return _localElementRegistry[ele]!;
}

/// Wraps [name] in backticks if it contains characters not allowed in a
/// scip simple-identifier (ex: operator methods like `==` or `[]`)
String _escapeIdentifier(String name) {
if (RegExp(r'^[\w+\-$]+$').hasMatch(name)) return name;
return '`${name.replaceAll('`', '``')}`';
}

String _escapeNamespacePath(String path) {
return path
.split('/')
Expand Down
66 changes: 66 additions & 0 deletions snapshots/input/basic-project/lib/async.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'dart:async';

Future<int> fetchValue() async {
await Future.delayed(Duration.zero);
return 42;
}

Stream<int> countTo(int n) async* {
for (var i = 1; i <= n; i++) {
yield i;
}
}

Iterable<int> evens(int max) sync* {
for (var i = 0; i < max; i += 2) {
yield i;
}
yield* [max];
}

class CustomException implements Exception {
final String message;
CustomException(this.message);
}

Future<void> main() async {
final value = await fetchValue();

await for (final i in countTo(value)) {
print(i);
}

final controller = StreamController<String>();
final subscription = controller.stream.listen(print);
controller.add('event');
await subscription.cancel();

try {
throw CustomException('boom');
} on CustomException catch (e, stackTrace) {
print('${e.message} $stackTrace');
} on FormatException {
rethrow;
} catch (e) {
print(e);
} finally {
print('done');
}

outer:
for (final i in evens(10)) {
var j = 0;
while (true) {
if (j > i) continue outer;
if (i > 6) break outer;
j++;
}
}

var k = 0;
do {
k++;
} while (k < 3);

assert(k == 3, 'k should be 3');
}
111 changes: 111 additions & 0 deletions snapshots/input/basic-project/lib/classes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
abstract interface class Shape {
double get area;
}

class Point {
final int x;
final int y;

const Point(this.x, this.y);

const Point.origin() : this(0, 0);

factory Point.fromList(List<int> list) = _ListPoint;

static int instances = 0;

static Point parse(String value) {
final parts = value.split(',');
return Point(int.parse(parts[0]), int.parse(parts[1]));
}
}

class _ListPoint extends Point {
_ListPoint(List<int> list) : super(list[0], list[1]);
}

class Rectangle implements Shape {
final double width;
final double height;
late final String description = 'Rectangle $width x $height';

Rectangle(this.width, this.height) : assert(width >= 0);

@override
double get area => width * height;
}

class Square extends Rectangle {
Square(double side) : super(side, side);
}

class ColoredSquare extends Square {
final String color;
ColoredSquare(super.side, {this.color = 'red'});
}

mixin class Walker {
void walk() => print('walking');
}

mixin Logger on Rectangle {
void log() => print('area: $area');
}

class LoggedRectangle extends Rectangle with Logger, Walker {
LoggedRectangle() : super(1, 2);
}

enum Planet implements Comparable<Planet> {
mercury(mass: 3.303e+23, radius: 2.4397e6),
earth(mass: 5.976e+24, radius: 6.37814e6);

const Planet({required this.mass, required this.radius});

final double mass;
final double radius;

static const gravitationalConstant = 6.67300E-11;

double get surfaceGravity => gravitationalConstant * mass / (radius * radius);

@override
int compareTo(Planet other) => mass.compareTo(other.mass);
}

String greet(String name, [String greeting = 'Hello', int? times]) =>
'$greeting $name ${times ?? 1}';

void main() {
const origin = Point.origin();
final fromList = Point.fromList([1, 2]);
Point.instances++;
Point.parse('1,2');
print([origin, fromList]);

final square = ColoredSquare(2, color: 'blue');
print(square.area);
print(square.description);

LoggedRectangle()
..log()
..walk();

Planet.earth.surfaceGravity;
Planet.values.byName('mercury');
Planet.earth.compareTo(Planet.mercury);
Planet.earth.index;
Planet.earth.name;

greet('Bob');
greet('Bob', 'Hi', 2);

// tear-offs
final greetFn = greet;
final parseFn = Point.parse;
final ctorFn = Point.new;
final namedCtorFn = Point.origin;
final methodFn = square.toString;
final listOf = List<int>.filled;
print([greetFn, parseFn, ctorFn, namedCtorFn, methodFn, listOf]);
}
29 changes: 29 additions & 0 deletions snapshots/input/basic-project/lib/extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
extension StringExtensions on String {
String get shout => toUpperCase();

bool get isBlank => trim().isEmpty;

String repeat(int times) => List.filled(times, this).join();

static String empty() => '';
}

extension<T> on List<T> {
T? get secondOrNull => length > 1 ? this[1] : null;
}

extension NumberParsing on String {
int parseInt() => int.parse(this);
}

void main() {
'hello'.shout;
' '.isBlank;
'ab'.repeat(3);
StringExtensions.empty();
[1, 2].secondOrNull;

// explicit extension application
StringExtensions('hi').repeat(2);
NumberParsing('42').parseInt();
}
49 changes: 49 additions & 0 deletions snapshots/input/basic-project/lib/generics.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Box<T> {
T value;
Box(this.value);

R map<R>(R Function(T value) transform) => transform(value);
}

class NumberBox<N extends num> extends Box<N> {
NumberBox(super.value);

N add(N other) => (value + other) as N;
}

abstract class Repository<K, V extends Object> {
V? find(K key);
void save(K key, V value);
}

class MemoryRepository<V extends Object> implements Repository<String, V> {
final Map<String, V> _store = {};

@override
V? find(String key) => _store[key];

@override
void save(String key, V value) => _store[key] = value;
}

T firstOf<T>(List<T> items) => items.first;

typedef Mapper<A, B> = B Function(A input);
typedef StringMap<V> = Map<String, V>;

void main() {
final box = Box<int>(1);
final str = box.map<String>((v) => '$v');
final numBox = NumberBox(2.5);
numBox.add(1.5);

final repo = MemoryRepository<Box<int>>();
repo.save('a', box);
repo.find('a');

firstOf<String>([str]);

Mapper<int, String> mapper = (input) => '$input';
StringMap<int> map = {'a': 1};
print([mapper(1), map]);
}
69 changes: 69 additions & 0 deletions snapshots/input/basic-project/lib/operators.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class Vector {
final int x;
final int y;

const Vector(this.x, this.y);

static const zero = Vector(0, 0);

Vector operator +(Vector other) => Vector(x + other.x, y + other.y);
Vector operator -() => Vector(-x, -y);
int operator [](int index) => index == 0 ? x : y;
bool operator <(Vector other) => length < other.length;

int get length => x * x + y * y;

@override
bool operator ==(Object other) =>
other is Vector && other.x == x && other.y == y;

@override
int get hashCode => Object.hash(x, y);
}

class Multiplier {
final int factor;
Multiplier(this.factor);

int call(int value) => value * factor;
}

Vector? maybeVector() => null;

int _counter = 0;
int get counter => _counter;
set counter(int value) => _counter = value;

void main() {
var a = const Vector(1, 2);
var b = Vector.zero;
var c = a + b;
c = -c;
print(c[0]);
print(a < b);
a += b;

final doubler = Multiplier(2);
doubler(4);

counter = 5;
counter++;
print(counter);

Vector? maybe = maybeVector();
maybe ??= a;
print(maybeVector()?.x ?? 0);
print(maybeVector()!.y);

final includeExtra = DateTime.now().isUtc;
final list = [
1,
...[2, 3],
...?null,
if (includeExtra) 4 else 5,
for (var i = 0; i < 3; i++) i * 10,
];
final set = {...list};
final map = {for (final v in set) v: '$v'};
print(map);
}
Loading
Loading