Skip to content

Commit

Permalink
feat: Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Apr 2, 2024
1 parent 4d4ada1 commit 53f12b5
Show file tree
Hide file tree
Showing 12 changed files with 1,375 additions and 2 deletions.
27 changes: 27 additions & 0 deletions packages/masamune_lints/.pubignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# See https://dart.dev/tools/pub/publishing#what-files-are-published

# github
example/.github/

# vscode
example/.vscode/

# firebase
example/firebase/

# document
example/document/

# lib
firebase_options.dart
example/ios/
example/android/
example/macos/
example/windows/
example/web/
example/linux/

# Yaml
pubspec_overrides.yaml
katana.yaml
lefthook.yaml
Empty file.
21 changes: 21 additions & 0 deletions packages/masamune_lints/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 mathru (https://mathru.net)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
40 changes: 40 additions & 0 deletions packages/masamune_lints/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<p align="center">
<a href="https://mathru.net">
<img width="240px" src="https://raw.githubusercontent.com/mathrunet/flutter_masamune/master/.github/images/icon.png" alt="Masamune logo" style="border-radius: 32px"s><br/>
</a>
<h1 align="center">Masamune Lints</h1>
</p>

<p align="center">
<a href="https://twitter.com/mathru">
<img src="https://img.shields.io/static/v1?label=Twitter&message=Follow&logo=Twitter&color=1DA1F2&link=https://twitter.com/mathru" alt="Follow on Twitter" />
</a>
<a href="https://threads.net/@mathrunet">
<img src="https://img.shields.io/static/v1?label=Threads&message=Follow&color=101010&link=https://threads.net/@mathrunet" alt="Follow on Threads" />
</a>
<a href="https://github.com/invertase/melos">
<img src="https://img.shields.io/static/v1?label=maintained%20with&message=melos&color=FF1493&link=https://github.com/invertase/melos" alt="Maintained with Melos" />
</a>
</p>

<p align="center">
<a href="https://github.com/sponsors/mathrunet"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=ff69b4&link=https://github.com/sponsors/mathrunet" alt="GitHub Sponsor" /></a>
</p>

---

[[GitHub]](https://github.com/mathrunet) | [[YouTube]](https://www.youtube.com/c/mathrunetchannel) | [[Packages]](https://pub.dev/publishers/mathru.net/packages) | [[Twitter]](https://twitter.com/mathru) | [[Threads]](https://threads.net/@mathrunet) | [[LinkedIn]](https://www.linkedin.com/in/mathrunet/) | [[mathru.net]](https://mathru.net)

---

Plug-in packages that add functionality to the Masamune Framework.

For more information about Masamune Framework, please click here.

[https://pub.dev/packages/masamune](https://pub.dev/packages/masamune)

# GitHub Sponsors

Sponsors are always welcome. Thank you for your support!

[https://github.com/sponsors/mathrunet](https://github.com/sponsors/mathrunet)
38 changes: 38 additions & 0 deletions packages/masamune_lints/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
use_build_context_synchronously: false
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

# Set to exclude json_serializable files.
# json_serializableのファイルを除外するための設定。
analyzer:
exclude:
- "**/*.g.dart"
- "**/*.freezed.dart"
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
part of '/masamune_lints.dart';

class _MasamuneCollectionModelShouldAddLimitQuery extends DartLintRule {
const _MasamuneCollectionModelShouldAddLimitQuery() : super(code: _code);

static const _code = LintCode(
name: "masamune_model_should_add_limit_query",
problemMessage:
"When querying the collection model, it is necessary to limit the number with [limitTo]. コレクションモデルをクエリする場合は[limitTo]で数の制限をする必要があります。",
errorSeverity: ErrorSeverity.WARNING,
);

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
final res = <_MasamuneCollectionModelShouldAddLimitQueryValue>[];

/// Functionの実行時
context.registry.addFunctionExpressionInvocation((node) {
final buildMethod = node.thisOrAncestorOfType<MethodDeclaration>();
if (buildMethod == null || buildMethod.name.lexeme != "build") {
return;
}
final found = res.firstWhereOrNull((e) => e.node == node);
if (found != null) {
// 直接limitToメソッドを呼び出しているときこちらは後に呼ばれる
found.isLimit = true;
return;
}
final function = node.function;
if (function is! PropertyAccess) {
return;
}
final functionType = function.staticType;
if (functionType is! InterfaceType ||
!functionType.toString().endsWith("ModelCollectionQuery")) {
return;
}
final variable = node.thisOrAncestorOfType<VariableDeclaration>();
if (variable == null) {
res.add(
_MasamuneCollectionModelShouldAddLimitQueryValue()
..method = node
..node = node,
);
} else {
res.add(
_MasamuneCollectionModelShouldAddLimitQueryValue()
..variableName = variable.name.lexeme
..variable = variable
..method = node
..node = node,
);
}
});

// メソッドの実行時
context.registry.addMethodInvocation((node) {
final buildMethod = node.thisOrAncestorOfType<MethodDeclaration>();
if (buildMethod == null || buildMethod.name.lexeme != "build") {
return;
}
final functionName = node.methodName.name;
switch (functionName) {
case "limitTo":
// 変数に入れていないとき
final parentMethodInvocationNode = node.target
?.thisOrAncestorOfType<FunctionExpressionInvocation>() ??
node.thisOrAncestorOfType<FunctionExpressionInvocation>();
if (parentMethodInvocationNode != null) {
final found = res.firstWhereOrNull(
(e) => e.method == parentMethodInvocationNode);
if (found != null) {
found.isLimit = true;
} else {
res.add(
_MasamuneCollectionModelShouldAddLimitQueryValue()
..method = parentMethodInvocationNode
..node = parentMethodInvocationNode,
);
}
return;
}
// 変数に入れていないときかつカスケードでメソッドを呼び出しているとき
final parentCascadeExpressionVariableNode =
node.parent?.thisOrAncestorOfType<CascadeExpression>();
if (parentCascadeExpressionVariableNode != null) {
final parentMethodInvocationNode =
parentCascadeExpressionVariableNode.target
.thisOrAncestorOfType<FunctionExpressionInvocation>();
if (parentMethodInvocationNode != null) {
final found = res.firstWhereOrNull(
(e) => e.method == parentMethodInvocationNode);
if (found != null) {
found.isLimit = true;
} else {
res.add(
_MasamuneCollectionModelShouldAddLimitQueryValue()
..method = parentMethodInvocationNode
..node = parentMethodInvocationNode,
);
}
return;
}
}
// 変数に入れているとき
final parentVariableDeclarationNode =
node.thisOrAncestorOfType<VariableDeclaration>();
if (parentVariableDeclarationNode != null) {
final found = res.firstWhereOrNull(
(e) => e.variable == parentVariableDeclarationNode);
if (found != null) {
found.isLimit = true;
return;
}
}
// 変数に入れているときかつメソッドを呼び出しているとき
final simpleIdentifier = node.thisOrTargetOfType<SimpleIdentifier>();
if (simpleIdentifier != null) {
final found = res.firstWhereOrNull(
(e) => e.variableName == simpleIdentifier.name);
found?.isLimit = true;
return;
}
final functionExpressionInvocation =
node.thisOrTargetOfType<FunctionExpressionInvocation>();
if (functionExpressionInvocation != null) {
final found = res.firstWhereOrNull(
(e) => e.method == functionExpressionInvocation);
if (found != null) {
found.isLimit = true;
} else {
res.add(
_MasamuneCollectionModelShouldAddLimitQueryValue()
..method = functionExpressionInvocation
..node = functionExpressionInvocation,
);
}
}
break;
}
});

// すべての処理が終わった後
context.addPostRunCallback(() {
if (res.isEmpty) {
return;
}
for (final node in res) {
if (node.isLimit) {
continue;
}
reporter.reportErrorForNode(
_code,
node.node!,
);
}
});
}
}

class _MasamuneCollectionModelShouldAddLimitQueryValue {
String? variableName;
FunctionExpressionInvocation? method;
VariableDeclaration? variable;
AstNode? node;
bool isLimit = false;

@override
String toString() {
return "Query: $variableName($variable) Node: $node IsLimit: $isLimit";
}
}

0 comments on commit 53f12b5

Please sign in to comment.