From 74a9950128738a6ebb7b63991207b327a7a959ce Mon Sep 17 00:00:00 2001 From: Maikel Steneker Date: Thu, 27 Aug 2020 13:16:56 +0200 Subject: [PATCH 1/3] Improvements for Dart interpolated strings The Dart tokenizer was incomplete, resulting in some strings that use string interpolation not being parsed correctly. This has been worked around by making sure that the contents of string interpolation are all included in the same string token. --- .../sourceforge/pmd/lang/dart/antlr4/Dart2.g4 | 34 ++++++++++++------- .../pmd/cpd/DartTokenizerTest.java | 5 ++- .../pmd/cpd/testdata/escape_sequences.dart | 2 +- .../pmd/cpd/testdata/escape_sequences.txt | 2 +- .../cpd/testdata/string_interpolation.dart | 2 ++ .../pmd/cpd/testdata/string_interpolation.txt | 12 +++++++ 6 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.dart create mode 100644 pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.txt diff --git a/pmd-dart/src/main/antlr4/net/sourceforge/pmd/lang/dart/antlr4/Dart2.g4 b/pmd-dart/src/main/antlr4/net/sourceforge/pmd/lang/dart/antlr4/Dart2.g4 index 7d8793edab..01bef8c093 100644 --- a/pmd-dart/src/main/antlr4/net/sourceforge/pmd/lang/dart/antlr4/Dart2.g4 +++ b/pmd-dart/src/main/antlr4/net/sourceforge/pmd/lang/dart/antlr4/Dart2.g4 @@ -351,16 +351,16 @@ SingleLineString fragment StringContentDQ - : ~('\\' | '"' /*| '$'*/ | '\n' | '\r') + : ~('\\' | '"' | '$' | '\n' | '\r') | '\\' ~('\n' | '\r') - //| stringInterpolation + | StringInterpolation ; fragment StringContentSQ - : ~('\\' | '\'' /*| '$'*/ | '\n' | '\r') + : ~('\\' | '\'' | '$' | '\n' | '\r') | '\\' ~('\n' | '\r') - //| stringInterpolation + | StringInterpolation ; MultiLineString @@ -372,15 +372,16 @@ MultiLineString fragment StringContentTDQ - : ~('\\' | '"' /*| '$'*/) + : ~('\\' | '"' | '$') | '"' ~'"' | '""' ~'"' - //| stringInterpolation + | StringInterpolation ; -fragment StringContentTSQ - : ~('\\' | '\'' /*| '$'*/) +fragment +StringContentTSQ + : ~('\\' | '\'' | '$') | '\'' ~'\'' | '\'\'' ~'\'' - //| stringInterpolation + | StringInterpolation ; NEWLINE @@ -390,10 +391,17 @@ NEWLINE ; // 16.5.1 String Interpolation -stringInterpolation -// : '$' IDENTIFIER_NO_DOLLAR - : '$' identifier// FIXME - | '${' expression '}' +fragment +StringInterpolation + : '$' IDENTIFIER_NO_DOLLAR + | '${' StringInterpolationContent* '}' + ; + +fragment +StringInterpolationContent + : ~('$' | '{' | '}') + | '$' IDENTIFIER_NO_DOLLAR + | '${' StringInterpolationContent* '}' ; // 16.6 Symbols diff --git a/pmd-dart/src/test/java/net/sourceforge/pmd/cpd/DartTokenizerTest.java b/pmd-dart/src/test/java/net/sourceforge/pmd/cpd/DartTokenizerTest.java index ec4a769c7e..72e0defad7 100644 --- a/pmd-dart/src/test/java/net/sourceforge/pmd/cpd/DartTokenizerTest.java +++ b/pmd-dart/src/test/java/net/sourceforge/pmd/cpd/DartTokenizerTest.java @@ -54,7 +54,10 @@ public class DartTokenizerTest extends CpdTextComparisonTest { doTest("imports"); } - + @Test + public void testStringInterpolation() { + doTest("string_interpolation"); + } @Test public void testRegex() { diff --git a/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/escape_sequences.dart b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/escape_sequences.dart index 7d70500426..995f806682 100644 --- a/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/escape_sequences.dart +++ b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/escape_sequences.dart @@ -1,3 +1,3 @@ var newline = '\n'; -var dollar = '$'; +var dollar = '$newLine'; var escaped_dollar = "\$"; \ No newline at end of file diff --git a/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/escape_sequences.txt b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/escape_sequences.txt index 12c378a8a6..54e5712200 100644 --- a/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/escape_sequences.txt +++ b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/escape_sequences.txt @@ -8,7 +8,7 @@ L2 [var] 1 3 [dollar] 5 10 [=] 12 12 - ['$'] 14 16 + ['$newLine'] 14 23 L3 [var] 1 3 [escaped_dollar] 5 18 diff --git a/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.dart b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.dart new file mode 100644 index 0000000000..2926939dbd --- /dev/null +++ b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.dart @@ -0,0 +1,2 @@ +var stringInStringUnicode = "${"∆"}"; +var stringInStringNewline = "${"\n")}"; diff --git a/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.txt b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.txt new file mode 100644 index 0000000000..cace985d1f --- /dev/null +++ b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.txt @@ -0,0 +1,12 @@ + [Image] or [Truncated image[ Bcol Ecol +L1 + [var] 1 3 + [stringInStringUnicode] 5 25 + [=] 27 27 + ["${"∆"}"] 29 36 +L2 + [var] 1 3 + [stringInStringNewline] 5 25 + [=] 27 27 + ["${"\\n")}"] 29 38 +EOF From c2069282a7493b087eb6f82a955d8d5f958b4295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sun, 30 Aug 2020 16:45:46 +0200 Subject: [PATCH 2/3] Add test case for more complicated interpolations --- .../pmd/cpd/testdata/string_interpolation.dart | 4 +++- .../pmd/cpd/testdata/string_interpolation.txt | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.dart b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.dart index 2926939dbd..9150decb41 100644 --- a/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.dart +++ b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.dart @@ -1,2 +1,4 @@ var stringInStringUnicode = "${"∆"}"; -var stringInStringNewline = "${"\n")}"; +var stringInStringNewline = "${"\n"}"; +var nestedInterpolation = "${"${"\n"}"}"; +var interpolationWithMethodCall = "${foo("")}"; diff --git a/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.txt b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.txt index cace985d1f..24e3d0dc1e 100644 --- a/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.txt +++ b/pmd-dart/src/test/resources/net/sourceforge/pmd/cpd/testdata/string_interpolation.txt @@ -8,5 +8,15 @@ L2 [var] 1 3 [stringInStringNewline] 5 25 [=] 27 27 - ["${"\\n")}"] 29 38 + ["${"\\n"}"] 29 37 +L3 + [var] 1 3 + [nestedInterpolation] 5 23 + [=] 25 25 + ["${"${"\\n"}"}"] 27 40 +L4 + [var] 1 3 + [interpolationWithMethodCall] 5 31 + [=] 33 33 + ["${foo("")}"] 35 46 EOF From 2cff5dd4b73908b1688bc2a54ac0d7d2a40a0098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sun, 30 Aug 2020 16:49:22 +0200 Subject: [PATCH 3/3] Update release notes --- docs/pages/release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 16a29ca5d7..6022ae5fbb 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -191,6 +191,7 @@ are deprecated as internal API. * [#2733](https://github.com/pmd/pmd/pull/2733): Cleanup: Collection::addAll issues - [XenoAmess](https://github.com/XenoAmess) * [#2734](https://github.com/pmd/pmd/pull/2734): Cleanup: use try with resources - [XenoAmess](https://github.com/XenoAmess) * [#2744](https://github.com/pmd/pmd/pull/2744): Cleanup: fix typos - [XenoAmess](https://github.com/XenoAmess) +* [#2749](https://github.com/pmd/pmd/pull/2749): \[dart] \[cpd] Improvements for Dart interpolated strings - [Maikel Steneker](https://github.com/maikelsteneker) {% endtocmaker %}