dart2ts 0.0.1

  • README.md
  • Installing
  • Versions
  • 43

Dart2Ts : a better TS interface

HIC SUNT DRACONES

Dart2Ts is an highly experimental dart to typescript compiler.

Why ?

Because I'm tired of the current limits of dartdevc. Also translating dart to TS allows us to leverage the tsc to produce code that works on every browser and optimizing, ecc.

Targets

  • obviously : semantic equivalence
  • Produce "natural" and readable TS code, just like how you would have written that same code in TS.
  • Make it simple to use that code from TS and vice versa, like:
    • Allow extending a dart2ts class from ts and vice versa.

Anti-targets

  • main target is to make it work on web, so:
    • initially don't want to port any "platform specific" library, try to use only @JS
    • I don't care about Isolate and stuff like that

What works

At the moment only translating e2e_test project, that means:

  • basic function definition

  • invocation

  • import of other modules (only core and current package for now)

  • string interpolation

  • named constructors

  • class extension

  • calling named constructors

  • basic class definition

  • basic instance creation

  • named parameters

  • optional parameters

  • cascading

  • (cond)?true:false conditional expressions

  • this implicit reference for method invocation

  • calling .first psuedo accessor on a List (it's replaced by an extension method)

  • property

  • property accessor

  • top level variable

  • redir constructors

  • field initializers

  • field parameters

  • properties initializers

  • support for @JS annotation

    • DART2TS extension

note 'e2e_test' project is now able to write on the HTML page !!!

UPDATE : now even package:html5 is compiling. Next step is to create a demo project using package:html5 and demo it.

Dart2TS extensions

Estension to @JS annotation

@JS can now be used to specify from which module the symbol should be loaded. Module path and name should be separated by the '#' character. Alternatively one can use the @Module('modulepath') to specify the module path and the @JS('name') annotation for the namespace only. When both ('#' char and @Module annotation) are used the latter wins. The final name is defined concatanating both module path and namespace path information from each enclosing element. For example

@JS('module#myjslib')
libary mylib;

@JS('submod#Thing')
class That {
  
}

or

@JS('myjslib')
@Module('module')
libary mylib;

@JS('Thing')
@Module('submod')
class That {
  
}

Will cause any reference to That to be translated as a reference to myjslib.Thing in module module/submod.

To declare only the module and not a namespace use @JS('module#'). For example the following will associate the $ top level variable to the $ symbol exported by module jquery:

@JS('jquery#')
library mylib;

@JS()
Function $;

or

@JS()
@Module('jquery')
library mylib;

@JS()
Function $;

@JS annotation can also be used to change the name of the corresponding native method or properties, thus allowing to resolve any name conflict between dart and TS (Notably the is static method inside a class used by polymer).

Roadmap

  • ~~a better way to deal with @anonymous @js objects. For instance "new ShadowRootInit()" should become ="{mode:null,delegatesFocus:null}" or something like that.~~
  • ~~using other libraries (easy)~~
    • declare dep on both pubspec.yaml and package.json
    • build the dep and produce typelibs and js
    • when main project is built the already compiled is used for runtime and the dart as analysis
      • dart could be replaced by summaries or by generated "dart type lib" : a version of the original lib with only external declarations.
  • dart "typelibs" (libs with only declaration and all method declared external) (boring)
  • make class definition work (easy)
  • flow control statemets (easy)

    • for in
    • ~~for (x;y;z)~~
    • ~~yield~~
    • ~~return~~
    • while
    • ~~do~~
    • ecc.
  • exports (medium ? easy ?)

  • nullable expression (?? operator ?= operator) (super easy)

  • literals

    • array
    • map
  • symbol (map to symbol)

  • complete expression (boring)

    • whats missing ?
  • factory constructors (super easy)

  • async, async* (should be easy as TS supports 'em all)

    • ~~map Future to Promise (difficulty level ?!?)~~
  • ~~manage scope reference to "this." things (boring)~~

  • mixin (with polymer approach) (should be easy but no named constructor should be allowed)

  • implements (subtle , typescript implement of classes is the same of dart?)

  • ~~deal with "rewriting" some method calls, like : (tricky)~~

    • List -> Array
    • Map<String,?> -> {}
  • ~~deal with "@JS" things (easy)~~

  • (light) dart_sdk port

    • collections (maybe tricky, expecially to maintain semantical compatibility)
    • package:html (easy)
    • what else ? (boh)

Notes

Dart2TS generated classes do not requires a top common class so it will be easy to implement native js classes extensions. DDC infact requires a new initializers on super classes otherwise it will complain in some circumstances (that's why we had to patch id in polymerize).

Caveat

When executed a second time it can throw. Probably a bug in builder package. A workaround is to remove the .dart_tool hidden folder inside the package folder you are building (it will be recreated in the next build).

When launching dart2ts with the watch flag (-w) there's no problem.

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart2ts: "^0.0.1"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart2ts/dart2ts.dart';
        
Version Uploaded Documentation Archive
0.0.1 Oct 31, 2017 Go to the documentation of dart2ts 0.0.1 Download dart2ts 0.0.1 archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • tool failures on Feb 18, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
7 / 100
Health:
Code health derived from static analysis. [more]
85 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
68 / 100
Overall score:
Weighted score of the above. [more]
43

Platforms

Detected platforms: other

Primary library: package:dart2ts/dart2ts.dart with components: io, build, mirrors.

Suggestions

  • Fix lib/src/code_generator.dart.

    Strong-mode analysis of lib/src/code_generator.dart failed with the following error:

    line: 38 col: 3
    Invalid override. The type of 'Dart2TsBuildCommand.run' ('() → void') isn't a subtype of 'Command<bool>.run' ('() → FutureOr<bool>').

  • Fix lib/src/deps_installer.dart.

    Strong-mode analysis of lib/src/deps_installer.dart failed with the following error:

    line: 44 col: 3
    Invalid override. The type of 'Dart2TsInstallCommand.run' ('() → void') isn't a subtype of 'Command<bool>.run' ('() → FutureOr<bool>').

  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • Fix issues reported by dartanalyzer.

    dartanalyzer reported 2 error(s) and 0 warning(s).

  • Use constrained dependencies.

    The pubspec.yaml contains 3 dependencies without version constraints. Specify version ranges for the following dependencies: args, build_runner, source_gen.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use dart2ts.dart.

  • Use analysis_options.yaml.

    Rename old .analysis_options file to analysis_options.yaml.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0
analyzer ^0.30.0 0.30.0+4 0.31.1
args any 1.3.0
build ^0.10.0 0.10.2+1 0.12.0+1
build_runner any 0.5.0 0.7.11
source_gen any 0.7.5+1
Transitive dependencies
async 1.13.3 2.0.4
barback 0.15.2+14
build_barback 0.4.0+1 0.5.0+3
charcode 1.1.1
cli_util 0.1.2+1
code_transformers 0.5.1+4
collection 1.14.5
convert 2.0.1
crypto 2.0.2+1
csslib 0.14.1
dart_style 1.0.9+1
front_end 0.1.0-alpha.4.1 0.1.0-alpha.10
glob 1.1.5
html 0.13.2+2
http_parser 3.1.1
isolate 1.1.0
kernel 0.3.0-alpha.1.1 0.3.0-alpha.10
logging 0.11.3+1
meta 1.1.2
mime 0.9.6
package_config 1.0.3
path 1.5.1
plugin 0.2.0+2
pool 1.3.4
shelf 0.7.2
source_maps 0.10.4
source_span 1.4.0
stack_trace 1.9.2
stream_channel 1.6.3
stream_transform 0.0.10
string_scanner 1.0.2
typed_data 1.1.5
utf 0.9.0+4
watcher 0.9.7+7
yaml 2.1.13