Skip to content

Commit d9c4086

Browse files
committed
Use const and let instead of var.
Closes #227
1 parent e74edeb commit d9c4086

29 files changed

+394
-370
lines changed

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
no-undef: 2,
108108
no-undefined: 0,
109109
no-unused-vars: [ 2, { args: "none" }],
110-
no-use-before-define: [2, "nofunc"],
110+
no-use-before-define: [2, { functions: false }],
111111

112112
// Node.js
113113
callback-return: 0,
@@ -143,6 +143,7 @@
143143
new-parens: 2,
144144
newline-after-var: 0,
145145
no-array-constructor: 2,
146+
no-const-assign: 2,
146147
no-continue: 2,
147148
no-inline-comments: 0,
148149
no-lonely-if: 2,
@@ -155,6 +156,7 @@
155156
no-trailing-spaces: 2,
156157
no-underscore-dangle: 0,
157158
no-unneeded-ternary: 2,
159+
no-var: 2,
158160
object-curly-spacing: [2, "always"],
159161
object-curly-newline: 0,
160162
object-property-newline: 0,
@@ -163,6 +165,7 @@
163165
operator-linebreak: [2, "after", { overrides: { ":": "ignore" } }],
164166
padded-blocks: [2, "never"],
165167
prefer-arrow-callback: 2,
168+
prefer-const: 2,
166169
quote-props: [2, "consistent-as-needed"],
167170
quotes: [2, "single", "avoid-escape"],
168171
semi-spacing: 2,

perf/N3Lexer-perf.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#!/usr/bin/env node
2-
var N3 = require('..');
3-
var fs = require('fs'),
2+
const N3 = require('..');
3+
const fs = require('fs'),
44
assert = require('assert');
55

66
if (process.argv.length !== 3) {
77
console.error('Usage: N3Lexer-perf.js filename');
88
process.exit(1);
99
}
1010

11-
var filename = process.argv[2];
11+
const filename = process.argv[2];
1212

13-
var TEST = '- Lexing file ' + filename;
13+
const TEST = '- Lexing file ' + filename;
1414
console.time(TEST);
1515

16-
var count = 0;
16+
let count = 0;
1717
new N3.Lexer().tokenize(fs.createReadStream(filename), (error, token) => {
1818
assert(!error, error);
1919
count++;

perf/N3Parser-perf.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
2-
var N3 = require('..');
3-
var fs = require('fs'),
2+
const N3 = require('..');
3+
const fs = require('fs'),
44
path = require('path'),
55
assert = require('assert');
66

@@ -9,13 +9,13 @@ if (process.argv.length !== 3) {
99
process.exit(1);
1010
}
1111

12-
var filename = path.resolve(process.cwd(), process.argv[2]),
12+
const filename = path.resolve(process.cwd(), process.argv[2]),
1313
base = 'file://' + filename;
1414

15-
var TEST = '- Parsing file ' + filename;
15+
const TEST = '- Parsing file ' + filename;
1616
console.time(TEST);
1717

18-
var count = 0;
18+
let count = 0;
1919
new N3.Parser({ baseIRI: base }).parse(fs.createReadStream(filename), (error, quad) => {
2020
assert(!error, error);
2121
if (quad)

perf/N3Store-perf.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
#!/usr/bin/env node
2-
var N3 = require('..');
3-
var assert = require('assert');
2+
const N3 = require('..');
3+
const assert = require('assert');
44

55
console.log('N3Store performance test');
66

7-
var prefix = 'http://example.org/#';
8-
var TEST, dim, dimSquared, dimCubed, dimQuads, store;
7+
const prefix = 'http://example.org/#';
98

109
/* Test triples */
11-
dim = Number.parseInt(process.argv[2], 10) || 256;
12-
dimSquared = dim * dim;
13-
dimCubed = dimSquared * dim;
10+
let dim = Number.parseInt(process.argv[2], 10) || 256;
11+
let dimSquared = dim * dim;
12+
let dimCubed = dimSquared * dim;
1413

15-
store = new N3.Store();
16-
TEST = '- Adding ' + dimCubed + ' triples to the default graph';
14+
let store = new N3.Store();
15+
let TEST = '- Adding ' + dimCubed + ' triples to the default graph';
1716
console.time(TEST);
18-
var i, j, k, l;
17+
let i, j, k, l;
1918
for (i = 0; i < dim; i++)
2019
for (j = 0; j < dim; j++)
2120
for (k = 0; k < dim; k++)
@@ -58,10 +57,10 @@ console.timeEnd(TEST);
5857
console.log();
5958

6059
/* Test quads */
61-
dim /= 4,
60+
dim /= 4;
6261
dimSquared = dim * dim;
6362
dimCubed = dimSquared * dim;
64-
dimQuads = dimCubed * dim;
63+
const dimQuads = dimCubed * dim;
6564

6665
store = new N3.Store();
6766
TEST = '- Adding ' + dimQuads + ' quads';

spec/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var StreamParser = require('..').StreamParser;
1+
const { StreamParser } = require('..');
22

33
// Implements the IParser interface from rdf-test-suite
44
// https://github.com/rubensworks/rdf-test-suite.js/blob/master/lib/testcase/rdfsyntax/IParser.ts

src/IRIs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
1+
const RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
22
XSD = 'http://www.w3.org/2001/XMLSchema#',
33
SWAP = 'http://www.w3.org/2000/10/swap/';
44

src/N3DataFactory.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import namespaces from './IRIs';
55
import { isDefaultGraph } from './N3Util';
66
const { rdf, xsd } = namespaces;
77

8+
// eslint-disable-next-line prefer-const
89
let DEFAULTGRAPH;
910
let _blankNodeCounter = 0;
1011

@@ -78,7 +79,8 @@ export class Literal extends Term {
7879
// ### The language of this literal
7980
get language() {
8081
// Find the last quotation mark (e.g., '"abc"@en-us')
81-
var id = this.id, atPos = id.lastIndexOf('"') + 1;
82+
const id = this.id;
83+
let atPos = id.lastIndexOf('"') + 1;
8284
// If "@" it follows, return the remaining substring; empty otherwise
8385
return atPos < id.length && id[atPos++] === '@' ? id.substr(atPos).toLowerCase() : '';
8486
}
@@ -91,11 +93,12 @@ export class Literal extends Term {
9193
// ### The datatype string of this literal
9294
get datatypeString() {
9395
// Find the last quotation mark (e.g., '"abc"^^http://ex.org/types#t')
94-
var id = this.id, dtPos = id.lastIndexOf('"') + 1, ch;
96+
const id = this.id, dtPos = id.lastIndexOf('"') + 1;
97+
const char = dtPos < id.length ? id[dtPos] : '';
9598
// If "^" it follows, return the remaining substring
96-
return dtPos < id.length && (ch = id[dtPos]) === '^' ? id.substr(dtPos + 2) :
99+
return char === '^' ? id.substr(dtPos + 2) :
97100
// If "@" follows, return rdf:langString; xsd:string otherwise
98-
(ch !== '@' ? xsd.string : rdf.langString);
101+
(char !== '@' ? xsd.string : rdf.langString);
99102
}
100103

101104
// ### Returns whether this object represents the same term as the other
@@ -202,7 +205,7 @@ export function termFromId(id, factory) {
202205
if (id[id.length - 1] === '"')
203206
return factory.literal(id.substr(1, id.length - 2));
204207
// Literal with datatype or language
205-
var endPos = id.lastIndexOf('"', id.length - 1);
208+
const endPos = id.lastIndexOf('"', id.length - 1);
206209
return factory.literal(id.substr(1, endPos - 1),
207210
id[endPos + 1] === '@' ? id.substr(endPos + 2)
208211
: factory.namedNode(id.substr(endPos + 3)));

src/N3Lexer.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import queueMicrotask from 'queue-microtask';
55
const { xsd } = namespaces;
66

77
// Regular expression and replacement string to escape N3 strings
8-
var escapeSequence = /\\u([a-fA-F0-9]{4})|\\U([a-fA-F0-9]{8})|\\([^])/g;
9-
var escapeReplacements = {
8+
const escapeSequence = /\\u([a-fA-F0-9]{4})|\\U([a-fA-F0-9]{8})|\\([^])/g;
9+
const escapeReplacements = {
1010
'\\': '\\', "'": "'", '"': '"',
1111
'n': '\n', 'r': '\r', 't': '\t', 'f': '\f', 'b': '\b',
1212
'_': '_', '~': '~', '.': '.', '-': '-', '!': '!', '$': '$', '&': '&',
1313
'(': '(', ')': ')', '*': '*', '+': '+', ',': ',', ';': ';', '=': '=',
1414
'/': '/', '?': '?', '#': '#', '@': '@', '%': '%',
1515
};
16-
var illegalIriChars = /[\x00-\x20<>\\"\{\}\|\^\`]/;
16+
const illegalIriChars = /[\x00-\x20<>\\"\{\}\|\^\`]/;
1717

18-
var lineModeRegExps = {
18+
const lineModeRegExps = {
1919
_iri: true,
2020
_unescapedIri: true,
2121
_simpleQuotedString: true,
@@ -26,7 +26,7 @@ var lineModeRegExps = {
2626
_whitespace: true,
2727
_endOfFile: true,
2828
};
29-
var invalidRegExp = /$0^/;
29+
const invalidRegExp = /$0^/;
3030

3131
// ## Constructor
3232
export default class N3Lexer {
@@ -57,7 +57,7 @@ export default class N3Lexer {
5757
if (this._lineMode = !!options.lineMode) {
5858
this._n3Mode = false;
5959
// Don't tokenize special literals
60-
for (var key in this) {
60+
for (const key in this) {
6161
if (!(key in lineModeRegExps) && this[key] instanceof RegExp)
6262
this[key] = invalidRegExp;
6363
}
@@ -77,10 +77,11 @@ export default class N3Lexer {
7777
// ### `_tokenizeToEnd` tokenizes as for as possible, emitting tokens through the callback
7878
_tokenizeToEnd(callback, inputFinished) {
7979
// Continue parsing as far as possible; the loop will return eventually
80-
var input = this._input, outputComments = this._comments;
80+
let input = this._input;
81+
const outputComments = this._comments;
8182
while (true) {
8283
// Count and skip whitespace lines
83-
var whiteSpaceMatch, comment;
84+
let whiteSpaceMatch, comment;
8485
while (whiteSpaceMatch = this._newline.exec(input)) {
8586
// Try to find a comment
8687
if (outputComments && (comment = this._comment.exec(whiteSpaceMatch[0])))
@@ -106,8 +107,9 @@ export default class N3Lexer {
106107
}
107108

108109
// Look for specific token types based on the first character
109-
var line = this._line, type = '', value = '', prefix = '',
110-
firstChar = input[0], match = null, matchLength = 0, inconclusive = false;
110+
const line = this._line, firstChar = input[0];
111+
let type = '', value = '', prefix = '',
112+
match = null, matchLength = 0, inconclusive = false;
111113
switch (firstChar) {
112114
case '^':
113115
// We need at least 3 tokens lookahead to distinguish ^^<IRI> and ^^pre:fixed
@@ -343,7 +345,7 @@ export default class N3Lexer {
343345
}
344346

345347
// Emit the parsed token
346-
var token = { line: line, type: type, value: value, prefix: prefix };
348+
const token = { line: line, type: type, value: value, prefix: prefix };
347349
callback(null, token);
348350
this.previousToken = token;
349351
this._previousMarker = type;
@@ -418,7 +420,7 @@ export default class N3Lexer {
418420
// ### `_syntaxError` creates a syntax error for the given issue
419421
_syntaxError(issue) {
420422
this._input = null;
421-
var err = new Error('Unexpected "' + issue + '" on line ' + this._line + '.');
423+
const err = new Error('Unexpected "' + issue + '" on line ' + this._line + '.');
422424
err.context = {
423425
token: undefined,
424426
line: this._line,
@@ -442,8 +444,9 @@ export default class N3Lexer {
442444
queueMicrotask(() => this._tokenizeToEnd(callback, true));
443445
// If no callback was passed, tokenize synchronously and return
444446
else {
445-
var tokens = [], error;
446-
this._tokenizeToEnd((e, t) => { e ? (error = e) : tokens.push(t); }, true);
447+
const tokens = [];
448+
let error;
449+
this._tokenizeToEnd((e, t) => e ? (error = e) : tokens.push(t), true);
447450
if (error) throw error;
448451
return tokens;
449452
}

0 commit comments

Comments
 (0)