De-duplicate comments shared by multiple definitions - #2703
Merged
Conversation
Comments were collected from every definition of a constant and concatenated. `Rubydex::Comment` has no value equality, so the existing `uniq!` never collapsed anything: gems whose files share a license header repeated it once per definition, e.g. 145 times for `module Selenium`. De-duplicate the rendered comment blocks per definition instead, which takes `selenium-webdriver@4.47.0.rbi` from 24,508 to 22,172 lines.
dduugg
marked this pull request as ready for review
August 19, 2026 21:27
dduugg
marked this pull request as draft
August 19, 2026 21:28
dduugg
marked this pull request as ready for review
August 19, 2026 21:29
amomchilov
approved these changes
Aug 20, 2026
vinistock
approved these changes
Aug 20, 2026
Avoid the intermediate array allocation in `comment_lines`, and add a spec documenting that partially overlapping comment blocks are not de-duplicated, since de-duplication happens at the block level.
Member
|
Thank you for the contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Fixes #2702.
tapioca gemrepeats a constant's comments once per definition:selenium-webdriver@4.47.0emits the Apache/SFC license header 145 times in front ofmodule Selenium; end, 2,336 of that file's 24,508 lines.documentation_commentsconcatenates the comments of every definition, and the existingcomments.uniq!cannot collapse them.Rubydex::Commentexposes onlystringandlocation, so==/hashcome fromBasicObject/Kernelanduniq!compares object identity.Implementation
Render and filter the comments per definition, then de-duplicate those blocks before flattening. Per block rather than per line keeps repeated lines within a block (the bare
#separators in a header); after filtering rather than before means headers differing only by an ignored comment (# frozen_string_literal: true) still collapse. The droppedreturn [] if comments.empty?was redundant: no definitions, or all comments filtered out, both fall through to[].Only byte-identical blocks collapse, so a header embedding a per-file copyright line still repeats (
ffigoes from 14x to 12x,kramdownfrom 22x to 3x), unchanged frommain. 2deb3fa also droppedyard_doc.rb'sreturn [] if /(copyright|license)/i.match?(docstring), which is why preambles are documented at all now; restoring it would clear most of the remainder but also drops real docs mentioning "license", so I left it for a follow-up.Tests
Added one spec: a module reopened in three files (two sharing a header, one distinct), a singleton method defined twice with the same doc, and a constant defined twice with the same doc. It fails on
mainwith all three doubled, and fails against a de-duplicate-before-filter variant, so it pins the ordering too.Also generated each of these twice, unpatched
mainworktree vs. this branch, with versions pinned by lockfile reuse:elasticsearch-api@9.5.0selenium-webdriver@4.47.0aws-sdk-core@3.254.1opentelemetry-api@1.11.0googleauth@1.17.3signet@0.22.0activesupport@8.1.3.1jwt@3.2.0faraday@2.14.3rack@3.2.7For all ten, non-comment lines are byte-identical, the set of distinct comment lines is identical (no documentation lost), and "after" is an exact line-subsequence of "before".
--no-docoutput is SHA-256-identical, and a sweep over 124,328 declarations in 138 installed gems found no case where output grew or lost a distinct doc line.