-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'exhaustive-deps' of https://github.com/chriscerie/selene …
…into exhaustive-deps
- Loading branch information
Showing
10 changed files
with
219 additions
and
2 deletions.
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
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,14 @@ | ||
# mixed_table | ||
## What it does | ||
Checks for mixed tables (tables that act as both an array and dictionary). | ||
|
||
## Why this is bad | ||
Mixed tables harms readability and are prone to bugs. There is almost always a better alternative. | ||
|
||
## Example | ||
```lua | ||
local foo = { | ||
"array field", | ||
bar = "dictionary field", | ||
} | ||
``` |
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,92 @@ | ||
use super::*; | ||
use crate::ast_util::range; | ||
use std::convert::Infallible; | ||
|
||
use full_moon::{ | ||
ast::{self, Ast}, | ||
visitors::Visitor, | ||
}; | ||
|
||
pub struct MixedTableLint; | ||
|
||
impl Lint for MixedTableLint { | ||
type Config = (); | ||
type Error = Infallible; | ||
|
||
const SEVERITY: Severity = Severity::Warning; | ||
const LINT_TYPE: LintType = LintType::Correctness; | ||
|
||
fn new(_: Self::Config) -> Result<Self, Self::Error> { | ||
Ok(MixedTableLint) | ||
} | ||
|
||
fn pass(&self, ast: &Ast, _: &Context, _: &AstContext) -> Vec<Diagnostic> { | ||
let mut visitor = MixedTableVisitor::default(); | ||
|
||
visitor.visit_ast(ast); | ||
|
||
let mut diagnostics = Vec::new(); | ||
|
||
for mixed_table in visitor.mixed_tables { | ||
diagnostics.push(Diagnostic::new_complete( | ||
"mixed_table", | ||
"mixed tables are not allowed".to_owned(), | ||
Label::new(mixed_table.range), | ||
vec!["help: change this table to either an array or dictionary".to_owned()], | ||
Vec::new(), | ||
)); | ||
} | ||
|
||
diagnostics | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct MixedTableVisitor { | ||
mixed_tables: Vec<MixedTable>, | ||
} | ||
|
||
struct MixedTable { | ||
range: (usize, usize), | ||
} | ||
|
||
impl Visitor for MixedTableVisitor { | ||
fn visit_table_constructor(&mut self, node: &ast::TableConstructor) { | ||
let mut last_key_field_starting_range = 0; | ||
let mut last_no_key_field_starting_range = 0; | ||
|
||
for field in node.fields() { | ||
if let ast::Field::NoKey(_) = field { | ||
if last_key_field_starting_range > 0 { | ||
self.mixed_tables.push(MixedTable { | ||
range: (last_key_field_starting_range, range(field).1), | ||
}); | ||
return; | ||
} | ||
last_no_key_field_starting_range = range(field).0; | ||
} else { | ||
if last_no_key_field_starting_range > 0 { | ||
self.mixed_tables.push(MixedTable { | ||
range: (last_no_key_field_starting_range, range(field).1), | ||
}); | ||
return; | ||
} | ||
last_key_field_starting_range = range(field).0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{super::test_util::test_lint, *}; | ||
|
||
#[test] | ||
fn test_mixed_table() { | ||
test_lint( | ||
MixedTableLint::new(()).unwrap(), | ||
"mixed_table", | ||
"mixed_table", | ||
); | ||
} | ||
} |
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,53 @@ | ||
local bad = { | ||
"", | ||
a = b, | ||
} | ||
|
||
bad = { | ||
{}, | ||
[a] = b, | ||
} | ||
|
||
bad = { | ||
a, | ||
[""] = b, | ||
} | ||
|
||
-- This is technically not a mixed table, but it's formatted like it harming readability | ||
-- so it should still be linted | ||
bad = { | ||
1, | ||
[2] = b, | ||
} | ||
|
||
bad = { | ||
[a] = b, | ||
[c] = d, | ||
"", | ||
} | ||
|
||
bad({ | ||
a = b, | ||
"", | ||
c = d, | ||
}) | ||
|
||
local good = { | ||
a = b, | ||
c = d, | ||
} | ||
|
||
good = { | ||
"", | ||
a, | ||
} | ||
|
||
good = { | ||
[1] = a, | ||
[3] = b, | ||
} | ||
|
||
good({ | ||
a = b, | ||
c = d, | ||
}) |
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,54 @@ | ||
error[mixed_table]: mixed tables are not allowed | ||
┌─ mixed_table.lua:2:5 | ||
│ | ||
2 │ ╭ "", | ||
3 │ │ a = b, | ||
│ ╰─────────^ | ||
│ | ||
= help: change this table to either an array or dictionary | ||
|
||
error[mixed_table]: mixed tables are not allowed | ||
┌─ mixed_table.lua:7:5 | ||
│ | ||
7 │ ╭ {}, | ||
8 │ │ [a] = b, | ||
│ ╰───────────^ | ||
│ | ||
= help: change this table to either an array or dictionary | ||
|
||
error[mixed_table]: mixed tables are not allowed | ||
┌─ mixed_table.lua:12:5 | ||
│ | ||
12 │ ╭ a, | ||
13 │ │ [""] = b, | ||
│ ╰────────────^ | ||
│ | ||
= help: change this table to either an array or dictionary | ||
|
||
error[mixed_table]: mixed tables are not allowed | ||
┌─ mixed_table.lua:19:5 | ||
│ | ||
19 │ ╭ 1, | ||
20 │ │ [2] = b, | ||
│ ╰───────────^ | ||
│ | ||
= help: change this table to either an array or dictionary | ||
|
||
error[mixed_table]: mixed tables are not allowed | ||
┌─ mixed_table.lua:25:5 | ||
│ | ||
25 │ ╭ [c] = d, | ||
26 │ │ "", | ||
│ ╰──────^ | ||
│ | ||
= help: change this table to either an array or dictionary | ||
|
||
error[mixed_table]: mixed tables are not allowed | ||
┌─ mixed_table.lua:30:5 | ||
│ | ||
30 │ ╭ a = b, | ||
31 │ │ "", | ||
│ ╰──────^ | ||
│ | ||
= help: change this table to either an array or dictionary | ||
|