Skip to content

Commit 84b513e

Browse files
v5.0.0-beta
PR (#27) * configure for typescript * prettier * convert to typescript * add default options * fix uppercase default for tests add count keyword * sort and dedupe keywords * add support for escaped keywords as identifiers * fix keyword identifier for non critical tests * add @typescript-eslint/eslint-plugin * fix webpack * add basic alias processing missing double word case * add missing keywords to redshift fix column alias edge case * add alias tests * ♻️ * fix handling of never alias option * prettier check * add newline for itemCount setting * add newline for lineWidth * update newline hybrid rule logic * implement newline mode tests * ♻️ * prettier check * add eslint-config-airbnb-typescript * add @typescript-eslint/parser * fix eslint errors * convert relevant import to type imports * remove ts extension from import in test files * remove sqlformatter d ts * add dense operator option * add operator test * add NewlineMode enum * move types to types file * add AliasMode enum * convert enums into string enums * ♻️ * add semicolon newline option * add unit test for semicolon newline * add maxLength function * add tabulation function * add KeywordMode type * add keyword style examples * remove TopLevelIndents and merge reserved token logic * add basic 10-space keyword positioning * add support for multi item clauses * add support for long keywords * add unit tests for tenSpace modes fix block end index logic * add tests to accept tenSpace modes * fix ON keyword * fix bug with TopLevelIndent on nested blocks starting with FROM ( * add commaPosition option * add post format method * add basic tabular mode * add formatting for comma before column * add unit test for comma modes fix tabular option * add tests for accepting comma modes * add AliasPosition flag * add basic tabular alias function * add unit tests for alias position fix alias joining * add tests for accepting tabular aliases * switch delta from unit test as potential keyword * fix regex flags * fix alias tests * add tests for aliasPosition x keywordPosition edge cases * limit keywordPosition to standard and tenSpace * add test case for long keywords * move keywords that should be in reservedNewlineWords * dedupe keywords * update redshift keywords * split reserved words into categories add links to docs * update Db2 keywords * update MariaDb keywords * update MySql keywords * update N1ql keywords * update Postgres keywords * update Spark keywords * update TSQL keywords * update union/intersect/except lists * split reservedDependentClause from reservedNewline * fix case statements to support dependent clause * update dependent clauses for remaining messages * fix tests and keyword discrepancies * rename TopLevelWord to Command * rename TopLevelNoIndent to Binary Command * move joins to BinaryCommands * rename Newline words to Logical Operators * rename reservedWords to Keywords * rename openParen to Block Start * rename closeParen to Block End * move Logical Operator down * add comments for reserved word categories * fix PlSql test with CROSS/OUTER APPLY * convert all members for formatters classes to static * add paren options type * add basic formatting for open paren position * add basic close paren position formatting * add test for open and close paren position * decline reservedFunctionParens and functionParenSpace * convert token type into TokenType enum * use TypeToken enum to build mapped Regex table * simplify tokenizer class * simplify isToken * update Tokenizer with PR comments * rename isTopLevel to IsCommand * add flag to toggle breaking before boolean operator * support break after operator add test * update tests with static class members * Update Db2FormatterTest.js * add changelog * update README * update package.json * fix semicolonNewline type * Create dependabot.yml * update sqlfmt node script to use config file * add changelog * Create dependabot.yml * update README * update package.json * fix semicolonNewline type
1 parent 3cd6d98 commit 84b513e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+19977
-16479
lines changed

.babelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"presets": ["@babel/preset-env"],
3-
"plugins": ["@babel/plugin-proposal-class-properties", "add-module-exports"]
2+
"presets": ["@babel/preset-env", "@babel/preset-typescript"],
3+
"plugins": ["@babel/plugin-proposal-class-properties", "add-module-exports"]
44
}

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ end_of_line = lf
55
charset = utf-8
66
trim_trailing_whitespace = true
77
insert_final_newline = true
8-
indent_style = space
8+
indent_style = tab
99
indent_size = 2

.eslintrc

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
11
{
2-
"parser": "babel-eslint",
3-
"extends": ["airbnb-base", "prettier"],
4-
"plugins": ["prettier"],
5-
"rules": {
6-
"prettier/prettier": ["error"],
7-
"curly": ["error", "all"],
8-
"require-unicode-regexp": ["error"],
9-
"func-names": "error",
10-
"no-useless-concat": "off",
11-
"class-methods-use-this": "off",
12-
"no-param-reassign": "off",
13-
"prefer-template": "off",
14-
"no-plusplus": "off",
15-
"no-else-return": "off",
16-
"no-use-before-define": "off"
17-
},
18-
"env": {
19-
"jest": true
20-
}
2+
"parser": "@typescript-eslint/parser",
3+
"parserOptions": { "project": "./tsconfig.json", "ecmaVersion": 6, "sourceType": "module" },
4+
"extends": ["airbnb-base", "airbnb-typescript/base", "plugin:import/typescript", "prettier"],
5+
"plugins": ["@typescript-eslint", "prettier"],
6+
"rules": {
7+
"class-methods-use-this": "off",
8+
"curly": ["error", "all"],
9+
"eqeqeq": "warn",
10+
"func-names": "error",
11+
"no-continue": "off",
12+
"no-param-reassign": "off",
13+
"no-plusplus": "off",
14+
"no-else-return": "off",
15+
"no-use-before-define": "warn",
16+
"no-useless-concat": "off",
17+
"prefer-template": "off",
18+
"prettier/prettier": ["error"],
19+
"@typescript-eslint/comma-dangle": "off",
20+
"@typescript-eslint/indent": "off",
21+
"@typescript-eslint/lines-between-class-members": "off",
22+
"@typescript-eslint/naming-convention": "warn",
23+
"@typescript-eslint/no-unused-vars": "warn",
24+
"@typescript-eslint/quotes": [
25+
"warn",
26+
"single",
27+
{ "avoidEscape": true, "allowTemplateLiterals": true }
28+
],
29+
"@typescript-eslint/semi": "warn"
30+
},
31+
"settings": {
32+
"import/resolver": {
33+
"node": {
34+
"extensions": [".js", ".ts"]
35+
}
36+
}
37+
},
38+
"env": {
39+
"jest": true
40+
}
2141
}

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
/dist
33
/lib
44
/node_modules
5+
yarn.lock
6+
LICENSE

.prettierrc.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
2-
"printWidth": 100,
3-
"singleQuote": true
2+
"printWidth": 100,
3+
"singleQuote": true,
4+
"arrowParens": "avoid",
5+
"semi": true,
6+
"bracketSpacing": true,
7+
"useTabs": true,
8+
"tabWidth": 2,
9+
"endOfLine": "auto"
410
}

CHANGELOG.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# CHANGELOG
2+
3+
## NEXT [5.0.0] : 2021-11-03
4+
5+
### Added
6+
7+
New Features:
8+
9+
- added option `aliasAs` to toggle use of AS in column, table, query aliases
10+
- modes: always (SELECT and table), select (SELECT only), never
11+
- added option `newline` to specify rules for inserting newlines within SQL Statements
12+
- modes: \
13+
always (break before all arguments) \
14+
lineWidth (break after line exceeds lineWidth) \
15+
itemCount (break after n items) \
16+
hybrid (lineWidth OR itemCount) \
17+
never (place all Statements on one line)
18+
- added flag `denseOperators` to toggle spaces around binary operators (=, +, %, etc.)
19+
- added flag `semicolonNewline` to toggle placing semicolon on newline vs same line
20+
- added flag `tabulateAlias` for alias tabular mode, aligned on longest line, not including AS
21+
- added option `commaPosition` to specify comma placement within listed Statements
22+
- modes: \
23+
before(comma goes before column), \
24+
after(standard), \
25+
tabular(aligned to longest line)
26+
- added option `keywordPosition` to support vertically aligned keywords
27+
- modes: \
28+
standard, \
29+
tenSpaceLeft(left-aligned within keyword column), \
30+
tenSpaceRight(right-aligned within keyword column)
31+
- added flag `breakBeforeBooleanOperator` to toggle breaking before or after logical operators like AND and OR
32+
- added options `parenOptions` for misc rules regarding parenthesis position
33+
- `openParenNewline` - flag for opening paren on newline or same line
34+
- `closeParenNewline` - flag for closing paren on newline or same line
35+
36+
Other:
37+
38+
- added enums for all typed config options
39+
40+
Files Added:
41+
42+
- test/comma.js (tests for comma position)
43+
- test/alias.js (tests for alias AS and alias position)
44+
- test/keywordPosition.js (tests for keyword position modes)
45+
- test/newline.js (tests for newline modes)
46+
- test/parenthesis.js (tests for paren positions)
47+
48+
### Removed
49+
50+
Files Removed:
51+
52+
- tokenTypes.ts (token types moved to TokenType enum in token.ts)
53+
- sqlFormatter.d.ts (converted to TypeScript)
54+
55+
### Updated
56+
57+
Major changes:
58+
59+
- converted repo to Typescript
60+
- overhauled Keyword lists for all languages
61+
62+
Other:
63+
64+
- added default options for all configs
65+
- updated CLI to use config file
66+
- renamed Keyword categories to semantic Keyword types
67+
- reservedTopLevelWord → reservedCommand
68+
- reservedTopLevelWordNoIndent → reservedBinaryCommand
69+
- reservedNewline → reservedDependentClause & reservedLogicalOperator
70+
- reservedWord → reservedKeyword
71+
- added reservedFunctions
72+
- updated Tokenizer class and token.ts to be more DRY

README.md

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ It does not support:
2121
- Stored procedures.
2222
- Changing of the delimiter type to something else than `;`.
2323

24-
→ [Try the demo.](https://zeroturnaround.github.io/sql-formatter/)
24+
[Try the demo.](https://zeroturnaround.github.io/sql-formatter/)
25+
26+
# Table of contents
27+
28+
- [Install](#install)
29+
- [Usage](#usage)
30+
- [Usage as library](#usage-as-library)
31+
- [Usage from command line](#usage-from-command-line)
32+
- [Usage without NPM](#usage-without-npm)
33+
- [Contributing](#contributing)
2534

2635
## Install
2736

@@ -31,7 +40,9 @@ Get the latest version from NPM:
3140
npm install sql-formatter
3241
```
3342

34-
## Usage as library
43+
## Usage
44+
45+
### Usage as library
3546

3647
```js
3748
import { format } from 'sql-formatter';
@@ -52,10 +63,10 @@ You can also pass in configuration options:
5263

5364
```js
5465
format('SELECT * FROM tbl', {
55-
language: 'spark', // Defaults to "sql" (see the above list of supported dialects)
56-
indent: ' ', // Defaults to two spaces
57-
uppercase: bool, // Defaults to false (not safe to use when SQL dialect has case-sensitive identifiers)
58-
linesBetweenQueries: 2, // Defaults to 1
66+
language: 'spark', // Defaults to "sql" (see the above list of supported dialects)
67+
indent: ' ', // Defaults to two spaces
68+
uppercase: false, // Defaults to true
69+
linesBetweenQueries: 2, // Defaults to 1
5970
});
6071
```
6172

@@ -75,7 +86,7 @@ format("SELECT * FROM tbl WHERE foo = ?", {
7586

7687
Both result in:
7788

78-
```
89+
```sql
7990
SELECT
8091
*
8192
FROM
@@ -84,7 +95,7 @@ WHERE
8495
foo = 'bar'
8596
```
8697

87-
## Usage from command line
98+
### Usage from command line
8899

89100
The CLI tool will be installed under `sql-formatter`
90101
and may be invoked via `npx sql-formatter`:
@@ -94,34 +105,30 @@ sql-formatter -h
94105
```
95106

96107
```
97-
usage: sql-formatter [-h] [-o OUTPUT] [-l {db2,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,tsql}]
98-
[-i N | -t] [-u] [--lines-between-queries N] [--version] [FILE]
108+
usage: sqlfmt.js [-h] [-o OUTPUT] \
109+
[-l {db2,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,tsql}] [-c CONFIG] [--version] [FILE]
99110
100111
SQL Formatter
101112
102113
positional arguments:
103-
FILE Input SQL file (defaults to stdin)
114+
FILE Input SQL file (defaults to stdin)
104115
105116
optional arguments:
106-
-h, --help show this help message and exit
107-
-o OUTPUT, --output OUTPUT
108-
File to write SQL output (defaults to stdout)
109-
-l {db2,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,tsql},
110-
--language {db2,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,tsql}
111-
SQL Formatter dialect (defaults to basic sql)
112-
-i N, --indent N Number of spaces to indent query blocks (defaults to 2)
113-
-t, --tab-indent Indent query blocks with tabs instead of spaces
114-
-u, --uppercase Capitalize language keywords
115-
--lines-between-queries N
116-
How many newlines to insert between queries (separated by ";")
117-
--version show program's version number and exit
117+
-h, --help show this help message and exit
118+
-o, --output OUTPUT
119+
File to write SQL output (defaults to stdout)
120+
-l, --language {db2,mariadb,mysql,n1ql,plsql,postgresql,redshift,spark,sql,tsql}
121+
SQL Formatter dialect (defaults to basic sql)
122+
-c, --config CONFIG
123+
Path to config json file (will use default configs if unspecified)
124+
--version show program's version number and exit
118125
```
119126

120127
By default, the tool takes queries from stdin and processes them to stdout but
121128
one can also name an input file name or use the `--output` option.
122129

123130
```sh
124-
echo 'select * from tbl where id = 3' | sql-formatter -u
131+
echo 'select * from tbl where id = 3' | sql-formatter
125132
```
126133

127134
```sql
@@ -133,7 +140,34 @@ WHERE
133140
id = 3
134141
```
135142

136-
## Usage without NPM
143+
The tool also accepts a JSON config file with the `--config` option that takes this form: \
144+
All fields are optional and all fields that are not specified will be filled with their default values
145+
146+
```json
147+
{
148+
"indent": string,
149+
"uppercase": boolean,
150+
"keywordPosition": "standard" | "tenSpaceLeft" | "tenSpaceRight",
151+
"newline": {
152+
"mode": "always" | "itemCount" | "lineWidth" | "hybrid" | "never",
153+
"itemCount":? number
154+
},
155+
"breakBeforeBooleanOperator": boolean,
156+
"aliasAs": "always" | "select" | "never",
157+
"tabulateAlias": boolean,
158+
"commaPosition": "before" | "after" | "tabular",
159+
"parenOptions": {
160+
"openParenNewline": boolean,
161+
"closeParenNewline": boolean
162+
},
163+
"lineWidth": number,
164+
"linesBetweenQueries": number,
165+
"denseOperators": boolean,
166+
"semicolonNewline": boolean,
167+
}
168+
```
169+
170+
### Usage without NPM
137171

138172
If you don't use a module bundler, clone the repository, run `npm install` and grab a file from `/dist` directory to use inside a `<script>` tag.
139173
This makes SQL Formatter available as a global variable `window.sqlFormatter`.

0 commit comments

Comments
 (0)