Skip to content
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

CompilationUnit.toSource() - removes comments #34539

Closed
DisDis opened this issue Sep 21, 2018 · 4 comments
Closed

CompilationUnit.toSource() - removes comments #34539

DisDis opened this issue Sep 21, 2018 · 4 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.

Comments

@DisDis
Copy link

DisDis commented Sep 21, 2018

  • Dart SDK Version (dart --version)
    2.0 Stable
    analyzer: 0.32.5
  • Whether you are using Windows, MacOSX, or Linux (if applicable)
    Linux
  • Whether you are using Chrome, Safari, Firefox, Edge (if applicable)
    Console App

If the code contains comments, then when Compilation Unit.toSource() is called, they are removed

   var source = '''
  /* Comment1 */
  /// Comment2 
  class C1 { 
  // Comment 3
  }''';
  var ast = parseCompilationUnit(source);
  print('Equal: ${source == ast.toSource()}');
  print('Orig: "${source}"');
  print('Ast:  "${ast.toSource()}"');

Output:

Equal: false
Orig: "  /* Comment1 */
  /// Comment2 
  class C1 { 
  // Comment 3
  }"
Ast:  "class C1 {}"
@bwilkerson bwilkerson added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Sep 21, 2018
@bwilkerson
Copy link
Member

That is true. The method toSource probably wasn't designed for your use case. Can you tell me what it is that you're trying to do? I might be able to suggest an alternative.

@DisDis
Copy link
Author

DisDis commented Sep 21, 2018

@bwilkerson I want to split the package into several packages. I need to patch the import/export section in dart files.
Code: https://github.com/DisDis/code_health/blob/code_transfer/lib/src/code_transfer/actions.dart#L52

@bwilkerson
Copy link
Member

Ok. The recommended / supported way to modify source code is by modifying the content of the file directly (string manipulation). The analyzer package doesn't support code modification via the AST structure.

The AST nodes carry position information (offset and length), so it's easy to know which range of characters to replace. The only "trick" is to edit the file from largest offset to smallest (by iterating over the directives in reverse order) so that each edit does not invalid the offsets for the next edit.

@DisDis
Copy link
Author

DisDis commented Sep 23, 2018

@bwilkerson Thanks for the help.

@DisDis DisDis closed this as completed Sep 23, 2018
debuggerx01 pushed a commit to debuggerx01/platform_code_builder_starter that referenced this issue Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion.
Projects
None yet
Development

No branches or pull requests

2 participants