diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..a0b2f7e --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,18 @@ +name: Check + +on: + push: + pull_request: + +permissions: + contents: read + +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 + - uses: dart-lang/setup-dart@6afc89df92d6eb3834022f73cd65adc8cdfcb92d # v1 + with: + sdk: 3.13.7 + - run: ./check.sh diff --git a/README.md b/README.md index 24331bc..6eebf1a 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,20 @@ # cb-lints -Cinderblock's own analyzer rules and repo checks, moved out of `cb-mobile`, which pins a tag of this repo. +Cinderblock's own analyzer rules and repo checks, moved out of `cb-mobile`, which pins a commit of this repo. The rules name that app's paths and classes, so this is not a general package. It is public so that `dart analyze` can fetch the plugin with no credentials. It has no licence. - `cb_lints/` is the analyzer plugin. `cb-mobile` enables it in `analysis_options.yaml`, with a `git:` source, so its rules report in `dart analyze` and the IDE. -- `cb_checks/` is a command line tool with `docs-check` and `proto-optional-check`. `cb-mobile` runs them - through `tools/cb_checks.sh`, which reads the same tag. +- `cb_checks/` holds `docs-check` and `proto-optional-check`. `cb-mobile` depends on it from `tool/`, whose + committed lockfile pins the commit, and registers both in `tool/bin/tools.dart`. ## Changing something -Run `./check.sh`, which is also the only thing that tests this repo. Tag the commit, then move the tag in -`cb-mobile`'s `analysis_options.yaml`, and run a bare `dart analyze` from its root. A plugin rule reports -only when the target is the package root: `dart analyze lib` prints no plugin warnings at all. +CI runs `./check.sh` on every push. In `cb-mobile`, move the commit in `analysis_options.yaml` and in +`tool/pubspec.yaml`, run `dart pub upgrade cb_checks` in `tool/`, and run a bare `fvm dart analyze` from the +root. A plugin rule reports only when the target is the package root: `dart analyze lib` prints no plugin +warnings at all. Pin full commit SHAs, as a tag can move. `cb_lints/pubspec.yaml` pins `analysis_server_plugin`, `analyzer` and `analyzer_plugin` to exact versions, which move together and follow the Dart SDK, so a Flutter bump in `cb-mobile` can need a release here first. diff --git a/cb_checks/lib/cb_checks.dart b/cb_checks/lib/cb_checks.dart new file mode 100644 index 0000000..fa64b89 --- /dev/null +++ b/cb_checks/lib/cb_checks.dart @@ -0,0 +1,5 @@ +/// The checks, for a repo's own tool to register in its command runner. +library; + +export 'src/docs_check_command.dart' show DocsCheckCommand; +export 'src/proto_optional_check_command.dart' show ProtoOptionalCheckCommand; diff --git a/cb_checks/lib/src/docs_check_command.dart b/cb_checks/lib/src/docs_check_command.dart index b3e0410..7f86107 100644 --- a/cb_checks/lib/src/docs_check_command.dart +++ b/cb_checks/lib/src/docs_check_command.dart @@ -191,8 +191,8 @@ class DocsCheckConfig { factory DocsCheckConfig.parse(String source) { final YamlMap yaml = loadYaml(source) as YamlMap; return DocsCheckConfig( - allowed: {...(yaml['allowed'] as YamlList).cast()}, - snapshots: {...(yaml['snapshots'] as YamlList).cast()}, + allowed: {...?(yaml['allowed'] as YamlList?)?.cast()}, + snapshots: {...?(yaml['snapshots'] as YamlList?)?.cast()}, ); } @@ -322,13 +322,17 @@ List findStaleReferences({ } /// Every word in the tracked source, leaving out the docs so that a doc cannot -/// vouch for itself, and private/, which is encrypted outside a worktree that -/// has unlocked it. +/// vouch for itself, the allowlist, which would vouch for anything built on a name +/// it lists, and private/, which is encrypted outside a worktree that has unlocked +/// it. Set _sourceWords(List tracked) { const int largestFile = 4 * 1024 * 1024; final Set words = {}; for (final String path in tracked) { - if (path.endsWith('.md') || path.startsWith('private/') || path.startsWith('third_party/')) { + if (path.endsWith('.md') || + path == 'docs_check.yaml' || + path.startsWith('private/') || + path.startsWith('third_party/')) { continue; } final File file = File(path); diff --git a/cb_checks/lib/src/proto_optional_check_command.dart b/cb_checks/lib/src/proto_optional_check_command.dart index 9bbf062..da84143 100644 --- a/cb_checks/lib/src/proto_optional_check_command.dart +++ b/cb_checks/lib/src/proto_optional_check_command.dart @@ -154,7 +154,7 @@ List checkOptionalFields({ if (unrecorded.isNotEmpty) { problems.add( '${unrecorded.length} optional fields are not in the ledger (${unrecorded.take(5).join(', ')}). ' - 'Run: tools/cb_checks.sh proto-optional-check --update', + 'Run: fvm dart run tool/bin/tools.dart proto-optional-check --update', ); } diff --git a/cb_checks/test/docs_check_command_test.dart b/cb_checks/test/docs_check_command_test.dart index 9c0e84c..3e7f735 100644 --- a/cb_checks/test/docs_check_command_test.dart +++ b/cb_checks/test/docs_check_command_test.dart @@ -84,6 +84,15 @@ snapshots: }); }); + group('DocsCheckConfig sections', () { + test('treats an empty or missing section as empty', () { + final DocsCheckConfig config = DocsCheckConfig.parse('allowed:\n'); + + expect(config.allowed, isEmpty); + expect(config.snapshots, isEmpty); + }); + }); + group('findStaleReferences', () { test('accepts existing files, folders and partial paths', () { expect(_stale('`tools/run.sh` `./tools/run.sh` `lib/src/data/` `persistence/entity_converters.dart`'), isEmpty); @@ -251,6 +260,18 @@ snapshots: expect(result.exitCode, 1); }); + test('does not let the allowlist vouch for a name built on one it lists', () { + File('${repo.path}/docs_check.yaml').writeAsStringSync('allowed:\n - GoneThing\nsnapshots: []\n'); + File('${repo.path}/tools/code.dart').writeAsStringSync('void aLiveMember() {}\n'); + File('${repo.path}/CLAUDE.md').writeAsStringSync('`GoneThing` and `GoneThing.aLiveMember`\n'); + + final ProcessResult result = check(); + + expect(result.stderr, contains('`GoneThing.aLiveMember` no longer exists')); + expect(result.stderr, isNot(contains('`GoneThing` no'))); + expect(result.exitCode, 1); + }); + test('passes once every reference is fixed', () { git(['mv', 'tools/run.sh', 'tools/run2.sh']); git(['mv', 'tools/gone.sh', 'tools/here.sh']); diff --git a/cb_lints/lib/src/add_ignore_no_connection.dart b/cb_lints/lib/src/add_ignore_no_connection.dart index bfe848d..725183e 100644 --- a/cb_lints/lib/src/add_ignore_no_connection.dart +++ b/cb_lints/lib/src/add_ignore_no_connection.dart @@ -60,9 +60,10 @@ List? ignoreNoConnectionInsertions(Expression expre if (goesThroughIgnoreNoConnection(expression)) { return []; } + if (startsFromAwait(expression)) { + return null; + } switch (expression) { - case AwaitExpression(): - return null; case ConditionalExpression(): final List? then = ignoreNoConnectionInsertions(expression.thenExpression, eol: eol); final List? otherwise = ignoreNoConnectionInsertions( diff --git a/cb_lints/lib/src/reader_without_ignore_no_connection.dart b/cb_lints/lib/src/reader_without_ignore_no_connection.dart index f4b8127..081d8c3 100644 --- a/cb_lints/lib/src/reader_without_ignore_no_connection.dart +++ b/cb_lints/lib/src/reader_without_ignore_no_connection.dart @@ -16,7 +16,7 @@ import 'package:analyzer/error/error.dart'; class ReaderWithoutIgnoreNoConnection extends AnalysisRule { static const LintCode code = LintCode( 'reader_without_ignore_no_connection', - 'A SyncEngine reader must end its stream with ignoreNoConnection(), or reads fail instead of degrading offline.', + 'A SyncEngine reader must send its stream through ignoreNoConnection(), or reads fail instead of degrading offline.', correctionMessage: 'Try adding .ignoreNoConnection() to this stream.', severity: DiagnosticSeverity.WARNING, ); @@ -86,7 +86,7 @@ bool goesThroughIgnoreNoConnection(Expression expression) { switch (current) { case MethodInvocation(): if (current.methodName.name == 'ignoreNoConnection') { - return true; + return !startsFromAwait(current.target); } current = current.target; case ParenthesizedExpression(): @@ -102,3 +102,24 @@ bool goesThroughIgnoreNoConnection(Expression expression) { } return false; } + +/// Whether the chain in [expression] starts from an `await`, which throws offline +/// before there is a stream for `ignoreNoConnection()` to guard. +bool startsFromAwait(Expression? expression) { + Expression? current = expression; + while (current != null) { + switch (current) { + case AwaitExpression(): + return true; + case MethodInvocation(): + current = current.target; + case ParenthesizedExpression(): + current = current.expression; + case PropertyAccess(): + current = current.target; + default: + return false; + } + } + return false; +} diff --git a/cb_lints/test/add_ignore_no_connection_test.dart b/cb_lints/test/add_ignore_no_connection_test.dart index 194a7da..3256623 100644 --- a/cb_lints/test/add_ignore_no_connection_test.dart +++ b/cb_lints/test/add_ignore_no_connection_test.dart @@ -69,5 +69,7 @@ void main() { test('offers nothing for an await, which throws before there is a stream', () { expect(_fixed('yield* await stream();'), isNull); expect(_fixed('yield* flag ? a() : await stream();'), isNull); + expect(_fixed('yield* (await stream());'), isNull); + expect(_fixed('yield* (await stream()).notEquals(1);'), isNull); }); } diff --git a/cb_lints/test/reader_without_ignore_no_connection_test.dart b/cb_lints/test/reader_without_ignore_no_connection_test.dart index 951ea8e..c939a5c 100644 --- a/cb_lints/test/reader_without_ignore_no_connection_test.dart +++ b/cb_lints/test/reader_without_ignore_no_connection_test.dart @@ -91,6 +91,10 @@ class ReaderWithoutIgnoreNoConnectionTest extends AnalysisRuleTest { await _assertLinted(_reader(' yield* flag ? api().ignoreNoConnection() : api();')); } + Future test_guardAfterAwait() async { + await _assertLinted(_reader(' yield* (await Future>.value(api())).ignoreNoConnection();')); + } + Future test_plainYield() async { await assertNoDiagnosticsInFile(_write(_syncEnginePath, _reader(' yield 2;'))); } diff --git a/check.sh b/check.sh index e747f91..9fd8524 100755 --- a/check.sh +++ b/check.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Analyzes and tests both packages. Nothing runs this on a push, so run it before one. +# Analyzes and tests both packages. CI runs this on every push. set -euo pipefail cd "$(dirname "$0")" for package in cb_lints cb_checks; do