Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
- Added support for `enclosing_range` on occurrences definitions
- Upgraded to Dart 3.12.2
- Formatted with Dart 3.12.2
- Upgraded to analyzer `^14.0.0` (migrated to the new element model)
- `unused_element` diagnostics are now reported with `Warning` severity
- same-package deprecated member usages are no longer reported as diagnostics

## 1.6.2
- Fixed a few minor bugs found in pubspec.yaml indexing (skips publish_to: none pubspecs, and considers "version" optional)
Expand Down
12 changes: 8 additions & 4 deletions lib/src/indexer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ Future<Index> indexPackage(

final collection = AnalysisContextCollection(
includedPaths: [...allPackageRoots, dirPath],
// only index dart files of the current dart package, to index nested
// packages, scip indexing can simply be re-run for that nested package
excludedPaths: nestedPackages,
);

if (Flags.instance.performance) print('Analyzing Source');
Expand All @@ -52,6 +49,13 @@ Future<Index> indexPackage(
final resolvedUnitFutures = context.contextRoot
.analyzedFiles()
.where((file) => p.extension(file) == '.dart')
// only index dart files of the current dart package, to index nested
// packages, scip indexing can simply be re-run for that nested package
.where(
(file) => !nestedPackages.any(
(nested) => p.isWithin(p.normalize(p.absolute(nested)), file),
),
)
.map(context.currentSession.getResolvedUnit);

final resolvedUnits = await Future.wait(resolvedUnitFutures);
Expand All @@ -71,7 +75,7 @@ Future<Index> indexPackage(
relativePath,
dirPath,
resUnit.lineInfo,
resUnit.errors,
resUnit.diagnostics,
packageConfig,
pubspec,
);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/kind_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ SymbolInformation_Kind symbolKindFor(Element el) {
return SymbolInformation_Kind.Constructor;
} else if (el is MethodElement) {
return SymbolInformation_Kind.Method;
} else if (el is FunctionElement) {
} else if (el is TopLevelFunctionElement || el is LocalFunctionElement) {
return SymbolInformation_Kind.Function;
} else if (el is TopLevelVariableElement) {
return SymbolInformation_Kind.Variable;
} else if (el is PrefixElement) {
return SymbolInformation_Kind.Namespace;
} else if (el is TypeParameterElement) {
return SymbolInformation_Kind.TypeParameter;
} else if (el is ParameterElement) {
} else if (el is FormalParameterElement) {
return SymbolInformation_Kind.Parameter;
} else if (el is PropertyAccessorElement) {
return SymbolInformation_Kind.Property;
Expand Down
16 changes: 7 additions & 9 deletions lib/src/metadata.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/diagnostic/diagnostic.dart';
import 'package:analyzer/error/error.dart';
import 'package:scip_dart/src/gen/scip.pb.dart' as proto;

/// The collection of AnalysisHint codes that should be considered "Unused".
Expand Down Expand Up @@ -44,12 +43,9 @@ class SymbolMetadata {
SymbolMetadata getSymbolMetadata(
Element element,
int offset,
List<AnalysisError> analysisErrors,
List<Diagnostic> analysisErrors,
) {
final displayString = element.getDisplayString(
withNullability: false,
multiline: true,
);
final displayString = element.displayString(multiline: true);

final docComment = element.documentationComment?.replaceAll(
RegExp(r'^\s*///\s*', multiLine: true),
Expand All @@ -60,12 +56,14 @@ SymbolMetadata getSymbolMetadata(
.where((error) => error.offset == offset)
.map(
(error) => proto.Diagnostic(
code: error.errorCode.name,
code: error.diagnosticCode.lowerCaseName.toUpperCase(),
message: error.message,
severity: error.severity.toProto(),
tags: [
if (element.hasDeprecated) proto.DiagnosticTag.Deprecated,
if (_unusedHintCodes.contains(error.errorCode.uniqueName))
if (element.metadata.hasDeprecated) proto.DiagnosticTag.Deprecated,
if (_unusedHintCodes.contains(
error.diagnosticCode.lowerCaseName.toUpperCase(),
))
proto.DiagnosticTag.Unnecessary,
],
),
Expand Down
10 changes: 5 additions & 5 deletions lib/src/relationship_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ List<Relationship>? relationshipsFor(
element is FieldElement ||
element is PropertyAccessorElement) {
final parentNode = node.thisOrAncestorOfType<ClassDeclaration>();
final parentElement = parentNode?.declaredElement;
final parentElement = parentNode?.declaredFragment?.element;

// this shouldn't happen, but if the parent element happens to be
// null, just fail fast
Expand All @@ -46,16 +46,16 @@ List<Relationship>? relationshipsFor(
.where((type) => type.name == element.name);
} else if (element is FieldElement) {
referencingElements = parentElement.allSupertypes
.expand((type) => type.accessors)
.expand((type) => [...type.getters, ...type.setters])
.map((acc) => acc.variable)
.where((variable) => variable.name == element.name)
.toSet(); // remove any duplicates caused from synthetic getters/setters
}
if (element is PropertyAccessorElement) {
referencingElements = parentElement.allSupertypes
.expand((type) => type.accessors)
.where((acc) => acc.isSetter == element.isSetter)
.where((acc) => acc.isGetter == element.isGetter)
.expand((type) => [...type.getters, ...type.setters])
.where((acc) => (acc is SetterElement) == (element is SetterElement))
.where((acc) => (acc is GetterElement) == (element is GetterElement))
.where((acc) => acc.variable.name == element.variable.name);
}

Expand Down
55 changes: 49 additions & 6 deletions lib/src/scip_visitor.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/diagnostic/diagnostic.dart' as analyzer;
import 'package:analyzer/source/line_info.dart';
import 'package:package_config/package_config.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
Expand All @@ -18,7 +18,7 @@ class ScipVisitor extends GeneralizingAstVisitor {
final String _relativePath;
final String _projectRoot;
final LineInfo _lineInfo;
final List<AnalysisError> _analysisErrors;
final List<analyzer.Diagnostic> _analysisErrors;

final SymbolGenerator _symbolGenerator;

Expand Down Expand Up @@ -52,10 +52,16 @@ class ScipVisitor extends GeneralizingAstVisitor {
// to correctly parse all [Declaration] ast nodes.
if (node is Declaration) {
_visitDeclaration(node);
} else if (node is NormalFormalParameter) {
_visitNormalFormalParameter(node);
} else if (node is FormalParameter) {
_visitFormalParameter(node);
} else if (node is SimpleIdentifier) {
_visitSimpleIdentifier(node);
} else if (node is NamedType) {
_visitNamedType(node);
} else if (node is ImportPrefixReference) {
_visitImportPrefixReference(node);
} else if (node is NamedArgument) {
_visitNamedArgument(node);
}

super.visitNode(node);
Expand All @@ -70,16 +76,17 @@ class ScipVisitor extends GeneralizingAstVisitor {
_registerAsDefinition(element, node, relationships: relationships);
}

void _visitNormalFormalParameter(NormalFormalParameter node) {
void _visitFormalParameter(FormalParameter node) {
final element = _symbolGenerator.elementFor(node);
if (element == null) return;

// if the parameter is a `this.someFieldOnThClass`, we need to register
// it as a reference to said field, as well as a declaration of a parameter.
if (node is FieldFormalParameter) {
final fieldElement = (element as FieldFormalParameterElement).field;
if (fieldElement == null) return;
_registerAsReference(
fieldElement!,
fieldElement,
node,
offset: node.name.offset,
length: node.name.length,
Expand Down Expand Up @@ -110,6 +117,42 @@ class ScipVisitor extends GeneralizingAstVisitor {
}
}

void _visitNamedType(NamedType node) {
final element = _symbolGenerator.elementFor(node);
if (element == null) return;

_registerAsReference(
element,
node,
offset: node.name.offset,
length: node.name.length,
);
}

void _visitImportPrefixReference(ImportPrefixReference node) {
final element = _symbolGenerator.elementFor(node);
if (element == null) return;

_registerAsReference(
element,
node,
offset: node.name.offset,
length: node.name.length,
);
}

void _visitNamedArgument(NamedArgument node) {
final element = _symbolGenerator.elementFor(node);
if (element == null) return;

_registerAsReference(
element,
node,
offset: node.name.offset,
length: node.name.length,
);
}

/// Registers the provided [element] as a reference to an existing definition
///
/// [node] refers to the ast node where the reference exists, [element]
Expand Down
Loading
Loading