Skip to content

Instantly share code, notes, and snippets.

@munificent
Created February 17, 2022 22:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save munificent/58e90ede090d75a46ee522b571a36f37 to your computer and use it in GitHub Desktop.
Save munificent/58e90ede090d75a46ee522b571a36f37 to your computer and use it in GitHub Desktop.
Scrape script to look at what kinds of parameters are most common
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/ast/ast.dart';
import 'package:scrape/scrape.dart';
void main(List<String> arguments) {
Scrape()
..addHistogram('Signatures')
..addHistogram('Parameters')
..addVisitor(() => ParameterVisitor())
..runCommandLine(arguments);
}
class ParameterVisitor extends ScrapeVisitor {
@override
void visitFunctionDeclaration(FunctionDeclaration node) {
recordParameters(node.functionExpression.parameters);
super.visitFunctionDeclaration(node);
}
@override
void visitMethodDeclaration(MethodDeclaration node) {
recordParameters(node.parameters);
super.visitMethodDeclaration(node);
}
void recordParameters(FormalParameterList? parameters) {
if (parameters == null) return;
var signature = <String>[];
for (var parameter in parameters.parameters) {
if (parameter.isRequiredPositional) {
record('Parameters', 'required positional');
signature.add('P');
} else if (parameter.isOptionalPositional) {
record('Parameters', 'optional positional');
signature.add('O');
} else if (parameter.isRequiredNamed) {
record('Parameters', 'required named');
signature.add('R');
} else if (parameter.isOptionalNamed) {
record('Parameters', 'optional named');
signature.add('N');
}
}
record('Signatures', '(${signature.join(',')})');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment