Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 5 additions & 0 deletions cb_checks/lib/cb_checks.dart
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 9 additions & 5 deletions cb_checks/lib/src/docs_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ class DocsCheckConfig {
factory DocsCheckConfig.parse(String source) {
final YamlMap yaml = loadYaml(source) as YamlMap;
return DocsCheckConfig(
allowed: <String>{...(yaml['allowed'] as YamlList).cast<String>()},
snapshots: <String>{...(yaml['snapshots'] as YamlList).cast<String>()},
allowed: <String>{...?(yaml['allowed'] as YamlList?)?.cast<String>()},
snapshots: <String>{...?(yaml['snapshots'] as YamlList?)?.cast<String>()},
);
}

Expand Down Expand Up @@ -322,13 +322,17 @@ List<DocReference> 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<String> _sourceWords(List<String> tracked) {
const int largestFile = 4 * 1024 * 1024;
final Set<String> words = <String>{};
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);
Expand Down
2 changes: 1 addition & 1 deletion cb_checks/lib/src/proto_optional_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ List<String> 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',
);
}

Expand Down
21 changes: 21 additions & 0 deletions cb_checks/test/docs_check_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(<String>['mv', 'tools/run.sh', 'tools/run2.sh']);
git(<String>['mv', 'tools/gone.sh', 'tools/here.sh']);
Expand Down
5 changes: 3 additions & 2 deletions cb_lints/lib/src/add_ignore_no_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ List<IgnoreNoConnectionInsertion>? ignoreNoConnectionInsertions(Expression expre
if (goesThroughIgnoreNoConnection(expression)) {
return <IgnoreNoConnectionInsertion>[];
}
if (startsFromAwait(expression)) {
return null;
}
switch (expression) {
case AwaitExpression():
return null;
case ConditionalExpression():
final List<IgnoreNoConnectionInsertion>? then = ignoreNoConnectionInsertions(expression.thenExpression, eol: eol);
final List<IgnoreNoConnectionInsertion>? otherwise = ignoreNoConnectionInsertions(
Expand Down
25 changes: 23 additions & 2 deletions cb_lints/lib/src/reader_without_ignore_no_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -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():
Expand All @@ -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;
}
2 changes: 2 additions & 0 deletions cb_lints/test/add_ignore_no_connection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
4 changes: 4 additions & 0 deletions cb_lints/test/reader_without_ignore_no_connection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class ReaderWithoutIgnoreNoConnectionTest extends AnalysisRuleTest {
await _assertLinted(_reader(' yield* flag ? api().ignoreNoConnection() : api();'));
}

Future<void> test_guardAfterAwait() async {
await _assertLinted(_reader(' yield* (await Future<Stream<int>>.value(api())).ignoreNoConnection();'));
}

Future<void> test_plainYield() async {
await assertNoDiagnosticsInFile(_write(_syncEnginePath, _reader(' yield 2;')));
}
Expand Down
2 changes: 1 addition & 1 deletion check.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading