Skip to content

Commit de6546d

Browse files
authored
feat: Use linters from regular dependencies (#276)
1 parent 688973b commit de6546d

File tree

5 files changed

+199
-87
lines changed

5 files changed

+199
-87
lines changed

packages/custom_lint/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Unreleased patch
2+
3+
- Support installing custom_lint plugins in `dependencies:` instead of `dev_dependencies` (thanks to @dickermoshe).
4+
15
## 0.6.9 - 2024-10-09
26

37
- `custom_lint_core` upgraded to `0.6.9`

packages/custom_lint/lib/src/workspace.dart

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -602,22 +602,26 @@ publish_to: 'none'
602602
}
603603

604604
void _writePubspecDependencies(StringBuffer buffer) {
605+
// Collect all the dependencies for each package.
605606
final uniqueDependencyNames = projects.expand((e) sync* {
607+
yield* e.pubspec.dependencies.keys;
606608
yield* e.pubspec.devDependencies.keys;
607609
yield* e.pubspec.dependencyOverrides.keys;
608610
}).toSet();
609611

610612
final dependenciesByName = {
611613
for (final name in uniqueDependencyNames)
612614
name: (
613-
devDependencies: projects
614-
.map((project) {
615-
final dependency = project.pubspec.devDependencies[name];
616-
if (dependency == null) return null;
617-
return (project: project, dependency: dependency);
618-
})
619-
.nonNulls
620-
.toList(),
615+
dependencies: projects.expand((project) {
616+
final dependency = project.pubspec.dependencies[name];
617+
final devDependency = project.pubspec.devDependencies[name];
618+
return [
619+
if (dependency != null)
620+
(project: project, dependency: dependency),
621+
if (devDependency != null)
622+
(project: project, dependency: devDependency),
623+
];
624+
}).toList(),
621625
dependencyOverrides: projects
622626
.map((project) {
623627
final dependency = project.pubspec.dependencyOverrides[name];
@@ -629,37 +633,37 @@ publish_to: 'none'
629633
),
630634
};
631635

632-
// A flag for whether the "dependencies:" header has been written.
633-
var didWriteDevDependenciesHeader = false;
634-
// Write dev_dependencies
636+
final dependencies = <String, String>{};
637+
638+
// Iterate over each plugin and compute their constraints.
635639
for (final name in uniquePluginNames) {
636640
final allDependencies = dependenciesByName[name];
637641
if (allDependencies == null) continue;
638642

639-
// // We don't write dev_dependencies which are sometimes prod dependencies,
640-
// // as dev_dependencies, because then we'd be specifying the dependency twice.
641-
if (
642-
// allDependencies.dependencies.isNotEmpty ||
643-
allDependencies.devDependencies.isEmpty) {
643+
if (allDependencies.dependencies.isEmpty) {
644644
continue;
645645
}
646646

647-
if (!didWriteDevDependenciesHeader) {
648-
didWriteDevDependenciesHeader = true;
649-
buffer.writeln('\ndev_dependencies:');
650-
}
651-
652647
final constraint = allDependencies.dependencyOverrides.isNotEmpty
653648
? ' any'
654649
: _buildDependencyConstraint(
655650
name,
656-
allDependencies.devDependencies,
651+
allDependencies.dependencies,
657652
workingDirectory: workingDirectory,
658653
fileName: 'pubspec.yaml',
659654
);
660-
buffer.writeln(' $name:$constraint');
655+
dependencies[name] = constraint;
656+
}
657+
658+
// Write the dependencies to the buffer.
659+
if (dependencies.isNotEmpty) {
660+
buffer.writeln('\ndependencies:');
661+
for (final dependency in dependencies.entries) {
662+
buffer.writeln(' ${dependency.key}:${dependency.value}');
663+
}
661664
}
662665

666+
// Write the dependency_overrides to the buffer.
663667
_writeDependencyOverrides(
664668
buffer,
665669
dependencyOverrides: {
@@ -923,9 +927,11 @@ class CustomLintProject {
923927
);
924928
});
925929

926-
// TODO check that only dev_dependencies are checked
927930
final plugins = await Future.wait(
928-
projectPubspec.devDependencies.entries.map((e) async {
931+
{
932+
...projectPubspec.dependencies,
933+
...projectPubspec.devDependencies,
934+
}.entries.map((e) async {
929935
final packageWithName = projectPackageConfig.packages
930936
.firstWhereOrNull((p) => p.name == e.key);
931937
if (packageWithName == null) {

packages/custom_lint/test/cli_test.dart

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -164,63 +164,68 @@ No issues found!
164164
expect(process.exitCode, 0);
165165
});
166166

167-
test('CLI lists warnings from all plugins and set exit code', () async {
168-
final plugin =
169-
createPlugin(name: 'test_lint', main: helloWordPluginSource);
170-
final plugin2 =
171-
createPlugin(name: 'test_lint2', main: oyPluginSource);
172-
173-
final app = createLintUsage(
174-
source: {
175-
'lib/main.dart': 'void fn() {}',
176-
'lib/another.dart': 'void fail() {}',
177-
},
178-
plugins: {'test_lint': plugin.uri, 'test_lint2': plugin2.uri},
179-
name: 'test_app',
180-
);
167+
for (final installAsDevDependency in [false, true]) {
168+
test(
169+
'CLI lists warnings from all plugins and set exit code installAsDevDependency: $installAsDevDependency',
170+
() async {
171+
final plugin =
172+
createPlugin(name: 'test_lint', main: helloWordPluginSource);
173+
final plugin2 =
174+
createPlugin(name: 'test_lint2', main: oyPluginSource);
175+
176+
final app = createLintUsage(
177+
source: {
178+
'lib/main.dart': 'void fn() {}',
179+
'lib/another.dart': 'void fail() {}',
180+
},
181+
plugins: {'test_lint': plugin.uri, 'test_lint2': plugin2.uri},
182+
name: 'test_app',
183+
installAsDevDependency: installAsDevDependency,
184+
);
181185

182-
final process = await Process.start(
183-
'dart',
184-
[
185-
customLintBinPath,
186-
'--format',
187-
format,
188-
],
189-
workingDirectory: app.path,
190-
);
186+
final process = await Process.start(
187+
'dart',
188+
[
189+
customLintBinPath,
190+
'--format',
191+
format,
192+
],
193+
workingDirectory: app.path,
194+
);
191195

192-
final out = process.stdout.map(utf8.decode);
193-
final err = process.stderr.map(utf8.decode);
196+
final out = process.stdout.map(utf8.decode);
197+
final err = process.stderr.map(utf8.decode);
194198

195-
expect(err, emitsDone);
199+
expect(err, emitsDone);
196200

197-
if (format == 'json') {
198-
expect(
199-
out.join(),
200-
completion(
201-
equals('${jsonLints(app.resolveSymbolicLinksSync())}\n'),
202-
),
203-
);
204-
} else {
205-
expect(
206-
out.join(),
207-
completion(
208-
allOf(
209-
startsWith('Analyzing...'),
210-
endsWith('''
201+
if (format == 'json') {
202+
expect(
203+
out.join(),
204+
completion(
205+
equals('${jsonLints(app.resolveSymbolicLinksSync())}\n'),
206+
),
207+
);
208+
} else {
209+
expect(
210+
out.join(),
211+
completion(
212+
allOf(
213+
startsWith('Analyzing...'),
214+
endsWith('''
211215
lib/another.dart:1:6 • Hello world • hello_world • INFO
212216
lib/another.dart:1:6 • Oy • oy • INFO
213217
lib/main.dart:1:6 • Hello world • hello_world • INFO
214218
lib/main.dart:1:6 • Oy • oy • INFO
215219
216220
4 issues found.
217221
'''),
222+
),
218223
),
219-
),
220-
);
221-
}
222-
expect(await process.exitCode, 1);
223-
});
224+
);
225+
}
226+
expect(await process.exitCode, 1);
227+
});
228+
}
224229
});
225230
}
226231
}

packages/custom_lint/test/create_project.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,10 @@ Directory createLintUsage({
214214
Map<String, Uri> plugins = const {},
215215
Map<String, String> source = const {},
216216
Map<String, Uri> extraPackageConfig = const {},
217+
bool installAsDevDependency = true,
217218
required String name,
218219
}) {
219-
final pluginDevDependencies = plugins.entries
220+
final pluginDependencies = plugins.entries
220221
.map(
221222
(e) => '''
222223
${e.key}:
@@ -244,11 +245,12 @@ environment:
244245
dependencies:
245246
analyzer: any
246247
analyzer_plugin: any
248+
${installAsDevDependency ? "" : pluginDependencies}
247249
248250
dev_dependencies:
249251
custom_lint:
250252
path: ${PeerProjectMeta.current.customLintPath}
251-
$pluginDevDependencies
253+
${installAsDevDependency ? pluginDependencies : ""}
252254
''',
253255
packageConfig: createPackageConfig(
254256
plugins: {...plugins, ...extraPackageConfig},

0 commit comments

Comments
 (0)