Skip to content
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

Rule L019: Ignore comma placement violations if the adjacent code is templated #2717

Conversation

barrywhart
Copy link
Member

@barrywhart barrywhart commented Feb 25, 2022

Brief summary of the change made

Fixes #2688, #2706

Are there any other side effects of this change that we should be aware of?

Pull Request checklist

  • Please confirm you have completed any of the necessary steps below.

  • Included test cases to demonstrate any code changes, which may be one or more of the following:

    • .yml rule test cases in test/fixtures/rules/std_rule_cases.
    • .sql/.yml parser test cases in test/fixtures/dialects (note YML files can be auto generated with tox -e generate-fixture-yml).
    • Full autofix test cases in test/fixtures/linter/autofix.
    • Other.
  • Added appropriate documentation for the change.

  • Created GitHub issues for any relevant followup/future enhancements if appropriate.

parsed.tree, [], dialect=cfg.get("dialect_obj"), templated_file=rendered[0]
)
lint_result = linter.lint_string(sql, config=cfg, fname="<STR>")
lerrs = lint_result.violations
Copy link
Member Author

@barrywhart barrywhart Feb 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing old tech debt that was breaking the new test: I changed this function to call the public lint_string() which runs the templater, parser, and linter in one step. The old code calls the rule directly which is hacky, and it was skipping the template-related filtering of lint results built into the linter, causing my new "pass" tests to fail.

@barrywhart barrywhart marked this pull request as draft February 25, 2022 21:02
@codecov
Copy link

codecov bot commented Feb 25, 2022

Codecov Report

Merging #2717 (cf7340a) into main (8c0fecf) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main     #2717   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          163       163           
  Lines        12064     12067    +3     
=========================================
+ Hits         12064     12067    +3     
Impacted Files Coverage Δ
...lfluff/core/rules/functional/segment_predicates.py 100.00% <ø> (ø)
...uff/core/rules/functional/templated_file_slices.py 100.00% <100.00%> (ø)
src/sqlfluff/rules/L019.py 100.00% <100.00%> (ø)
src/sqlfluff/testing/rules.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8c0fecf...cf7340a. Read the comment docs.

@barrywhart barrywhart marked this pull request as ready for review February 25, 2022 22:24
@barrywhart barrywhart changed the title Rule L019: Anchor on column, not comma, to reduce templating false positives Rule L019: For leading comma violations, anchor on following code, not comma, to reduce templating false positives Feb 25, 2022
@barrywhart barrywhart changed the title Rule L019: For leading comma violations, anchor on following code, not comma, to reduce templating false positives Rule L019: Ignore comma placement violations if the adjacent code is templated Feb 25, 2022
@@ -168,7 +168,6 @@ def templated_slices(
raise ValueError(
'templated_slices: "templated_file" parameter is required.'
) # pragma: no cover
templated_slices = []
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused variable

if (
stop >= slice_.templated_slice.start and start < slice_.templated_slice.stop
)
if (stop > slice_.templated_slice.start and start < slice_.templated_slice.stop)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed a boundary condition issue that was returning slices that it shouldn't

"""Override new operator."""
return super(TemplatedFileSlices, cls).__new__(cls, raw_slices)
return super(TemplatedFileSlices, cls).__new__(cls, templated_slices)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix old copy-paste naming issue

break
if select_if is None or select_if(slice_):
buff.append(slice_)
return TemplatedFileSlices(*buff, templated_file=self.templated_file)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this method, equivalent to the select() methods on Segments and RawFileSlices. Ended up not using it, but thought it was worth keeping for future use.

if (
last_seg
and last_seg.is_type("newline")
and not last_seg.is_templated
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is the actual fix: Ignore templated newlines when looking for rule violations.

@@ -202,7 +206,7 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:
# no code precedes the current position: no issue
if last_seg is None:
return None
if last_seg.is_type("comma"):
if last_seg.is_type("comma") and not context.segment.is_templated:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same fix as above, but for the leading comma case.

@barrywhart barrywhart merged commit bd3914f into sqlfluff:main Feb 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

leading comma + dbt macro mistakenly produces "expected only leading" error
2 participants