typescript-monkey 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.editorconfig +27 -0
  4. data/.gitignore +52 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +27 -0
  8. data/.vscode/tasks.json +40 -0
  9. data/CHANGES.md +48 -0
  10. data/Gemfile +16 -0
  11. data/LICENSE.txt +22 -0
  12. data/README.md +351 -0
  13. data/Rakefile +13 -0
  14. data/contrib/example_package.json +10 -0
  15. data/contrib/example_typescript.rb +14 -0
  16. data/gulpfile.js +67 -0
  17. data/lib/assets/javascripts/transpiler.ts.erb +1 -0
  18. data/lib/assets/javascripts/transpiler_pkg.js.erb +2 -0
  19. data/lib/assets/javascripts/typescript.js.erb +1 -0
  20. data/lib/assets/typescripts/transpile_once.ts +13 -0
  21. data/lib/assets/typescripts/transpiler.ts +203 -0
  22. data/lib/rails/generators/typescript/assets/assets_generator.rb +13 -0
  23. data/lib/rails/generators/typescript/assets/templates/javascript.ts +3 -0
  24. data/lib/typescript-monkey.rb +8 -0
  25. data/lib/typescript/monkey.rb +9 -0
  26. data/lib/typescript/monkey/cli.rb +22 -0
  27. data/lib/typescript/monkey/compiler.rb +177 -0
  28. data/lib/typescript/monkey/configuration.rb +49 -0
  29. data/lib/typescript/monkey/engine.rb +18 -0
  30. data/lib/typescript/monkey/js_hook.rb +15 -0
  31. data/lib/typescript/monkey/package.rb +239 -0
  32. data/lib/typescript/monkey/railtie.rb +20 -0
  33. data/lib/typescript/monkey/template.rb +32 -0
  34. data/lib/typescript/monkey/template_handler.rb +24 -0
  35. data/lib/typescript/monkey/transformer.rb +20 -0
  36. data/lib/typescript/monkey/transpiler.rb +256 -0
  37. data/lib/typescript/monkey/version.rb +3 -0
  38. data/package.json +32 -0
  39. data/test/assets_generator_test.rb +15 -0
  40. data/test/assets_test.rb +41 -0
  41. data/test/controller_generator_test.rb +19 -0
  42. data/test/fixtures/assets/javascripts/hello.js.ts +3 -0
  43. data/test/fixtures/assets/javascripts/included.ts +4 -0
  44. data/test/fixtures/assets/javascripts/reference.ts +2 -0
  45. data/test/fixtures/references/ref1_1.js.ts +3 -0
  46. data/test/fixtures/references/ref1_2.js.ts +3 -0
  47. data/test/fixtures/references/ref2_1.d.ts +1 -0
  48. data/test/fixtures/references/ref2_2.js.ts +3 -0
  49. data/test/fixtures/references/ref3_1.js.ts +5 -0
  50. data/test/fixtures/references/ref3_2.ts +3 -0
  51. data/test/fixtures/references/ref3_3.ts +3 -0
  52. data/test/fixtures/routes.rb +0 -0
  53. data/test/fixtures/site/es5.js.ts +7 -0
  54. data/test/fixtures/site/index.js.ts +1 -0
  55. data/test/fixtures/site/script_tags.html.erb +23 -0
  56. data/test/fixtures/sprockets/ref1_1.js.ts +3 -0
  57. data/test/fixtures/sprockets/ref1_2.js.ts +3 -0
  58. data/test/fixtures/sprockets/ref1_manifest.js.ts +2 -0
  59. data/test/references_test.rb +48 -0
  60. data/test/scaffold_generator_test.rb +19 -0
  61. data/test/sprockets_test.rb +36 -0
  62. data/test/support/routes.rb +1 -0
  63. data/test/template_handler_test.rb +35 -0
  64. data/test/test_helper.rb +111 -0
  65. data/tsconfig.json +23 -0
  66. data/typescript-monkey.gemspec +34 -0
  67. metadata +175 -0
@@ -0,0 +1,3 @@
1
+ module Typescript::Monkey
2
+ VERSION = '0.9.0'
3
+ end
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "typescript-monkey",
3
+ "version": "1.0.0",
4
+ "description": "Typescript adapter for the Rails asset pipeline.",
5
+ "directories": {
6
+ "test": "test"
7
+ },
8
+ "scripts": {
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/markeissler/typescript-monkey.git"
14
+ },
15
+ "author": "Mark Eissler",
16
+ "license": "MIT",
17
+ "bugs": {
18
+ "url": "https://github.com/markeissler/typescript-monkey/issues"
19
+ },
20
+ "homepage": "https://github.com/markeissler/typescript-monkey#readme",
21
+ "devDependencies": {
22
+ "gulp": "^3.9.1",
23
+ "gulp-concat": "^2.6.1",
24
+ "gulp-debug": "^3.1.0",
25
+ "gulp-ignore": "^2.0.2",
26
+ "gulp-rename": "^1.2.2",
27
+ "gulp-typescript": "^3.1.6",
28
+ "gulp-uglify": "^2.1.2",
29
+ "run-sequence": "^1.2.2",
30
+ "typescript": "^2.3.2"
31
+ }
32
+ }
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+ require 'rails/generators/typescript/assets/assets_generator'
3
+
4
+ class AssetGeneratorTest < Rails::Generators::TestCase
5
+ tests Typescript::Generators::AssetsGenerator
6
+
7
+ destination File.expand_path("../tmp", __FILE__)
8
+ setup :prepare_destination
9
+
10
+ def test_assets
11
+ run_generator %w(posts)
12
+ assert_no_file "app/assets/javascripts/posts.js"
13
+ assert_file "app/assets/javascripts/posts.ts"
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+ require 'typescript-monkey'
3
+
4
+ require 'action_controller/railtie'
5
+ require 'sprockets/railtie'
6
+
7
+ class AssetsTest < ActiveSupport::TestCase
8
+ include Minitest::PowerAssert::Assertions
9
+
10
+ @@app_setup = false
11
+ @@app = nil
12
+
13
+ def setup
14
+ unless @@app_setup == true
15
+ @@app_setup = true
16
+ # reconfigure compiler to resolve references and concatenate files
17
+ Typescript::Monkey.configure do |config|
18
+ config.compile = true
19
+ end
20
+
21
+ @@app = RailsApp.instance.app()
22
+ RailsApp.instance.asset_paths_append("#{File.dirname(__FILE__)}/fixtures/assets")
23
+ end
24
+ end
25
+
26
+ def teardown
27
+ end
28
+
29
+ #
30
+ # These tests require sprockets processing with --noResolve turned off which
31
+ # results in reference resolution and concatenated files.
32
+ #
33
+ # Typescript::Monkey::Compiler.compile = true
34
+ #
35
+
36
+ test 'assets .js.ts is compiled from TypeScript to JavaScript' do
37
+ assert { @@app.assets['javascripts/hello.js'].present? }
38
+ assert { @@app.assets['javascripts/hello.js'].source.include?('var log_to_console = function (x) {') }
39
+ assert { @@app.assets['javascripts/hello.js'].source.include?('var s = "Hello, world!";') }
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+ require 'rails/generators/rails/controller/controller_generator'
3
+ require 'rails/generators/typescript/assets/assets_generator'
4
+
5
+ class ControllerGeneratorTest < Rails::Generators::TestCase
6
+ tests Rails::Generators::ControllerGenerator
7
+
8
+ destination File.expand_path("../tmp", __FILE__)
9
+ setup do
10
+ prepare_destination
11
+ copy_routes
12
+ end
13
+
14
+ def test_assets
15
+ run_generator %w(posts --javascript-engine=typescript --orm=false)
16
+ assert_no_file "app/assets/javascripts/posts.js"
17
+ assert_file "app/assets/javascripts/posts.ts"
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ /// <reference path="reference.ts" />
2
+ var s: string = "Hello, world!";
3
+ log_to_console(s);
@@ -0,0 +1,4 @@
1
+ /// <reference path="reference.ts" />
2
+ var log_to_console = function(x: string): void {
3
+ console.log(x)
4
+ }
@@ -0,0 +1,2 @@
1
+ /// <reference path="hello.js.ts" />
2
+ /// <reference path="included.ts" />
@@ -0,0 +1,3 @@
1
+ var f = function(x: number, y: number): number {
2
+ return x + y
3
+ }
@@ -0,0 +1,3 @@
1
+ /// <reference path="ref1_1.js.ts" />
2
+
3
+ f(1, 2)
@@ -0,0 +1 @@
1
+ declare var f: (x: number, y: number) => number;
@@ -0,0 +1,3 @@
1
+ /// <reference path="./ref2_1.d.ts" />
2
+
3
+ f(1, 2)
@@ -0,0 +1,5 @@
1
+ /// <reference path="ref3_2.ts" />
2
+ /// <reference path="ref3_3.ts" />
3
+
4
+ f1();
5
+ f2();
@@ -0,0 +1,3 @@
1
+ var f1 = function(): void {
2
+
3
+ };
@@ -0,0 +1,3 @@
1
+ var f2 = function(): void {
2
+
3
+ };
File without changes
@@ -0,0 +1,7 @@
1
+ class Person {
2
+ private _name = "Alice";
3
+
4
+ public get name(): string {
5
+ return this._name
6
+ }
7
+ }
@@ -0,0 +1 @@
1
+ var x:number = 5
@@ -0,0 +1,23 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <meta http-equiv="X-UA-Compatible" content="ie=edge">
7
+ <%= javascript_include_tag 'application' %>
8
+ <title>Document</title>
9
+ </head>
10
+ <body>
11
+ <div id="target"></div>
12
+ <script type="text/typescript">
13
+ class Person {
14
+ private _name = "Alice";
15
+
16
+ public get name(): string {
17
+ return this._name
18
+ }
19
+ }
20
+ </script>
21
+ <script src="es5.js"></script>
22
+ </body>
23
+ </html>
@@ -0,0 +1,3 @@
1
+ var f = function(x: number, y: number): number {
2
+ return x + y
3
+ }
@@ -0,0 +1,3 @@
1
+ /// <reference path="ref1_1.js.ts" />
2
+
3
+ f(1, 2)
@@ -0,0 +1,2 @@
1
+ //= require ref1_1
2
+ //= require ref1_2
@@ -0,0 +1,48 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+ require 'typescript-monkey'
3
+
4
+ require 'action_controller/railtie'
5
+ require 'sprockets/railtie'
6
+
7
+ class ReferencesTest < ActiveSupport::TestCase
8
+ include Minitest::PowerAssert::Assertions
9
+
10
+ @@app_setup = false
11
+ @@app = nil
12
+
13
+ def setup
14
+ unless @@app_setup == true
15
+ @@app_setup = true
16
+ # reconfigure compiler to resolve references and concatenate files
17
+ Typescript::Monkey.configure do |config|
18
+ config.compile = true
19
+ end
20
+
21
+ @@app = RailsApp.instance.app()
22
+ RailsApp.instance.asset_paths_append("#{File.dirname(__FILE__)}/fixtures/references")
23
+ end
24
+ end
25
+
26
+ def teardown
27
+ end
28
+
29
+ #
30
+ # These tests require sprockets processing with --noResolve turned off which
31
+ # results in reference resolution and concatenated files.
32
+ #
33
+ # Typescript::Monkey::Compiler.compile = true
34
+ #
35
+
36
+ test '<reference> to other .ts file works' do
37
+ assert {@@app.assets['ref1_2.js'].source.match(/var f = function \(x, y\) \{\s*return x \+ y\;\s*\}\;\s*f\(1, 2\)\;\s*/) }
38
+ end
39
+
40
+ test '<reference> to other .d.ts file works' do
41
+ assert {@@app.assets['ref2_2.js'].source.match(/f\(1, 2\)\;\s*/) }
42
+ end
43
+
44
+ test '<reference> to multiple .ts files works' do
45
+ assert {@@app.assets['ref3_1.js'].source.match(/var f1 = function \(\) \{\s*\}\;\s*var f2 = function \(\) \{\s*\}\;\s*f1\(\)\;\s*f2\(\)\;/) }
46
+ end
47
+
48
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+ require 'rails/generators/rails/scaffold/scaffold_generator'
3
+ require 'rails/generators/typescript/assets/assets_generator'
4
+
5
+ class ScaffoldGeneratorTest < Rails::Generators::TestCase
6
+ tests Rails::Generators::ScaffoldGenerator
7
+
8
+ destination File.expand_path("../tmp", __FILE__)
9
+ setup do
10
+ prepare_destination
11
+ copy_routes
12
+ end
13
+
14
+ def test_assets
15
+ run_generator %w(posts --javascript-engine=typescript --orm=false)
16
+ assert_no_file "app/assets/javascripts/posts.js"
17
+ assert_file "app/assets/javascripts/posts.ts"
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+ require 'typescript-monkey'
3
+
4
+ require 'action_controller/railtie'
5
+ require 'sprockets/railtie'
6
+
7
+ class SprocketsTest < ActiveSupport::TestCase
8
+ include Minitest::PowerAssert::Assertions
9
+
10
+ @@app_setup = false
11
+ @@app = nil
12
+
13
+ def setup
14
+ unless @@app_setup == true
15
+ @@app_setup = true
16
+ @@app = RailsApp.instance.app()
17
+ RailsApp.instance.asset_paths_append("#{File.dirname(__FILE__)}/fixtures/sprockets")
18
+ end
19
+ end
20
+
21
+ def teardown
22
+ end
23
+
24
+ #
25
+ # These tests require sprockets processing with --noResolve turned on which
26
+ # results in no reference resolution. Separate files will be output.
27
+ #
28
+ #
29
+ # Typescript::Monkey::Compiler.compile = false (default setting)
30
+ #
31
+
32
+ test '//= require directives work' do
33
+ assert { @@app.assets['ref1_manifest.js'].source.match(/var f = function \(x, y\) \{\s*return x \+ y\;\s*\}\;\s*f\(1, 2\)\;\s*/) }
34
+ end
35
+
36
+ end
@@ -0,0 +1 @@
1
+ # routes dummy file
@@ -0,0 +1,35 @@
1
+ # require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+ require 'test_helper'
3
+ require 'action_controller'
4
+ require 'typescript-monkey'
5
+ require 'sprockets/railtie'
6
+
7
+ class SiteController < ActionController::Base
8
+ self.view_paths = File.expand_path('../fixtures', __FILE__)
9
+ end
10
+
11
+ DummyApp = ActionDispatch::Routing::RouteSet.new
12
+ DummyApp.draw do
13
+ get 'site/index'
14
+ get 'site/es5'
15
+ end
16
+
17
+ class TemplateHandlerTest < ActiveSupport::TestCase
18
+ include Rack::Test::Methods
19
+
20
+ def app
21
+ @app ||= DummyApp
22
+ end
23
+
24
+ test 'typescript views are served as javascript' do
25
+ get '/site/index.js'
26
+ assert_match(/var x = 5;\s*/, strip_comments(last_response.body))
27
+ end
28
+
29
+ test 'ES5 features' do
30
+ get '/site/es5.js'
31
+ assert_equal(200, last_response.status)
32
+ end
33
+
34
+ # @TODO: need a test for <script type="text/typescript"></script>
35
+ end
@@ -0,0 +1,111 @@
1
+ # Configure coveralls environment
2
+
3
+ require 'coveralls'
4
+ require 'simplecov'
5
+
6
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
7
+ SimpleCov::Formatter::HTMLFormatter,
8
+ Coveralls::SimpleCov::Formatter
9
+ ])
10
+ SimpleCov.start do
11
+ add_filter '.bundle/'
12
+ end
13
+
14
+ # Configure Rails environment
15
+ ENV['RAILS_ENV'] = 'test'
16
+
17
+ require 'rails'
18
+ require 'rails/test_help'
19
+ require 'minitest-power_assert'
20
+ require 'byebug'
21
+
22
+ # For generators
23
+ require 'rails/generators/test_case'
24
+
25
+ # Set ActiveSupport test order (:random, :sorted, :parallel)
26
+ ActiveSupport::TestCase.test_order = :random
27
+
28
+ # Default Typescript::Monkey compiler configuration
29
+ require 'typescript/monkey/configuration'
30
+ Typescript::Monkey.configure do |config|
31
+ # option config
32
+ config.compile = false
33
+ # setup logging for debugging
34
+ debug_log_path = Pathname.new("#{File.dirname(__FILE__)}/tmp/log")
35
+ FileUtils.mkdir_p(debug_log_path)
36
+ config.logger = Logger.new(debug_log_path.join("typescript-monkey.log").to_s)
37
+ end
38
+
39
+ def copy_routes
40
+ routes = File.expand_path('../support/routes.rb', __FILE__)
41
+ destination = File.join(destination_root, 'config')
42
+
43
+ FileUtils.mkdir_p(destination)
44
+ FileUtils.cp routes, destination
45
+ end
46
+
47
+ def strip_comments(source)
48
+ source.gsub(%r{^//[^\n]*}m, '')
49
+ end
50
+
51
+ require 'rails'
52
+
53
+ class RailsApp
54
+ attr_accessor :app
55
+
56
+ def initialize(tmp_path, log_path)
57
+ FileUtils.mkdir_p(tmp_path)
58
+
59
+ @app = Class.new(Rails::Application)
60
+ @app.config.eager_load = false
61
+ @app.config.active_support.deprecation = :stderr
62
+ @app.config.assets.configure do |env|
63
+ env.cache = ActiveSupport::Cache.lookup_store(:memory_store)
64
+ end
65
+ # note: you could configure all asset paths here at once but that prevents
66
+ # isolation for each test:
67
+ #
68
+ # @app.config.assets.paths << "#{File.dirname(__FILE__)}/fixtures/references"
69
+ #
70
+ @app.paths['log'] = log_path.to_s
71
+ @app.paths['tmp'] = tmp_path.to_s
72
+ @app.config.secret_key_base = "abcd1234"
73
+ @app.initialize!
74
+
75
+ ObjectSpace.define_finalizer(self, self.class.method(:finalize))
76
+ end
77
+
78
+ private_class_method :new
79
+
80
+ def asset_paths_append(*paths)
81
+ updated_paths = @app.config.assets.paths.to_set
82
+ paths.each { |path| updated_paths.add(path) }
83
+ @app.config.assets.paths = updated_paths.to_a
84
+ # rebuild assets -- this will slow things down!
85
+ @app.assets = Sprockets::Railtie.build_environment(@app)
86
+ end
87
+
88
+ def asset_paths_delete(*paths)
89
+ updated_paths = @app.config.assets.paths.to_set
90
+ paths.each { |path| updated_paths.delete(path) }
91
+ @app.config.assets.paths = updated_paths.to_a
92
+ # rebuild assets -- this will slow things down!
93
+ @app.assets = Sprockets::Railtie.build_environment(@app)
94
+ end
95
+
96
+ def self.instance
97
+ @__instance__ ||= begin
98
+ @__tmp_path__ ||= Pathname.new("#{File.dirname(__FILE__)}/tmp")
99
+ @__log_path__ ||= @__tmp_path__.join("log/test.log")
100
+ @__instance__ ||= new(@__tmp_path__, @__log_path__)
101
+ end
102
+ end
103
+
104
+ def self.finalize(object_id)
105
+ # if logging has been configured, assume we want to keep the logs!
106
+ unless Typescript::Monkey.configuration.logger
107
+ FileUtils.rm_rf(@__tmp_path__)
108
+ end
109
+ end
110
+
111
+ end