-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(linter): add line-length
rule
#232
Merged
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7ccf122
add line length rule
felipeasimos 4b6ef74
add better diagnostic message for line-length
felipeasimos beaf5cf
add docs, remove runOnLine and use runOnce for line-length
felipeasimos 083d35b
fixed typos
felipeasimos b1acffc
moved Line struct to span.zig
felipeasimos d91e200
add default config for line_lengt and fix spans in diagnostics
felipeasimos eb74aac
diagnostics hightlights only excess part of the line
felipeasimos 10ca5da
use platform-dependent newline data
felipeasimos 536c741
use newline data from util
felipeasimos 1cf2ac7
add max_length property properly
felipeasimos 144da84
removed zlint.json
felipeasimos 40cf1ab
removed dead code
felipeasimos 008d01d
removed unnecessary diff and removed unused import
felipeasimos 494a9fc
make line length span cover the whole line
felipeasimos b6ad31f
detect newline type being used in the file
felipeasimos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# `line-length` | ||
|
||
> Category: style | ||
> | ||
> Enabled by default?: No | ||
|
||
## What This Rule Does | ||
|
||
Checks if any line goes beyond a given number of columns. | ||
|
||
## Examples | ||
|
||
Examples of **incorrect** code for this rule (with a threshold of 120 columns): | ||
|
||
```zig | ||
const std = @import("std"); | ||
const longStructDeclarationInOneLine = struct { max_length: u32 = 120, a: usize = 123, b: usize = 12354, c: usize = 1234352 }; | ||
fn reallyExtraVerboseFunctionNameToThePointOfBeingACodeSmellAndProbablyAHintThatYouCanGetAwayWithAnotherNameOrSplittingThisIntoSeveralFunctions() u32 { | ||
return 123; | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule (with a threshold of 120 columns): | ||
|
||
```zig | ||
const std = @import("std"); | ||
const longStructInMultipleLines = struct { | ||
max_length: u32 = 120, | ||
a: usize = 123, | ||
b: usize = 12354, | ||
c: usize = 1234352, | ||
}; | ||
fn Get123Constant() u32 { | ||
return 123; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
//! ## What This Rule Does | ||
//! | ||
//! Checks if any line goes beyond a given number of columns. | ||
//! | ||
//! ## Examples | ||
//! | ||
//! Examples of **incorrect** code for this rule (with a threshold of 120 columns): | ||
//! ```zig | ||
//! const std = @import("std"); | ||
//! const longStructDeclarationInOneLine = struct { max_length: u32 = 120, a: usize = 123, b: usize = 12354, c: usize = 1234352 }; | ||
//! fn reallyExtraVerboseFunctionNameToThePointOfBeingACodeSmellAndProbablyAHintThatYouCanGetAwayWithAnotherNameOrSplittingThisIntoSeveralFunctions() u32 { | ||
//! return 123; | ||
//! } | ||
//! ``` | ||
//! | ||
//! Examples of **correct** code for this rule (with a threshold of 120 columns): | ||
//! ```zig | ||
//! const std = @import("std"); | ||
//! const longStructInMultipleLines = struct { | ||
//! max_length: u32 = 120, | ||
//! a: usize = 123, | ||
//! b: usize = 12354, | ||
//! c: usize = 1234352, | ||
//! }; | ||
//! fn Get123Constant() u32 { | ||
//! return 123; | ||
//! } | ||
//! ``` | ||
|
||
const builtin = @import("builtin"); | ||
const std = @import("std"); | ||
DonIsaac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const util = @import("util"); | ||
const semantic = @import("../../semantic.zig"); | ||
const _rule = @import("../rule.zig"); | ||
const span = @import("../../span.zig"); | ||
|
||
const Ast = std.zig.Ast; | ||
const Node = Ast.Node; | ||
const Scope = semantic.Scope; | ||
const LinterContext = @import("../lint_context.zig"); | ||
const Rule = _rule.Rule; | ||
const NodeWrapper = _rule.NodeWrapper; | ||
const Symbol = semantic.Symbol; | ||
const Error = @import("../../Error.zig"); | ||
const Cow = util.Cow(false); | ||
const LabeledSpan = span.LabeledSpan; | ||
|
||
max_length: u32 = 120, | ||
|
||
const LineLength = @This(); | ||
pub const meta: Rule.Meta = .{ | ||
.name = "line-length", | ||
.category = .style, | ||
.default = .off, | ||
}; | ||
|
||
pub fn lineLengthDiagnostic(ctx: *LinterContext, line_start: u32, threshold: u32, line_length: u32) Error { | ||
return ctx.diagnosticf( | ||
"line length of {} characters is too big.", | ||
.{line_length}, | ||
.{LabeledSpan.unlabeled(line_start + threshold, line_start + line_length)}, | ||
); | ||
} | ||
|
||
pub fn runOnce(self: *const LineLength, ctx: *LinterContext) void { | ||
var line_start_idx: u32 = 0; | ||
var lines = std.mem.splitSequence(u8, ctx.source.text(), util.NEWLINE); | ||
while (lines.next()) |line| { | ||
const line_length = @as(u32, @intCast(line.len)); | ||
if (line.len > self.max_length) { | ||
ctx.report(lineLengthDiagnostic(ctx, line_start_idx, self.max_length, line_length)); | ||
} | ||
line_start_idx += line_length + @as(u32, @intCast(util.NEWLINE.len)); | ||
} | ||
} | ||
|
||
pub fn rule(self: *LineLength) Rule { | ||
return Rule.init(self); | ||
} | ||
|
||
const RuleTester = @import("../tester.zig"); | ||
test LineLength { | ||
const t = std.testing; | ||
|
||
var line_length = LineLength{}; | ||
var runner = RuleTester.init(t.allocator, line_length.rule()); | ||
defer runner.deinit(); | ||
|
||
const pass = &[_][:0]const u8{ | ||
\\const std = @import("std"); | ||
\\fn foo() std.mem.Allocator.Error!void { | ||
\\ _ = try std.heap.page_allocator.alloc(u8, 8); | ||
\\} | ||
, | ||
\\const std = @import("std"); | ||
\\const longStructInMultipleLines = struct { | ||
\\ max_length: u32 = 120, | ||
\\ a: usize = 123, | ||
\\ b: usize = 12354, | ||
\\ c: usize = 1234352, | ||
\\}; | ||
\\fn Get123Constant() u32 { | ||
\\ return 123; | ||
\\} | ||
}; | ||
|
||
const fail = &[_][:0]const u8{ | ||
\\const std = @import("std"); | ||
\\fn foo() std.mem.Allocator.Error!void { | ||
\\ // ok so this is a super unnecessary line that is artificially being made long through this self-referential comment thats keeps on going until hitting a number of columns that violates the rule | ||
\\ _ = try std.heap.page_allocator.alloc(u8, 8); | ||
\\} | ||
, | ||
\\const std = @import("std"); | ||
\\const longStructDeclarationInOneLine = struct { max_length: u32 = 120, a: usize = 123, b: usize = 12354, c: usize = 1234352 }; | ||
\\fn reallyExtraVerboseFunctionNameToThePointOfBeingACodeSmellAndProbablyAHintThatYouCanGetAwayWithAnotherNameOrSplittingThisIntoSeveralFunctions() u32 { | ||
\\ return 123; | ||
\\} | ||
}; | ||
|
||
try runner | ||
.withPass(pass) | ||
.withFail(fail) | ||
.run(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
𝙭 line-length: line length of 196 characters is too big. | ||
╭─[line-length.zig:3:121] | ||
2 │ fn foo() std.mem.Allocator.Error!void { | ||
3 │ // ok so this is a super unnecessary line that is artificially being made long through this self-referential comment thats keeps on going until hitting a number of columns that violates the rule | ||
· ──────────────────────────────────────────────────────────────────────────── | ||
4 │ _ = try std.heap.page_allocator.alloc(u8, 8); | ||
╰──── | ||
|
||
𝙭 line-length: line length of 126 characters is too big. | ||
╭─[line-length.zig:2:121] | ||
1 │ const std = @import("std"); | ||
2 │ const longStructDeclarationInOneLine = struct { max_length: u32 = 120, a: usize = 123, b: usize = 12354, c: usize = 1234352 }; | ||
DonIsaac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
· ────── | ||
3 │ fn reallyExtraVerboseFunctionNameToThePointOfBeingACodeSmellAndProbablyAHintThatYouCanGetAwayWithAnotherNameOrSplittingThisIntoSeveralFunctions() u32 { | ||
╰──── | ||
|
||
𝙭 line-length: line length of 151 characters is too big. | ||
╭─[line-length.zig:3:121] | ||
2 │ const longStructDeclarationInOneLine = struct { max_length: u32 = 120, a: usize = 123, b: usize = 12354, c: usize = 1234352 }; | ||
3 │ fn reallyExtraVerboseFunctionNameToThePointOfBeingACodeSmellAndProbablyAHintThatYouCanGetAwayWithAnotherNameOrSplittingThisIntoSeveralFunctions() u32 { | ||
· ─────────────────────────────── | ||
4 │ return 123; | ||
╰──── | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove dead code (zlint --fix-dangerously)