简体   繁体   English

Dart分析仪源需要处理

[英]Dart Analyzer Sources Needing Processing

I am currently using the Dart Analyzer package to parse and analyze Dart files. 我目前正在使用Dart Analyzer包来解析和分析Dart文件。 Currently it is mostly working with the following code. 当前,它主要使用以下代码。

  PhysicalResourceProvider resourceProvider = PhysicalResourceProvider.INSTANCE;
  DartSdk sdk = new FolderBasedDartSdk(
  resourceProvider, resourceProvider.getFolder('D:\\Dart\\dart-sdk'));

  var resolvers = [
    new DartUriResolver(sdk),
    new ResourceUriResolver(resourceProvider)
  ];

  AnalysisContextImpl context = AnalysisEngine.instance.createAnalysisContext()
  ..sourceFactory = new SourceFactory(resolvers);

  Source source = new FileSource(resourceProvider.getFile(item.path));
  ChangeSet changeSet = new ChangeSet()..addedSource(source);
  context.applyChanges(changeSet);
  LibraryElement libElement = context.computeLibraryElement(source);

  CompilationUnit resolvedUnit =
      context.resolveCompilationUnit(source, libElement);

  var element = resolvedUnit.declaredElement;

But this element isn't getting all the information, if it implements or extends classes that are in a different file. 但是,如果该元素实现或扩展了不同文件中的类,则该元素不会获取所有信息。 I eventually found this 我终于找到了

 context.sourcesNeedingProcessing

Where it shows the additional files that are referenced from the dart file, but it seems like they need processing. 它显示了从dart文件引用的其他文件,但似乎需要处理。

If I copy those existing classes, all into 1 file, I get all the information I need, so I know its particularly about getting these files and processing them into the context. 如果将所有现有的类全部复制到1个文件中,则会获得所需的所有信息,因此我特别了解如何获取这些文件并将它们处理到上下文中。

Without seeing the code you're trying to analyze I can't be certain, but I suspect that the problem is that you haven't included a URI resolver for package: URIs. 没有看到您尝试分析的代码,我不确定,但是我怀疑问题是您没有为package:包括URI解析器package: URI。 It isn't necessary to add all of the needed files to the analysis context, you just need to tell it how to find any files referenced from the added files. 不必将所有需要的文件添加到分析上下文中,您只需要告诉它如何查找从添加的文件中引用的任何文件即可。

Unfortunately, building a resolver for package: URIs is non-trivial. 不幸的是,为package:构建一个解析器package: URI是不平凡的。 You need to create an instance of PackageMapUriResolver . 您需要创建PackageMapUriResolver的实例。 That in turn requires using the package_config package to create an instance of Packages , and then convert that into a map (an example of doing that can be found in ContextBuilder.convertPackagesToMap ). 反过来,这需要使用package_config包创建Packages的实例,然后将其转换为映射(可以在ContextBuilder.convertPackagesToMap找到这样做的示例)。

That said, if you're using a recent enough version of the package you could use the newer API that we're designing. 也就是说,如果您使用的软件包版本足够新,则可以使用我们正在设计的较新API。 I believe that what you're trying to do would be achieved by the following code: 我相信您要执行的操作将通过以下代码实现:

import 'package:analyzer/dart/analysis/analysis_context.dart';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/physical_file_system.dart';

main() async {
  ResolvedUnitResult result = await resolveFile(item.path);
  CompilationUnit resolvedUnit = result.unit;
  CompilationUnitElement element = resolvedUnit.declaredElement;
}

Future<ResolvedUnitResult> resolveFile(String path) async {
  AnalysisContextCollection collection = new AnalysisContextCollection(
    includedPaths: <String>[path],
    resourceProvider: PhysicalResourceProvider.INSTANCE,
  );
  AnalysisContext context = collection.contextFor(path);
  return await context.currentSession.getResolvedUnit(path);
}

The collection is able to do all of the work of configuring the contexts for you, simplifying the process of getting analysis results. 该集合能够完成为您配置上下文的所有工作,从而简化了获取分析结果的过程。

A couple of things to note. 需要注意的几件事。 First, the AnalysisContext referenced here is not the same class as the AnalysisContext referenced in your sample code. 首先, AnalysisContext这里引用的是不一样的类作为AnalysisContext在你的示例代码引用。

Second, if you're going to be resolving multiple files then it will potentially be more efficient if you can pass in all of the file paths when creating the AnalysisContextCollection . 其次,如果您要解析多个文件,那么在创建AnalysisContextCollection时可以传入所有文件路径,则可能会更有效。

I added a PackageMapUriResolver as such 我这样添加了PackageMapUriResolver

var resolvers = [
    new DartUriResolver(sdk),
    new ResourceUriResolver(resourceProvider),
    packageResolver(resourceProvider, 'packageName', resourceProvider.getFolder('C:\\folderLocationOfPackage'))
  ];

Then in a separate file I created the packageResolver method. 然后,在一个单独的文件中,我创建了packageResolver方法。

import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/source/package_map_resolver.dart';

PackageMapUriResolver packageResolver(
    ResourceProvider provider, String packageName, Folder folder) {
  Map<String, List<Folder>> packageMap = new Map<String, List<Folder>>();
  packageMap.putIfAbsent(packageName, () => [folder]);
  var resolver = new PackageMapUriResolver(provider, packageMap);
  return resolver;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM