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

New Rule L052: Semi colon alignment #1902

Merged
merged 22 commits into from Nov 18, 2021
Merged

New Rule L052: Semi colon alignment #1902

merged 22 commits into from Nov 18, 2021

Conversation

jpy-git
Copy link
Contributor

@jpy-git jpy-git commented Nov 14, 2021

Brief summary of the change made

This PR introduces a new rule L052. It serves two purposes, one to snap semi-colons to the end of the preceding statement, and the other to add the final trailing semi-colon if it is missing:

Screen-Recording-2021-11-14-at-15 59 30

I have added a configuration option called allow_final_semi_colon (which defaults to False) so users can allow missing final semi-colons if they desire (I'm happy to change the default to True on this if you would prefer, but my preference is to add the final semi-colons by default).

This change impacts quite a few unit tests, solely because we weren't previously including final semi-colon, so I have gone through and added the semi-colon/excluded the rule where I think appropriate.

Fixes #1748

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

No

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 python test/generate_parse_fixture_yml.py or by running tox locally).
    • 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.

@jpy-git
Copy link
Contributor Author

jpy-git commented Nov 14, 2021

@barrywhart took a couple attempts to track down all the places this impacted in the dbt tests, but this should be good for review now 😄

@codecov
Copy link

codecov bot commented Nov 14, 2021

Codecov Report

Merging #1902 (3b2892b) into main (93c4eb3) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main     #1902   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          142       143    +1     
  Lines        10090     10152   +62     
=========================================
+ Hits         10090     10152   +62     
Impacted Files Coverage Δ
src/sqlfluff/core/rules/config_info.py 100.00% <ø> (ø)
src/sqlfluff/rules/L052.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 93c4eb3...3b2892b. Read the comment docs.

@WittierDinosaur
Copy link
Contributor

I don't think this is an anti-pattern. It's fairly standard in large queries

        -- Newlines between the end of the statement
        -- and the semi-colon are forbidden.
        SELECT
            a
        FROM foo
        ;

@tunetheweb
Copy link
Member

Personally I like this rule!

What about an optional config to also allow them to be on a newline (with no extra newlines in between) to keep the @WittierDinosaur 's of the world happy too? In which case should this be enforced or only allowed? Which should fix use if it's missing and this optional param is set?

@jpy-git
Copy link
Contributor Author

jpy-git commented Nov 14, 2021

@WittierDinosaur @tunetheweb sure thing! I don't think it should be too hard to add that option.

The way it will work is user has an option of either semi-colon on same line OR new line, but not both. Consistency should be encouraged.

The default will be to use same line.

@alanmcruickshank
Copy link
Member

alanmcruickshank commented Nov 14, 2021

So - potential spanner in the works for this rule. Trailing semi colons are explicitly a problem for dbt users:
image

Reason being that often queries are "wrapped" by other sql and so the trailing semicolon becomes an issue.

Potential solutions:

  • This rule is disabled by default for users when using the dbt templater (although I'm not sure exactly what the mechanism should be to do that).
  • That semi-colons between statements are linted by default, but that the default is that trailing semicolons aren't added.

I think I prefer the latter, that this rule:

  • moves any semi-colons present.
  • optionally adds trailing semi-colons if not present, but this is not enabled by default.

@jpy-git
Copy link
Contributor Author

jpy-git commented Nov 14, 2021

@alanmcruickshank ah was not aware of that, agree that your approach to disable the trailing semi-colon by default is pragmatic though, will tweak the default config 👍

@alanmcruickshank
Copy link
Member

@alanmcruickshank ah was not aware of that, agree that your approach to disable the trailing semi-colon by default is pragmatic though, will tweak the default config 👍

If we're taking that option - would you mind removing the semi-colons from the tests in the dbt tests. I'm happy for them to stay on the core tests - but I think that sets the right expectation for performance with dbt. Bonus points if you make a note in the default config to that effect.

@jpy-git
Copy link
Contributor Author

jpy-git commented Nov 14, 2021

Will do! 😄

jacopofar and others added 7 commits November 14, 2021 18:31
* Implement generic placeholder templating

* Add %s placeholder too

* Add tests and check for the config state

* Remove typo and improve wording of the documentation for placeholder

Co-authored-by: Barry Hart <barrywhart@yahoo.com>
Co-authored-by: Barry Pollard <barry@tunetheweb.com>
* Add tox commands to streamline release workflow

* typo in tox env name

Co-authored-by: Alan Cruickshank <alanmcruickshank@gmail.com>
@jpy-git
Copy link
Contributor Author

jpy-git commented Nov 14, 2021

Addressed the "allow_final_semi_colon" being set to true by default point. Will move on to looking at the option for semi-colons on a new line now

@jpy-git
Copy link
Contributor Author

jpy-git commented Nov 14, 2021

@WittierDinosaur @tunetheweb @alanmcruickshank I've added the options to configure the behaviour of rule L052 as we described above. I've also added extra unit tests for the config options.

Demo:
semi-colon-config-options

Should be good to go if you're happy with it 😄

@jpy-git
Copy link
Contributor Author

jpy-git commented Nov 17, 2021

@barrywhart Ready for review 😄

@barrywhart
Copy link
Member

Tagging other commenters for review. I'll take a look as well.

src/sqlfluff/core/default_config.cfg Outdated Show resolved Hide resolved
src/sqlfluff/core/default_config.cfg Outdated Show resolved Hide resolved
Copy link
Member

@tunetheweb tunetheweb left a comment

Choose a reason for hiding this comment

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

LGTM

@jpy-git
Copy link
Contributor Author

jpy-git commented Nov 18, 2021

@tunetheweb @barrywhart think we can merge this, L053, and L054 down once the final CI checks are done, I've addressed all the suggestions and test cases look good

@pwildenhain pwildenhain merged commit 4d628b3 into sqlfluff:main Nov 18, 2021
@jpy-git jpy-git deleted the semi_colon_alignment branch November 18, 2021 20:28
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.

New TSQL rule suggestion: Flag statements that do not end with semi-colon
7 participants