Skip to content

Commit c9b0c84

Browse files
committed
Javascript: Add typescript generation + adjust jsdoc typing
1 parent c2b0dbc commit c9b0c84

File tree

6 files changed

+58
-32
lines changed

6 files changed

+58
-32
lines changed

cratedb_sqlparse_js/cratedb_sqlparse/AstBuilder.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import SqlBaseParserVisitor from "./generated_parser/SqlBaseParserVisitor.js";
2+
import SqlBaseParser from "./generated_parser/SqlBaseParser.js";
23
import {Statement} from "./parser.js"
34
import {Table} from "./models.js"
45

56

67
/**
78
*
8-
* @param {String} text
9+
* @param {string} text
910
* @returns {Boolean}
1011
*/
1112
function isDigit(text) {
@@ -26,7 +27,7 @@ export class AstBuilder extends SqlBaseParserVisitor {
2627
/**
2728
*
2829
* @param {Object} node
29-
* @returns {(String|null)}
30+
* @returns {(string|null)}
3031
*/
3132
getText(node) {
3233
if (node) {

cratedb_sqlparse_js/cratedb_sqlparse/models.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,22 @@
55
export class Metadata {
66

77
/**
8-
*
9-
* @param {Table[]} tables
10-
* @param parameterizedProperties
11-
* @param withProperties
8+
* @param {Table[]} tables - The referenced tables in the query.
9+
* @param {object} parameterizedProperties - The properties whose value can be used to inject parameters, they start with '$', example: `CREATE TABLE a (b text) WITH ("allocation.max_retries" = $1)`
10+
* @param {object} withProperties - SQL properties, defined after a `WITH` statement. Example: `CREATE TABLE a (b text) WITH ("allocation.max_retries" = 5)`
1211
*/
1312
constructor(tables, parameterizedProperties, withProperties) {
1413
this.tables = tables || []
1514
this.parameterizedProperties = parameterizedProperties || {}
1615
this.withProperties = withProperties || {}
1716
}
18-
19-
toString() {
20-
21-
}
2217
}
2318

2419
/**
2520
*
26-
* @param {String} text
27-
* @param {String} quoted_with
21+
* @param {string} text
22+
* @param {string} quoted_with
23+
* @return {string}
2824
*/
2925
function quoted(text, quoted_with = '"') {
3026
return quoted_with + text + quoted_with
@@ -33,8 +29,9 @@ function quoted(text, quoted_with = '"') {
3329
export class Table {
3430
/**
3531
*
36-
* @param {String} name
37-
* @param {String} schema
32+
* @param {string} name
33+
* @param {string} schema
34+
* @property {string} fqn - Full qualified name, example: "sys"."shards"
3835
*/
3936
constructor(name, schema) {
4037
this.name = name
@@ -43,7 +40,7 @@ export class Table {
4340
}
4441

4542
/**
46-
* @return {String} The full qualified name, quoted.
43+
* @return {string} - The full qualified name, quoted.
4744
*/
4845
_getFqn() {
4946
return (this.schema ? quoted(this.schema) + "." : "") + quoted(this.name)

cratedb_sqlparse_js/cratedb_sqlparse/parser.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ export class ParseError extends Error {
3131

3232
/**
3333
*
34-
* @param {String} query
35-
* @param {String} msg
36-
* @param offending_token
37-
* @param e
38-
* @member {String} errorMessage
39-
* @member {String} errorMessageVerbose
34+
* @param {string} query
35+
* @param {string} msg
36+
* @param {object} offending_token
37+
* @param {object} e
38+
* @member {string} errorMessage
39+
* @member {string} errorMessageVerbose
4040
*/
4141
constructor(query, msg, offending_token, e) {
4242
super(msg);
@@ -53,14 +53,26 @@ export class ParseError extends Error {
5353
return `[line ${this.line}:${this.column} ${this.message}]`
5454
}
5555

56+
/**
57+
*
58+
* @returns {Number}
59+
*/
5660
getColumn() {
5761
return this.offendingToken.column
5862
}
5963

64+
/**
65+
*
66+
* @returns {Number}
67+
*/
6068
getLine() {
6169
return this.offendingToken.line
6270
}
6371

72+
/**
73+
*
74+
* @returns {string}
75+
*/
6476
getOriginalQueryWithErrorMarked() {
6577
const query = this.offendingToken.source[1].strdata
6678
const offendingTokenText = query.substring(this.offendingToken.start, this.offendingToken.stop + 1)
@@ -133,12 +145,12 @@ export class Statement {
133145

134146
/**
135147
*
136-
* @property {String} query
137-
* @property {String} originalQuery
138-
* @property {Metadata} metadata
139-
* @property {String} type
140-
* @property {String} tree
141-
* @param {Object} ctx
148+
* @member {Statement} query
149+
* @member {string} originalQuery
150+
* @member {Metadata} metadata
151+
* @member {string} type - The type of query, example: 'SELECT'
152+
* @member {string} tree
153+
* @param {object} ctx
142154
* @param {ParseError} exception
143155
*/
144156
constructor(ctx, exception) {
@@ -159,8 +171,8 @@ export class Statement {
159171

160172
/**
161173
*
162-
* @param {String} string
163-
* @returns {String}
174+
* @param {string} string
175+
* @returns {string}
164176
*/
165177
function trim(string) {
166178
return string.replace(/^\s+|\s+$/gm, '');
@@ -187,7 +199,7 @@ function findSuitableError(statement, errors) {
187199

188200
/**
189201
*
190-
* @param {String} query
202+
* @param {string} query
191203
* @param {Boolean} raise_exception
192204
* @returns {Statement[]}
193205
*/

cratedb_sqlparse_js/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
],
3333
"main": "./dist/sqlparse.umd.cjs",
3434
"module": "./dist/sqlparse.js",
35+
"types": "./dist/index.d.ts",
3536
"exports": {
3637
".": {
3738
"import": "./dist/sqlparse.js",
@@ -42,11 +43,14 @@
4243
"antlr4": "^4.13.1-patch-1"
4344
},
4445
"devDependencies": {
46+
"jsdoc": "^4.0.3",
47+
"terser": "^5.31.1",
4548
"vite": "^5.3.2",
4649
"vitest": "^1.6.0"
4750
},
4851
"scripts": {
4952
"test": "vitest run",
50-
"build": "vite build"
53+
"build": "vite build && tsc",
54+
"docs": "jsdoc *"
5155
}
5256
}

cratedb_sqlparse_js/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"include": ["cratedb_sqlparse/**/*"],
3+
"compilerOptions": {
4+
"allowJs": true,
5+
"declaration": true,
6+
"emitDeclarationOnly": true,
7+
"outDir": "./dist",
8+
"declarationMap": true,
9+
}
10+
}

cratedb_sqlparse_js/vite.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import packageJson from './package.json';
99

1010
export default defineConfig({
1111
build: {
12+
minify: "terser",
1213
lib: {
1314
// Could also be a dictionary or array of multiple entry points
1415
entry: resolve(__dirname, 'cratedb_sqlparse/index.js'),
@@ -17,7 +18,8 @@ export default defineConfig({
1718
}, rollupOptions: {
1819
// make sure to externalize deps that shouldn't be bundled
1920
// into your library
20-
external: [], output: {},
21+
external: [],
22+
output: {},
2123
},
2224
},
2325
})

0 commit comments

Comments
 (0)