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
16 changes: 14 additions & 2 deletions web_generator/lib/src/ast/builtin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ class BuiltinType extends NamedType {
@override
bool isNullable;

/// This denotes a type that has a discardable result
///
/// These are for types such as `void` or `never`
bool discardable;

BuiltinType(
{required this.name,
this.typeParams = const [],
this.fromDartJSInterop = false,
bool? isNullable})
bool? isNullable,
this.discardable = false})
: isNullable = isNullable ?? false;

@override
Expand Down Expand Up @@ -82,6 +88,11 @@ class BuiltinType extends NamedType {
PrimitiveType.any => (isNullable ?? false)
? anyType
: BuiltinType(name: 'JSAny', fromDartJSInterop: true),
PrimitiveType.never => BuiltinType(
name: 'JSAny',
fromDartJSInterop: true,
discardable: true,
isNullable: true),
PrimitiveType.unknown => anyType,
PrimitiveType.object => BuiltinType(
name: 'JSObject', fromDartJSInterop: true, isNullable: isNullable),
Expand Down Expand Up @@ -185,5 +196,6 @@ enum PrimitiveType {
undefined,
symbol,
array,
bigint
bigint,
never
}
38 changes: 33 additions & 5 deletions web_generator/lib/src/ast/declarations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ class VariableDeclaration extends FieldDeclaration
if (modifier == VariableModifier.$const) {
return Method((m) => m
..docs.addAll([...doc])
..annotations.addAll([...annotations])
..annotations.addAll([
...annotations,
if (_checkIfDiscardable(type))
refer('doNotStore', 'package:meta/meta.dart')
])
..name = name
..type = MethodType.getter
..annotations.add(generateJSAnnotation())
Expand Down Expand Up @@ -226,6 +230,14 @@ class VariableDeclaration extends FieldDeclaration
}
}

bool _checkIfDiscardable(Type type) {
if (type case BuiltinType(discardable: final typeIsDiscardable)
when typeIsDiscardable) {
return true;
}
return false;
}

enum VariableModifier { let, $const, $var }

class FunctionDeclaration extends CallableDeclaration
Expand Down Expand Up @@ -274,7 +286,11 @@ class FunctionDeclaration extends CallableDeclaration

return Method((m) => m
..docs.addAll([...doc])
..annotations.addAll([...annotations])
..annotations.addAll([
...annotations,
if (_checkIfDiscardable(returnType))
refer('doNotStore', 'package:meta/meta.dart')
])
..external = true
..name = dartName ?? name
..annotations.add(generateJSAnnotation(
Expand Down Expand Up @@ -750,7 +766,11 @@ class PropertyDeclaration extends FieldDeclaration
if (readonly) {
return Method((m) => m
..docs.addAll([...doc])
..annotations.addAll([...annotations])
..annotations.addAll([
...annotations,
if (_checkIfDiscardable(type))
refer('doNotStore', 'package:meta/meta.dart')
])
..external = true
..name = dartName ?? name
..type = MethodType.getter
Expand Down Expand Up @@ -836,7 +856,11 @@ class MethodDeclaration extends CallableDeclaration
if (isNullable) {
return Method((m) => m
..docs.addAll([...doc])
..annotations.addAll([...annotations])
..annotations.addAll([
...annotations,
if (_checkIfDiscardable(returnType))
refer('doNotStore', 'package:meta/meta.dart')
])
..external = true
..name = dartName ?? name
..type = MethodType.getter
Expand Down Expand Up @@ -1015,7 +1039,11 @@ class OperatorDeclaration extends CallableDeclaration

return Method((m) => m
..docs.addAll([...doc])
..annotations.addAll([...annotations])
..annotations.addAll([
...annotations,
if (_checkIfDiscardable(returnType))
refer('doNotStore', 'package:meta/meta.dart')
])
..external = true
..name = 'operator $name'
..types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,7 @@ class Transformer {
TSSyntaxKind.VoidKeyword => PrimitiveType.$void,
TSSyntaxKind.BigIntKeyword => PrimitiveType.bigint,
TSSyntaxKind.SymbolKeyword => PrimitiveType.symbol,
TSSyntaxKind.NeverKeyword => PrimitiveType.never,
_ => throw UnsupportedError(
'The given type with kind ${type.kind} is not supported yet')
};
Expand Down
1 change: 1 addition & 0 deletions web_generator/lib/src/js/typescript.types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ extension type const TSSyntaxKind._(num _) {
static const TSSyntaxKind VoidKeyword = TSSyntaxKind._(116);
static const TSSyntaxKind BigIntKeyword = TSSyntaxKind._(163);
static const TSSyntaxKind SymbolKeyword = TSSyntaxKind._(155);
static const TSSyntaxKind NeverKeyword = TSSyntaxKind._(146);

// types
static const TSSyntaxKind UnionType = TSSyntaxKind._(192);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:js_interop' as _i1;

import 'package:meta/meta.dart' as _i2;

@_i1.JS()
external String greetUser(String name);
@_i1.JS()
Expand Down Expand Up @@ -38,8 +40,12 @@ external _i1.JSObject createUser(
]);
@_i1.JS()
external T firstElement<T extends _i1.JSAny?>(_i1.JSArray<T> arr);
@_i2.doNotStore
@_i1.JS()
external void throwError(String msg);
external _i1.JSAny? throwError([String? msg]);
@_i2.doNotStore
@_i1.JS('throwError')
external _i1.JSAny? throwError$1();
@_i1.JS()
external _i1.JSArray<T> wrapInArray<T extends _i1.JSAny?>(T value);
@_i1.JS()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ export declare function greetUser(name: string): string;
export declare function logMessages(...messages: string[]): void;
export declare function delay<U>(ms: number, returnValue?: U): Promise<U>;
export declare function toArray(a: number): number[];
export declare function toArray(a: string): string[];
export declare function square(a: number): number;
export declare function pow(a: number): number;
export declare function pow(a: number, power: number): number;
export declare function toArray(a: string): string[];
export declare function createUser(name: string, age?: number, role?: string): object;
export declare function firstElement<T>(arr: T[]): T;
export declare function throwError(msg: string): void;
export declare function throwError(msg?: string): never;
export declare function throwError(): never;
export declare function wrapInArray<T>(value: T): T[];
export declare function identity<T = string>(value: T): T;
export declare function someFunction<A>(arr: A[]): undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:js_interop' as _i1;

import 'package:meta/meta.dart' as _i2;

@_i1.JS()
external double counter;
@_i1.JS()
Expand Down Expand Up @@ -35,3 +37,6 @@ external _i1.JSAny? get maybeValue;
external _i1.JSArray<_i1.JSString> get names;
@_i1.JS()
external _i1.JSArray<_i1.JSString> get newNames;
@_i2.doNotStore
@_i1.JS()
external _i1.JSAny? get neverUseThis;
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export declare let something: any;
export declare const maybeValue: unknown;
export declare const names: string[];
export declare const newNames: Array<string>;
export declare const neverUseThis: never;