├── .coveralls.yml ├── .gitignore ├── .travis.yml ├── CHANGES.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── lib ├── assets │ └── javascripts │ │ └── typescript.js.erb ├── rails │ └── generators │ │ └── typescript │ │ └── assets │ │ ├── assets_generator.rb │ │ └── templates │ │ └── javascript.ts ├── typescript-rails.rb └── typescript │ ├── rails.rb │ └── rails │ ├── compiler.rb │ ├── engine.rb │ ├── js_hook.rb │ ├── railtie.rb │ ├── template.rb │ ├── template_handler.rb │ ├── transformer.rb │ └── version.rb ├── test ├── assets_generator_test.rb ├── assets_test.rb ├── controller_generator_test.rb ├── fixtures │ ├── assets │ │ └── javascripts │ │ │ ├── hello.js.ts │ │ │ ├── included.ts │ │ │ └── reference.ts │ ├── routes.rb │ └── site │ │ ├── es5.js.ts │ │ ├── index.js.ts │ │ ├── ref1_1.js.ts │ │ ├── ref1_2.js.ts │ │ ├── ref2_1.d.ts │ │ ├── ref2_2.js.ts │ │ ├── ref3_1.js.ts │ │ ├── ref3_2.ts │ │ └── ref3_3.ts ├── scaffold_generator_test.rb ├── support │ └── routes.rb ├── template_handler_test.rb └── test_helper.rb └── typescript-rails.gemspec /.coveralls.yml: -------------------------------------------------------------------------------- 1 | service_name: travis-ci 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | *.gem 3 | *.rbc 4 | .bundle 5 | .config 6 | .yardoc 7 | Gemfile.lock 8 | InstalledFiles 9 | _yardoc 10 | coverage 11 | doc/ 12 | lib/bundler/man 13 | pkg 14 | rdoc 15 | spec/reports 16 | test/tmp 17 | test/version_tmp 18 | tmp 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | rvm: 2 | - 2.0.0 3 | - 2.1.1 4 | notifications: 5 | email: false 6 | script: bundle exec rake test 7 | matrix: 8 | allow_failures: 9 | - rvm: 10 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | ## v0.6.2.4 2016-09-12 13:48:00+0300 2 | 3 | * Add typescript template generator (not defaulted). Thanks to @mjfaga 4 | 5 | ## v0.6.2.3 2016-07-27 17:25:00+0300 6 | 7 | * Fixed incorrect value of register_mime_type (@mihai-dinculescu) 8 | 9 | ## v0.6.2.1 2016-07-26 18:27:00+0300 10 | 11 | * Added Sprockets 4 support. Thanks to @mihai-dinculescu 12 | 13 | ## v0.6.2 2015-07-27 21:52:50+0300 14 | 15 | * Updated to TS v1.6.2 16 | 17 | ## v0.6.1 2015-07-27 21:52:50+0300 18 | 19 | * Now code raises exception with correct file name when TS compilation error occurs 20 | 21 | ## v0.6.0 2015-07-06 22:36:25+0300 22 | 23 | * Updated version to 0.6.0 for using Typescript source 1.4.1.3 24 | 25 | ## v0.5.0 2015-03-25 10:46:29+0900 26 | 27 | * Expire root TS cache in response to change in referenced `.ts` files (#24) 28 | 29 | ## v0.4.2 2014-11-26 07:30:12+0900 30 | 31 | * Fix newlines (#15) 32 | * Mention to `default_options` in README (#18) 33 | 34 | ## v0.4.1 2014-08-12 00:05:17+0900 35 | 36 | * Fix a runtime error 37 | 38 | ## v0.4.0 2014-08-11 23:04:20+0900 39 | 40 | * Set `--noImplicitAny` by default to tsc compiler 41 | * Typescript::Rails::Compiler.default_options to provide tsc compiler with otpions 42 | * default values: `--target ES5 --noImplicitAny` 43 | * A lot of refactoring 44 | 45 | ## v0.3.0 2014-08-09 10:27:54+0900 46 | 47 | * Compiles TypeScript files in ES5 mode 48 | 49 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in typescript-rails.gemspec 4 | gemspec 5 | 6 | group :test do 7 | gem 'rails', '~> 4.0' 8 | gem 'sprockets-rails', '> 2.0' 9 | gem 'minitest-power_assert' 10 | gem 'coveralls' 11 | gem 'simplecov' 12 | gem 'tzinfo-data', platforms: [:mingw, :mswin] 13 | end 14 | 15 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Klaus Zanders 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TypeScript for Rails [![Build Status](https://travis-ci.org/typescript-ruby/typescript-rails.svg?branch=master)](https://travis-ci.org/typescript-ruby/typescript-rails) [![Coverage Status](https://coveralls.io/repos/typescript-ruby/typescript-rails/badge.png)](https://coveralls.io/r/typescript-ruby/typescript-rails) 2 | 3 | This is a wrapper for the [TypeScript](http://www.typescriptlang.org/), JavaScript superset language by Microsoft. 4 | 5 | It enables you to use the `.ts` extension in the Asset Pipeline and also in ActionView Templates. 6 | 7 | This gem uses the 8 | [typescript-node-ruby](https://github.com/typescript-ruby/typescript-node-ruby) 9 | library by KAWACHI Takashi for the typescript parsing with node js. 10 | 11 | The credit for the overall structure and the tests goes to the people that wrote the [coffee-rails](https://github.com/rails/coffee-rails) Gem, since I shamelessly copy&pasted some of their code. 12 | 13 | ## Requirements 14 | 15 | The current version requires that [node.js](http://nodejs.org/) is 16 | installed on the system. 17 | 18 | The requirement for node is tested upon starting the application. If 19 | the node command is not available you get the following error message: 20 | 21 | ``` 22 | typescript-node requires node command, but it's not found. Please install it. Set TS_NODE environmental variable If you want to use node command in non-standard path. 23 | ``` 24 | 25 | ## Installation 26 | 27 | Add this line to your application's Gemfile: 28 | 29 | gem 'typescript-rails' 30 | 31 | And then execute: 32 | 33 | $ bundle 34 | 35 | Note: We need to install (if we haven't already) the Typescript definitions, [more info](https://github.com/DefinitelyTyped/DefinitelyTyped#how-do-i-get-them). 36 | 37 | npm install --save-dev @types/node 38 | 39 | ## Usage 40 | 41 | Just add a `.js.ts` file in your `app/assets/javascripts` directory and include it just like you are used to do. 42 | 43 | Configurations: 44 | 45 | ``` 46 | Typescript::Rails::Compiler.default_options = %w(--target ES5 --noImplicitAny --module commonjs) 47 | ``` 48 | 49 | Default Configurations: 50 | 51 | ``` 52 | Typescript::Rails::Compiler.default_options = %w(--target ES5 --noImplicitAny) 53 | ``` 54 | 55 | 56 | ## Default Javascript Compilation 57 | 58 | Add this line to your `config/application.rb` as show below, above the `config.assets.enabled = true`: 59 | 60 | ```ruby 61 | config.app_generators.javascript_engine :typescript 62 | 63 | # Enable the asset pipeline 64 | config.assets.enabled = true 65 | ``` 66 | 67 | If you don't want it to be the default javascript engine, you can also use it adhoc as show below: 68 | 69 | ```ruby 70 | rails g controller MyController --javascript_engine=typescript 71 | ``` 72 | 73 | ## Referenced TypeScript dependencies 74 | 75 | `typescript-rails` recurses through all [TypeScript-style](https://github.com/teppeis/typescript-spec-md/blob/master/en/ch11.md#1111-source-files-dependencies) referenced files and tells its [`Sprockets::Context`](https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/context.rb) that the TS file being processed [`depend`s`_on`](https://github.com/sstephenson/sprockets#the-depend_on-directive) each file listed as a reference. This activates Sprocket’s cache-invalidation behavior when any of the descendant references of the root TS file is changed. 76 | 77 | ## Contributing 78 | 79 | 1. Fork it 80 | 2. Create your feature branch (`git checkout -b my-new-feature`) 81 | 3. Commit your changes (`git commit -am 'Add some feature'`) 82 | 4. Push to the branch (`git push origin my-new-feature`) 83 | 5. Create new Pull Request 84 | 85 | ## Maintainers 86 | 87 | FUJI Goro 88 | 89 | ## Authors 90 | 91 | Klaus Zanders 92 | 93 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler' 2 | Bundler::GemHelper.install_tasks 3 | 4 | require 'rake/testtask' 5 | 6 | Rake::TestTask.new(:test) do |t| 7 | t.libs << 'lib' 8 | t.libs << 'test' 9 | t.pattern = 'test/**/*_test.rb' 10 | t.verbose = false 11 | end 12 | 13 | task :default => :test -------------------------------------------------------------------------------- /lib/assets/javascripts/typescript.js.erb: -------------------------------------------------------------------------------- 1 | <%= TypeScript::Src.js_content %> 2 | -------------------------------------------------------------------------------- /lib/rails/generators/typescript/assets/assets_generator.rb: -------------------------------------------------------------------------------- 1 | require "rails/generators/named_base" 2 | 3 | module Typescript 4 | module Generators 5 | class AssetsGenerator < ::Rails::Generators::NamedBase 6 | source_root File.expand_path("../templates", __FILE__) 7 | 8 | def copy_typescript 9 | template "javascript.ts", File.join('app/assets/javascripts', class_path, "#{file_name}.ts") 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /lib/rails/generators/typescript/assets/templates/javascript.ts: -------------------------------------------------------------------------------- 1 | // Place all the behaviors and hooks related to the matching controller here. 2 | // All this logic will automatically be available in application.js. 3 | // You can use TypeScript in this file: www.typescriptlang.org -------------------------------------------------------------------------------- /lib/typescript-rails.rb: -------------------------------------------------------------------------------- 1 | require 'typescript/rails' 2 | require 'typescript/rails/railtie' 3 | require 'typescript/rails/engine' 4 | require 'typescript/rails/template' 5 | require 'typescript/rails/template_handler' 6 | require 'typescript/rails/transformer' 7 | require 'typescript/rails/version' 8 | -------------------------------------------------------------------------------- /lib/typescript/rails.rb: -------------------------------------------------------------------------------- 1 | module Typescript 2 | module Rails 3 | # just to define the module Typescript::Rails 4 | end 5 | end -------------------------------------------------------------------------------- /lib/typescript/rails/compiler.rb: -------------------------------------------------------------------------------- 1 | require 'typescript/rails' 2 | require 'typescript-node' 3 | 4 | module Typescript::Rails::Compiler 5 | class << self 6 | # @!scope class 7 | cattr_accessor :default_options 8 | 9 | # Replace relative paths specified in /// with absolute paths. 10 | # 11 | # @param [String] ts_path Source .ts path 12 | # @param [String] source. It might be pre-processed by erb. 13 | # @return [String] replaces source 14 | def replace_relative_references(ts_path, source) 15 | ts_dir = File.dirname(File.expand_path(ts_path)) 16 | escaped_dir = ts_dir.gsub(/["\\]/, '\\\\\&') # "\"" => "\\\"", '\\' => '\\\\' 17 | 18 | # Why don't we just use gsub? Because it display odd behavior with File.join on Ruby 2.0 19 | # So we go the long way around. 20 | (source.each_line.map do |l| 21 | if l.starts_with?('///') && !(m = %r!^///\s*\s*!.match(l)).nil? 22 | matched_path = m.captures.compact[0] 23 | l = l.sub(matched_path, File.join(escaped_dir, matched_path)) 24 | end 25 | next l 26 | end).join 27 | end 28 | 29 | # Get all references 30 | # 31 | # @param [String] path Source .ts path 32 | # @param [String] source. It might be pre-processed by erb. 33 | # @yieldreturn [String] matched ref abs_path 34 | def get_all_reference_paths(path, source, visited_paths=Set.new, &block) 35 | visited_paths << path 36 | source ||= File.read(path) 37 | source.each_line do |l| 38 | if l.starts_with?('///') && !(m = %r!^///\s*\s*!.match(l)).nil? 39 | matched_path = m.captures.compact[0] 40 | abs_matched_path = File.expand_path(matched_path, File.dirname(path)) 41 | unless visited_paths.include? abs_matched_path 42 | block.call abs_matched_path 43 | get_all_reference_paths(abs_matched_path, nil, visited_paths, &block) 44 | end 45 | end 46 | end 47 | end 48 | 49 | # @param [String] ts_path 50 | # @param [String] source TypeScript source code 51 | # @param [Sprockets::Context] sprockets context object 52 | # @return [String] compiled JavaScript source code 53 | def compile(ts_path, source, context=nil, *options) 54 | if context 55 | get_all_reference_paths(File.expand_path(ts_path), source) do |abs_path| 56 | context.depend_on abs_path 57 | end 58 | end 59 | s = replace_relative_references(ts_path, source) 60 | begin 61 | ::TypeScript::Node.compile(s, *default_options, *options) 62 | rescue Exception => e 63 | raise "Typescript error in file '#{ts_path}':\n#{e.message}" 64 | end 65 | end 66 | 67 | end 68 | 69 | self.default_options = %w(--target ES5 --noImplicitAny) 70 | end 71 | -------------------------------------------------------------------------------- /lib/typescript/rails/engine.rb: -------------------------------------------------------------------------------- 1 | require 'rails/engine' 2 | require 'rails/generators' 3 | require 'typescript/rails/js_hook' 4 | 5 | class Typescript::Rails::Engine < Rails::Engine 6 | # To become the default generator... 7 | # config.app_generators.javascript_engine :typescript 8 | 9 | if config.respond_to?(:annotations) 10 | config.annotations.register_extensions(".ts") { |annotation| /#\s*(#{annotation}):?\s*(.*)$/ } 11 | end 12 | 13 | initializer 'override js_template hook' do |app| 14 | if app.config.generators.rails[:javascript_engine] == :typescript 15 | ::Rails::Generators::NamedBase.send :include, Typescript::Rails::JsHook 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/typescript/rails/js_hook.rb: -------------------------------------------------------------------------------- 1 | module Typescript 2 | module Rails 3 | module JsHook 4 | extend ActiveSupport::Concern 5 | 6 | included do 7 | no_tasks do 8 | redefine_method :js_template do |source, destination| 9 | template(source + '.ts', destination + '.ts') 10 | end 11 | end 12 | end 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /lib/typescript/rails/railtie.rb: -------------------------------------------------------------------------------- 1 | require 'typescript/rails' 2 | 3 | class Typescript::Rails::Railtie < ::Rails::Railtie 4 | config.before_initialize do |app| 5 | if ::Rails::VERSION::MAJOR >= 4 || app.config.assets.enabled 6 | require 'typescript/rails/template' 7 | require 'typescript/rails/transformer' 8 | require 'sprockets' 9 | 10 | if Sprockets.respond_to?(:register_engine) 11 | Sprockets.register_engine '.ts', Typescript::Rails::Template, silence_deprecation: true 12 | end 13 | 14 | if Sprockets.respond_to?(:register_transformer) 15 | Sprockets.register_mime_type 'text/typescript', extensions: ['.ts'] 16 | Sprockets.register_transformer 'text/typescript', 'application/javascript', Typescript::Rails::Transformer 17 | end 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/typescript/rails/template.rb: -------------------------------------------------------------------------------- 1 | require 'typescript/rails' 2 | require 'tilt/template' 3 | 4 | class Typescript::Rails::Template < ::Tilt::Template 5 | self.default_mime_type = 'application/javascript' 6 | 7 | # @!scope class 8 | class_attribute :default_bare 9 | 10 | def self.engine_initialized? 11 | defined? ::Typescript::Rails::Compiler 12 | end 13 | 14 | def initialize_engine 15 | require_template_library 'typescript/rails/compiler' 16 | end 17 | 18 | def prepare 19 | if !options.key?(:bare) and !options.key?(:no_wrap) 20 | options[:bare] = self.class.default_bare 21 | end 22 | end 23 | 24 | def evaluate(context, locals, &block) 25 | @output ||= ::Typescript::Rails::Compiler.compile(file, data, context) 26 | end 27 | 28 | # @override 29 | def allows_script? 30 | false 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/typescript/rails/template_handler.rb: -------------------------------------------------------------------------------- 1 | require 'typescript/rails/compiler' 2 | 3 | class Typescript::Rails::TemplateHandler 4 | class << self 5 | def erb_handler 6 | @erb_handler ||= ActionView::Template.registered_template_handler(:erb) 7 | end 8 | 9 | def call(template) 10 | compiled_source = erb_handler.call(template) 11 | path = template.identifier.gsub(/['\\]/, '\\\\\&') # "'" => "\\'", '\\' => '\\\\' 12 | <<-EOS 13 | ::Typescript::Rails::Compiler.compile('#{path}', (begin;#{compiled_source};end)) 14 | EOS 15 | end 16 | end 17 | end 18 | 19 | ActiveSupport.on_load(:action_view) do 20 | ActionView::Template.register_template_handler :ts, Typescript::Rails::TemplateHandler 21 | end 22 | -------------------------------------------------------------------------------- /lib/typescript/rails/transformer.rb: -------------------------------------------------------------------------------- 1 | require 'typescript/rails' 2 | 3 | class Typescript::Rails::Transformer 4 | def self.instance 5 | @instance ||= new 6 | end 7 | 8 | def self.call(input) 9 | instance.call(input) 10 | end 11 | 12 | def call(input) 13 | filename = input[:filename] 14 | source = input[:data] 15 | context = input[:environment].context_class.new(input) 16 | 17 | result = ::Typescript::Rails::Compiler.compile(filename, source, context) 18 | { data: result } 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/typescript/rails/version.rb: -------------------------------------------------------------------------------- 1 | module Typescript 2 | module Rails 3 | VERSION = '0.6.2.4' 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /test/assets_generator_test.rb: -------------------------------------------------------------------------------- 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 16 | -------------------------------------------------------------------------------- /test/assets_test.rb: -------------------------------------------------------------------------------- 1 | require File.join(File.dirname(__FILE__), 'test_helper.rb') 2 | require 'typescript-rails' 3 | 4 | require 'action_controller/railtie' 5 | require 'sprockets/railtie' 6 | 7 | 8 | class AssetsTest < ActiveSupport::TestCase 9 | include Minitest::PowerAssert::Assertions 10 | 11 | def setup 12 | FileUtils.mkdir_p tmp_path 13 | 14 | @app = Class.new(Rails::Application) 15 | 16 | @app.config.eager_load = false 17 | @app.config.active_support.deprecation = :stderr 18 | @app.config.assets.configure do |env| 19 | env.cache = ActiveSupport::Cache.lookup_store(:memory_store) 20 | end 21 | @app.config.assets.paths << "#{File.dirname(__FILE__)}/fixtures/assets" 22 | @app.paths['log'] = "#{tmp_path}/log/test.log" 23 | @app.initialize! 24 | end 25 | 26 | def teardown 27 | FileUtils.rm_rf tmp_path 28 | end 29 | 30 | def tmp_path 31 | "#{File.dirname(__FILE__)}/tmp" 32 | end 33 | 34 | def assets 35 | @app.assets 36 | end 37 | 38 | test 'typescript.js is included in Sprockets environment' do 39 | assert { assets["typescript"].filename.to_s.end_with?('/lib/assets/javascripts/typescript.js.erb') } 40 | assert { assets["typescript"].source.include?('var ts;') } 41 | end 42 | 43 | test 'assets .js.ts is compiled from TypeScript to JavaScript' do 44 | assert { assets['javascripts/hello.js'].present? } 45 | assert { assets['javascripts/hello.js'].source.include?('var log_to_console = function (x) {') } 46 | assert { assets['javascripts/hello.js'].source.include?('var s = "Hello, world!";') } 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /test/controller_generator_test.rb: -------------------------------------------------------------------------------- 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 20 | -------------------------------------------------------------------------------- /test/fixtures/assets/javascripts/hello.js.ts: -------------------------------------------------------------------------------- 1 | /// 2 | var s: string = "Hello, world!"; 3 | log_to_console(s); 4 | -------------------------------------------------------------------------------- /test/fixtures/assets/javascripts/included.ts: -------------------------------------------------------------------------------- 1 | /// 2 | var log_to_console = function(x: string): void { 3 | console.log(x) 4 | } 5 | -------------------------------------------------------------------------------- /test/fixtures/assets/javascripts/reference.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | -------------------------------------------------------------------------------- /test/fixtures/routes.rb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/typescript-ruby/typescript-rails/56ff7aac0f3486959a49489b69895bd4a4014d60/test/fixtures/routes.rb -------------------------------------------------------------------------------- /test/fixtures/site/es5.js.ts: -------------------------------------------------------------------------------- 1 | class Person { 2 | private _name = "Alice"; 3 | 4 | public get name(): string { 5 | return this._name 6 | } 7 | } -------------------------------------------------------------------------------- /test/fixtures/site/index.js.ts: -------------------------------------------------------------------------------- 1 | var x:number = 5 2 | -------------------------------------------------------------------------------- /test/fixtures/site/ref1_1.js.ts: -------------------------------------------------------------------------------- 1 | var f = function(x: number, y: number): number { 2 | return x + y 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/site/ref1_2.js.ts: -------------------------------------------------------------------------------- 1 | /// 2 | 3 | f(1, 2) 4 | -------------------------------------------------------------------------------- /test/fixtures/site/ref2_1.d.ts: -------------------------------------------------------------------------------- 1 | declare var f: (x: number, y: number) => number; 2 | -------------------------------------------------------------------------------- /test/fixtures/site/ref2_2.js.ts: -------------------------------------------------------------------------------- 1 | /// 2 | 3 | f(1, 2) 4 | -------------------------------------------------------------------------------- /test/fixtures/site/ref3_1.js.ts: -------------------------------------------------------------------------------- 1 | /// 2 | /// 3 | 4 | f1(); 5 | f2(); -------------------------------------------------------------------------------- /test/fixtures/site/ref3_2.ts: -------------------------------------------------------------------------------- 1 | var f1 = function(): void { 2 | 3 | }; 4 | -------------------------------------------------------------------------------- /test/fixtures/site/ref3_3.ts: -------------------------------------------------------------------------------- 1 | var f2 = function(): void { 2 | 3 | }; 4 | -------------------------------------------------------------------------------- /test/scaffold_generator_test.rb: -------------------------------------------------------------------------------- 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 -------------------------------------------------------------------------------- /test/support/routes.rb: -------------------------------------------------------------------------------- 1 | # routes dummy file 2 | -------------------------------------------------------------------------------- /test/template_handler_test.rb: -------------------------------------------------------------------------------- 1 | require File.join(File.dirname(__FILE__), 'test_helper.rb') 2 | require 'action_controller' 3 | require 'typescript-rails' 4 | 5 | class SiteController < ActionController::Base 6 | self.view_paths = File.expand_path('../fixtures', __FILE__) 7 | end 8 | 9 | DummyApp = ActionDispatch::Routing::RouteSet.new 10 | DummyApp.draw do 11 | get 'site/index' 12 | get 'site/ref1_1' 13 | get 'site/ref1_2' 14 | get 'site/ref2_1' 15 | get 'site/ref2_2' 16 | get 'site/ref3_1' 17 | get 'site/es5' 18 | end 19 | 20 | class TemplateHandlerTest < ActiveSupport::TestCase 21 | include Rack::Test::Methods 22 | 23 | def app 24 | @app ||= DummyApp 25 | end 26 | 27 | def source 28 | # source without comments 29 | last_response.body.gsub(%r{^//[^\n]*}m, '') 30 | end 31 | 32 | test 'typescript views are served as javascript' do 33 | get '/site/index.js' 34 | assert_match /var x = 5;\s*/, 35 | source 36 | end 37 | 38 | test ' to other .ts file works' do 39 | get '/site/ref1_2.js' 40 | assert_match /var f = function \(x, y\) \{\s*return x \+ y;\s*\};\s*f\(1, 2\);\s*/, 41 | source 42 | end 43 | 44 | test ' to other .d.ts file works' do 45 | get '/site/ref2_2.js' 46 | assert_match /f\(1, 2\);\s*/, 47 | source 48 | end 49 | 50 | test ' to multiple .ts files works' do 51 | get '/site/ref3_1.js' 52 | assert_match /var f1 = function \(\) \{\s*\};\s*var f2 = function \(\) \{\s*\};\s*f1\(\);\s*f2\(\);/, 53 | source 54 | end 55 | 56 | test 'ES5 features' do 57 | get '/site/es5.js' 58 | assert_equal 200, last_response.status 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # Configure coveralls environment 2 | 3 | require 'coveralls' 4 | require 'simplecov' 5 | 6 | SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ 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 | 21 | # For generators 22 | require 'rails/generators/test_case' 23 | 24 | def copy_routes 25 | routes = File.expand_path('../support/routes.rb', __FILE__) 26 | destination = File.join(destination_root, 'config') 27 | 28 | FileUtils.mkdir_p(destination) 29 | FileUtils.cp routes, destination 30 | end -------------------------------------------------------------------------------- /typescript-rails.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | 5 | require 'typescript/rails/version' 6 | 7 | Gem::Specification.new do |gem| 8 | gem.name = 'typescript-rails' 9 | gem.version = Typescript::Rails::VERSION 10 | gem.platform = Gem::Platform::RUBY 11 | gem.authors = ['FUJI, Goro', 'Klaus Zanders'] 12 | gem.email = %w(gfuji@cpan.org klaus.zanders@gmail.com) 13 | gem.description = %q{Adds Typescript to the Rails Asset pipeline} 14 | gem.summary = %q{Adds Typescript to the Rails Asset pipeline} 15 | gem.homepage = 'https://github.com/typescript-ruby/typescript-rails' 16 | 17 | gem.add_runtime_dependency 'typescript-node', '>= 1.6.2' 18 | gem.add_runtime_dependency 'tilt' 19 | gem.add_runtime_dependency 'railties' 20 | 21 | gem.files = `git ls-files`.split($/) 22 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 23 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 24 | gem.require_paths = ['lib'] 25 | 26 | gem.required_ruby_version = '>= 2.0.0' 27 | end 28 | --------------------------------------------------------------------------------