Skip to content

Commit

Permalink
revert to iteration-based flatten in compiled JavaScript if built-in …
Browse files Browse the repository at this point in the history
…flatten method on array throws range error
  • Loading branch information
mojavelinux committed Jan 7, 2025
1 parent d89be1c commit 10171e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
This document provides a curated view of the changes to Asciidoctor Reducer in each release.
For a detailed view of what has changed, refer to the {url-repo}/commits/main[commit history] on GitHub.

== Unreleased

=== Fixed

* Revert to iteration-based flatten in compiled JavaScript if built-in flatten method on array throws range error

== 1.1.0 (2024-11-24) - @mojavelinux

=== Added
Expand Down
32 changes: 31 additions & 1 deletion lib/asciidoctor/reducer/tree_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def process doc
end
target_lines[idx] = lines if target_lines
end
reduced_source_lines = inc_replacements[0][:lines].flatten
if RUBY_ENGINE == 'opal'
reduced_source_lines = flatten inc_replacements[0][:lines]
else
reduced_source_lines = inc_replacements[0][:lines].flatten
end
if doc.sourcemap
logger = doc.logger
opts = doc.options.merge logger: nil, parse: false, reduced: true
Expand All @@ -42,5 +46,31 @@ def process doc
end
doc
end

private

def flatten input_list
input_list.flatten
rescue ::Exception => ex
raise unless `ex.name === 'RangeError'`
result = []
stack = [[0, input_list, input_list.length]]
until stack.empty?
idx, list, len = stack.pop
while idx < len
if Array === (item = list[idx])
if (next_idx = idx + 1) < len
stack << [next_idx, list, len]
end
idx = 0
len = (list = item).length
else
result << item
idx += 1
end
end
end
result
end
end
end

0 comments on commit 10171e0

Please sign in to comment.