This is a follow-up to a recent post: Creating removable comment command without extra space. I'm trying to do a common thing: make a removable comment command so contributors to a document can leave notes for each other and themselves but so that the notes can be automatically removed before production. The trouble is that it's very easy to generate extra space here.
The solution in the aforementioned post turned out to be this:
\newif\ifnotes
\makeatletter
\newcommand{\note}[1]{\ifnotes{#1}\else\@bsphack\@esphack\fi}
\makeatother
It works pretty well, but it's not perfect. Specifically, extra space is added in the event that two notes occur in a row. For instance, consider:
\notesfalse
Testing \note{X} testing testing.
Testing \note{X}\note{Y} testing testing.
Testing \note{X} \note{Y} testing testing.
\notestrue
Testing \note{X} testing testing.
Testing \note{X}\note{Y} testing testing.
Testing \note{X} \note{Y} testing testing.
The above yields:

As we can see, the \@bsphack\@esphack is properly eliminating some of the redundant space and works quite well if only one note is left. But if two notes appear adjacent to each other, more space is generated in the result.
One approach that I've considered is that the \note command could figure out that it was next to another note and then avoid emitting the extra \@bsphack\@esphack. My attempts to build similar commands in the past have gone quite poorly, though. Has anyone encountered something like this? My searches came up empty.
Thanks!
\newcommand{\note}[1]{\ifnotes{#1}\else\ignorespaces\fiwould do the right thing on these tests. – David Carlisle Feb 18 '16 at 19:40\unskipbe a good bit of insurance? – barbara beeton Feb 18 '16 at 20:40