sqlint
TypeScript icon, indicating that this package has built-in type declarations

1.7.0 • Public • Published

SQLint

Lint tool for SQL

sqlint

Installation and Usage

You can install SQLint using npm:

$ npm install sqlint --save-dev

Or using yarn:

$ yarn add sqlint -D

Editor Support

You can use sql-language-server to use SQLint on your editor.

sqlint-on-editor

CLI

Example

$ sqlint .
Options:

Options:
  --version     Show version number                                    [boolean]
  -h            Show help                                              [boolean]
  --config, -c  Configuration file path                                 [string]
  --output, -o  Specify file to write report to                         [string]
  --format, -f  Select a output format
                      [string] [choices: "stylish", "json"] [default: "stylish"]
  --stdin       Lint code provide on <STDIN>          [boolean] [default: false]
  --fix         Automatically fix problems            [boolean] [default: false]

Use stdin example:

$ cat ./test/cli/fixtures/lint/errorSql.sql | sqlint --stdin

Fixing problems

--fix option will work to try to fix as many problems as possible.

example:

$ sqlint --fix .

Configuration

Example

{
  "rules": {
    "align-column-to-the-first": "error",
    "column-new-line": "error",
    "linebreak-after-clause-keyword": "error",
    "reserved-word-case": ["error", "upper"],
    "space-surrounding-operators": "error",
    "where-clause-new-line": "error",
    "align-where-clause-to-the-first": "error",
  }
}

Personal config file and project config file

Personal confuguration file

Personal configuration file is located on ~/.config/sql-language-server/.sqlintrc.json. It'll be applied when it can't find a project configuration file.

Project confuguration file

Project configuration file is located on ${YOUR_PROJECT_ROOT}/.sqlintrc.json.

Rule Configuration

Rule shoud be set the ruleID and one of these values:

  • "off" or 0
  • "warn" or 1
  • "error" or 2

Some rules have option that you can modify checking behaviour. For kind of those rules, you can set Array value that the first element is error level and the second element is option value.

"rule-id": ["error", "option"]

Option value's type depends on each rules. Please check each rules description to set it.

Rules

We're always finding new rules to add. If you have any prefer rules you want feel free to open a issue to discuss.

align-column-to-the-first

Align all columns to the first column on their own line.

Good

SELECT
  foo.a,
  foo.b
FROM
  foo

Bad

SELECT
  foo.a,
    foo.b
FROM
  foo
column-new-line

Columns must go on a new line.

Good

SELECT
  foo.a,
  foo.b
FROM
  foo

Bad

SELECT
  foo.a, foo.b
FROM
  foo
linebreak-after-clause-keyword

Require linebreak after SELECT, FROM, WHERE keyword.

Good

SELECT
  foo.aj
FROM
  foo
WHERE
  foo.a > 1

Bad

SELECT foo.aj
FROM foo
WHERE foo.a > 1
reserved-word-case

Reserved word's case should be unified by upper or lower.

Option: "upper" | "lower" (default: "upper")

Good

SELECT * FROM foo

Bad

select * FROM foo
space-surrounding-operators

Spaces around operators.

Option: "always" | "never" (default: "always")

Good("always")

SELECT *
FROM foo
WHERE foo.a > 1
   OR foo.b >= 2
  AND foo.c = true
   OR foo.d <> false

Good("never")

SELECT *
FROM foo
WHERE foo.a>1
   OR foo.b>=2
  AND foo.c=true
   OR foo.d<>false

Bad

SELECT *
FROM foo
WHERE foo.a > 1
   OR foo.b>=2
  AND foo.c=true
   OR foo.d <> false
where-clause-new-line

Multiple where clause must go on a new line.

Good

SELECT foo.a, foo.b
FROM foo
WHERE
  foo.a = 'a'
  AND foo.b = 'b'

Bad

SELECT foo.a, foo.b
FROM foo
WHERE
  foo.a = 'a' AND foo.b = 'b'
align-where-clause-to-the-first

Where clauses must align to the first clause.

Good

SELECT foo.a
FROM foo 
WHERE foo.a = 'a' AND foo.b = 'b' AND
      foo.c = 'c' AND
      foo.d = 'd'

Bad

SELECT foo.a
FROM foo 
WHERE foo.a = 'a' AND foo.b = 'b' AND
foo.c = 'c' AND
foo.d = 'd'
align-where-clause-to-the-first

Where clauses must align to the first clause.

Good

SELECT foo.a
FROM foo 
WHERE foo.a = 'a' AND foo.b = 'b' AND
      foo.c = 'c' AND
      foo.d = 'd'

Bad

SELECT foo.a
FROM foo 
WHERE foo.a = 'a' AND foo.b = 'b' AND
foo.c = 'c' AND
foo.d = 'd'
require-as-to-rename-column

As is always required to rename a column name.

Good

SELECT
  employees.name AS employee_name,
  COUNT(tasks.id) AS assigned_task_count
FROM
  employees LEFT JOIN tasks
    ON employees.id = tasks.id

Bad

SELECT
  employees.name employee_name,
  COUNT(tasks.id) assigned_task_count
FROM
  employees LEFT JOIN tasks
    ON employees.id = tasks.id

Package Sidebar

Install

npm i sqlint

Weekly Downloads

1,136

Version

1.7.0

License

MIT

Unpacked Size

109 kB

Total Files

50

Last publish

Collaborators

  • joe-re