Skip to content

Commit

Permalink
New linters/formatter for dart (#650)
Browse files Browse the repository at this point in the history
Adds definitions for `format`, `fix`, and `analyze`. From a brief audit,
users may prefer to remove individual subcommands but the full set is
the best default.
  • Loading branch information
TylerJang27 authored Mar 5, 2024
1 parent 9fd55bd commit 28be597
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ trunk check enable {linter}
| CMake | [cmake-format] |
| CSS, SCSS | [stylelint], [prettier] |
| Cue | [cue-fmt] |
| Dart | [dart] |
| Docker | [hadolint], [checkov] |
| Dotenv | [dotenv-linter] |
| GitHub | [actionlint] |
Expand Down Expand Up @@ -104,6 +105,7 @@ trunk check enable {linter}
[codespell]: https://github.com/codespell-project/codespell#readme
[cspell]: https://github.com/streetsidesoftware/cspell#readme
[cue-fmt]: https://cuelang.org/
[dart]: https://dart.dev/tools/sdk
[deno]: https://deno.land/manual
[detekt]: https://github.com/detekt/detekt#readme
[djlint]: https://github.com/Riverside-Healthcare/djlint#readme
Expand Down
17 changes: 17 additions & 0 deletions linters/dart/dart.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import path from "path";
import { customLinterCheckTest } from "tests";
import { TrunkLintDriver } from "tests/driver";
import { TEST_DATA } from "tests/utils";

const preCheck = (driver: TrunkLintDriver) => {
driver.moveFile(path.join(TEST_DATA, "pubspec.yaml"), "pubspec.yaml");
driver.moveFile(path.join(TEST_DATA, "analysis_options.yaml"), "analysis_options.yaml");
};

customLinterCheckTest({
linterName: "dart",
args: `${TEST_DATA} -y`,
testName: "basic",
preCheck,
pathsToSnapshot: [path.join(TEST_DATA, "basic.in.dart")],
});
62 changes: 62 additions & 0 deletions linters/dart/plugin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
version: 0.1
downloads:
- name: dart
downloads:
- os:
linux: linux
macos: macos
windows: windows
cpu:
x86_64: x64
arm_64: arm64
url: https://storage.googleapis.com/dart-archive/channels/stable/release/${version}/sdk/dartsdk-${os}-${cpu}-release.zip
strip_components: 1
tools:
definitions:
- name: dart
download: dart
known_good_version: 3.2.6
shims: [dart, dartaotruntime]
environment:
- name: PATH
list: ["${tool}/bin"]
lint:
files:
- name: dart
extensions: [dart]
definitions:
- name: dart
main_tool: dart
files: [dart]
known_good_version: 3.2.6
suggest_if: never
affects_cache:
- analysis_options.yaml
- pubspec.yaml
commands:
- name: format
output: rewrite
run: dart format ${target}
batch: true
in_place: true
formatter: true
success_codes: [0]
- name: fix
# Some analyze results are autofixable, others are not.
output: rewrite
run: dart fix --apply ${target}
batch: true
fix_prompt: Quick fix available
fix_verb: fix
in_place: true
success_codes: [0]
- name: analyze
output: regex
# The output only includes the filename so in order to use a relative path we need to run from parent.
run_from: ${parent}
parse_regex:
\s*(?P<severity>.*) - (?P<path>.*):(?P<line>\d+):(?P<col>\d+) - (?P<message>.*) -
(?P<code>.*)
run: dart analyze --no-fatal-warnings ${target}
success_codes: [0, 3]
batch: true
3 changes: 3 additions & 0 deletions linters/dart/test_data/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linter:
rules:
- use_super_parameters
14 changes: 14 additions & 0 deletions linters/dart/test_data/basic.in.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
library;
import 'dart:io';
class Vector2d {
final double x, y;
Vector2d(this.x, this.y);
}
class Vector3d extends Vector2d {
final double z;
Vector3d(final double x, final double y, this.z) : super(x, y);
}

void main() {
expect(find.text(''), empty);
}
92 changes: 92 additions & 0 deletions linters/dart/test_data/dart_v3.2.6_basic.check.shot
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing linter dart test basic 1`] = `
{
"issues": [
{
"code": "undefined_identifier",
"column": "10",
"file": "test_data/basic.in.dart",
"issueClass": "ISSUE_CLASS_EXISTING",
"level": "LEVEL_HIGH",
"line": "14",
"linter": "dart",
"message": "Undefined name 'find'. Try correcting the name to one that is defined, or defining the name.",
"targetType": "dart",
},
{
"code": "undefined_identifier",
"column": "25",
"file": "test_data/basic.in.dart",
"issueClass": "ISSUE_CLASS_EXISTING",
"level": "LEVEL_HIGH",
"line": "14",
"linter": "dart",
"message": "Undefined name 'empty'. Try correcting the name to one that is defined, or defining the name.",
"targetType": "dart",
},
{
"code": "undefined_function",
"column": "3",
"file": "test_data/basic.in.dart",
"issueClass": "ISSUE_CLASS_EXISTING",
"level": "LEVEL_HIGH",
"line": "14",
"linter": "dart",
"message": "The function 'expect' isn't defined. Try importing the library that defines 'expect', correcting the name to the name of an existing function, or defining a function named 'expect'.",
"targetType": "dart",
},
],
"lintActions": [
{
"command": "analyze",
"fileGroupName": "dart",
"linter": "dart",
"paths": [
"test_data/basic.in.dart",
],
"verb": "TRUNK_VERB_CHECK",
},
{
"command": "analyze",
"fileGroupName": "dart",
"linter": "dart",
"paths": [
"test_data/basic.in.dart",
],
"upstream": true,
"verb": "TRUNK_VERB_CHECK",
},
{
"command": "fix",
"fileGroupName": "dart",
"linter": "dart",
"paths": [
"test_data/basic.in.dart",
],
"verb": "TRUNK_VERB_CHECK",
},
{
"command": "fix",
"fileGroupName": "dart",
"linter": "dart",
"paths": [
"test_data/basic.in.dart",
],
"upstream": true,
"verb": "TRUNK_VERB_CHECK",
},
{
"command": "format",
"fileGroupName": "dart",
"linter": "dart",
"paths": [
"test_data/basic.in.dart",
],
"verb": "TRUNK_VERB_FMT",
},
],
"taskFailures": [],
"unformattedFiles": [],
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing linter dart test basic 1`] = `
"library;

class Vector2d {
final double x, y;
Vector2d(this.x, this.y);
}

class Vector3d extends Vector2d {
final double z;
Vector3d(super.x, super.y, this.z);
}

void main() {
expect(find.text(''), empty);
}
"
`;
2 changes: 2 additions & 0 deletions linters/dart/test_data/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
environment:
sdk: ">=2.17.0 <4.0.0"

0 comments on commit 28be597

Please sign in to comment.