How to create AnalysisContextCollection without having to pass paths? (using MemoryResourceProvider maybe)

66 views
Skip to first unread message

Abenezer Belay

unread,
Mar 4, 2024, 12:33:06 PM3/4/24
to Dart Analyzer Discussion
This was my attempt to create an in-memory-based AnalysisContext. But it doesn't work. In addition, I am using class I shouldn't be using according to the doc, like ContextLocator and ContextBuilder.

```
import 'package:analyzer/dart/analysis/analysis_context.dart';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/context_builder.dart';
import 'package:analyzer/dart/analysis/context_locator.dart';
import 'package:analyzer/dart/analysis/context_root.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/analysis/session.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/file_system/memory_file_system.dart';
import 'package:analyzer/src/dart/analysis/context_builder.dart';

Future<void> main() async {
final myCode = '''
void main() {
print('Hello, world!')
}
''';

final resourceProvider = MemoryResourceProvider();

final codeFile = resourceProvider.newFile('/my.dart', myCode);
print("path: " + codeFile.path);

final contextLocator = ContextLocator(resourceProvider: resourceProvider);
final contextRoot =
contextLocator.locateRoots(includedPaths: [codeFile.path]).first;
final contextBuilder = ContextBuilder(resourceProvider: resourceProvider);
final analysisContext =
contextBuilder.createContext(contextRoot: contextRoot);
final session = analysisContext.currentSession;
var issueCount = 0;
final errorsResult = await session.getErrors(codeFile.path);
if (errorsResult is ErrorsResult) {
for (final error in errorsResult.errors) {
if (error.errorCode.type != ErrorType.TODO) {
print(' \u001b[1m${error.source.shortName}\u001b[0m ${error.message}');
issueCount++;
}
}
}
}



```

Is there any way to analyze code strings without creating a temporary file?

Brian Wilkerson

unread,
Mar 4, 2024, 12:46:59 PM3/4/24
to analyzer...@dartlang.org
Is there any way to analyze code strings without creating a temporary file?

There are a couple of utility methods in https://github.com/dart-lang/sdk/blob/main/pkg/analyzer/lib/dart/analysis/utilities.dart. I don't know whether they meet your needs.

If you're going to be analyzing more than one file, though, it's more efficient to create analysis contexts in which to do the analysis. The document https://github.com/dart-lang/sdk/blob/main/pkg/analyzer/doc/tutorial/analysis.md explains how to create an `AnalysisContextCollection` to do that (which you can use with a `MemoryResourceProvider`).

Let me know if you have other questions.

If it's easier, you can also reach us on the analyzer-hackers channel on Discord: https://discord.com/channels/608014603317936148/1171510601655275612.

--
You received this message because you are subscribed to the Google Groups "Dart Analyzer Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to analyzer-discu...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/analyzer-discuss/1b9c1a98-7510-496e-957d-be39a3374f99n%40dartlang.org.

Abenezer Belay

unread,
Mar 7, 2024, 1:16:55 PM3/7/24
to Dart Analyzer Discussion, brianwilkerson
Hi @brianwilkerson, 

That was exactly what I wanted. ParseString worked perfectly. Thank you!

Reply all
Reply to author
Forward
0 new messages