Skip to content

Commit

Permalink
Add indent to the ignore comment (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrRogulski authored Dec 4, 2023
1 parent bb81611 commit 5b5b0dd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/custom_lint/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased fix

- `// ignore` comments now correctly respect indentation when they are inserted (thanks to @PiotrRogulski)

## 0.5.7 - 2023-11-20

- Support JSON output format via CLI parameter `--format json|default` (thanks to @kuhnroyal)
Expand Down
28 changes: 28 additions & 0 deletions packages/custom_lint/test/ignore_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ void fn2() {}
expectMatchesGoldenFixes(fixes);
});

test('Emits indented ignore quick-fix', () async {
final plugin = createPlugin(
name: 'test_lint',
main: source,
);

final app = createLintUsage(
name: 'test_app',
plugins: {'test_lint': plugin.uri},
source: {
'lib/main.dart': '''
void fn() {}
''',
},
);

final runner = await startRunnerForApp(app);
await runner.getLints(reload: false);
final fixes = await runner
.getFixes(app.file('lib', 'main.dart').path, 10)
.then((e) => e.fixes);

expect(
fixes[0].fixes[0].change.edits[0].edits[0].replacement,
startsWith('${' ' * 4}// ignore: hello_world'),
);
});

test('supports `// ignore: code`', () async {
final plugin = createPlugin(
name: 'test_lint',
Expand Down
4 changes: 4 additions & 0 deletions packages/custom_lint_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased fix

- `// ignore` comments now correctly respect indentation when they are inserted (thanks to @PiotrRogulski)

## 0.5.7 - 2023-11-20

- `custom_lint` upgraded to `0.5.7`
Expand Down
6 changes: 5 additions & 1 deletion packages/custom_lint_builder/lib/src/ignore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,13 @@ class IgnoreCode extends DartFix {

final startLineOffset = resolver.lineInfo.getOffsetOfLine(offsetLine);

final indentLength = resolver.source.contents.data
.substring(startLineOffset)
.indexOf(RegExp(r'\S'));

builder.addSimpleInsertion(
startLineOffset,
'// ignore: ${analysisError.errorCode.name}\n',
'${' ' * indentLength}// ignore: ${analysisError.errorCode.name}\n',
);
}
});
Expand Down

1 comment on commit 5b5b0dd

@vercel
Copy link

@vercel vercel bot commented on 5b5b0dd Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.