Created
February 17, 2022 22:55
-
-
Save munificent/58e90ede090d75a46ee522b571a36f37 to your computer and use it in GitHub Desktop.
Scrape script to look at what kinds of parameters are most common
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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