diff --git a/src/core/file/fileManipulate.ts b/src/core/file/fileManipulate.ts index 5ddf5b160..243d867e7 100644 --- a/src/core/file/fileManipulate.ts +++ b/src/core/file/fileManipulate.ts @@ -97,6 +97,161 @@ class CssPreprocessorManipulator extends BaseManipulator { } } +// Dart raw strings (`r"..."`, `r'''...'''`) open on an `r` prefix. The `c` strip profile +// reads that prefix as an identifier, so the following quote never starts a string and a +// `//` inside the literal is cut as a comment — corrupting e.g. `r"https://example.com/a//b"` +// into `r"https:`. Masking the literals before stripping cannot do this safely: a regex +// cannot tell `/* r' prefix */` from a literal, so it swallowed the comment terminator, and +// it only ever saw the two-quote form of a raw string. Read the source the way Dart's own +// lexer does instead — literals copied verbatim, both comment forms dropped (Dart nests +// block comments), newlines kept so the packed output keeps its line structure. +const stripDartComments = (content: string): string => { + let out = ''; + let at = 0; + while (at < content.length) { + if (content.startsWith('//', at)) { + const end = content.indexOf('\n', at); + at = end === -1 ? content.length : end; + continue; + } + if (content.startsWith('/*', at)) { + const end = dartBlockCommentEnd(content, at); + // Newlines stay: the profile this replaces preserves them too. + out += content.slice(at, end).replace(/[^\n]/g, ''); + at = end; + continue; + } + const raw = isDartRawPrefix(content, at); + const quoteAt = raw ? at + 1 : at; + if (content[quoteAt] === "'" || content[quoteAt] === '"') { + const end = dartLiteralEnd(content, quoteAt, raw); + out += content.slice(at, end); + at = end; + continue; + } + out += content[at]; + at += 1; + } + return out; +}; + +// `r` immediately before a quote, and not the tail of an identifier (`bar"x"`). +const isDartRawPrefix = (content: string, at: number): boolean => + content[at] === 'r' && + (content[at + 1] === "'" || content[at + 1] === '"') && + !/[A-Za-z0-9_$]/.test(content[at - 1] ?? ''); + +// Where a block comment ends. Dart nests them, so the closing marker that counts is the one +// that brings the depth back to zero; an unterminated comment runs to the end of the file. +const dartBlockCommentEnd = (content: string, at: number): number => { + let depth = 1; + let index = at + 2; + while (index < content.length) { + if (content.startsWith('/*', index)) { + depth += 1; + index += 2; + continue; + } + if (content.startsWith('*/', index)) { + depth -= 1; + index += 2; + if (depth === 0) return index; + continue; + } + index += 1; + } + return content.length; +}; + +// Where a string literal ends, from the index of its first quote. A raw literal has no +// escapes, and a triple-quoted one is closed by three matching quotes; a single-quoted +// literal cannot span a line, so an unterminated one stops there. +// +// Every non-raw literal can hold a `${...}`, a triple-quoted one included: Dart's lexer +// keeps reading the expression across newlines, and only a single-line literal's own text +// is bound to the line. The scan therefore carries an interpolation depth, and the newline +// rule only applies to a single-line literal at depth zero. Nested literals inside the +// expression are skipped whole, so a quote or a `}` inside one of them does not close the +// outer literal or the interpolation early. Comments inside the expression are skipped the +// same way: a `// note` runs to the end of its line, and a `/* … */` may even nest, so a `}` +// written in one must not count as the end of the interpolation. +const dartLiteralEnd = (content: string, quoteAt: number, raw: boolean): number => { + const quote = content[quoteAt]; + const close = content.startsWith(quote.repeat(3), quoteAt) ? quote.repeat(3) : quote; + const interpolates = !raw; + let index = quoteAt + close.length; + let depth = 0; + while (index < content.length) { + if (!raw && content[index] === '\\') { + index += 2; + continue; + } + if (interpolates && depth === 0 && content.startsWith('${', index)) { + depth = 1; + index += 2; + continue; + } + if (depth > 0) { + if (content.startsWith('//', index)) { + const newline = content.indexOf('\n', index + 2); + index = newline === -1 ? content.length : newline + 1; + continue; + } + if (content.startsWith('/*', index)) { + index = skipDartBlockComment(content, index); + continue; + } + if (content[index] === '{') { + depth += 1; + index += 1; + continue; + } + if (content[index] === '}') { + depth -= 1; + index += 1; + continue; + } + if (content[index] === "'" || content[index] === '"') { + index = dartLiteralEnd(content, index, isDartRawPrefix(content, index - 1)); + continue; + } + index += 1; + continue; + } + if (content.startsWith(close, index)) return index + close.length; + if (close.length === 1 && content[index] === '\n') return index; + index += 1; + } + return content.length; +}; + +// The index just past a `/* … */` comment. Dart lets them nest, so the closing `*/` has to +// match the depth rather than end the first one found. +const skipDartBlockComment = (content: string, start: number): number => { + let depth = 1; + let index = start + 2; + while (index < content.length && depth > 0) { + if (content.startsWith('/*', index)) { + depth += 1; + index += 2; + continue; + } + if (content.startsWith('*/', index)) { + depth -= 1; + index += 2; + continue; + } + index += 1; + } + return index; +}; + +class DartManipulator extends BaseManipulator { + removeComments(content: string): string { + return rtrimLines(stripDartComments(content)); + } +} + // Matches a shell heredoc opener (`< = { '.cxx': new StripCommentsManipulator('cpp'), '.cs': new StripCommentsManipulator('csharp'), '.css': new CssManipulator(), - '.dart': new StripCommentsManipulator('c'), + '.dart': new DartManipulator(), '.go': new StripCommentsManipulator('go'), '.html': new StripCommentsManipulator('html'), '.java': new StripCommentsManipulator('java'), diff --git a/tests/core/file/fileManipulate.test.ts b/tests/core/file/fileManipulate.test.ts index 044a668a1..00b6ef717 100644 --- a/tests/core/file/fileManipulate.test.ts +++ b/tests/core/file/fileManipulate.test.ts @@ -892,6 +892,126 @@ r2 := '\\\\'`, println!("Hello"); } `, + }, + { + name: 'Dart raw strings keep `//` inside them', + ext: '.dart', + input: `void main() { + var double = r"https://example.com/a//b"; + var single = r'https://example.com/a//b'; + var mixedQuote = r'has "double" quotes // not a comment'; + // real comment + print('$double $single'); +}`, + expected: `void main() { + var double = r"https://example.com/a//b"; + var single = r'https://example.com/a//b'; + var mixedQuote = r'has "double" quotes // not a comment'; + + print('$double $single'); +}`, + }, + { + name: 'Dart triple-quoted raw strings keep `//` inside them', + ext: '.dart', + input: `void main() { + var text = r"""hello // world"""; + var other = r'''and // this'''; + print(text); +}`, + expected: `void main() { + var text = r"""hello // world"""; + var other = r'''and // this'''; + print(text); +}`, + }, + { + name: 'Dart raw string syntax inside a block comment is not a literal', + ext: '.dart', + input: `void main() { /* r' prefix */ print('ok'); }`, + expected: `void main() { print('ok'); }`, + }, + { + name: 'Dart adjacent literals keep the comment marker of each one', + ext: '.dart', + input: `var both = 'plain // text' r'raw // text';`, + expected: `var both = 'plain // text' r'raw // text';`, + }, + { + name: 'Dart nested block comments end at the outer marker', + ext: '.dart', + input: `/* outer /* inner */ still */ print('ok');`, + expected: ` print('ok');`, + }, + { + name: 'Dart single-line literals keep a multi-line interpolation intact', + ext: '.dart', + input: `final text = ' \${ + 1 + 2 // still inside the literal +} seen'; +print(text);`, + expected: `final text = ' \${ + 1 + 2 // still inside the literal +} seen'; +print(text);`, + }, + { + name: 'Dart interpolation skips a nested literal that holds a comment marker', + ext: '.dart', + input: `var label = '\${join('a//b')} tail'; +print(label);`, + expected: `var label = '\${join('a//b')} tail'; +print(label);`, + }, + { + name: 'Dart triple-quoted literal keeps a multi-line interpolation comment', + ext: '.dart', + input: `var text = ''' +\${a + + b // still inside the expression +} +'''; +print(text);`, + expected: `var text = ''' +\${a + + b // still inside the expression +} +'''; +print(text);`, + }, + { + name: 'Dart interpolation ignores a line comment that holds a brace', + ext: '.dart', + input: `var s = '\${a // } + + b} tail // kept'; +print(s);`, + expected: `var s = '\${a // } + + b} tail // kept'; +print(s);`, + }, + { + name: 'Dart interpolation keeps a nested block comment that holds a brace', + ext: '.dart', + input: `var a = '\${x /* ' */ + y}'; +var b = 1; // dropped`, + expected: `var a = '\${x /* ' */ + y}'; +var b = 1;`, + }, + { + name: 'Dart triple-quoted literal keeps an interpolation that holds the same quotes', + ext: '.dart', + input: `var s = '''\${'''a // b'''} tail // kept'''; +print(s);`, + expected: `var s = '''\${'''a // b'''} tail // kept'''; +print(s);`, + }, + { + name: 'Dart interpolation skips a nested block comment', + ext: '.dart', + input: `var a = '\${x /* /* ' */ } */ + y}'; +var b = 1; // dropped`, + expected: `var a = '\${x /* /* ' */ } */ + y}'; +var b = 1;`, }, { name: 'Shell script comment removal',