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

UIP-3197 Ignore imports in strings #21

Closed
nickclmb opened this issue Mar 14, 2018 · 7 comments
Closed

UIP-3197 Ignore imports in strings #21

nickclmb opened this issue Mar 14, 2018 · 7 comments

Comments

@nickclmb
Copy link

At the moment dependency validator thinks that this is real import:

void main() {
  final content = '''import 'package:test/test.dart';'''
  ...
}
@evanweible-wf
Copy link
Contributor

Thanks for reporting this @nickclmb! We currently use a regex to quickly detect imports in files, which is why this is getting picked up. This one might be tough to fix without properly using the analyzer to build the AST... any ideas @Workiva/app-frameworks?

@aaronlademann-wf
Copy link

aaronlademann-wf commented Mar 15, 2018

@evanweible-wf a "real" import would always start with the word import, so we could use the ^ character in the regex to specify that?

final RegExp importExportPackageRegex =
new RegExp(r'''\b(import|export)\s+['"]{1,3}package:([a-zA-Z0-9_]+)\/[^;]+''', multiLine: true);

final RegExp importExportPackageRegex =
    new RegExp(r'''^\s*(import|export)\s+['"]{1,3}package:([a-zA-Z0-9_]+)\/[^;]+''', multiLine: true);

Something like that?

@nickclmb
Copy link
Author

nickclmb commented Mar 15, 2018

Hi @edwardcupps-wf, the ^ character will undo the feature when multiple imports written in one row.
Probably something like this will work:

final RegExp importExportPackageRegex = 
     new RegExp(r'''^(\s*(import|export)\s+['"]{1,3}package:([a-zA-Z0-9_]+)\/[^;]+['"]{1,3};)+'''); 

it wraps single import into group, so it can handle multiple imports in a row, and ignore strings like

'import "package:name/path.dart"';
// import 'package:name/path.dart';

@aaronlademann-wf
Copy link

@evanweible-wf ^

@jacehensley-wf
Copy link
Contributor

I think Evan is right, the real path forward would be to use the AST

@greglittlefield-wf
Copy link
Contributor

@evanweible-wf Agreed; we could make adjustments to the Regex to fix this case, but I think our time would be better spent just implementing the AST solution (which I don't foresee taking much time) vs wrestling with edge cases and the Regex.

@rm-astro-wf rm-astro-wf changed the title Ignore imports in strings UIP-3197 Ignore imports in strings Apr 18, 2018
@matthewnitschke-wk
Copy link
Contributor

This issue has been resolved in #111, and will be included in the v4.0.0 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants