Skip to content

Commit 8fea354

Browse files
authored
- add eslint rules max-len-80, top-level-use-strict (sql-js#403)
1 parent 5c40349 commit 8fea354

File tree

3 files changed

+81
-28
lines changed

3 files changed

+81
-28
lines changed

.eslintrc.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use strict";
2+
13
module.exports = {
24
env: {
35
browser: true,
@@ -26,22 +28,40 @@ module.exports = {
2628
sourceType: "script"
2729
},
2830
rules: {
31+
// reason - sqlite exposes functions with underscore-naming-convention
2932
camelcase: "off",
33+
// reason - They make it easier to add new elements to arrays
34+
// and parameters to functions, and make commit diffs clearer
3035
"comma-dangle": "off",
36+
// reason - string-notation needed to prevent closure-minifier
37+
// from mangling property-name
3138
"dot-notation": "off",
39+
// reason - enforce 4-space indent
3240
indent: ["error", 4, { SwitchCase: 1 }],
41+
// reason - enforce 80-column-width limit
42+
"max-len": ["error", { code: 80 }],
43+
// reason - src/api.js uses bitwise-operators
3344
"no-bitwise": "off",
3445
"no-cond-assign": ["error", "except-parens"],
3546
"no-param-reassign": "off",
3647
"no-throw-literal": "off",
48+
// reason - parserOptions is set to es5 language-syntax
3749
"no-var": "off",
50+
// reason - parserOptions is set to es5 language-syntax
3851
"object-shorthand": "off",
52+
// reason - parserOptions is set to es5 language-syntax
3953
"prefer-arrow-callback": "off",
54+
// reason - parserOptions is set to es5 language-syntax
4055
"prefer-destructuring": "off",
56+
// reason - parserOptions is set to es5 language-syntax
4157
"prefer-spread": "off",
58+
// reason - parserOptions is set to es5 language-syntax
4259
"prefer-template": "off",
60+
// reason - sql.js frequently use sql-query-strings containing
61+
// single-quotes
4362
quotes: ["error", "double"],
44-
strict: ["error", "function"],
63+
// reason - allow top-level "use-strict" in commonjs-modules
64+
strict: ["error", "safe"],
4565
"vars-on-top": "off"
4666
}
4767
};

src/api.js

+57-21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
stackSave
1717
*/
1818

19+
"use strict";
20+
1921
/**
2022
* @typedef {{Database:Database}} SqlJs
2123
* @property {Database} Database the database constructor
@@ -48,8 +50,6 @@
4850
*/
4951
// Wait for preRun to run, and then finish our initialization
5052
Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
51-
"use strict";
52-
5353
// Declare toplevel variables
5454
// register, used for temporary stack values
5555
var apiTemp = stackAlloc(4);
@@ -166,10 +166,18 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
166166
]
167167
);
168168
var sqlite3_value_type = cwrap("sqlite3_value_type", "number", ["number"]);
169-
var sqlite3_value_bytes = cwrap("sqlite3_value_bytes", "number", ["number"]);
169+
var sqlite3_value_bytes = cwrap(
170+
"sqlite3_value_bytes",
171+
"number",
172+
["number"]
173+
);
170174
var sqlite3_value_text = cwrap("sqlite3_value_text", "string", ["number"]);
171175
var sqlite3_value_blob = cwrap("sqlite3_value_blob", "number", ["number"]);
172-
var sqlite3_value_double = cwrap("sqlite3_value_double", "number", ["number"]);
176+
var sqlite3_value_double = cwrap(
177+
"sqlite3_value_double",
178+
"number",
179+
["number"]
180+
);
173181
var sqlite3_result_double = cwrap(
174182
"sqlite3_result_double",
175183
"",
@@ -190,7 +198,11 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
190198
"",
191199
["number", "number", "number", "number"]
192200
);
193-
var sqlite3_result_int = cwrap("sqlite3_result_int", "", ["number", "number"]);
201+
var sqlite3_result_int = cwrap(
202+
"sqlite3_result_int",
203+
"",
204+
["number", "number"]
205+
);
194206
var sqlite3_result_error = cwrap(
195207
"sqlite3_result_error",
196208
"",
@@ -236,13 +248,16 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
236248
}
237249

238250
/** @typedef {string|number|null|Uint8Array} Database.SqlValue */
239-
/** @typedef {Database.SqlValue[]|Object<string, Database.SqlValue>|null} Statement.BindParams
251+
/** @typedef {
252+
Database.SqlValue[]|Object<string, Database.SqlValue>|null
253+
} Statement.BindParams
240254
*/
241255

242256
/** Bind values to the parameters, after having reseted the statement.
243257
* If values is null, do nothing and return true.
244258
*
245-
* SQL statements can have parameters, named *'?', '?NNN', ':VVV', '@VVV', '$VVV'*,
259+
* SQL statements can have parameters,
260+
* named *'?', '?NNN', ':VVV', '@VVV', '$VVV'*,
246261
* where NNN is a number and VVV a string.
247262
* This function binds these parameters to the given values.
248263
*
@@ -282,7 +297,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
282297
}
283298
this["reset"]();
284299
if (Array.isArray(values)) return this.bindFromArray(values);
285-
if (values != null && typeof values === "object") return this.bindFromObject(values);
300+
if (values != null && typeof values === "object") {
301+
return this.bindFromObject(values);
302+
}
286303
return true;
287304
};
288305

@@ -355,7 +372,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
355372
to the statement before it is executed
356373
@return {Database.SqlValue[]} One row of result
357374
358-
@example <caption>Print all the rows of the table test to the console</caption>
375+
@example
376+
<caption>Print all the rows of the table test to the console</caption>
359377
var stmt = db.prepare("SELECT * FROM test");
360378
while (stmt.step()) console.log(stmt.get());
361379
*/
@@ -392,7 +410,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
392410
/** Get the list of column names of a row of result of a statement.
393411
@return {string[]} The names of the columns
394412
@example
395-
var stmt = db.prepare("SELECT 5 AS nbr, x'616200' AS data, NULL AS null_value;");
413+
var stmt = db.prepare(
414+
"SELECT 5 AS nbr, x'616200' AS data, NULL AS null_value;"
415+
);
396416
stmt.step(); // Execute the statement
397417
console.log(stmt.getColumnNames());
398418
// Will print ['nbr','data','null_value']
@@ -648,7 +668,10 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
648668
649669
@example
650670
// Insert values in a table
651-
db.run("INSERT INTO test VALUES (:age, :name)", { ':age' : 18, ':name' : 'John' });
671+
db.run(
672+
"INSERT INTO test VALUES (:age, :name)",
673+
{ ':age' : 18, ':name' : 'John' }
674+
);
652675
653676
@return {Database} The database object (useful for method chaining)
654677
*/
@@ -671,7 +694,10 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
671694
};
672695

673696
/**
674-
* @typedef {{columns:string[], values:Database.SqlValue[][]}} Database.QueryExecResult
697+
* @typedef {{
698+
columns:string[],
699+
values:Database.SqlValue[][]
700+
}} Database.QueryExecResult
675701
* @property {string[]} columns the name of the columns of the result
676702
* (as returned by {@link Statement.getColumnNames})
677703
* @property {Database.SqlValue[][]} values one array per row, containing
@@ -798,7 +824,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
798824
@param {Statement.BindParams} [params=[]] Parameters to bind to the query
799825
@param {function(Object<string, Database.SqlValue>):void} callback
800826
Function to call on each row of result
801-
@param {function():void} done A function that will be called when all rows have been retrieved
827+
@param {function():void} done A function that will be called when
828+
all rows have been retrieved
802829
803830
@return {Database} The database object. Useful for method chaining
804831
@@ -874,8 +901,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
874901
* The memory associated to the database and all associated statements
875902
* will be freed.
876903
*
877-
* **Warning**: A statement belonging to a database that has been closed cannot
878-
* be used anymore.
904+
* **Warning**: A statement belonging to a database that has been closed
905+
* cannot be used anymore.
879906
*
880907
* Databases **must** be closed when you're finished with them, or the
881908
* memory consumption will grow forever
@@ -908,8 +935,8 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
908935
throw new Error(errmsg);
909936
};
910937

911-
/** Returns the number of changed rows (modified, inserted or deleted) by the
912-
latest completed INSERT, UPDATE or DELETE statement on the
938+
/** Returns the number of changed rows (modified, inserted or deleted)
939+
by the latest completed INSERT, UPDATE or DELETE statement on the
913940
database. Executing any other type of SQL statement does not modify
914941
the value returned by this function.
915942
@@ -924,27 +951,36 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
924951
db.create_function("addOne", function (x) {return x+1;})
925952
db.exec("SELECT addOne(1)") // = 2
926953
927-
@param {string} name the name of the function as referenced in SQL statements.
954+
@param {string} name the name of the function as referenced in
955+
SQL statements.
928956
@param {function} func the actual function to be executed.
929957
@return {Database} The database object. Useful for method chaining
930958
*/
931-
Database.prototype["create_function"] = function create_function(name, func) {
959+
Database.prototype["create_function"] = function create_function(
960+
name,
961+
func
962+
) {
932963
var func_ptr;
933964
function wrapped_func(cx, argc, argv) {
934965
var result;
935966
function extract_blob(ptr) {
936967
var size = sqlite3_value_bytes(ptr);
937968
var blob_ptr = sqlite3_value_blob(ptr);
938969
var blob_arg = new Uint8Array(size);
939-
for (var j = 0; j < size; j += 1) blob_arg[j] = HEAP8[blob_ptr + j];
970+
for (var j = 0; j < size; j += 1) {
971+
blob_arg[j] = HEAP8[blob_ptr + j];
972+
}
940973
return blob_arg;
941974
}
942975
var args = [];
943976
for (var i = 0; i < argc; i += 1) {
944977
var value_ptr = getValue(argv + (4 * i), "i32");
945978
var value_type = sqlite3_value_type(value_ptr);
946979
var arg;
947-
if (value_type === SQLITE_INTEGER || value_type === SQLITE_FLOAT) {
980+
if (
981+
value_type === SQLITE_INTEGER
982+
|| value_type === SQLITE_FLOAT
983+
) {
948984
arg = sqlite3_value_double(value_ptr);
949985
} else if (value_type === SQLITE_TEXT) {
950986
arg = sqlite3_value_text(value_ptr);

src/worker.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* global initSqlJs */
22
/* eslint-env worker */
33
/* eslint no-restricted-globals: ["error"] */
4+
5+
"use strict";
6+
47
var db;
58

69
function onModuleReady(SQL) {
7-
"use strict";
8-
910
function createDb(data) {
1011
if (db != null) db.close();
1112
db = new SQL.Database(data);
@@ -75,8 +76,6 @@ function onModuleReady(SQL) {
7576
}
7677

7778
function onError(err) {
78-
"use strict";
79-
8079
return postMessage({
8180
id: this["data"]["id"],
8281
error: err["message"]
@@ -87,8 +86,6 @@ if (typeof importScripts === "function") {
8786
db = null;
8887
var sqlModuleReady = initSqlJs();
8988
self.onmessage = function onmessage(event) {
90-
"use strict";
91-
9289
return sqlModuleReady
9390
.then(onModuleReady.bind(event))
9491
.catch(onError.bind(event));

0 commit comments

Comments
 (0)