Skip to content

Commit

Permalink
Fix rule L011 for implicit aliases (#2683)
Browse files Browse the repository at this point in the history
* Fix rule L011

* Revert unnecessary change

* Pin markupsafe
  • Loading branch information
tunetheweb committed Feb 18, 2022
1 parent d6a918a commit 5f01cc5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/sqlfluff/rules/L011.py
Expand Up @@ -56,12 +56,14 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:
The use of `raw_stack` is just for working out how much whitespace to add.
"""
# Config type hints
self.aliasing: str
fixes = []

if context.segment.is_type("alias_expression"):
if context.parent_stack[-1].is_type(*self._target_elems):
if any(e.name.lower() == "as" for e in context.segment.segments):
if self.aliasing == "implicit": # type: ignore
if self.aliasing == "implicit":
if context.segment.segments[0].name.lower() == "as":

# Remove the AS as we're using implict aliasing
Expand All @@ -84,7 +86,7 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:

return LintResult(anchor=anchor, fixes=fixes)

else:
elif self.aliasing != "implicit":
insert_buff: List[Union[WhitespaceSegment, KeywordSegment]] = []

# Add initial whitespace if we need to...
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/rules/std_rule_cases/L011.yml
@@ -1,5 +1,28 @@
rule: L011

test_fail_default_explicit:
# Add whitespace when fixing implicit aliasing
fail_str: select foo.bar from table1 foo
fix_str: select foo.bar from table1 AS foo

test_fail_explicit:
# Add whitespace when fixing implicit aliasing
fail_str: select foo.bar from table1 foo
fix_str: select foo.bar from table1 AS foo
configs:
rules:
L011:
aliasing: explicit

test_fail_implicit:
# Add whitespace when fixing implicit aliasing
fail_str: select foo.bar from table1 AS foo
fix_str: select foo.bar from table1 foo
configs:
rules:
L011:
aliasing: implicit

test_fail_implicit_alias:
# Add whitespace when fixing implicit aliasing
fail_str: select foo.bar from (select 1 as bar)foo
Expand Down

0 comments on commit 5f01cc5

Please sign in to comment.