mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 00:13:50 +00:00
[analysis_server] Make CompilationUnit.lineInfo non-nullable
Change-Id: Ib628751a14dc3257cf7325432ab20f2bdb9d6f3b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/234290 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
parent
26abf4cae2
commit
1e9fb81697
|
@ -45,11 +45,6 @@ abstract class SimpleEditCommandHandler
|
|||
}
|
||||
|
||||
final lineInfo = unit.lineInfo;
|
||||
if (lineInfo == null) {
|
||||
return error(ErrorCodes.InternalError,
|
||||
'Unable to produce edits for $docIdentifier as no LineInfo was found');
|
||||
}
|
||||
|
||||
final workspaceEdit = toWorkspaceEdit(
|
||||
clientCapabilities,
|
||||
[FileEditInformation(docIdentifier, lineInfo, edits)],
|
||||
|
|
|
@ -25,9 +25,6 @@ abstract class AbstractIgnoreDiagnostic extends CorrectionProducer {
|
|||
String ignoreCommentType,
|
||||
) async {
|
||||
final lineInfo = unit.lineInfo;
|
||||
if (lineInfo == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
await builder.addDartFileEdit(file, (builder) {
|
||||
final offset = insertDesc.offset;
|
||||
|
|
|
@ -69,7 +69,7 @@ class ImportOrganizer {
|
|||
|
||||
/// Organize all [Directive]s.
|
||||
void _organizeDirectives() {
|
||||
var lineInfo = unit.lineInfo ?? LineInfo.fromContent(code);
|
||||
var lineInfo = unit.lineInfo;
|
||||
var hasLibraryDirective = false;
|
||||
var directives = <_DirectiveInfo>[];
|
||||
// Track the end offset of any library-level comment/annotations that should
|
||||
|
|
|
@ -792,7 +792,7 @@ class CorrectionUtils {
|
|||
// end
|
||||
var endOffset = sourceRange.end;
|
||||
var afterEndLineOffset = endOffset;
|
||||
var lineInfo = unit.lineInfo!;
|
||||
var lineInfo = unit.lineInfo;
|
||||
var lineStart = lineInfo
|
||||
.getOffsetOfLine(lineInfo.getLocation(startLineOffset).lineNumber - 1);
|
||||
if (lineStart == startLineOffset) {
|
||||
|
|
|
@ -29,7 +29,7 @@ abstract class BaseRangeFactoryTest extends AbstractSingleUnitTest {
|
|||
|
||||
void _assertArgumentRange(int index, SourceRange expectedRange) {
|
||||
var list = _argumentList;
|
||||
expect(range.nodeInListWithComments(testUnit.lineInfo!, list, list[index]),
|
||||
expect(range.nodeInListWithComments(testUnit.lineInfo, list, list[index]),
|
||||
expectedRange);
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ abstract class BaseRangeFactoryTest extends AbstractSingleUnitTest {
|
|||
var class_ = findNode.classDeclaration('class');
|
||||
var list = class_.members;
|
||||
for (var entry in expectedRanges.entries) {
|
||||
expect(range.nodeWithComments(testUnit.lineInfo!, list[entry.key]),
|
||||
expect(range.nodeWithComments(testUnit.lineInfo, list[entry.key]),
|
||||
entry.value);
|
||||
}
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ abstract class BaseRangeFactoryTest extends AbstractSingleUnitTest {
|
|||
void _assertUnitRanges(Map<int, SourceRange> expectedRanges) {
|
||||
var list = testUnit.declarations;
|
||||
for (var entry in expectedRanges.entries) {
|
||||
expect(range.nodeWithComments(testUnit.lineInfo!, list[entry.key]),
|
||||
expect(range.nodeWithComments(testUnit.lineInfo, list[entry.key]),
|
||||
entry.value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,14 +82,14 @@ ParseStringResult parseString(
|
|||
featureSet: featureSet,
|
||||
);
|
||||
var token = scanner.tokenize();
|
||||
var lineInfo = LineInfo(scanner.lineStarts);
|
||||
var parser = Parser(
|
||||
source,
|
||||
errorCollector,
|
||||
featureSet: scanner.featureSet,
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
var unit = parser.parseCompilationUnit(token);
|
||||
var lineInfo = LineInfo(scanner.lineStarts);
|
||||
unit.lineInfo = lineInfo;
|
||||
ParseStringResult result =
|
||||
ParseStringResultImpl(content, unit, errorCollector.errors);
|
||||
if (throwIfDiagnostics && result.errors.isNotEmpty) {
|
||||
|
|
|
@ -1063,7 +1063,7 @@ abstract class CompilationUnit implements AstNode {
|
|||
LanguageVersionToken? get languageVersionToken;
|
||||
|
||||
/// Return the line information for this compilation unit.
|
||||
LineInfo? get lineInfo;
|
||||
LineInfo get lineInfo;
|
||||
|
||||
/// Return the script tag at the beginning of the compilation unit, or `null`
|
||||
/// if there is no script tag in this compilation unit.
|
||||
|
|
|
@ -7,6 +7,7 @@ import 'package:analyzer/dart/analysis/features.dart';
|
|||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:analyzer/source/line_info.dart';
|
||||
import 'package:analyzer/src/generated/utilities_dart.dart';
|
||||
|
||||
/// A collection of factory methods which may be used to create concrete
|
||||
|
@ -175,13 +176,15 @@ abstract class AstFactory {
|
|||
/// `null` (or omitted) if there are no directives in the compilation unit.
|
||||
/// The list of `declarations` can be `null` (or omitted) if there are no
|
||||
/// declarations in the compilation unit.
|
||||
CompilationUnit compilationUnit(
|
||||
{required Token beginToken,
|
||||
ScriptTag? scriptTag,
|
||||
List<Directive>? directives,
|
||||
List<CompilationUnitMember>? declarations,
|
||||
required Token endToken,
|
||||
required FeatureSet featureSet});
|
||||
CompilationUnit compilationUnit({
|
||||
required Token beginToken,
|
||||
ScriptTag? scriptTag,
|
||||
List<Directive>? directives,
|
||||
List<CompilationUnitMember>? declarations,
|
||||
required Token endToken,
|
||||
required FeatureSet featureSet,
|
||||
LineInfo? lineInfo,
|
||||
});
|
||||
|
||||
/// Returns a newly created conditional expression.
|
||||
ConditionalExpression conditionalExpression(
|
||||
|
|
|
@ -550,11 +550,11 @@ class FileState {
|
|||
source,
|
||||
errorListener,
|
||||
featureSet: scanner.featureSet,
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
parser.enableOptionalNewAndConst = true;
|
||||
|
||||
var unit = parser.parseCompilationUnit(token);
|
||||
unit.lineInfo = lineInfo;
|
||||
unit.languageVersion = LibraryLanguageVersion(
|
||||
package: packageLanguageVersion,
|
||||
override: scanner.overrideVersion,
|
||||
|
@ -636,7 +636,7 @@ class FileState {
|
|||
hasPartOfDirective: hasPartOfDirective,
|
||||
imports: imports,
|
||||
informativeBytes: writeUnitInformative(unit),
|
||||
lineStarts: Uint32List.fromList(unit.lineInfo!.lineStarts),
|
||||
lineStarts: Uint32List.fromList(unit.lineInfo.lineStarts),
|
||||
partOfName: null,
|
||||
partOfUri: null,
|
||||
parts: parts,
|
||||
|
|
|
@ -565,7 +565,7 @@ class LibraryAnalyzer {
|
|||
String content = file.content;
|
||||
var unit = file.parse(errorListener);
|
||||
|
||||
_fileToLineInfo[file] = unit.lineInfo!;
|
||||
_fileToLineInfo[file] = unit.lineInfo;
|
||||
_fileToIgnoreInfo[file] = IgnoreInfo.forDart(unit, content);
|
||||
|
||||
return unit;
|
||||
|
|
|
@ -136,7 +136,7 @@ class ParseStringResultImpl implements ParseStringResult {
|
|||
ParseStringResultImpl(this.content, this.unit, this.errors);
|
||||
|
||||
@override
|
||||
LineInfo get lineInfo => unit.lineInfo!;
|
||||
LineInfo get lineInfo => unit.lineInfo;
|
||||
}
|
||||
|
||||
class ResolvedForCompletionResultImpl {
|
||||
|
|
|
@ -2115,7 +2115,7 @@ class CompilationUnitImpl extends AstNodeImpl implements CompilationUnit {
|
|||
|
||||
/// The line information for this compilation unit.
|
||||
@override
|
||||
LineInfo? lineInfo;
|
||||
final LineInfo lineInfo;
|
||||
|
||||
/// The language version information.
|
||||
LibraryLanguageVersion? languageVersion;
|
||||
|
@ -2134,7 +2134,8 @@ class CompilationUnitImpl extends AstNodeImpl implements CompilationUnit {
|
|||
List<Directive>? directives,
|
||||
List<CompilationUnitMember>? declarations,
|
||||
this.endToken,
|
||||
this.featureSet) {
|
||||
this.featureSet,
|
||||
this.lineInfo) {
|
||||
_becomeParentOf(_scriptTag);
|
||||
_directives._initialize(this, directives);
|
||||
_declarations._initialize(this, declarations);
|
||||
|
|
|
@ -8,6 +8,7 @@ import 'package:analyzer/dart/ast/ast.dart';
|
|||
import 'package:analyzer/dart/ast/ast_factory.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:analyzer/source/line_info.dart';
|
||||
import 'package:analyzer/src/dart/ast/ast.dart';
|
||||
import 'package:analyzer/src/generated/utilities_dart.dart';
|
||||
|
||||
|
@ -225,9 +226,14 @@ class AstFactoryImpl extends AstFactory {
|
|||
List<Directive>? directives,
|
||||
List<CompilationUnitMember>? declarations,
|
||||
required Token endToken,
|
||||
required FeatureSet featureSet}) =>
|
||||
required FeatureSet featureSet,
|
||||
// TODO(dantup): LineInfo should be made required and non-nullable
|
||||
// when breaking API changes can be made. Callers that do not
|
||||
// provide lineInfos may have offsets incorrectly mapped to line/col
|
||||
// for LSP.
|
||||
LineInfo? lineInfo}) =>
|
||||
CompilationUnitImpl(beginToken, scriptTag as ScriptTagImpl?, directives,
|
||||
declarations, endToken, featureSet);
|
||||
declarations, endToken, featureSet, lineInfo ?? LineInfo([0]));
|
||||
|
||||
@override
|
||||
ConditionalExpressionImpl conditionalExpression(
|
||||
|
|
|
@ -495,7 +495,7 @@ class LibraryAnalyzer {
|
|||
AnalysisErrorListener errorListener = _getErrorListener(file);
|
||||
var unit = file.parse(errorListener, content);
|
||||
|
||||
LineInfo lineInfo = unit.lineInfo!;
|
||||
LineInfo lineInfo = unit.lineInfo;
|
||||
_fileToLineInfo[file] = lineInfo;
|
||||
_fileToIgnoreInfo[file] = IgnoreInfo.forDart(unit, content);
|
||||
|
||||
|
|
|
@ -870,10 +870,10 @@ class _FileStateUnlinked {
|
|||
location.source,
|
||||
errorListener,
|
||||
featureSet: scanner.featureSet,
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
parser.enableOptionalNewAndConst = true;
|
||||
var unit = parser.parseCompilationUnit(token);
|
||||
unit.lineInfo = lineInfo;
|
||||
|
||||
// StringToken uses a static instance of StringCanonicalizer, so we need
|
||||
// to clear it explicitly once we are done using it for this file.
|
||||
|
@ -961,7 +961,7 @@ class _FileStateUnlinked {
|
|||
hasPartOfDirective: hasPartOfDirective,
|
||||
imports: imports,
|
||||
informativeBytes: writeUnitInformative(unit),
|
||||
lineStarts: Uint32List.fromList(unit.lineInfo!.lineStarts),
|
||||
lineStarts: Uint32List.fromList(unit.lineInfo.lineStarts),
|
||||
partOfName: partOfName,
|
||||
partOfUri: partOfUriStr,
|
||||
parts: parts,
|
||||
|
|
|
@ -204,7 +204,7 @@ class FileResolver {
|
|||
references.add(CiderSearchMatch(
|
||||
path,
|
||||
offsets
|
||||
.map((offset) => lineInfo?.getLocation(offset))
|
||||
.map((offset) => lineInfo.getLocation(offset))
|
||||
.toList()));
|
||||
}
|
||||
});
|
||||
|
|
|
@ -34,7 +34,7 @@ class TodoFinder {
|
|||
///
|
||||
/// @param unit the compilation unit containing the to-do comments
|
||||
void findIn(CompilationUnit unit) {
|
||||
_gatherTodoComments(unit.beginToken, unit.lineInfo!);
|
||||
_gatherTodoComments(unit.beginToken, unit.lineInfo);
|
||||
}
|
||||
|
||||
/// Search the comment tokens reachable from the given token and create errors
|
||||
|
|
|
@ -58,6 +58,7 @@ import 'package:analyzer/dart/analysis/features.dart';
|
|||
import 'package:analyzer/dart/ast/ast.dart';
|
||||
import 'package:analyzer/dart/ast/token.dart' show Token, TokenType;
|
||||
import 'package:analyzer/error/listener.dart';
|
||||
import 'package:analyzer/source/line_info.dart';
|
||||
import 'package:analyzer/src/dart/analysis/experiments.dart';
|
||||
import 'package:analyzer/src/dart/ast/ast.dart'
|
||||
show
|
||||
|
@ -172,8 +173,10 @@ class AstBuilder extends StackListener {
|
|||
|
||||
final FeatureSet _featureSet;
|
||||
|
||||
final LineInfo _lineInfo;
|
||||
|
||||
AstBuilder(ErrorReporter? errorReporter, this.fileUri, this.isFullAst,
|
||||
this._featureSet,
|
||||
this._featureSet, this._lineInfo,
|
||||
[Uri? uri])
|
||||
: errorReporter = FastaErrorReporter(errorReporter),
|
||||
enableNonNullable = _featureSet.isEnabled(Feature.non_nullable),
|
||||
|
@ -1129,7 +1132,8 @@ class AstBuilder extends StackListener {
|
|||
directives: directives,
|
||||
declarations: declarations,
|
||||
endToken: endToken,
|
||||
featureSet: _featureSet);
|
||||
featureSet: _featureSet,
|
||||
lineInfo: _lineInfo);
|
||||
push(unit);
|
||||
}
|
||||
|
||||
|
|
|
@ -30,17 +30,20 @@ class Parser {
|
|||
final AstBuilder astBuilder;
|
||||
|
||||
Parser(Source source, AnalysisErrorListener errorListener,
|
||||
{required FeatureSet featureSet, bool allowNativeClause = true})
|
||||
{required FeatureSet featureSet,
|
||||
bool allowNativeClause = true,
|
||||
required LineInfo lineInfo})
|
||||
: astBuilder = AstBuilder(
|
||||
ErrorReporter(
|
||||
errorListener,
|
||||
source,
|
||||
isNonNullableByDefault:
|
||||
featureSet.isEnabled(Feature.non_nullable),
|
||||
),
|
||||
source.uri,
|
||||
true,
|
||||
featureSet) {
|
||||
ErrorReporter(
|
||||
errorListener,
|
||||
source,
|
||||
isNonNullableByDefault: featureSet.isEnabled(Feature.non_nullable),
|
||||
),
|
||||
source.uri,
|
||||
true,
|
||||
featureSet,
|
||||
lineInfo,
|
||||
) {
|
||||
fastaParser = fasta.Parser(astBuilder);
|
||||
astBuilder.parser = fastaParser;
|
||||
astBuilder.allowNativeClause = allowNativeClause;
|
||||
|
|
|
@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/ast.dart';
|
|||
import 'package:analyzer/dart/ast/token.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/nullability_suffix.dart';
|
||||
import 'package:analyzer/source/line_info.dart';
|
||||
import 'package:analyzer/src/dart/ast/ast.dart';
|
||||
import 'package:analyzer/src/dart/ast/ast_factory.dart';
|
||||
import 'package:analyzer/src/dart/element/type.dart';
|
||||
|
@ -323,7 +324,8 @@ class AstTestFactory {
|
|||
directives: directives,
|
||||
declarations: declarations,
|
||||
endToken: TokenFactory.tokenFromType(TokenType.EOF),
|
||||
featureSet: FeatureSet.latestLanguageVersion());
|
||||
featureSet: FeatureSet.latestLanguageVersion(),
|
||||
lineInfo: LineInfo.fromContent(''));
|
||||
|
||||
static CompilationUnitImpl compilationUnit9(
|
||||
{String? scriptTag,
|
||||
|
@ -337,7 +339,8 @@ class AstTestFactory {
|
|||
directives: directives,
|
||||
declarations: declarations,
|
||||
endToken: TokenFactory.tokenFromType(TokenType.EOF),
|
||||
featureSet: featureSet);
|
||||
featureSet: featureSet,
|
||||
lineInfo: LineInfo.fromContent(''));
|
||||
|
||||
static ConditionalExpressionImpl conditionalExpression(Expression condition,
|
||||
Expression thenExpression, Expression elseExpression) =>
|
||||
|
|
|
@ -77,7 +77,7 @@ class IgnoreInfo {
|
|||
/// Initialize a newly created instance of this class to represent the ignore
|
||||
/// comments in the given compilation [unit].
|
||||
IgnoreInfo.forDart(CompilationUnit unit, String content) {
|
||||
var lineInfo = unit.lineInfo!;
|
||||
var lineInfo = unit.lineInfo;
|
||||
for (var comment in unit.ignoreComments) {
|
||||
var lexeme = comment.lexeme;
|
||||
if (lexeme.contains('ignore:')) {
|
||||
|
|
|
@ -1317,7 +1317,7 @@ class _File {
|
|||
}
|
||||
|
||||
void _buildFileDeclarations(CompilationUnit unit) {
|
||||
lineInfo = unit.lineInfo!;
|
||||
lineInfo = unit.lineInfo;
|
||||
lineStarts = lineInfo.lineStarts;
|
||||
|
||||
isLibrary = true;
|
||||
|
|
|
@ -1051,7 +1051,7 @@ class _InformativeDataWriter {
|
|||
sink.writeUInt30(unit.offset);
|
||||
sink.writeUInt30(unit.length);
|
||||
|
||||
sink.writeUint30List(unit.lineInfo?.lineStarts ?? [0]);
|
||||
sink.writeUint30List(unit.lineInfo.lineStarts);
|
||||
|
||||
_writeLibraryName(unit);
|
||||
|
||||
|
|
|
@ -279,10 +279,12 @@ class FastaParserTestCase
|
|||
? ScannerConfiguration.nonNullable
|
||||
: ScannerConfiguration.classic,
|
||||
includeComments: true);
|
||||
var lineInfo = LineInfo(result.lineStarts);
|
||||
_fastaTokens = result.tokens;
|
||||
parserProxy = ParserProxy(_fastaTokens, featureSet,
|
||||
allowNativeClause: allowNativeClause,
|
||||
expectedEndOffset: expectedEndOffset);
|
||||
expectedEndOffset: expectedEndOffset,
|
||||
lineInfo: lineInfo);
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -416,8 +418,8 @@ class FastaParserTestCase
|
|||
source,
|
||||
isNonNullableByDefault: false,
|
||||
);
|
||||
AstBuilder astBuilder =
|
||||
AstBuilder(errorReporter, source.uri, true, featureSet!);
|
||||
AstBuilder astBuilder = AstBuilder(errorReporter, source.uri, true,
|
||||
featureSet!, LineInfo.fromContent(content));
|
||||
fasta.Parser parser = fasta.Parser(astBuilder);
|
||||
astBuilder.parser = parser;
|
||||
astBuilder.allowNativeClause = allowNativeClause;
|
||||
|
@ -743,19 +745,26 @@ class ParserProxy extends analyzer.Parser {
|
|||
/// Creates a [ParserProxy] which is prepared to begin parsing at the given
|
||||
/// Fasta token.
|
||||
factory ParserProxy(Token firstToken, FeatureSet featureSet,
|
||||
{bool allowNativeClause = false, int? expectedEndOffset}) {
|
||||
{bool allowNativeClause = false,
|
||||
int? expectedEndOffset,
|
||||
required LineInfo lineInfo}) {
|
||||
TestSource source = TestSource();
|
||||
var errorListener = GatheringErrorListener(checkRanges: true);
|
||||
return ParserProxy._(firstToken, source, errorListener, featureSet,
|
||||
allowNativeClause: allowNativeClause,
|
||||
expectedEndOffset: expectedEndOffset);
|
||||
expectedEndOffset: expectedEndOffset,
|
||||
lineInfo: lineInfo);
|
||||
}
|
||||
|
||||
ParserProxy._(Token firstToken, Source source, this.errorListener,
|
||||
FeatureSet featureSet,
|
||||
{bool allowNativeClause = false, this.expectedEndOffset})
|
||||
{bool allowNativeClause = false,
|
||||
this.expectedEndOffset,
|
||||
required LineInfo lineInfo})
|
||||
: super(source, errorListener,
|
||||
featureSet: featureSet, allowNativeClause: allowNativeClause) {
|
||||
featureSet: featureSet,
|
||||
allowNativeClause: allowNativeClause,
|
||||
lineInfo: lineInfo) {
|
||||
_eventListener = ForwardingTestListener(astBuilder);
|
||||
fastaParser.listener = _eventListener;
|
||||
currentToken = firstToken;
|
||||
|
@ -1029,12 +1038,14 @@ class ParserTestCase with ParserTestHelpers implements AbstractParserTestCase {
|
|||
|
||||
fasta.ScannerResult result =
|
||||
fasta.scanString(content, includeComments: true);
|
||||
listener.setLineInfo(source, result.lineStarts);
|
||||
LineInfo lineInfo = LineInfo(result.lineStarts);
|
||||
listener.setLineInfo(source, lineInfo);
|
||||
|
||||
parser = analyzer.Parser(
|
||||
source,
|
||||
listener,
|
||||
featureSet: featureSet,
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
parser.allowNativeClause = allowNativeClause;
|
||||
parser.parseFunctionBodies = parseFunctionBodies;
|
||||
|
@ -1147,12 +1158,14 @@ class ParserTestCase with ParserTestHelpers implements AbstractParserTestCase {
|
|||
|
||||
fasta.ScannerResult result =
|
||||
fasta.scanString(content, includeComments: true);
|
||||
listener.setLineInfo(source, result.lineStarts);
|
||||
LineInfo lineInfo = LineInfo(result.lineStarts);
|
||||
listener.setLineInfo(source, lineInfo);
|
||||
|
||||
analyzer.Parser parser = analyzer.Parser(
|
||||
source,
|
||||
listener,
|
||||
featureSet: FeatureSets.latestWithExperiments,
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
parser.enableOptionalNewAndConst = enableOptionalNewAndConst;
|
||||
CompilationUnit unit = parser.parseCompilationUnit(result.tokens);
|
||||
|
@ -1175,15 +1188,16 @@ class ParserTestCase with ParserTestHelpers implements AbstractParserTestCase {
|
|||
|
||||
fasta.ScannerResult result =
|
||||
fasta.scanString(content, includeComments: true);
|
||||
LineInfo lineInfo = LineInfo(result.lineStarts);
|
||||
|
||||
analyzer.Parser parser = analyzer.Parser(
|
||||
source,
|
||||
listener,
|
||||
featureSet: FeatureSet.latestLanguageVersion(),
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
parser.enableOptionalNewAndConst = enableOptionalNewAndConst;
|
||||
var unit = parser.parseCompilationUnit(result.tokens);
|
||||
unit.lineInfo = LineInfo(result.lineStarts);
|
||||
return unit;
|
||||
}
|
||||
|
||||
|
@ -1452,12 +1466,14 @@ class ParserTestCase with ParserTestHelpers implements AbstractParserTestCase {
|
|||
|
||||
fasta.ScannerResult result =
|
||||
fasta.scanString(content, includeComments: true);
|
||||
listener.setLineInfo(source, result.lineStarts);
|
||||
LineInfo lineInfo = LineInfo(result.lineStarts);
|
||||
listener.setLineInfo(source, lineInfo);
|
||||
|
||||
analyzer.Parser parser = analyzer.Parser(
|
||||
source,
|
||||
listener,
|
||||
featureSet: FeatureSet.latestLanguageVersion(),
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
parser.enableOptionalNewAndConst = enableOptionalNewAndConst;
|
||||
Statement statement = parser.parseStatement(result.tokens);
|
||||
|
|
|
@ -193,7 +193,8 @@ class LineInfoTest {
|
|||
featureSet: featureSet,
|
||||
);
|
||||
Token result = scanner.tokenize();
|
||||
listener.setLineInfo(TestSource(), scanner.lineStarts);
|
||||
LineInfo lineInfo = LineInfo(scanner.lineStarts);
|
||||
listener.setLineInfo(TestSource(), lineInfo);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -398,10 +398,9 @@ class GatheringErrorListener implements AnalysisErrorListener {
|
|||
_errors.add(error);
|
||||
}
|
||||
|
||||
/// Set the line information associated with the given [source] to the given
|
||||
/// list of [lineStarts].
|
||||
void setLineInfo(Source source, List<int> lineStarts) {
|
||||
_lineInfoMap[source] = LineInfo(lineStarts);
|
||||
/// Set the line information associated with the given [source] to [lineInfo].
|
||||
void setLineInfo(Source source, LineInfo lineInfo) {
|
||||
_lineInfoMap[source] = lineInfo;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -33,22 +33,23 @@ class ParseBase with ResourceProviderMixin {
|
|||
);
|
||||
|
||||
var token = scanner.tokenize();
|
||||
var lineInfo = LineInfo(scanner.lineStarts);
|
||||
featureSet = scanner.featureSet;
|
||||
|
||||
var parser = Parser(
|
||||
source,
|
||||
errorListener,
|
||||
featureSet: featureSet,
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
parser.enableOptionalNewAndConst = true;
|
||||
|
||||
var unit = parser.parseCompilationUnit(token);
|
||||
unit.lineInfo = LineInfo(scanner.lineStarts);
|
||||
|
||||
return ParseResult(
|
||||
path,
|
||||
content,
|
||||
unit.lineInfo!,
|
||||
unit.lineInfo,
|
||||
unit,
|
||||
errorListener.errors,
|
||||
);
|
||||
|
|
|
@ -25,6 +25,7 @@ CompilationUnit parseText(
|
|||
featureSet: featureSet,
|
||||
);
|
||||
Token token = scanner.tokenize();
|
||||
LineInfo lineInfo = LineInfo(scanner.lineStarts);
|
||||
// Pass the feature set from the scanner to the parser
|
||||
// because the scanner may have detected a language version comment
|
||||
// and downgraded the feature set it holds.
|
||||
|
@ -32,10 +33,9 @@ CompilationUnit parseText(
|
|||
source,
|
||||
AnalysisErrorListener.NULL_LISTENER,
|
||||
featureSet: scanner.featureSet,
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
var unit = parser.parseCompilationUnit(token);
|
||||
unit.lineInfo = LineInfo(scanner.lineStarts);
|
||||
|
||||
unit.languageVersion = LibraryLanguageVersion(
|
||||
package: ExperimentStatus.currentVersion,
|
||||
override: null,
|
||||
|
|
|
@ -196,7 +196,7 @@ Future<TestResult<T>> runTestForConfig<T>(
|
|||
});
|
||||
} else {
|
||||
String _formatError(AnalysisError e) {
|
||||
var locationInfo = result.unit.lineInfo!.getLocation(e.offset);
|
||||
var locationInfo = result.unit.lineInfo.getLocation(e.offset);
|
||||
return '$locationInfo: ${e.errorCode}: ${e.message}';
|
||||
}
|
||||
|
||||
|
|
|
@ -79,13 +79,15 @@ void collectSources(Source? start, Set<Source?> files) {
|
|||
|
||||
/// Uses the diet-parser to parse only directives in [source].
|
||||
CompilationUnit parseDirectives(Source source) {
|
||||
var token = tokenize(source);
|
||||
var result = tokenize(source);
|
||||
var lineInfo = LineInfo(result.lineStarts);
|
||||
var parser = Parser(
|
||||
source,
|
||||
AnalysisErrorListener.NULL_LISTENER,
|
||||
featureSet: FeatureSet.latestLanguageVersion(),
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
return parser.parseDirectives(token);
|
||||
return parser.parseDirectives(result.tokens);
|
||||
}
|
||||
|
||||
/// Parses every file in [files] and reports the time spent doing so.
|
||||
|
@ -113,13 +115,15 @@ void parseFiles(Set<Source?> files) {
|
|||
|
||||
/// Parse the full body of [source] and return it's compilation unit.
|
||||
CompilationUnit parseFull(Source source) {
|
||||
var token = tokenize(source);
|
||||
var result = tokenize(source);
|
||||
var lineInfo = LineInfo(result.lineStarts);
|
||||
var parser = Parser(
|
||||
source,
|
||||
AnalysisErrorListener.NULL_LISTENER,
|
||||
featureSet: FeatureSet.latestLanguageVersion(),
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
return parser.parseCompilationUnit(token);
|
||||
return parser.parseCompilationUnit(result.tokens);
|
||||
}
|
||||
|
||||
/// Report that metric [name] took [time] micro-seconds to process
|
||||
|
@ -207,7 +211,7 @@ Future setup(String path) async {
|
|||
}
|
||||
|
||||
/// Scan [source] and return the first token produced by the scanner.
|
||||
Token tokenize(Source source) {
|
||||
ScannerResult tokenize(Source source) {
|
||||
scanTimer.start();
|
||||
var contents = source.contents.data;
|
||||
scanTotalChars += contents.length;
|
||||
|
@ -224,5 +228,12 @@ Token tokenize(Source source) {
|
|||
..preserveComments = false;
|
||||
var token = scanner.tokenize();
|
||||
scanTimer.stop();
|
||||
return token;
|
||||
return ScannerResult(token, scanner.lineStarts);
|
||||
}
|
||||
|
||||
class ScannerResult {
|
||||
final Token tokens;
|
||||
final List<int> lineStarts;
|
||||
|
||||
ScannerResult(this.tokens, this.lineStarts);
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import 'package:_fe_analyzer_shared/src/scanner/io.dart'
|
|||
show readBytesFromFileSync;
|
||||
|
||||
import 'package:analyzer/dart/analysis/features.dart';
|
||||
import 'package:analyzer/source/line_info.dart';
|
||||
import 'package:analyzer/src/fasta/ast_builder.dart';
|
||||
import 'package:args/args.dart';
|
||||
|
||||
|
@ -95,11 +96,11 @@ Future setup(Uri entryUri) async {
|
|||
}
|
||||
|
||||
/// Scan [contents] and return the first token produced by the scanner.
|
||||
Token tokenize(List<int> contents) {
|
||||
ScannerResult tokenize(List<int> contents) {
|
||||
scanTimer.start();
|
||||
var token = scan(contents).tokens;
|
||||
var result = scan(contents);
|
||||
scanTimer.stop();
|
||||
return token;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Scans every file in [files] and reports the time spent doing so.
|
||||
|
@ -173,7 +174,7 @@ Set<String> extractDirectiveUris(List<int> contents) {
|
|||
var listener = new DirectiveListenerWithNative();
|
||||
new TopLevelParser(listener,
|
||||
useImplicitCreationExpression: useImplicitCreationExpressionInCfe)
|
||||
.parseUnit(tokenize(contents));
|
||||
.parseUnit(tokenize(contents).tokens);
|
||||
return new Set<String>()
|
||||
..addAll(listener.imports.map((directive) => directive.uri!))
|
||||
..addAll(listener.exports.map((directive) => directive.uri!))
|
||||
|
@ -203,17 +204,19 @@ void parseFiles(Map<Uri, List<int>> files) {
|
|||
|
||||
/// Parse the full body of [source].
|
||||
void parseFull(Uri uri, List<int> source) {
|
||||
var tokens = tokenize(source);
|
||||
Parser parser = new Parser(new _PartialAstBuilder(uri),
|
||||
var result = tokenize(source);
|
||||
var lineInfo = LineInfo(result.lineStarts);
|
||||
Parser parser = new Parser(new _PartialAstBuilder(uri, lineInfo),
|
||||
useImplicitCreationExpression: useImplicitCreationExpressionInCfe);
|
||||
parser.parseUnit(tokens);
|
||||
parser.parseUnit(result.tokens);
|
||||
}
|
||||
|
||||
// Note: AstBuilder doesn't build compilation-units or classes, only method
|
||||
// bodies. So this listener is not feature complete.
|
||||
class _PartialAstBuilder extends AstBuilder {
|
||||
_PartialAstBuilder(Uri uri)
|
||||
: super(null, uri, true, FeatureSet.latestLanguageVersion(), uri);
|
||||
_PartialAstBuilder(Uri uri, LineInfo lineInfo)
|
||||
: super(
|
||||
null, uri, true, FeatureSet.latestLanguageVersion(), lineInfo, uri);
|
||||
}
|
||||
|
||||
// Invoke the fasta kernel generator for the program starting in [entryUri]
|
||||
|
|
|
@ -96,13 +96,15 @@ void collectSources(Source start, Set<Source> files) {
|
|||
|
||||
/// Uses the diet-parser to parse only directives in [source].
|
||||
CompilationUnit parseDirectives(Source source) {
|
||||
var token = tokenize(source);
|
||||
var result = tokenize(source);
|
||||
var lineInfo = LineInfo(result.lineStarts);
|
||||
var parser = new Parser(
|
||||
source,
|
||||
AnalysisErrorListener.NULL_LISTENER,
|
||||
featureSet: FeatureSet.latestLanguageVersion(),
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
return parser.parseDirectives(token);
|
||||
return parser.parseDirectives(result.tokens);
|
||||
}
|
||||
|
||||
/// Parses every file in [files] and reports the time spent doing so.
|
||||
|
@ -119,14 +121,16 @@ void parseFiles(Set<Source> files) {
|
|||
|
||||
/// Parse the full body of [source] and return it's compilation unit.
|
||||
CompilationUnit parseFull(Source source) {
|
||||
var token = tokenize(source);
|
||||
var result = tokenize(source);
|
||||
var lineInfo = LineInfo(result.lineStarts);
|
||||
parseTimer.start();
|
||||
var parser = new Parser(
|
||||
source,
|
||||
AnalysisErrorListener.NULL_LISTENER,
|
||||
featureSet: FeatureSet.latestLanguageVersion(),
|
||||
lineInfo: lineInfo,
|
||||
);
|
||||
var unit = parser.parseCompilationUnit(token);
|
||||
var unit = parser.parseCompilationUnit(result.tokens);
|
||||
parseTimer.stop();
|
||||
return unit;
|
||||
}
|
||||
|
@ -218,7 +222,7 @@ void setup(String path) {
|
|||
}
|
||||
|
||||
/// Scan [source] and return the first token produced by the scanner.
|
||||
Token tokenize(Source source) {
|
||||
ScannerResult tokenize(Source source) {
|
||||
scanTimer.start();
|
||||
// TODO(sigmund): is there a way to scan from a random-access-file without
|
||||
// first converting to String?
|
||||
|
@ -232,7 +236,7 @@ Token tokenize(Source source) {
|
|||
}
|
||||
}
|
||||
scanTimer.stop();
|
||||
return token;
|
||||
return result;
|
||||
}
|
||||
|
||||
String _findSdkPath() {
|
||||
|
|
|
@ -29,7 +29,7 @@ class CodeReference {
|
|||
factory CodeReference.fromAstNode(AstNode node) {
|
||||
var compilationUnit = node.thisOrAncestorOfType<CompilationUnit>()!;
|
||||
var source = compilationUnit.declaredElement!.source;
|
||||
var location = compilationUnit.lineInfo!.getLocation(node.offset);
|
||||
var location = compilationUnit.lineInfo.getLocation(node.offset);
|
||||
return CodeReference(source.fullName, node.offset, location.lineNumber,
|
||||
location.columnNumber, _computeEnclosingName(node));
|
||||
}
|
||||
|
|
|
@ -3515,7 +3515,7 @@ class EdgeBuilder extends GeneralizingAstVisitor<DecoratedType>
|
|||
buffer.write(' in "');
|
||||
buffer.write(node.toSource());
|
||||
buffer.write('" on line ');
|
||||
buffer.write(unit.lineInfo!.getLocation(node.offset).lineNumber);
|
||||
buffer.write(unit.lineInfo.getLocation(node.offset).lineNumber);
|
||||
buffer.write(' of "');
|
||||
buffer.write(unit.declaredElement!.source.fullName);
|
||||
buffer.write('"');
|
||||
|
|
|
@ -433,7 +433,7 @@ class InfoBuilder {
|
|||
var edits = info != null
|
||||
? _computeEdits(info, sourceOffset, result)
|
||||
: <EditDetail>[];
|
||||
var lineNumber = lineInfo!.getLocation(sourceOffset).lineNumber;
|
||||
var lineNumber = lineInfo.getLocation(sourceOffset).lineNumber;
|
||||
var traces = info == null
|
||||
? const <TraceInfo>[]
|
||||
: _computeTraces(info.fixReasons);
|
||||
|
|
|
@ -976,7 +976,7 @@ class NodeBuilder extends GeneralizingAstVisitor<DecoratedType>
|
|||
buffer.write(' in "');
|
||||
buffer.write(node.toSource());
|
||||
buffer.write('" on line ');
|
||||
buffer.write(unit.lineInfo!.getLocation(node.offset).lineNumber);
|
||||
buffer.write(unit.lineInfo.getLocation(node.offset).lineNumber);
|
||||
buffer.write(' of "');
|
||||
buffer.write(unit.declaredElement!.source.fullName);
|
||||
buffer.write('"');
|
||||
|
|
|
@ -414,7 +414,7 @@ class MigrationVisitorTestBase extends AbstractSingleUnitTest with EdgeTester {
|
|||
/// file [offset], with the given [function] name.
|
||||
TypeMatcher<CodeReference> matchCodeRef(
|
||||
{required int offset, required String function}) {
|
||||
var location = testUnit!.lineInfo!.getLocation(offset);
|
||||
var location = testUnit!.lineInfo.getLocation(offset);
|
||||
return TypeMatcher<CodeReference>()
|
||||
.having((cr) => cr.line, 'line', location.lineNumber)
|
||||
.having((cr) => cr.column, 'column', location.columnNumber)
|
||||
|
|
|
@ -267,9 +267,11 @@ class Scrape {
|
|||
scanner.configureFeatures(
|
||||
featureSet: featureSet, featureSetForOverriding: featureSet);
|
||||
var startToken = scanner.tokenize();
|
||||
var lineInfo = LineInfo(scanner.lineStarts);
|
||||
|
||||
// Parse it.
|
||||
var parser = Parser(stringSource, errorListener, featureSet: featureSet);
|
||||
var parser = Parser(stringSource, errorListener,
|
||||
featureSet: featureSet, lineInfo: lineInfo);
|
||||
parser.enableOptionalNewAndConst = true;
|
||||
parser.enableSetLiterals = true;
|
||||
|
||||
|
@ -301,8 +303,6 @@ class Scrape {
|
|||
return;
|
||||
}
|
||||
|
||||
var lineInfo = LineInfo(scanner.lineStarts);
|
||||
|
||||
_scrapedFileCount++;
|
||||
_scrapedLineCount += lineInfo.lineCount;
|
||||
|
||||
|
|
Loading…
Reference in a new issue