start method
List<AnalysisErrorFixes>
start(
- AnalysisDriver driver,
- String filePath,
- CompilationUnit unit,
- ResolvedUnitResult analysisResult,
Implementation
List<plugin.AnalysisErrorFixes> start(AnalysisDriver driver, String filePath,
CompilationUnit unit, ResolvedUnitResult analysisResult) {
final errors = <plugin.AnalysisErrorFixes>[];
final root = driver.analysisContext?.contextRoot.root.path;
var relative = '';
if (root != null) {
relative = p.relative(filePath, from: root);
final _options = options[driver];
if (_options != null && _options.isExcluded(relative)) {
return [];
}
}
final visitor = StringLiteralVisitor<dynamic>(
filePath: filePath,
unit: unit,
foundStringLiteral: (foundStringLiteral) {
final location = plugin.Location(
foundStringLiteral.filePath,
foundStringLiteral.charOffset,
foundStringLiteral.charLength,
foundStringLiteral.loc.lineNumber,
foundStringLiteral.loc.columnNumber,
endLine: foundStringLiteral.locEnd.lineNumber,
endColumn: foundStringLiteral.locEnd.columnNumber,
);
final content = analysisResult.content;
final semicolonOffset = content.lastIndexOf(
';',
analysisResult.lineInfo
.getOffsetOfLineAfter(foundStringLiteral.charEnd));
final fix = plugin.PrioritizedSourceChange(
1,
plugin.SourceChange(
'Add // NON-NLS',
edits: [
plugin.SourceFileEdit(
filePath,
analysisResult.libraryElement.source.modificationStamp,
edits: [
plugin.SourceEdit(semicolonOffset + 1, 0, ' // NON-NLS'),
],
),
],
),
);
String stringValue() {
if (content.length < foundStringLiteral.charEnd) {
return '';
}
return content
.substring(
foundStringLiteral.charOffset,
foundStringLiteral.charEnd,
)
.trim();
}
final stringCode = foundStringLiteral.stringValue ?? stringValue();
errors.add(
plugin.AnalysisErrorFixes(
plugin.AnalysisError(
plugin.AnalysisErrorSeverity.WARNING,
plugin.AnalysisErrorType.LINT,
location,
'Found string literal: $stringCode',
'found_string_literal',
correction:
'Externalize string or add nonNls() decorator method, '
'or add // NON-NLS to end of line. ($filePath) ($relative)',
hasFix: true,
),
fixes: [fix]),
);
},
);
unit.visitChildren(visitor);
final visitor2 = RtlWidgetVisitor<dynamic>(
filePath: filePath,
unit: unit,
foundRtlWidget: (foundRtlWidget) {
final location = plugin.Location(
foundRtlWidget.filePath,
foundRtlWidget.offset,
foundRtlWidget.length,
foundRtlWidget.startLine!,
foundRtlWidget.startColumn!,
endLine: foundRtlWidget.endLine,
endColumn: foundRtlWidget.endColumn,
);
final content = analysisResult.content;
final semicolonOffset = content.lastIndexOf(
',',
analysisResult.lineInfo
.getOffsetOfLineAfter(foundRtlWidget.offset + foundRtlWidget.length));
final markNone = plugin.PrioritizedSourceChange(
1,
plugin.SourceChange(
'Add // NON-NLS',
edits: [
plugin.SourceFileEdit(
filePath,
analysisResult.libraryElement.source.modificationStamp,
edits: [
plugin.SourceEdit(semicolonOffset + 1, 0, ' // NON-NLS'),
],
),
],
),
);
final fix = plugin.PrioritizedSourceChange(
100,
plugin.SourceChange(
foundRtlWidget.msg,
edits: [
plugin.SourceFileEdit(
filePath,
analysisResult.libraryElement.source.modificationStamp,
edits: [
plugin.SourceEdit(foundRtlWidget.offset, foundRtlWidget.length, foundRtlWidget.fix),
],
),
],
),
);
errors.add(
plugin.AnalysisErrorFixes(
plugin.AnalysisError(
plugin.AnalysisErrorSeverity.WARNING,
plugin.AnalysisErrorType.LINT,
location,
foundRtlWidget.error,
'rtl_incompatiable',
correction:
'${foundRtlWidget.msg} or add // NON-NLS to end of line. ($filePath) ($relative)',
hasFix: true,
),
fixes: [fix, markNone]),
);
},
);
unit.visitChildren(visitor2);
return errors;
}