Repository: sql-formatter-org/sql-formatter
Pull request #285 (2022-07-05 15:12:41)

Split core/ directory into smaller sub directories
broke up core/ into core/, formatter/ and parser/

nene: I approve the reorganization. Makes sense.

But I'm not in favor of the file naming. I'd prefer following the principle of naming the file the same as the main class or function it exports. If it exports multitude of things, then sure, the name could be different.

Like currently you have renamed `Formatter.ts` to `formatter.ts`. (Although imports still mistakenly reference `Formatter.ts`). I'd prefer keeping the name as `Formatter.ts`.
inferrinizzard: sure, I'll rename modules back to PascalCase instead

Patch:
diff --git a/src/core/AliasAs.ts b/src/formatter/AliasAs.ts
similarity index 99%
rename from src/core/AliasAs.ts
rename to src/formatter/AliasAs.ts
index 83f34ab8..75349c4a 100644
--- a/src/core/AliasAs.ts
+++ b/src/formatter/AliasAs.ts
@@ -1,8 +1,7 @@
 import type { AliasMode, FormatOptions } from 'src/types';
+import { isCommand, isToken, type Token, TokenType, EOF_TOKEN, isReserved } from 'src/core/token';
 import AsTokenFactory from './AsTokenFactory';
 
-import { isCommand, isToken, type Token, TokenType, EOF_TOKEN, isReserved } from './token';
-
 /** Adds and removes AS tokens as configured by aliasAs option */
 export default class AliasAs {
   private index = 0;
diff --git a/src/core/AsTokenFactory.ts b/src/formatter/AsTokenFactory.ts
similarity index 93%
rename from src/core/AsTokenFactory.ts
rename to src/formatter/AsTokenFactory.ts
index b4162b09..2530ffb7 100644
--- a/src/core/AsTokenFactory.ts
+++ b/src/formatter/AsTokenFactory.ts
@@ -1,6 +1,5 @@
 import type { KeywordCase } from 'src/types';
-
-import { isToken, type Token, TokenType } from './token';
+import { isToken, type Token, TokenType } from 'src/core/token';
 
 export default class AsTokenFactory {
   private detectedCase: KeywordCase;
diff --git a/src/core/ExpressionFormatter.ts b/src/formatter/ExpressionFormatter.ts
similarity index 98%
rename from src/core/ExpressionFormatter.ts
rename to src/formatter/ExpressionFormatter.ts
index 74097844..3ee8d3e1 100644
--- a/src/core/ExpressionFormatter.ts
+++ b/src/formatter/ExpressionFormatter.ts
@@ -1,9 +1,9 @@
 import type { FormatOptions } from 'src/types';
 import { equalizeWhitespace } from 'src/utils';
 
-import InlineBlock from './InlineBlock';
-import Params from './Params';
-import { isReserved, type Token, TokenType } from './token';
+import Params from 'src/core/Params';
+import { isTabularStyle } from 'src/core/config';
+import { isReserved, type Token, TokenType } from 'src/core/token';
 import {
   AllColumnsAsterisk,
   ArraySubscript,
@@ -15,8 +15,9 @@ import {
   LimitClause,
   NodeType,
   Parenthesis,
-} from './ast';
-import { isTabularStyle } from './config';
+} from 'src/parser/ast';
+
+import InlineBlock from './InlineBlock';
 import Layout, { WS } from './Layout';
 import toTabularFormat, { isTabularToken } from './tabularStyle';
 
diff --git a/src/core/Formatter.ts b/src/formatter/Formatter.ts
similarity index 92%
rename from src/core/Formatter.ts
rename to src/formatter/Formatter.ts
index 0d94ad12..0adc2f8a 100644
--- a/src/core/Formatter.ts
+++ b/src/formatter/Formatter.ts
@@ -1,13 +1,15 @@
 import type { FormatOptions } from 'src/types';
-import Params from './Params';
-import Tokenizer from './Tokenizer';
+import { indentString } from 'src/core/config';
+import Params from 'src/core/Params';
+import Tokenizer from 'src/core/Tokenizer';
+
+import Parser from 'src/parser/Parser';
+import { Statement } from 'src/parser/ast';
+
 import formatCommaPositions from './formatCommaPositions';
 import formatAliasPositions from './formatAliasPositions';
-import Parser from './Parser';
 import ExpressionFormatter from './ExpressionFormatter';
-import { indentString } from './config';
 import AliasAs from './AliasAs';
-import { Statement } from './ast';
 import Layout, { WS } from './Layout';
 import Indentation from './Indentation';
 
diff --git a/src/core/Indentation.ts b/src/formatter/Indentation.ts
similarity index 100%
rename from src/core/Indentation.ts
rename to src/formatter/Indentation.ts
diff --git a/src/core/InlineBlock.ts b/src/formatter/InlineBlock.ts
similarity index 94%
rename from src/core/InlineBlock.ts
rename to src/formatter/InlineBlock.ts
index bb7d5ff5..00a64e62 100644
--- a/src/core/InlineBlock.ts
+++ b/src/formatter/InlineBlock.ts
@@ -1,6 +1,6 @@
 import { sum } from 'src/utils';
-import { BetweenPredicate, NodeType, Parenthesis } from './ast';
-import { isToken, type Token, TokenType } from './token';
+import { isToken, type Token, TokenType } from 'src/core/token';
+import { BetweenPredicate, NodeType, Parenthesis } from 'src/parser/ast';
 
 /**
  * Bookkeeper for inline blocks.
diff --git a/src/core/Layout.ts b/src/formatter/Layout.ts
similarity index 100%
rename from src/core/Layout.ts
rename to src/formatter/Layout.ts
diff --git a/src/core/formatAliasPositions.ts b/src/formatter/formatAliasPositions.ts
similarity index 100%
rename from src/core/formatAliasPositions.ts
rename to src/formatter/formatAliasPositions.ts
diff --git a/src/core/formatCommaPositions.ts b/src/formatter/formatCommaPositions.ts
similarity index 100%
rename from src/core/formatCommaPositions.ts
rename to src/formatter/formatCommaPositions.ts
diff --git a/src/core/tabularStyle.ts b/src/formatter/tabularStyle.ts
similarity index 95%
rename from src/core/tabularStyle.ts
rename to src/formatter/tabularStyle.ts
index 230fb9d5..eef1a718 100644
--- a/src/core/tabularStyle.ts
+++ b/src/formatter/tabularStyle.ts
@@ -1,5 +1,5 @@
 import type { IndentStyle } from 'src/types';
-import { Token, TokenType } from './token';
+import { Token, TokenType } from 'src/core/token';
 
 /**
  * When tabular style enabled,
diff --git a/src/languages/bigquery.formatter.ts b/src/languages/bigquery.formatter.ts
index 68445dd4..988bd702 100644
--- a/src/languages/bigquery.formatter.ts
+++ b/src/languages/bigquery.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { EOF_TOKEN, isToken, TokenType, type Token } from 'src/core/token';
 import { dedupe } from 'src/utils';
diff --git a/src/languages/db2.formatter.ts b/src/languages/db2.formatter.ts
index 5dd49b55..a5bb5831 100644
--- a/src/languages/db2.formatter.ts
+++ b/src/languages/db2.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { dedupe } from 'src/utils';
 
diff --git a/src/languages/hive.formatter.ts b/src/languages/hive.formatter.ts
index 7bb9978e..d67340bf 100644
--- a/src/languages/hive.formatter.ts
+++ b/src/languages/hive.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { dedupe } from 'src/utils';
 
diff --git a/src/languages/mariadb.formatter.ts b/src/languages/mariadb.formatter.ts
index 718e16bb..4c357073 100644
--- a/src/languages/mariadb.formatter.ts
+++ b/src/languages/mariadb.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { EOF_TOKEN, isToken, type Token, TokenType } from 'src/core/token';
 import { dedupe } from 'src/utils';
diff --git a/src/languages/mysql.formatter.ts b/src/languages/mysql.formatter.ts
index ab9381a1..77a35b7c 100644
--- a/src/languages/mysql.formatter.ts
+++ b/src/languages/mysql.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { EOF_TOKEN, isToken, type Token, TokenType } from 'src/core/token';
 import { dedupe } from 'src/utils';
diff --git a/src/languages/n1ql.formatter.ts b/src/languages/n1ql.formatter.ts
index 303206bd..9eccaa99 100644
--- a/src/languages/n1ql.formatter.ts
+++ b/src/languages/n1ql.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { dedupe } from 'src/utils';
 
diff --git a/src/languages/plsql.formatter.ts b/src/languages/plsql.formatter.ts
index 537c5dfa..463468c1 100644
--- a/src/languages/plsql.formatter.ts
+++ b/src/languages/plsql.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { EOF_TOKEN, isReserved, isToken, type Token, TokenType } from 'src/core/token';
 import { dedupe } from 'src/utils';
diff --git a/src/languages/postgresql.formatter.ts b/src/languages/postgresql.formatter.ts
index 44519b30..5b9f6673 100644
--- a/src/languages/postgresql.formatter.ts
+++ b/src/languages/postgresql.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { dedupe } from 'src/utils';
 
diff --git a/src/languages/redshift.formatter.ts b/src/languages/redshift.formatter.ts
index e6a4eb0b..ca068c1b 100644
--- a/src/languages/redshift.formatter.ts
+++ b/src/languages/redshift.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { dedupe } from 'src/utils';
 
diff --git a/src/languages/spark.formatter.ts b/src/languages/spark.formatter.ts
index df4fca14..541145c6 100644
--- a/src/languages/spark.formatter.ts
+++ b/src/languages/spark.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { EOF_TOKEN, isToken, type Token, TokenType } from 'src/core/token';
 import { dedupe } from 'src/utils';
diff --git a/src/languages/sql.formatter.ts b/src/languages/sql.formatter.ts
index 0a049b7c..0c5e6828 100644
--- a/src/languages/sql.formatter.ts
+++ b/src/languages/sql.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { dedupe } from 'src/utils';
 
diff --git a/src/languages/sqlite.formatter.ts b/src/languages/sqlite.formatter.ts
index 4381f05e..bf64e555 100644
--- a/src/languages/sqlite.formatter.ts
+++ b/src/languages/sqlite.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { dedupe } from 'src/utils';
 
diff --git a/src/languages/tsql.formatter.ts b/src/languages/tsql.formatter.ts
index b16a92c6..9ff584c0 100644
--- a/src/languages/tsql.formatter.ts
+++ b/src/languages/tsql.formatter.ts
@@ -1,4 +1,4 @@
-import Formatter from 'src/core/Formatter';
+import Formatter from 'src/formatter/Formatter';
 import Tokenizer from 'src/core/Tokenizer';
 import { dedupe } from 'src/utils';
 
diff --git a/src/core/Parser.ts b/src/parser/Parser.ts
similarity index 98%
rename from src/core/Parser.ts
rename to src/parser/Parser.ts
index a65f89a2..5920e2ce 100644
--- a/src/core/Parser.ts
+++ b/src/parser/Parser.ts
@@ -1,4 +1,5 @@
 /* eslint-disable no-cond-assign */
+import { EOF_TOKEN, type Token, TokenType, isToken } from 'src/core/token';
 import {
   AllColumnsAsterisk,
   ArraySubscript,
@@ -13,7 +14,6 @@ import {
   Statement,
   TokenNode,
 } from './ast';
-import { EOF_TOKEN, type Token, TokenType, isToken } from './token';
 
 /**
  * A simple parser that creates a very rudimentary syntax tree.
diff --git a/src/core/ast.ts b/src/parser/ast.ts
similarity index 97%
rename from src/core/ast.ts
rename to src/parser/ast.ts
index 4a542fc3..c09439be 100644
--- a/src/core/ast.ts
+++ b/src/parser/ast.ts
@@ -1,4 +1,4 @@
-import { Token } from './token';
+import { Token } from 'src/core/token';
 
 export enum NodeType {
   statement = 'statement',


Test patch:
diff --git a/test/unit/Parser.test.ts b/test/unit/Parser.test.ts
index 140558d5..6656525a 100644
--- a/test/unit/Parser.test.ts
+++ b/test/unit/Parser.test.ts
@@ -1,4 +1,4 @@
-import Parser from 'src/core/Parser';
+import Parser from 'src/parser/Parser';
 import Tokenizer from 'src/core/Tokenizer';
 
 describe('Parser', () => {
diff --git a/test/unit/tabularStyle.test.ts b/test/unit/tabularStyle.test.ts
index 7aca39ea..3ae93794 100644
--- a/test/unit/tabularStyle.test.ts
+++ b/test/unit/tabularStyle.test.ts
@@ -1,4 +1,4 @@
-import toTabularFormat from 'src/core/tabularStyle';
+import toTabularFormat from 'src/formatter/tabularStyle';
 
 describe('toTabularFormat()', () => {
   it('does nothing in standard style', () => {