syntax_tree-haml 2.0.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61fd378df6679c193bda86bdb1f1f78de949b0978c7ec02ec114e1d8fadcd3b5
4
- data.tar.gz: 1011cad24c3f91ab3086c14b9dda49c6a0093d20c8d3af8fc86286e8dd8851ed
3
+ metadata.gz: '090ea651f3d163420a8d52ec028c152238f2184770d1a1de3771391e37804550'
4
+ data.tar.gz: b9b1241849203a2b0ded757e135319d3f05c200cc6add2ca42991de03de28f7c
5
5
  SHA512:
6
- metadata.gz: 3cfb430120300e2fe6a593a7a47e8be22c09d160960b5a6f6d324e117827d752c648d6111c47d664a8d137cff584aa5fb856d93241c5f7edf2be2360e7e8f8c4
7
- data.tar.gz: 4e4597ea20a6514cc10ff326db244616ebdd41f11d71095f76ca4aabfb1a1c355e76620e024223ff761e5f9f5bcd0eb868ca62323b2ba669a202da8c0ba13d55
6
+ metadata.gz: 9ce10d5f934ab991630d7893a4439a829db161a796f54fa560407e07ab513046dd495cf8b84cd91c3aa59acb68543e0a5b6f622c18c48aa9c9aa2c54ce30378a
7
+ data.tar.gz: 6c706e13ce097c7dbf3484701cb88cb6007683dc825f0a4a0b9666fd0fc0bf0dee5fa1b92ba4e63528d118480a0e66df5aa363a9bb0607c5e08ec8b638e0694b
data/CHANGELOG.md CHANGED
@@ -6,6 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [4.0.0] - 2023-03-07
10
+
11
+ ### Changed
12
+
13
+ - Required syntax_tree version 6.0.0 or higher.
14
+ - Fixed up hash attribute parser to handle multiline hashes using Syntax Tree itself.
15
+
16
+ ## [3.0.0] - 2022-12-23
17
+
18
+ ### Changed
19
+
20
+ - Required syntax_tree version 5.0.1 or higher.
21
+ - Drop internal pattern matching in order to support Ruby implementations that don't support it.
22
+
9
23
  ## [2.0.0] - 2022-10-18
10
24
 
11
25
  ### Added
@@ -72,7 +86,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
72
86
 
73
87
  - 🎉 Initial release! 🎉
74
88
 
75
- [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v2.0.0...HEAD
89
+ [unreleased]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v3.0.0...HEAD
90
+ [3.0.0]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v2.0.0...v3.0.0
76
91
  [2.0.0]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v1.3.2...v2.0.0
77
92
  [1.3.2]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v1.3.1...v1.3.2
78
93
  [1.3.1]: https://github.com/ruby-syntax-tree/syntax_tree-haml/compare/v1.3.0...v1.3.1
data/Gemfile.lock CHANGED
@@ -1,40 +1,36 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree-haml (2.0.0)
5
- haml (>= 5.2, != 6.0.0)
4
+ syntax_tree-haml (4.0.0)
5
+ haml (>= 5.2)
6
6
  prettier_print (>= 1.0.0)
7
- syntax_tree (>= 4.0.0)
7
+ syntax_tree (>= 6.0.0)
8
8
 
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
12
  docile (1.4.0)
13
- haml (6.0.7)
13
+ haml (6.1.1)
14
14
  temple (>= 0.8.2)
15
15
  thor
16
16
  tilt
17
- minitest (5.16.3)
18
- prettier_print (1.0.1)
17
+ minitest (5.18.0)
18
+ prettier_print (1.2.0)
19
19
  rake (13.0.6)
20
- simplecov (0.21.2)
20
+ simplecov (0.22.0)
21
21
  docile (~> 1.1)
22
22
  simplecov-html (~> 0.11)
23
23
  simplecov_json_formatter (~> 0.1)
24
24
  simplecov-html (0.12.3)
25
25
  simplecov_json_formatter (0.1.4)
26
- syntax_tree (4.0.1)
27
- prettier_print (>= 1.0.1)
28
- temple (0.8.2)
26
+ syntax_tree (6.0.2)
27
+ prettier_print (>= 1.2.0)
28
+ temple (0.10.0)
29
29
  thor (1.2.1)
30
- tilt (2.0.11)
30
+ tilt (2.1.0)
31
31
 
32
32
  PLATFORMS
33
- ruby
34
- x86_64-darwin-19
35
- x86_64-darwin-20
36
- x86_64-darwin-21
37
- x86_64-linux
33
+ arm64-darwin-22
38
34
 
39
35
  DEPENDENCIES
40
36
  bundler
@@ -44,4 +40,4 @@ DEPENDENCIES
44
40
  syntax_tree-haml!
45
41
 
46
42
  BUNDLED WITH
47
- 2.3.6
43
+ 2.4.1
@@ -4,9 +4,13 @@ module SyntaxTree
4
4
  module Haml
5
5
  class Format < Visitor
6
6
  class Formatter < ::SyntaxTree::Formatter
7
- attr_reader :literal_lines, :quote
7
+ attr_reader :literal_lines
8
8
 
9
- def initialize(source, *rest)
9
+ def initialize(
10
+ source,
11
+ *rest,
12
+ options: ::SyntaxTree::Formatter::Options.new
13
+ )
10
14
  @literal_lines = {}
11
15
  source
12
16
  .lines
@@ -15,7 +19,7 @@ module SyntaxTree
15
19
  @literal_lines[index] = line.rstrip if line.start_with?("!")
16
20
  end
17
21
 
18
- super(source, *rest)
22
+ super(source, *rest, options: options)
19
23
  end
20
24
  end
21
25
 
@@ -157,7 +161,6 @@ module SyntaxTree
157
161
  end
158
162
 
159
163
  LiteralHashValue = Struct.new(:value)
160
- StringHashValue = Struct.new(:value, :quote)
161
164
 
162
165
  # When formatting a tag, there are a lot of different kinds of things that
163
166
  # can be printed out. There's the tag name, the attributes, the content,
@@ -270,7 +273,7 @@ module SyntaxTree
270
273
  q.group do
271
274
  level == 0 ? q.breakable_empty : q.breakable_space
272
275
  q.seplist(hash, nil, :each_pair) do |key, value|
273
- if key.match?(/^@|[-:]/)
276
+ if key.match?(/^@|[-:]/) && !key.match?(/^["']/)
274
277
  q.text("#{quote}#{Quotes.normalize(key, quote)}#{quote}:")
275
278
  else
276
279
  q.text("#{key}:")
@@ -348,17 +351,7 @@ module SyntaxTree
348
351
  # we're going to print them out here.
349
352
  if node.value[:dynamic_attributes].old
350
353
  parts << PlainPart.new("%div") if parts.empty?
351
-
352
- if ::Haml::AttributeParser.available?
353
- dynamic = parse_attributes(node.value[:dynamic_attributes].old)
354
- parts << if dynamic.is_a?(LiteralHashValue)
355
- PlainPart.new(dynamic.value)
356
- else
357
- HashAttributesPart.new(dynamic)
358
- end
359
- else
360
- parts << PlainPart.new(node.value[:dynamic_attributes].old)
361
- end
354
+ parts << parse_attributes(node.value[:dynamic_attributes].old)
362
355
  end
363
356
 
364
357
  # https://haml.info/docs/yardoc/file.REFERENCE.html#object-reference-
@@ -412,11 +405,11 @@ module SyntaxTree
412
405
  def continuation?(node, child)
413
406
  return false if child.type != :silent_script
414
407
 
415
- case [node.value[:keyword], child.value[:keyword]]
416
- in ["case", "in" | "when" | "else"]
417
- true
418
- in ["if" | "unless", "elsif" | "else"]
419
- true
408
+ case node.value[:keyword]
409
+ when "case"
410
+ %w[in when else].include?(child.value[:keyword])
411
+ when "if", "unless"
412
+ %w[elsif else].include?(child.value[:keyword])
420
413
  else
421
414
  false
422
415
  end
@@ -434,17 +427,52 @@ module SyntaxTree
434
427
  # Take a source string and attempt to parse it into a set of attributes
435
428
  # that can be used to format the source.
436
429
  def parse_attributes(source)
437
- case Ripper.sexp(source)
438
- in [:program, [[:hash, *], *]] if parsed =
439
- ::Haml::AttributeParser.parse(source)
440
- parsed.to_h { |key, value| [key, parse_attributes(value)] }
441
- in [:program, [[:string_literal, *], *]]
442
- SyntaxTree.parse(source).statements.body[0]
443
- else
444
- LiteralHashValue.new(source)
430
+ source = source.strip
431
+ source = source.start_with?("{") ? source : "{#{source}}"
432
+
433
+ program = SyntaxTree.parse(source)
434
+ return PlainPart.new(source) if program.nil?
435
+
436
+ node = program.statements.body.first
437
+ return PlainPart.new(source) unless node.is_a?(HashLiteral)
438
+
439
+ HashAttributesPart.new(parse_attributes_hash(source, node))
440
+ rescue Parser::ParseError
441
+ PlainPart.new(source)
442
+ end
443
+
444
+ def parse_attributes_hash(source, node, level = 1)
445
+ node.assocs.to_h do |assoc|
446
+ key =
447
+ case assoc.key
448
+ when StringLiteral
449
+ format(assoc.key)
450
+ when Label
451
+ assoc.key.value.delete_suffix(":")
452
+ when DynaSymbol
453
+ format(assoc.key).delete_prefix(":")
454
+ else
455
+ format(assoc.key)
456
+ end
457
+
458
+ value =
459
+ case assoc.value
460
+ when HashLiteral
461
+ parse_attributes_hash(source, assoc.value, level + 1)
462
+ when StringLiteral
463
+ assoc.value
464
+ else
465
+ LiteralHashValue.new(format(assoc.value, level * 2).lstrip)
466
+ end
467
+
468
+ [key, value]
445
469
  end
446
470
  end
447
471
 
472
+ def format(node, column = 0)
473
+ SyntaxTree::Formatter.format(+"", node, column)
474
+ end
475
+
448
476
  def with_children(node)
449
477
  if node.children.empty?
450
478
  q.group { yield }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module SyntaxTree
4
4
  module Haml
5
- VERSION = "2.0.0"
5
+ VERSION = "4.0.0"
6
6
  end
7
7
  end
@@ -33,8 +33,8 @@ module SyntaxTree
33
33
 
34
34
  # This is the main entrypoint for the formatter. It parses the source,
35
35
  # builds a formatter, then pretty prints the result.
36
- def self.format(source, maxwidth = 80)
37
- formatter = Format::Formatter.new(source, +"", maxwidth)
36
+ def self.format(source, maxwidth = 80, options: Formatter::Options.new)
37
+ formatter = Format::Formatter.new(source, +"", maxwidth, options: options)
38
38
  parse(source).format(formatter)
39
39
 
40
40
  formatter.flush
@@ -59,24 +59,26 @@ class Haml::Parser::ParseNode
59
59
  # accept a visitor in order to walk through the tree.
60
60
  def accept(visitor)
61
61
  case type
62
- in :comment
62
+ when :comment
63
63
  visitor.visit_comment(self)
64
- in :doctype
64
+ when :doctype
65
65
  visitor.visit_doctype(self)
66
- in :filter
66
+ when :filter
67
67
  visitor.visit_filter(self)
68
- in :haml_comment
68
+ when :haml_comment
69
69
  visitor.visit_haml_comment(self)
70
- in :plain
70
+ when :plain
71
71
  visitor.visit_plain(self)
72
- in :root
72
+ when :root
73
73
  visitor.visit_root(self)
74
- in :script
74
+ when :script
75
75
  visitor.visit_script(self)
76
- in :silent_script
76
+ when :silent_script
77
77
  visitor.visit_silent_script(self)
78
- in :tag
78
+ when :tag
79
79
  visitor.visit_tag(self)
80
+ else
81
+ raise "Unknown node type: #{type}"
80
82
  end
81
83
  end
82
84
 
@@ -24,10 +24,9 @@ Gem::Specification.new do |spec|
24
24
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
25
  spec.require_paths = %w[lib]
26
26
 
27
- # Can't use 6.0.0 due to https://github.com/haml/haml/issues/1085
28
- spec.add_dependency "haml", ">= 5.2", "!= 6.0.0"
27
+ spec.add_dependency "haml", ">= 5.2"
29
28
  spec.add_dependency "prettier_print", ">= 1.0.0"
30
- spec.add_dependency "syntax_tree", ">= 4.0.0"
29
+ spec.add_dependency "syntax_tree", ">= 6.0.0"
31
30
 
32
31
  spec.add_development_dependency "bundler"
33
32
  spec.add_development_dependency "minitest"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syntax_tree-haml
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Newton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-18 00:00:00.000000000 Z
11
+ date: 2023-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml
@@ -17,9 +17,6 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '5.2'
20
- - - "!="
21
- - !ruby/object:Gem::Version
22
- version: 6.0.0
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,9 +24,6 @@ dependencies:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '5.2'
30
- - - "!="
31
- - !ruby/object:Gem::Version
32
- version: 6.0.0
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: prettier_print
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -50,14 +44,14 @@ dependencies:
50
44
  requirements:
51
45
  - - ">="
52
46
  - !ruby/object:Gem::Version
53
- version: 4.0.0
47
+ version: 6.0.0
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
51
  requirements:
58
52
  - - ">="
59
53
  - !ruby/object:Gem::Version
60
- version: 4.0.0
54
+ version: 6.0.0
61
55
  - !ruby/object:Gem::Dependency
62
56
  name: bundler
63
57
  requirement: !ruby/object:Gem::Requirement
@@ -159,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
153
  - !ruby/object:Gem::Version
160
154
  version: '0'
161
155
  requirements: []
162
- rubygems_version: 3.3.21
156
+ rubygems_version: 3.4.1
163
157
  signing_key:
164
158
  specification_version: 4
165
159
  summary: Syntax Tree support for Haml