Skip to content

Could some formatting decisions be based on sexpred nodes? #333

Open
@lread

Description

Hi @kkinnear, I hope all is well with you!

While reviewing PR clj-commons/rewrite-clj#306 for rewrite-clj, I noticed that a single zprint test failed when running zprint 1.2.9 tests against the changes in the rewrite-clj PR.

Here's the failing test:

FAIL in (guide-tests) (guide_test.cljc:2538)
(zprint-str jr3 {:parse-string? true, :fn-map {":require" [:none {:list {:option-fn (partial jrequireguide :require)}}]}})

matches: "(ns ^:no-doc zprint.zprint\n  #?@(:cljs [[:require-macros\n              [zprint.macros :refer [dbg dbg-pr dbg-form dbg-print zfuture]]]])\n  (:require\n    #?@(:clj [[zprint.macros :refer [dbg-pr dbg dbg-form dbg-print zfuture]]])\n    [clojure.string     "
>>>  expected diverges: ":as s]\n    [zprint.finish      :refer [newline-vec]]\n    [zprint.zfns        :refer [zstring znumstr zbyte-array? zcomment? zsexpr\n                                zseqnws zseqnws-w-nl zfocus-style zstart zfirst\n                                zfirst-no-comment zsecond znthnext zcount zmap\n                                zanonfn? zfn-obj? zfocus zfind-path zwhitespace?\n                                zlist? zcount-zloc-seq-nc-nws zvector? zmap?\n                                zset? zcoll? zuneval? zmeta? ztag zlast zarray?\n                                zatom? zderef zrecord? zns? zobj-to-vec\n                                zexpandarray znewline? zwhitespaceorcomment?\n                                zmap-all zpromise? zfuture? zdelay? zkeyword?\n                                zconstant? zagent? zreader-macro?\n                                zarray-to-shift-seq zdotdotdot zsymbol? znil?\n                                zreader-cond-w-symbol? zreader-cond-w-coll?\n                                zlift-ns zfind zmap-w-nl zmap-w-nl-comma\n                                ztake-append znextnws-w-nl znextnws\n                                znamespacedmap? zmap-w-bl zseqnws-w-bl zsexpr?]]\n    [zprint.comment     :refer [blanks inlinecomment? length-before]]\n    '[zprint.ansi :refer [color-str]]\n    ~`[zprint.config :refer [validate-options merge-deep]]\n    [zprint.zutil       :refer [add-spec-to-docstring]]\n    [rewrite-clj.parser :as p]\n    [rewrite-clj.zip    :as z]\n    #_[taoensso.tufte :as tufte :refer (p defnp profiled profile)]))"
>>>    actual diverges: "  :as s]\n    [zprint.finish        :refer [newline-vec]]\n    [zprint.zfns          :refer\n                            [zstring znumstr zbyte-array? zcomment? zsexpr\n                             zseqnws zseqnws-w-nl zfocus-style zstart zfirst\n                             zfirst-no-comment zsecond znthnext zcount zmap\n                             zanonfn? zfn-obj? zfocus zfind-path zwhitespace?\n                             zlist? zcount-zloc-seq-nc-nws zvector? zmap? zset?\n                             zcoll? zuneval? zmeta? ztag zlast zarray? zatom?\n                             zderef zrecord? zns? zobj-to-vec zexpandarray\n                             znewline? zwhitespaceorcomment? zmap-all zpromise?\n                             zfuture? zdelay? zkeyword? zconstant? zagent?\n                             zreader-macro? zarray-to-shift-seq zdotdotdot\n                             zsymbol? znil? zreader-cond-w-symbol?\n                             zreader-cond-w-coll? zlift-ns zfind zmap-w-nl\n                             zmap-w-nl-comma ztake-append znextnws-w-nl znextnws\n                             znamespacedmap? zmap-w-bl zseqnws-w-bl zsexpr?]]\n    [zprint.comment       :refer [blanks inlinecomment? length-before]]\n    '[zprint.ansi :refer [color-str]]\n    ~`[zprint.config :refer [validate-options merge-deep]]\n    [zprint.zutil         :refer [add-spec-to-docstring]]\n    [rewrite-clj.parser   :as p]\n    [rewrite-clj.zip      :as z]\n    #_[taoensso.tufte :as tufte :refer (p defnp profiled profile)]))"

expected: "(ns ^:no-doc zprint.zprint\n  #?@(:cljs [[:require-macros\n              [zprint.macros :refer [dbg dbg-pr dbg-form dbg-print zfuture]]]])\n  (:require\n    #?@(:clj [[zprint.macros :refer [dbg-pr dbg dbg-form dbg-print zfuture]]])\n    [clojure.string     :as s]\n    [zprint.finish      :refer [newline-vec]]\n    [zprint.zfns        :refer [zstring znumstr zbyte-array? zcomment? zsexpr\n                                zseqnws zseqnws-w-nl zfocus-style zstart zfirst\n                                zfirst-no-comment zsecond znthnext zcount zmap\n                                zanonfn? zfn-obj? zfocus zfind-path zwhitespace?\n                                zlist? zcount-zloc-seq-nc-nws zvector? zmap?\n                                zset? zcoll? zuneval? zmeta? ztag zlast zarray?\n                                zatom? zderef zrecord? zns? zobj-to-vec\n                                zexpandarray znewline? zwhitespaceorcomment?\n                                zmap-all zpromise? zfuture? zdelay? zkeyword?\n                                zconstant? zagent? zreader-macro?\n                                zarray-to-shift-seq zdotdotdot zsymbol? znil?\n                                zreader-cond-w-symbol? zreader-cond-w-coll?\n                                zlift-ns zfind zmap-w-nl zmap-w-nl-comma\n                                ztake-append znextnws-w-nl znextnws\n                                znamespacedmap? zmap-w-bl zseqnws-w-bl zsexpr?]]\n    [zprint.comment     :refer [blanks inlinecomment? length-before]]\n    '[zprint.ansi :refer [color-str]]\n    ~`[zprint.config :refer [validate-options merge-deep]]\n    [zprint.zutil       :refer [add-spec-to-docstring]]\n    [rewrite-clj.parser :as p]\n    [rewrite-clj.zip    :as z]\n    #_[taoensso.tufte :as tufte :refer (p defnp profiled profile)]))"
  actual: "(ns ^:no-doc zprint.zprint\n  #?@(:cljs [[:require-macros\n              [zprint.macros :refer [dbg dbg-pr dbg-form dbg-print zfuture]]]])\n  (:require\n    #?@(:clj [[zprint.macros :refer [dbg-pr dbg dbg-form dbg-print zfuture]]])\n    [clojure.string       :as s]\n    [zprint.finish        :refer [newline-vec]]\n    [zprint.zfns          :refer\n                            [zstring znumstr zbyte-array? zcomment? zsexpr\n                             zseqnws zseqnws-w-nl zfocus-style zstart zfirst\n                             zfirst-no-comment zsecond znthnext zcount zmap\n                             zanonfn? zfn-obj? zfocus zfind-path zwhitespace?\n                             zlist? zcount-zloc-seq-nc-nws zvector? zmap? zset?\n                             zcoll? zuneval? zmeta? ztag zlast zarray? zatom?\n                             zderef zrecord? zns? zobj-to-vec zexpandarray\n                             znewline? zwhitespaceorcomment? zmap-all zpromise?\n                             zfuture? zdelay? zkeyword? zconstant? zagent?\n                             zreader-macro? zarray-to-shift-seq zdotdotdot\n                             zsymbol? znil? zreader-cond-w-symbol?\n                             zreader-cond-w-coll? zlift-ns zfind zmap-w-nl\n                             zmap-w-nl-comma ztake-append znextnws-w-nl znextnws\n                             znamespacedmap? zmap-w-bl zseqnws-w-bl zsexpr?]]\n    [zprint.comment       :refer [blanks inlinecomment? length-before]]\n    '[zprint.ansi :refer [color-str]]\n    ~`[zprint.config :refer [validate-options merge-deep]]\n    [zprint.zutil         :refer [add-spec-to-docstring]]\n    [rewrite-clj.parser   :as p]\n    [rewrite-clj.zip      :as z]\n    #_[taoensso.tufte :as tufte :refer (p defnp profiled profile)]))"
    diff: - "(ns ^:no-doc zprint.zprint\n  #?@(:cljs [[:require-macros\n              [zprint.macros :refer [dbg dbg-pr dbg-form dbg-print zfuture]]]])\n  (:require\n    #?@(:clj [[zprint.macros :refer [dbg-pr dbg dbg-form dbg-print zfuture]]])\n    [clojure.string     :as s]\n    [zprint.finish      :refer [newline-vec]]\n    [zprint.zfns        :refer [zstring znumstr zbyte-array? zcomment? zsexpr\n                                zseqnws zseqnws-w-nl zfocus-style zstart zfirst\n                                zfirst-no-comment zsecond znthnext zcount zmap\n                                zanonfn? zfn-obj? zfocus zfind-path zwhitespace?\n                                zlist? zcount-zloc-seq-nc-nws zvector? zmap?\n                                zset? zcoll? zuneval? zmeta? ztag zlast zarray?\n                                zatom? zderef zrecord? zns? zobj-to-vec\n                                zexpandarray znewline? zwhitespaceorcomment?\n                                zmap-all zpromise? zfuture? zdelay? zkeyword?\n                                zconstant? zagent? zreader-macro?\n                                zarray-to-shift-seq zdotdotdot zsymbol? znil?\n                                zreader-cond-w-symbol? zreader-cond-w-coll?\n                                zlift-ns zfind zmap-w-nl zmap-w-nl-comma\n                                ztake-append znextnws-w-nl znextnws\n                                znamespacedmap? zmap-w-bl zseqnws-w-bl zsexpr?]]\n    [zprint.comment     :refer [blanks inlinecomment? length-before]]\n    '[zprint.ansi :refer [color-str]]\n    ~`[zprint.config :refer [validate-options merge-deep]]\n    [zprint.zutil       :refer [add-spec-to-docstring]]\n    [rewrite-clj.parser :as p]\n    [rewrite-clj.zip    :as z]\n    #_[taoensso.tufte :as tufte :refer (p defnp profiled profile)]))"
          + "(ns ^:no-doc zprint.zprint\n  #?@(:cljs [[:require-macros\n              [zprint.macros :refer [dbg dbg-pr dbg-form dbg-print zfuture]]]])\n  (:require\n    #?@(:clj [[zprint.macros :refer [dbg-pr dbg dbg-form dbg-print zfuture]]])\n    [clojure.string       :as s]\n    [zprint.finish        :refer [newline-vec]]\n    [zprint.zfns          :refer\n                            [zstring znumstr zbyte-array? zcomment? zsexpr\n                             zseqnws zseqnws-w-nl zfocus-style zstart zfirst\n                             zfirst-no-comment zsecond znthnext zcount zmap\n                             zanonfn? zfn-obj? zfocus zfind-path zwhitespace?\n                             zlist? zcount-zloc-seq-nc-nws zvector? zmap? zset?\n                             zcoll? zuneval? zmeta? ztag zlast zarray? zatom?\n                             zderef zrecord? zns? zobj-to-vec zexpandarray\n                             znewline? zwhitespaceorcomment? zmap-all zpromise?\n                             zfuture? zdelay? zkeyword? zconstant? zagent?\n                             zreader-macro? zarray-to-shift-seq zdotdotdot\n                             zsymbol? znil? zreader-cond-w-symbol?\n                             zreader-cond-w-coll? zlift-ns zfind zmap-w-nl\n                             zmap-w-nl-comma ztake-append znextnws-w-nl znextnws\n                             znamespacedmap? zmap-w-bl zseqnws-w-bl zsexpr?]]\n    [zprint.comment       :refer [blanks inlinecomment? length-before]]\n    '[zprint.ansi :refer [color-str]]\n    ~`[zprint.config :refer [validate-options merge-deep]]\n    [zprint.zutil         :refer [add-spec-to-docstring]]\n    [rewrite-clj.parser   :as p]\n    [rewrite-clj.zip      :as z]\n    #_[taoensso.tufte :as tufte :refer (p defnp profiled profile)]))"

I dug around in zprint for a while, and my current best guess is that zprint is (at least sometimes) measuring column widths against sexpred nodes.
As you know, a sexpred node does not necessarily match the length of the node in the original source.
With the new PR we are testing zprint against, the width of certain sexpred nodes is a wider, which might have helped to uncover the (potential) bug in zprint.

Before rewrite-clj PR:

user=> (-> "~a" z/of-string z/sexpr)
(unquote a)

After rewrite-clj PR clojure vars are qualified:

user=> (-> "~a" z/of-string z/sexpr)
(clojure.core/unquote a)

What do you think? Could I be right about my assumption?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions