-
Notifications
You must be signed in to change notification settings - Fork 359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #417 preserve location of trailing loud comments #849
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for not realizing this in advance, but I think you can actually do this without touching the parser at all. A comment is trailing iff it starts on the same line as the previous (or parent) statement, right? So you can just compare their span.start.line
values in the serializer to tell if the comment should be treated as "trailing".
No worries. I've reverted the parser changes and instead am now using the span line numbers to look for trailing comments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sass-spec found a crash with the following input:
@media screen and (orientation:landscape) {
span {
background: blue;
}
/* fudge */
// @include foo;
/* budge */
div {
color: red;
}
}
@mixin testComments {
/* crash */
p {
width: 100px;
}
}
@media screen and (orientation:landscape) {
@include testComments;
}
Unexpected exception:
RangeError: Invalid value: Not in range 0..70, inclusive: -44
dart:core _StringBase.lastIndexOf
package:sass/src/visitor/serialize.dart 1146:41 _SerializeVisitor._isTrailingComment
package:sass/src/visitor/serialize.dart 1094:11 _SerializeVisitor._visitChildren
package:sass/src/visitor/serialize.dart 236:5 _SerializeVisitor.visitCssMediaRule
package:sass/src/ast/css/modifiable/media_rule.dart 26:15 ModifiableCssMediaRule.accept
package:sass/src/visitor/serialize.dart 172:13 _SerializeVisitor.visitCssStylesheet
package:sass/src/ast/css/modifiable/stylesheet.dart 19:15 ModifiableCssStylesheet.accept
package:sass/src/visitor/serialize.dart 62:8 serialize
package:sass/src/compile.dart 142:25 _compileStylesheet
package:sass/src/compile.dart 64:10 compile
package:sass/src/executable/compile_stylesheet.dart 91:13 compileStylesheet
package:sass/src/executable.dart 65:15 main
Can you investigate?
I looked at the crash and reduced the repro to the following:
In short, I think we just need to check for |
After the most recent SassSpec test run, there are 4 remaining issues. 2 of them I think are reasonable formatting changes (i.e. we could update SassSpec test cases to match):
One of them involves a standalone loud comment following a block end (rbrace) that's now getting treated as a trailing comment. I can add a test case for this and investigate.
The last one I could use some help parsing/understanding:
e.g. the expected output seems to have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The last one I could use some help parsing/understanding:
--- expected +++ actual "/* upstream comment before import */ -@import \"upstream.css\"; /* midstream comment before use */ +@import \"upstream.css\"; +/* midstream comment before use */ /* midstream comment before first import */ @import \"midstream1.css\"; /* midstream comment before second import */ -@import \"midstream2.css\"; /* input comment before use */ +@import \"midstream2.css\"; +/* input comment before use */ /* input comment before import */ @import \"input.css\"; /* upstream comment after import */
e.g. the expected output seems to have
@import \"upstream.css\"; /* midstream comment before use */
all on one line but I don't see that in the.hrx
file (?)
Don't forget that this PRs is running against the fix-417
branch in sass-spec, so you'll want to look at that branch's version of the HRX file.
test/output_test.dart
Outdated
@@ -162,5 +162,16 @@ selector[href*=\"{\"] { /* please don't move me */ } | |||
|
|||
@rule3;""")); | |||
}); | |||
|
|||
test("loud comments in mixin", () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case raises a couple other possible edge cases:
- What if the loud comment comes after the style rule but still isn't contained within it, for example when the style rule is also in a mixin?
- What if the loud comment is in a totally different file from the mixin?
I'd probably solve these by writing a bool spanContainsLocation(SourceSpan span, SourceLocation location)
helper and checking that in the outer conditional.
parent 1a5102b author Nick Behrens <nbehrens@google.com> 1571200514 -0700 committer Carlos Israel Ortiz García <goodwine@google.com> 1654024100 -0700 Delete now unused _indent helper method in serialize.dart. Update lib/src/visitor/serialize.dart to stop using old-style int-based for loop. Co-Authored-By: Natalie Weizenbaum <nweiz@google.com> Address PR feedback. Revert parser changes. Instead look at node span line numbers to determine whether a loud comment is trailing. Add some more test cases. Remove TODO in _isTrailingComment helper and add a better comment in there. Revert one unintentional chunk of edits to lib/src/parse/sass.dart from commit d937f87. Address some review feedback Update lib/src/visitor/serialize.dart to use var instead of CssNode as for loop variable. Co-Authored-By: Natalie Weizenbaum <nweiz@google.com> Addressing review feedback Remove some now irrelevant comments after last commit. Add some comments on some code I'd like some feedback on in the PR. Fix typo in recently added comment. Remove dupe impl of beforeChildren from CssStylesheet and some other minor cleanup. Rewrite _visitChildren to be simpler. Remove duplicate math import. Restore for loop in visitChildren to how it used to be. Comment cleanup. Don't add a trailing newline after an only-child trailing comment Minor style tweaks Short-circuit _isTrailingComment in compressed mode Update isTrailingComment to handle case where parent node contains left braces that don't open a child block. Address PR feedback, fixing indexing issue in isTrailingComment. Fix isTrailingComment to handle mixins that include loud comments.
…e test is now failing - not done yet
So taking back this conversation to what Nick spotted I saw the following problems:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one comment still WIP
… it to validate whether a comment is trailing
See Issue sass/dart-sass#417 See PR sass/dart-sass#849 Co-authored-by: Carlos Israel Ortiz García <goodwine@google.com> Co-Authored-By: Natalie Weizenbaum <nweiz@google.com>
Fix #417 by preserving the location of trailing loud comments.
Loud comment AST nodes now have the notion of whether they're "trailing". During parsing, we look for loud comments at the end of lines and if found, tag them as trailing. This information is then plumbed through and made available during serialization.
During serialization, where we would previously unconditionally insert newlines between nodes, we now check to see if the next node is a trailing loud comment. If so, we instead write out a space followed by the trailing loud comment.
Ran the following locally, all tests passing:
$ pub run grinder && dartanalyzer lib test && pub run test -x node
See sass/sass-spec#1485