syntax_tree-json 0.1.0

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: 4eb0907bcd118a21b8b293ee2f9b9da43bafd4779d1b3153b107b79cba7d839c
4
+ data.tar.gz: 50b87fe6e6184c083c5bc458e0e88d4b5530e28c766f610930c908fe86f12a4a
5
+ SHA512:
6
+ metadata.gz: 416bf44911bf0c239f9f85f7c00c18c16a2ec5473ae3597c627b91ee25b3866cc6719fcd475eccc32b9b005187e150dbb1392da0825d55ade6680f711ec85df2
7
+ data.tar.gz: 4ba346c41486bcbc9f748ebe88c09bc2da26633031aeb16f6b3fc1705bf6ce3d9d862c6bd59c8c7b5596b11f4f3d724667fc2994a1a4835b1f1e84ddc138d272
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "bundler"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "daily"
@@ -0,0 +1,32 @@
1
+ name: Main
2
+ on:
3
+ - push
4
+ - pull_request_target
5
+ jobs:
6
+ ci:
7
+ name: CI
8
+ runs-on: ubuntu-latest
9
+ env:
10
+ CI: true
11
+ steps:
12
+ - uses: actions/checkout@master
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ bundler-cache: true
16
+ ruby-version: '3.1'
17
+ - name: Test
18
+ run: bundle exec rake test
19
+ automerge:
20
+ name: AutoMerge
21
+ needs: ci
22
+ runs-on: ubuntu-latest
23
+ if: github.event_name == 'pull_request_target' && github.actor == 'dependabot[bot]'
24
+ steps:
25
+ - uses: actions/github-script@v3
26
+ with:
27
+ script: |
28
+ github.pulls.merge({
29
+ owner: context.payload.repository.owner.login,
30
+ repo: context.payload.repository.name,
31
+ pull_number: context.payload.pull_request.number
32
+ })
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ test.json
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2022-03-31
10
+
11
+ ### Added
12
+
13
+ - 🎉 Initial release! 🎉
14
+
15
+ [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree-json/compare/v0.1.0...HEAD
16
+ [0.1.0]: https://github.com/ruby-syntax-tree/syntax_tree-json/compare/c52ff8...v0.1.0
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ syntax_tree-json (0.1.0)
5
+ json
6
+ syntax_tree (>= 2.0.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ docile (1.4.0)
12
+ json (2.6.1)
13
+ minitest (5.15.0)
14
+ rake (13.0.6)
15
+ simplecov (0.21.2)
16
+ docile (~> 1.1)
17
+ simplecov-html (~> 0.11)
18
+ simplecov_json_formatter (~> 0.1)
19
+ simplecov-html (0.12.3)
20
+ simplecov_json_formatter (0.1.4)
21
+ syntax_tree (2.0.1)
22
+
23
+ PLATFORMS
24
+ x86_64-darwin-21
25
+
26
+ DEPENDENCIES
27
+ bundler
28
+ minitest
29
+ rake
30
+ simplecov
31
+ syntax_tree-json!
32
+
33
+ BUNDLED WITH
34
+ 2.3.6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022-present Kevin Newton
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,68 @@
1
+ # SyntaxTree::JSON
2
+
3
+ [![Build Status](https://github.com/ruby-syntax-tree/syntax_tree-json/actions/workflows/main.yml/badge.svg)](https://github.com/ruby-syntax-tree/syntax_tree-json/actions/workflows/main.yml)
4
+ [![Gem Version](https://img.shields.io/gem/v/syntax_tree-json.svg)](https://rubygems.org/gems/syntax_tree-json)
5
+
6
+ [Syntax Tree](https://github.com/ruby-syntax-tree/syntax_tree) support for JSON.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "syntax_tree-json"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install syntax_tree-json
23
+
24
+ ## Usage
25
+
26
+ From code:
27
+
28
+ ```ruby
29
+ require "syntax_tree/json"
30
+
31
+ pp SyntaxTree::JSON.parse(source) # print out the AST
32
+ puts SyntaxTree::JSON.format(source) # format the AST
33
+ ```
34
+
35
+ From the CLI:
36
+
37
+ ```sh
38
+ $ stree ast --plugins=json file.json
39
+ (root children=[(tag name="span" value="Hello, world!")])
40
+ ```
41
+
42
+ or
43
+
44
+ ```sh
45
+ $ stree format --plugins=json file.json
46
+ %span Hello, world!
47
+ ```
48
+
49
+ or
50
+
51
+ ```sh
52
+ $ stree write --plugins=json file.json
53
+ file.json 1ms
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ruby-syntax-tree/syntax_tree-json.
65
+
66
+ ## License
67
+
68
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "syntax_tree/json"
6
+
7
+ require "irb"
8
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SyntaxTree
4
+ module JSON
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "syntax_tree"
5
+
6
+ require_relative "json/version"
7
+
8
+ module SyntaxTree
9
+ module JSON
10
+ # This is always the root of the syntax tree.
11
+ class RootNode < Struct.new(:object)
12
+ def format(q)
13
+ object.format(q)
14
+ q.breakable(force: true)
15
+ end
16
+
17
+ def pretty_print(q)
18
+ q.group(2, "(root", ")") do
19
+ q.breakable
20
+ q.text("object=")
21
+ q.pp(object)
22
+ end
23
+ end
24
+ end
25
+
26
+ # This contains an object node. The values argument to the struct is a hash.
27
+ class ObjectNode < Struct.new(:values)
28
+ def format(q)
29
+ q.group do
30
+ q.text("{")
31
+
32
+ q.indent do
33
+ q.breakable
34
+ q.seplist(values, nil, :each_pair) do |key, value|
35
+ q.group do
36
+ q.text(key.to_json)
37
+ q.text(": ")
38
+ value.format(q)
39
+ end
40
+ end
41
+ end
42
+
43
+ q.breakable
44
+ q.text("}")
45
+ end
46
+ end
47
+
48
+ def pretty_print(q)
49
+ q.group(2, "(object", ")") do
50
+ q.breakable
51
+ q.text("values=")
52
+ q.pp(values)
53
+ end
54
+ end
55
+ end
56
+
57
+ # This contains an array node. The values argument to the struct is an
58
+ # array.
59
+ class ArrayNode < Struct.new(:values)
60
+ def format(q)
61
+ q.group do
62
+ q.text("[")
63
+
64
+ q.indent do
65
+ q.breakable("")
66
+ q.seplist(values) { |value| value.format(q) }
67
+ end
68
+
69
+ q.breakable("")
70
+ q.text("]")
71
+ end
72
+ end
73
+
74
+ def pretty_print(q)
75
+ q.group(2, "(array", ")") do
76
+ q.breakable
77
+ q.text("values=")
78
+ q.pp(values)
79
+ end
80
+ end
81
+ end
82
+
83
+ # This contains a literal node. The value argument to the struct is a
84
+ # literal value like a string, number, or boolean.
85
+ class LiteralNode < Struct.new(:value)
86
+ def format(q)
87
+ q.text(value.to_json)
88
+ end
89
+
90
+ def pretty_print(q)
91
+ q.group(2, "(literal", ")") do
92
+ q.breakable
93
+ q.text("value=")
94
+ q.text(value.inspect)
95
+ end
96
+ end
97
+ end
98
+
99
+ class << self
100
+ def format(source)
101
+ formatter = PP.new([])
102
+ parse(source).format(formatter)
103
+
104
+ formatter.flush
105
+ formatter.output.join
106
+ end
107
+
108
+ def parse(source)
109
+ RootNode.new(translate(::JSON.parse(source)))
110
+ end
111
+
112
+ def read(filepath)
113
+ File.read(filepath)
114
+ end
115
+
116
+ private
117
+
118
+ def translate(object)
119
+ case object
120
+ when Hash
121
+ ObjectNode.new(object.to_h { |key, value| [key, translate(value)] })
122
+ when Array
123
+ ArrayNode.new(object.map { |value| translate(value) })
124
+ else
125
+ LiteralNode.new(object)
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ register_handler(".json", JSON)
132
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/syntax_tree/json/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "syntax_tree-json"
7
+ spec.version = SyntaxTree::JSON::VERSION
8
+ spec.authors = ["Kevin Newton"]
9
+ spec.email = ["kddnewton@gmail.com"]
10
+
11
+ spec.summary = "Syntax Tree support for JSON"
12
+ spec.homepage = "https://github.com/ruby-syntax-tree/syntax_tree-json"
13
+ spec.license = "MIT"
14
+ spec.metadata = { "rubygems_mfa_required" => "true" }
15
+
16
+ spec.files = Dir.chdir(__dir__) do
17
+ `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ end
21
+
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = %w[lib]
25
+
26
+ spec.add_dependency "json"
27
+ spec.add_dependency "syntax_tree", ">= 2.0.1"
28
+
29
+ spec.add_development_dependency "bundler"
30
+ spec.add_development_dependency "minitest"
31
+ spec.add_development_dependency "rake"
32
+ spec.add_development_dependency "simplecov"
33
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: syntax_tree-json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin Newton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: syntax_tree
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - kddnewton@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".github/dependabot.yml"
105
+ - ".github/workflows/main.yml"
106
+ - ".gitignore"
107
+ - CHANGELOG.md
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/syntax_tree/json.rb
116
+ - lib/syntax_tree/json/version.rb
117
+ - syntax_tree-json.gemspec
118
+ homepage: https://github.com/ruby-syntax-tree/syntax_tree-json
119
+ licenses:
120
+ - MIT
121
+ metadata:
122
+ rubygems_mfa_required: 'true'
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.3.3
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Syntax Tree support for JSON
142
+ test_files: []