Skip to content

Commit

Permalink
Rules Reorg Mopup (#4462)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanmcruickshank committed Mar 5, 2023
1 parent ce57b75 commit eea8e46
Show file tree
Hide file tree
Showing 104 changed files with 272 additions and 216 deletions.
10 changes: 5 additions & 5 deletions docs/source/configuration.rst
Expand Up @@ -240,13 +240,13 @@ active for a given file.
rollout of new rules because we manage by exception.


For example, to disable the rules :class:`L022 <sqlfluff.core.rules.Rule_L022>`
For example, to disable the rules :class:`LT08 <sqlfluff.core.rules.Rule_LT08>`
and :class:`RF02 <sqlfluff.core.rules.Rule_RF02>`:

.. code-block:: cfg
[sqlfluff]
exclude_rules = L022, RF02
exclude_rules = LT08, RF02
To enable individual rules, configure :code:`rules`, respectively.

Expand Down Expand Up @@ -291,7 +291,7 @@ above:
.. code-block:: cfg
[sqlfluff]
warnings = L019, LT01
warnings = LT01, LT04
With this configuration, files with no other issues (other than
those set to warn) will pass. If there are still other issues, then
Expand Down Expand Up @@ -892,7 +892,7 @@ You already know you can pass arguments (:code:`--verbose`,

.. code-block:: text
$ sqlfluff lint my_code.sql -v --exclude-rules L022,RF02
$ sqlfluff lint my_code.sql -v --exclude-rules LT08,RF02
You might have arguments that you pass through every time, e.g rules you
*always* want to ignore. These can also be configured:
Expand All @@ -901,7 +901,7 @@ You might have arguments that you pass through every time, e.g rules you
[sqlfluff]
verbose = 1
exclude_rules = L022,RF02
exclude_rules = LT08,RF02
Note that while the :code:`exclude_rules` config looks similar to the
above example, the :code:`verbose` config has an integer value. This is
Expand Down
2 changes: 1 addition & 1 deletion docs/source/developingrules.rst
Expand Up @@ -24,7 +24,7 @@ Typical reasons include:
These rules can override ``BaseRule``'s ``recurse_into`` field, setting it to
``False``. For these rules ``False``, ``_eval()`` is only called *once*, with
the root segment of the tree. This can be much more efficient, especially on
large files. For example, see rules ``L050`` and ``L009`` , which only look at
large files. For example, see rules ``L050`` and ``LT12`` , which only look at
the beginning or end of the file, respectively.

``_works_on_unparsable``
Expand Down
12 changes: 6 additions & 6 deletions docs/source/gettingstarted.rst
Expand Up @@ -83,7 +83,7 @@ file.
$ sqlfluff lint test.sql --dialect ansi
== [test.sql] FAIL
L: 1 | P: 1 | L036 | Select targets should be on a new line unless there is
L: 1 | P: 1 | LT09 | Select targets should be on a new line unless there is
| only one select target.
L: 1 | P: 1 | ST06 | Select wildcards then simple targets before calculations
| and aggregates. [structure.column_order]
Expand Down Expand Up @@ -128,7 +128,7 @@ error (violation of *LT01*) no longer shows up.
== [test.sql] FAIL
L: 1 | P: 1 | ST06 | Select wildcards then simple targets before calculations
| and aggregates.
L: 1 | P: 1 | L036 | Select targets should be on a new line unless there is
L: 1 | P: 1 | LT09 | Select targets should be on a new line unless there is
| only one select target.
L: 1 | P: 13 | LT01 | Unnecessary whitespace found.
L: 2 | P: 1 | LT02 | Expected 1 indentations, found 0 [compared to line 01]
Expand All @@ -142,11 +142,11 @@ and there may be some situations where a fix may not be able to be
applied because of the context of the query, but in many simple cases
it's a good place to start.

For now, we only want to fix the following rules: *LT02*, *L009*, *CP01*
For now, we only want to fix the following rules: *LT02*, *LT12*, *CP01*

.. code-block:: text
$ sqlfluff fix test.sql --rules LT02,L009,CP01 --dialect ansi
$ sqlfluff fix test.sql --rules LT02,LT12,CP01 --dialect ansi
==== finding violations ====
== [test.sql] FAIL
L: 2 | P: 1 | LT02 | Expected 1 indentations, found 0 [compared to line 01]
Expand Down Expand Up @@ -193,7 +193,7 @@ specifying :code:`--rules`.
== [test.sql] FAIL
L: 1 | P: 1 | ST06 | Select wildcards then simple targets before calculations
| and aggregates.
L: 1 | P: 1 | L036 | Select targets should be on a new line unless there is
L: 1 | P: 1 | LT09 | Select targets should be on a new line unless there is
| only one select target.
L: 1 | P: 13 | LT01 | Unnecessary whitespace found.
==== fixing violations ====
Expand Down Expand Up @@ -251,7 +251,7 @@ Then rerun the same command as before.

.. code-block:: text
$ sqlfluff fix test.sql --rules LT02,L009,CP01,ST06,L036,LT01
$ sqlfluff fix test.sql --rules LT02,LT12,CP01,ST06,LT09,LT01
Then examine the file again, and you'll notice that the
file has been fixed accordingly.
Expand Down
2 changes: 1 addition & 1 deletion docs/source/rules.rst
Expand Up @@ -56,7 +56,7 @@ Multiple rules can be ignored by placing them in a comma-delimited list.

.. code-block:: sql
a.a * a.b AS bad_2, -- noqa: L007, LT01
a.a * a.b AS bad_2, -- noqa: LT01, LT03
It is also possible to ignore non-rule based errors, and instead opt to
ignore templating (``TMP``) & parsing (``PRS``) errors.
Expand Down
Expand Up @@ -540,7 +540,7 @@ def make_template(in_str):
# compiling. Unless fixed (like below), this will cause:
# 1. Assertion errors in TemplatedFile, when it sanity checks the
# contents of the sliced_file array.
# 2. L009 linting errors when running "sqlfluff lint foo_bar.sql"
# 2. LT12 linting errors when running "sqlfluff lint foo_bar.sql"
# since the linter will use the compiled code with the newlines
# removed.
# 3. "No newline at end of file" warnings in Git/GitHub since
Expand Down
2 changes: 1 addition & 1 deletion plugins/sqlfluff-templater-dbt/test/fixtures/dbt/.sqlfluff
@@ -1,7 +1,7 @@
[sqlfluff]
templater = dbt
dialect = postgres
# exclude_rules = L009
# exclude_rules = LT12

[sqlfluff:templater:dbt]
profiles_dir = ./
Expand Down
2 changes: 1 addition & 1 deletion plugins/sqlfluff-templater-dbt/test/rules_test.py
Expand Up @@ -21,7 +21,7 @@
# Group By
("AM01", "models/my_new_project/select_distinct_group_by.sql", [(1, 8)]),
# Multiple trailing newline
("L009", "models/my_new_project/multiple_trailing_newline.sql", [(3, 1)]),
("LT12", "models/my_new_project/multiple_trailing_newline.sql", [(3, 1)]),
],
)
def test__rules__std_file_dbt(rule, path, violations, project_dir): # noqa
Expand Down
2 changes: 1 addition & 1 deletion src/sqlfluff/cli/commands.py
Expand Up @@ -391,7 +391,7 @@ def get_linter_and_formatter(
epilog="""\b\bExamples:\n
sqlfluff lint --dialect postgres .\n
sqlfluff lint --dialect postgres --rules ST05 .\n
sqlfluff fix --dialect sqlite --rules L041,ST05 src/queries\n
sqlfluff fix --dialect sqlite --rules LT10,ST05 src/queries\n
sqlfluff parse --dialect sqlite --templater jinja src/queries/common.sql
""",
)
Expand Down
6 changes: 3 additions & 3 deletions src/sqlfluff/core/config.py
Expand Up @@ -115,7 +115,7 @@ class _RemovedConfig:
(lambda x: x),
),
_RemovedConfig(
("rules", "L007", "operator_new_lines"),
("rules", "LT03", "operator_new_lines"),
(
"Use the line_position config in the appropriate "
"sqlfluff:layout section (e.g. sqlfluff:layout:type"
Expand All @@ -134,9 +134,9 @@ class _RemovedConfig:
("layout", "type", "comma", "line_position"),
(lambda x: x),
),
# L019 used to have a more specific version of the same /config itself.
# LT04 used to have a more specific version of the same /config itself.
_RemovedConfig(
("rules", "L019", "comma_style"),
("rules", "LT04", "comma_style"),
(
"Use the line_position config in the appropriate "
"sqlfluff:layout section (e.g. sqlfluff:layout:type"
Expand Down
4 changes: 2 additions & 2 deletions src/sqlfluff/core/default_config.cfg
Expand Up @@ -247,7 +247,7 @@ aliasing = explicit
# Aliasing preference for columns
aliasing = explicit

[sqlfluff:rules:L016]
[sqlfluff:rules:LT05]
# Line length
ignore_comment_lines = False
ignore_comment_clauses = False
Expand Down Expand Up @@ -280,7 +280,7 @@ ignore_words_regex = None
# Disabled for some dialects (e.g. bigquery)
force_enable = False

[sqlfluff:rules:L036]
[sqlfluff:rules:LT09]
wildcard_policy = single

[sqlfluff:rules:CV03]
Expand Down
2 changes: 1 addition & 1 deletion src/sqlfluff/core/linter/linter.py
Expand Up @@ -267,7 +267,7 @@ def parse_noqa(
# Also trim any whitespace afterward

# Comment lines can also have noqa e.g.
# --dafhsdkfwdiruweksdkjdaffldfsdlfjksd -- noqa: L016
# --dafhsdkfwdiruweksdkjdaffldfsdlfjksd -- noqa: LT05
# Therefore extract last possible inline ignore.
comment = [c.strip() for c in comment.split("--")][-1]

Expand Down
2 changes: 1 addition & 1 deletion src/sqlfluff/core/rules/base.py
Expand Up @@ -956,7 +956,7 @@ def _adjust_anchors_for_fixes(cls, context, lint_result) -> None:
"""Makes simple fixes to the anchor position for fixes.
Some rules return fixes where the anchor is too low in the tree. These
are most often rules like LT02 and L016 that make whitespace changes
are most often rules like LT02 and LT05 that make whitespace changes
without a "deep" understanding of the parse structure. This function
attempts to correct those issues automatically. It may not be perfect,
but it should be an improvement over the old behaviour, where rules like
Expand Down
10 changes: 5 additions & 5 deletions src/sqlfluff/rules/L007.py → src/sqlfluff/rules/layout/LT03.py
@@ -1,4 +1,4 @@
"""Implementation of Rule L007."""
"""Implementation of Rule LT03."""

from typing import List

Expand All @@ -7,7 +7,7 @@
from sqlfluff.utils.reflow import ReflowSequence


class Rule_L007(BaseRule):
class Rule_LT03(BaseRule):
"""Operators should follow a standard for being before/after newlines.
**Anti-pattern**
Expand Down Expand Up @@ -45,9 +45,9 @@ class Rule_L007(BaseRule):
FROM foo
"""

name = "line-break.operators"
aliases = ("LB03",)
groups = ("all", "layout", "line-break")
name = "layout.operators"
aliases = ("L007",)
groups = ("all", "layout")
crawl_behaviour = SegmentSeekerCrawler({"binary_operator", "comparison_operator"})
is_fix_compatible = True

Expand Down
@@ -1,4 +1,4 @@
"""Implementation of Rule L019."""
"""Implementation of Rule LT04."""

from typing import List

Expand All @@ -7,7 +7,7 @@
from sqlfluff.utils.reflow import ReflowSequence


class Rule_L019(BaseRule):
class Rule_LT04(BaseRule):
"""Leading/Trailing comma enforcement.
**Anti-pattern**
Expand Down Expand Up @@ -46,7 +46,9 @@ class Rule_L019(BaseRule):
FROM foo
"""

groups = ("all",)
name = "layout.commas"
aliases = ("L019",)
groups = ("all", "layout")
crawl_behaviour = SegmentSeekerCrawler({"comma"})
_adjust_anchors = True
is_fix_compatible = True
Expand Down
@@ -1,4 +1,4 @@
"""Implementation of Rule L016."""
"""Implementation of Rule LT05."""

from typing import List, cast

Expand All @@ -11,10 +11,12 @@
from sqlfluff.utils.reflow.sequence import ReflowSequence


class Rule_L016(BaseRule):
class Rule_LT05(BaseRule):
"""Line is too long."""

groups = ("all", "core")
name = "layout.long_lines"
aliases = ("L016",)
groups = ("all", "core", "layout")
crawl_behaviour = RootOnlyCrawler()
targets_templated = True
template_safe_fixes = True
Expand Down
13 changes: 10 additions & 3 deletions src/sqlfluff/rules/L017.py → src/sqlfluff/rules/layout/LT06.py
@@ -1,11 +1,11 @@
"""Implementation of Rule L017."""
"""Implementation of Rule LT06."""

from sqlfluff.core.rules import BaseRule, LintFix, LintResult, RuleContext
from sqlfluff.core.rules.crawlers import SegmentSeekerCrawler
from sqlfluff.utils.functional import sp, FunctionalContext


class Rule_L017(BaseRule):
class Rule_LT06(BaseRule):
"""Function name not immediately followed by parenthesis.
**Anti-pattern**
Expand All @@ -30,7 +30,9 @@ class Rule_L017(BaseRule):
"""

groups = ("all", "core")
name = "layout.functions"
aliases = ("L017",)
groups = ("all", "core", "layout")
crawl_behaviour = SegmentSeekerCrawler({"function"})
is_fix_compatible = True

Expand All @@ -39,6 +41,11 @@ def _eval(self, context: RuleContext) -> LintResult:
Look for Function Segment with anything other than the
function name before brackets
NOTE: This hasn't been combined with LT01 because it has
some special treatment for comments. That might be something
we revisit at a later point if duplicate errors become
problematic.
"""
segment = FunctionalContext(context).segment
# We only trigger on start_bracket (open parenthesis)
Expand Down
@@ -1,4 +1,4 @@
"""Implementation of Rule L018."""
"""Implementation of Rule LT07."""

from typing import cast

Expand All @@ -13,7 +13,7 @@
from sqlfluff.utils.functional import sp, FunctionalContext


class Rule_L018(BaseRule):
class Rule_LT07(BaseRule):
"""``WITH`` clause closing bracket should be on a new line.
**Anti-pattern**
Expand Down Expand Up @@ -42,7 +42,9 @@ class Rule_L018(BaseRule):
"""

groups = ("all", "core")
name = "layout.functions"
aliases = ("L018",)
groups = ("all", "core", "layout")
crawl_behaviour = SegmentSeekerCrawler(
{"with_compound_statement"}, provide_raw_stack=True
)
Expand Down
@@ -1,4 +1,4 @@
"""Implementation of Rule L022."""
"""Implementation of Rule LT08."""

from typing import Optional, List
from sqlfluff.core.parser import NewlineSegment
Expand All @@ -7,7 +7,7 @@
from sqlfluff.core.rules.crawlers import SegmentSeekerCrawler


class Rule_L022(BaseRule):
class Rule_LT08(BaseRule):
"""Blank line expected but not found after CTE closing bracket.
**Anti-pattern**
Expand Down Expand Up @@ -36,7 +36,9 @@ class Rule_L022(BaseRule):
"""

groups = ("all", "core")
name = "layout.cte_newline"
aliases = ("L022",)
groups = ("all", "core", "layout")
crawl_behaviour = SegmentSeekerCrawler({"with_compound_statement"})
is_fix_compatible = True

Expand Down
@@ -1,4 +1,4 @@
"""Implementation of Rule L036."""
"""Implementation of Rule LT09."""

from typing import List, NamedTuple, Optional, Sequence

Expand All @@ -24,7 +24,7 @@ class SelectTargetsInfo(NamedTuple):
pre_from_whitespace: List[BaseSegment]


class Rule_L036(BaseRule):
class Rule_LT09(BaseRule):
"""Select targets should be on a new line unless there is only one select target.
.. note::
Expand Down Expand Up @@ -69,7 +69,9 @@ class Rule_L036(BaseRule):
"""

groups = ("all",)
name = "layout.select_targets"
aliases = ("L036",)
groups = ("all", "layout")
config_keywords = ["wildcard_policy"]
crawl_behaviour = SegmentSeekerCrawler({"select_clause"})
is_fix_compatible = True
Expand Down

0 comments on commit eea8e46

Please sign in to comment.