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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ abstract interface class Declaration implements AstNode, Availability {
abstract final String id;
abstract final String name;
abstract final InputConfig? source;
abstract final int? lineNumber;
}

extension AsDeclaredType<T extends Declaration> on T {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class BuiltInDeclaration extends AstNode
@override
final String name;

@override
final int? lineNumber;

@override
InputConfig? get source => null;

Expand All @@ -26,7 +29,11 @@ class BuiltInDeclaration extends AstNode
@override
bool get hasObjCAnnotation => true;

const BuiltInDeclaration({required this.id, required this.name});
const BuiltInDeclaration({
required this.id,
required this.name,
this.lineNumber,
});

@override
void visit(Visitation visitation) => visitation.visitBuiltInDeclaration(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class ClassDeclaration extends AstNode
@override
List<AvailabilityInfo> availability;

@override
final int? lineNumber;

@override
List<PropertyDeclaration> properties;

Expand Down Expand Up @@ -76,6 +79,7 @@ class ClassDeclaration extends AstNode
required this.name,
required this.source,
required this.availability,
this.lineNumber,
this.properties = const [],
this.methods = const [],
this.nestingParent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class EnumDeclaration extends AstNode implements CompoundDeclaration {
@override
List<AvailabilityInfo> availability;

@override
final int? lineNumber;

List<EnumCaseDeclaration> cases;

@override
Expand Down Expand Up @@ -58,6 +61,7 @@ class EnumDeclaration extends AstNode implements CompoundDeclaration {
required this.name,
required this.source,
required this.availability,
this.lineNumber,
required this.cases,
required this.properties,
required this.methods,
Expand Down Expand Up @@ -100,13 +104,17 @@ class EnumCaseDeclaration extends AstNode
@override
List<AvailabilityInfo> availability;

@override
final int? lineNumber;

@override
List<EnumCaseParam> params;

EnumCaseDeclaration({
required this.id,
required this.name,
required this.source,
this.lineNumber,
required this.availability,
required this.params,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class InitializerDeclaration extends AstNode
@override
InputConfig? source;

@override
final int? lineNumber;

@override
List<AvailabilityInfo> availability;

Expand Down Expand Up @@ -61,6 +64,7 @@ class InitializerDeclaration extends AstNode
InitializerDeclaration({
required this.id,
required this.source,
this.lineNumber,
required this.availability,
required this.params,
this.statements = const [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class MethodDeclaration extends AstNode
@override
InputConfig? source;

@override
final int? lineNumber;

@override
List<AvailabilityInfo> availability;

Expand Down Expand Up @@ -65,6 +68,7 @@ class MethodDeclaration extends AstNode
required this.availability,
required this.returnType,
required this.params,
this.lineNumber,
this.typeParams = const [],
this.hasObjCAnnotation = false,
this.statements = const [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class PropertyDeclaration extends AstNode
@override
InputConfig? source;

@override
final int? lineNumber;

@override
List<AvailabilityInfo> availability;

Expand All @@ -45,6 +48,8 @@ class PropertyDeclaration extends AstNode

bool hasSetter;

bool hasExplicitGetter;

PropertyStatements? getter;
PropertyStatements? setter;

Expand All @@ -62,11 +67,13 @@ class PropertyDeclaration extends AstNode
required this.source,
required this.availability,
required this.type,
this.lineNumber,
this.hasSetter = false,
this.isConstant = false,
this.hasObjCAnnotation = false,
this.getter,
this.setter,
this.hasExplicitGetter = false,
this.isStatic = false,
this.throws = false,
this.async = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class ProtocolDeclaration extends AstNode implements CompoundDeclaration {
@override
List<AvailabilityInfo> availability;

@override
final int? lineNumber;

@override
List<PropertyDeclaration> properties;

Expand All @@ -51,6 +54,7 @@ class ProtocolDeclaration extends AstNode implements CompoundDeclaration {
required this.id,
required this.name,
required this.source,
this.lineNumber,
required this.availability,
required this.properties,
required this.methods,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class StructDeclaration extends AstNode implements CompoundDeclaration {
@override
List<AvailabilityInfo> availability;

@override
final int? lineNumber;

@override
List<PropertyDeclaration> properties;

Expand All @@ -52,6 +55,7 @@ class StructDeclaration extends AstNode implements CompoundDeclaration {
required this.id,
required this.name,
required this.source,
this.lineNumber,
required this.availability,
this.properties = const [],
this.methods = const [],
Expand Down
8 changes: 8 additions & 0 deletions pkgs/swift2objc/lib/src/ast/declarations/globals/globals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class GlobalFunctionDeclaration extends AstNode implements FunctionDeclaration {
@override
InputConfig? source;

@override
final int? lineNumber;

@override
List<AvailabilityInfo> availability;

Expand All @@ -55,6 +58,7 @@ class GlobalFunctionDeclaration extends AstNode implements FunctionDeclaration {
required this.id,
required this.name,
required this.source,
this.lineNumber,
required this.availability,
required this.params,
required this.returnType,
Expand Down Expand Up @@ -88,6 +92,9 @@ class GlobalVariableDeclaration extends AstNode implements VariableDeclaration {
@override
InputConfig? source;

@override
final int? lineNumber;

@override
List<AvailabilityInfo> availability;

Expand All @@ -107,6 +114,7 @@ class GlobalVariableDeclaration extends AstNode implements VariableDeclaration {
required this.id,
required this.name,
required this.source,
this.lineNumber,
required this.availability,
required this.type,
required this.isConstant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class TypealiasDeclaration extends AstNode implements InnerNestableDeclaration {
@override
InputConfig? source;

@override
final int? lineNumber;

@override
List<AvailabilityInfo> availability;

Expand All @@ -31,6 +34,7 @@ class TypealiasDeclaration extends AstNode implements InnerNestableDeclaration {
required this.id,
required this.name,
required this.source,
this.lineNumber,
required this.target,
required this.availability,
});
Expand Down
13 changes: 13 additions & 0 deletions pkgs/swift2objc/lib/src/parser/_core/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ String parseSymbolName(Json symbolJson) => symbolJson['declarationFragments']
.firstJsonWhereKey('kind', 'identifier')['spelling']
.get();

int? parseLineNumber(Json symbolJson) {
final locationJson = symbolJson['location'];
if (!locationJson.exists) return null;

final positionJson = locationJson['position'];
if (!positionJson.exists) return null;

final lineJson = positionJson['line'];
if (!lineJson.exists) return null;

return lineJson.get<int>();
}

bool parseSymbolHasObjcAnnotation(Json symbolJson) =>
symbolJson['declarationFragments'].any(
(json) => matchFragment(json, 'attribute', '@objc'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ MethodDeclaration parseMethodDeclaration(
id: parseSymbolId(symbol.json),
name: parseSymbolName(symbol.json),
source: symbol.source,
lineNumber: parseLineNumber(symbol.json),
availability: parseAvailability(symbol.json),
returnType: _parseFunctionReturnType(context, symbol.json, symbolgraph),
params: info.params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ InitializerDeclaration parseInitializerDeclaration(
return InitializerDeclaration(
id: id,
source: symbol.source,
lineNumber: parseLineNumber(symbol.json),
availability: parseAvailability(symbol.json),
params: info.params,
hasObjCAnnotation: parseSymbolHasObjcAnnotation(symbol.json),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ PropertyDeclaration parsePropertyDeclaration(
bool isStatic = false,
}) {
final info = parsePropertyInfo(symbol.json['declarationFragments']);

return PropertyDeclaration(
id: parseSymbolId(symbol.json),
name: parseSymbolName(symbol.json),
source: symbol.source,
lineNumber: parseLineNumber(symbol.json),
availability: parseAvailability(symbol.json),
type: _parseVariableType(context, symbol.json, symbolgraph),
hasObjCAnnotation: parseSymbolHasObjcAnnotation(symbol.json),
Expand All @@ -32,7 +34,8 @@ PropertyDeclaration parsePropertyDeclaration(
unowned: info.unowned,
weak: info.weak,
lazy: info.lazy,
hasSetter: info.constant ? false : info.setter,
hasSetter: info.constant ? false : (info.getter ? info.setter : true),
hasExplicitGetter: info.getter,
);
}

Expand All @@ -47,6 +50,7 @@ GlobalVariableDeclaration parseGlobalVariableDeclaration(
id: parseSymbolId(symbol.json),
name: parseSymbolName(symbol.json),
source: symbol.source,
lineNumber: parseLineNumber(symbol.json),
availability: parseAvailability(symbol.json),
type: _parseVariableType(context, symbol.json, symbolgraph),
isConstant: info.constant || !info.setter,
Expand Down Expand Up @@ -138,8 +142,8 @@ ParsedPropertyInfo parsePropertyInfo(Json json) {
'Properties can not have a setter without a getter',
);
} else {
// has implicit getter and implicit setter
return (true, true);
// Stored property - no explicit getter or setter
return (false, false);
}
}
}
14 changes: 13 additions & 1 deletion pkgs/swift2objc/lib/src/transformer/_core/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,17 @@ InitializerDeclaration buildWrapperInitializer(
}

extension SortById<T extends Declaration> on Iterable<T> {
List<T> sortedById() => toList()..sort((T a, T b) => a.id.compareTo(b.id));
List<T> sortedById() => toList()
..sort((T a, T b) {
// Sort by line number if both declarations have it
final aLine = a.lineNumber;
final bLine = b.lineNumber;

if (aLine != null && bLine != null) {
final lineCompare = aLine.compareTo(bLine);
if (lineCompare != 0) return lineCompare;
}

return a.id.compareTo(b.id);
});
}
Loading
Loading