diff --git a/src/sqlfluff/rules/L011.py b/src/sqlfluff/rules/L011.py index e5b1b602f2f..c49f82533cc 100644 --- a/src/sqlfluff/rules/L011.py +++ b/src/sqlfluff/rules/L011.py @@ -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 @@ -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... diff --git a/test/fixtures/rules/std_rule_cases/L011.yml b/test/fixtures/rules/std_rule_cases/L011.yml index c12b19aec15..99af52641e2 100644 --- a/test/fixtures/rules/std_rule_cases/L011.yml +++ b/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