tscheck 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c65c1c519d31eaa7e9e282fd15a1c254b50c319c1b809eed23cb7e34f50f3cc0
4
+ data.tar.gz: 2981cc4576df0e093575d09c7b0e0933cc553efa31bf2f2d8673df33f27e68fb
5
+ SHA512:
6
+ metadata.gz: 1fe2236102b3d723c02499a4318fc316214fa1bc3ca726cf35e2e8b285e1dc8a1b8a675807a2111c019016ce99e21a49bf1a1403d63d9f73913a849e1fe3f483
7
+ data.tar.gz: fc4dda95fc66695290d03c203702424044c2b3d4be262beb727680a1d4fdced9a4227134210e68e3df1d4aa53d77a79db032a4a7b458dae4fa2907faf464a64b
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ example/Gemfile.lock
14
+ example/.bundle
15
+ *.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # Changelog
2
+
3
+ ## 0.0.1 - initial experimental release
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tscheck.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Leonid Shevtsov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # TSCheck - validate Ruby hashes against TypeScript types
2
+
3
+ If you are making a Ruby API that is consumed by a TypeScript client, and you would like to ensure that the objects you return match the types you expect in TypeScript, this gem is for you.
4
+
5
+ ## Contributing
6
+
7
+ Bug reports and pull requests are welcome on GitHub at https://github.com/leonid-shevtsov/tscheck.
8
+
9
+ ## License
10
+
11
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/tscheck.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "tscheck/version"
2
+
3
+ module TScheck
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,27 @@
1
+ require 'tscheck/tsserver'
2
+ require 'tscheck/rspec/matchers'
3
+
4
+ module TSCheck
5
+ module RSpec
6
+
7
+ def self.setup(options = {})
8
+ @server = TSServer.new(options)
9
+
10
+ ::RSpec.configure do |config|
11
+ config.before(:suite) do
12
+ TSCheck::RSpec.server.start_server
13
+ end
14
+
15
+ config.after(:suite) do
16
+ TSCheck::RSpec.server.stop_server
17
+ end
18
+
19
+ config.include TSCheck::RSpec::Matchers
20
+ end
21
+ end
22
+
23
+ def self.server
24
+ @server
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ require 'tscheck/rspec/matchers/ts_type_matcher'
2
+
3
+ module TSCheck
4
+ module RSpec
5
+ module Matchers
6
+
7
+ def match_ts_type(type, options = {})
8
+ TSCheck::RSpec::Matchers::TSTypeMatcher.new(type, options)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,30 @@
1
+ module TSCheck
2
+ module RSpec
3
+ module Matchers
4
+ class TSTypeMatcher
5
+ def initialize(type, options)
6
+ @type = type
7
+ @options = options
8
+ end
9
+
10
+ def description
11
+ "match TypeScript type #{@type}"
12
+ end
13
+
14
+ def matches?(actual)
15
+ @actual_string = actual.is_a?(String) ? actual : actual.to_json
16
+ @ts_error = TSCheck::RSpec.server.check_type(@actual_string, type: @type, **@options)
17
+ @ts_error.nil?
18
+ end
19
+
20
+ def failure_message
21
+ "expected #{@actual_string} to be of TypeScript type #{@type}, but type check failed with error:\n#{@ts_error}"
22
+ end
23
+
24
+ def failure_message_when_negated
25
+ "expected #{@actual_string} not to be of TypeScript type #{@type}, but type check succeeded"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,101 @@
1
+ require 'json'
2
+
3
+ module TSCheck
4
+ class TSServer
5
+
6
+ def initialize(options = {})
7
+ @ts_base_path = options.fetch(:ts_base_path) { Dir.pwd }
8
+ @ts_types_file_path = options.key?(:ts_types_file_path) ? File.join(@ts_base_path, options[:ts_types_file_path]) : nil
9
+ @tsconfig_path = options[:tsconfig_path] && File.join(@ts_base_path, options[:tsconfig_path])
10
+ @dummy_file_path = "#{@ts_base_path}/__typescript_schema_validator.ts"
11
+ end
12
+
13
+ def start_server
14
+ @tsserver_out, cwrite = IO.pipe
15
+ cread, @tsserver_in = IO.pipe
16
+
17
+ @tsserver_pid = Process.spawn(
18
+ # { 'TSS_LOG' => '-level verbose -logToFile true -file tsserver.log' },
19
+ 'tsserver',
20
+ in: cread,
21
+ out: cwrite,
22
+ err: File::NULL
23
+ )
24
+ cwrite.close
25
+ cread.close
26
+
27
+ @seq = 0
28
+ end
29
+
30
+ def stop_server
31
+ Process.kill('TERM', @tsserver_pid)
32
+ end
33
+
34
+ def check_type(json_string, type:, path: nil, default: false)
35
+ import_statement =
36
+ if path
37
+ import_reference = default ? type : "{ #{type} }"
38
+ fullpath = File.join(@ts_base_path, path)
39
+ "import #{import_reference} from '#{fullpath}';"
40
+ elsif @ts_types_file_path
41
+ "import { #{type} } from '#{@ts_types_file_path}';"
42
+ else
43
+ ''
44
+ end
45
+
46
+ test_file_content = <<~TS
47
+ #{import_statement}
48
+
49
+ const actual: #{type} = #{json_string};
50
+ TS
51
+
52
+ open_arguments = { file: @dummy_file_path, fileContent: test_file_content }
53
+ open_arguments[:projectFileName] = @tsconfig_path if @tsconfig_path
54
+ send_command('open', open_arguments)
55
+ seq = send_command('semanticDiagnosticsSync', file: @dummy_file_path)
56
+ read_response(seq)
57
+ end
58
+
59
+ private
60
+
61
+ def send_command(command, arguments)
62
+ this_seq = @seq
63
+
64
+ payload = {
65
+ seq: this_seq,
66
+ type: 'request',
67
+ command: command,
68
+ arguments: arguments
69
+ }.to_json
70
+
71
+ @tsserver_in.write(payload)
72
+ @tsserver_in.write("\n")
73
+ @seq += 1 # increment for the next request
74
+
75
+ this_seq
76
+ end
77
+
78
+ def read_response(seq)
79
+ content_length_line = @tsserver_out.readline
80
+ unless content_length_line.start_with? 'Content-Length:'
81
+ raise TSCheck::Error, "tsserver: unexpected response, wanted content-length, got: #{content_length_line}"
82
+ end
83
+
84
+ @tsserver_out.readline
85
+ response_line = @tsserver_out.readline
86
+
87
+ response = JSON.parse(response_line, symbolize_names: true)
88
+
89
+ unless response[:request_seq] == seq
90
+ return read_response(seq) # discard and read next response
91
+ end
92
+
93
+ unless response[:success]
94
+ raise TSCheck::Error, "tsserver: error: #{response[:message]}"
95
+ end
96
+
97
+
98
+ response[:body].empty? ? nil : response[:body].map { |report| report[:text] }.join("\n")
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,3 @@
1
+ module TSCheck
2
+ VERSION = "0.0.1"
3
+ end
data/tscheck.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ require_relative 'lib/tscheck/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'tscheck'
5
+ spec.version = TSCheck::VERSION
6
+ spec.authors = ['Leonid Shevtsov']
7
+ spec.email = ['leonid@shevtsov.me']
8
+
9
+ spec.summary = 'Validate Ruby hashes against TypeScript types.'
10
+ spec.description = 'If you are making a Ruby API that is consumed by a TypeScript client, and you would like to ensure that the objects you return match the types you expect in TypeScript, this gem is for you.'
11
+ spec.homepage = 'https://github.com/leonid-shevtsov/tscheck-rb'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/leonid-shevtsov/tscheck-rb'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/leonid-shevtsov/tscheck-rb/blob/master/CHANGELOG.md'
18
+
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|example)/}) }
21
+ end
22
+ spec.require_paths = ['lib']
23
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tscheck
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Leonid Shevtsov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: If you are making a Ruby API that is consumed by a TypeScript client,
14
+ and you would like to ensure that the objects you return match the types you expect
15
+ in TypeScript, this gem is for you.
16
+ email:
17
+ - leonid@shevtsov.me
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".gitignore"
23
+ - ".rspec"
24
+ - CHANGELOG.md
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - lib/tscheck.rb
30
+ - lib/tscheck/rspec.rb
31
+ - lib/tscheck/rspec/matchers.rb
32
+ - lib/tscheck/rspec/matchers/ts_type_matcher.rb
33
+ - lib/tscheck/tsserver.rb
34
+ - lib/tscheck/version.rb
35
+ - tscheck.gemspec
36
+ homepage: https://github.com/leonid-shevtsov/tscheck-rb
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://github.com/leonid-shevtsov/tscheck-rb
41
+ source_code_uri: https://github.com/leonid-shevtsov/tscheck-rb
42
+ changelog_uri: https://github.com/leonid-shevtsov/tscheck-rb/blob/master/CHANGELOG.md
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.3.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.0.3
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Validate Ruby hashes against TypeScript types.
62
+ test_files: []