Skip to content

Commit b18d6c1

Browse files
committed
chore: migrate eslint config and fix lint errors
1 parent ec497a9 commit b18d6c1

File tree

8 files changed

+77
-78
lines changed

8 files changed

+77
-78
lines changed

.eslintrc.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

eslint.config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* eslint-disable no-undef */
2+
const js = require("@eslint/js");
3+
const tseslint = require("@typescript-eslint/eslint-plugin");
4+
const tsParser = require("@typescript-eslint/parser");
5+
6+
/** @type {import("eslint").Linter.Config[]} */
7+
module.exports = [
8+
js.configs.recommended,
9+
{
10+
files: ["**/*.ts", "**/*.tsx"],
11+
languageOptions: {
12+
parser: tsParser,
13+
parserOptions: {
14+
ecmaVersion: 6,
15+
sourceType: "module",
16+
},
17+
},
18+
plugins: {
19+
"@typescript-eslint": tseslint,
20+
},
21+
rules: {
22+
"@typescript-eslint/naming-convention": [
23+
"warn",
24+
{
25+
selector: "typeLike",
26+
format: ["PascalCase"],
27+
},
28+
],
29+
curly: "warn",
30+
eqeqeq: "warn",
31+
"no-throw-literal": "warn",
32+
semi: "warn",
33+
"no-unused-expressions": "warn",
34+
"no-duplicate-imports": "warn",
35+
"new-parens": "warn",
36+
},
37+
},
38+
];

src/impl/parser.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
SyntaxKind
2020
} from '../main';
2121

22-
namespace ParseOptions {
22+
namespace ParseOptionsConfigs {
2323
export const DEFAULT = {
2424
allowTrailingComma: false
2525
};
@@ -61,7 +61,7 @@ export function getLocation(text: string, position: number): Location {
6161
try {
6262

6363
visit(text, {
64-
onObjectBegin: (offset: number, length: number) => {
64+
onObjectBegin: (offset: number) => {
6565
if (position <= offset) {
6666
throw earlyReturnException;
6767
}
@@ -79,21 +79,21 @@ export function getLocation(text: string, position: number): Location {
7979
throw earlyReturnException;
8080
}
8181
},
82-
onObjectEnd: (offset: number, length: number) => {
82+
onObjectEnd: (offset: number) => {
8383
if (position <= offset) {
8484
throw earlyReturnException;
8585
}
8686
previousNode = undefined;
8787
segments.pop();
8888
},
89-
onArrayBegin: (offset: number, length: number) => {
89+
onArrayBegin: (offset: number) => {
9090
if (position <= offset) {
9191
throw earlyReturnException;
9292
}
9393
previousNode = undefined;
9494
segments.push(0);
9595
},
96-
onArrayEnd: (offset: number, length: number) => {
96+
onArrayEnd: (offset: number) => {
9797
if (position <= offset) {
9898
throw earlyReturnException;
9999
}
@@ -110,7 +110,7 @@ export function getLocation(text: string, position: number): Location {
110110
throw earlyReturnException;
111111
}
112112
},
113-
onSeparator: (sep: string, offset: number, length: number) => {
113+
onSeparator: (sep: string, offset: number) => {
114114
if (position <= offset) {
115115
throw earlyReturnException;
116116
}
@@ -159,7 +159,7 @@ export function getLocation(text: string, position: number): Location {
159159
* Parses the given text and returns the object the JSON content represents. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
160160
* Therefore always check the errors list to find out if the input was valid.
161161
*/
162-
export function parse(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): any {
162+
export function parse(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptionsConfigs.DEFAULT): any {
163163
let currentProperty: string | null = null;
164164
let currentParent: any = [];
165165
const previousParents: any[] = [];
@@ -209,7 +209,7 @@ export function parse(text: string, errors: ParseError[] = [], options: ParseOpt
209209
/**
210210
* Parses the given text and returns a tree representation the JSON content. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
211211
*/
212-
export function parseTree(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): Node | undefined {
212+
export function parseTree(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptionsConfigs.DEFAULT): Node | undefined {
213213
let currentParent: NodeImpl = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root
214214

215215
function ensurePropertyComplete(endOffset: number) {
@@ -239,7 +239,7 @@ export function parseTree(text: string, errors: ParseError[] = [], options: Pars
239239
currentParent = currentParent.parent!;
240240
ensurePropertyComplete(offset + length);
241241
},
242-
onArrayBegin: (offset: number, length: number) => {
242+
onArrayBegin: (offset: number) => {
243243
currentParent = onValue({ type: 'array', offset, length: -1, parent: currentParent, children: [] });
244244
},
245245
onArrayEnd: (offset: number, length: number) => {
@@ -251,7 +251,7 @@ export function parseTree(text: string, errors: ParseError[] = [], options: Pars
251251
onValue({ type: getNodeType(value), offset, length, parent: currentParent, value });
252252
ensurePropertyComplete(offset + length);
253253
},
254-
onSeparator: (sep: string, offset: number, length: number) => {
254+
onSeparator: (sep: string, offset: number) => {
255255
if (currentParent.type === 'property') {
256256
if (sep === ':') {
257257
currentParent.colonOffset = offset;
@@ -335,7 +335,7 @@ export function getNodeValue(node: Node): any {
335335
switch (node.type) {
336336
case 'array':
337337
return node.children!.map(getNodeValue);
338-
case 'object':
338+
case 'object': {
339339
const obj = Object.create(null);
340340
for (let prop of node.children!) {
341341
const valueNode = prop.children![1];
@@ -344,6 +344,7 @@ export function getNodeValue(node: Node): any {
344344
}
345345
}
346346
return obj;
347+
}
347348
case 'null':
348349
case 'string':
349350
case 'number':
@@ -383,7 +384,7 @@ export function findNodeAtOffset(node: Node, offset: number, includeRightBound =
383384
/**
384385
* Parses the given text and invokes the visitor functions for each object, array and literal reached.
385386
*/
386-
export function visit(text: string, visitor: JSONVisitor, options: ParseOptions = ParseOptions.DEFAULT): any {
387+
export function visit(text: string, visitor: JSONVisitor, options: ParseOptions = ParseOptionsConfigs.DEFAULT): any {
387388

388389
const _scanner = createScanner(text, false);
389390
// Important: Only pass copies of this to visitor functions to prevent accidental modification, and
@@ -512,16 +513,17 @@ export function visit(text: string, visitor: JSONVisitor, options: ParseOptions
512513

513514
function parseLiteral(): boolean {
514515
switch (_scanner.getToken()) {
515-
case SyntaxKind.NumericLiteral:
516-
const tokenValue = _scanner.getTokenValue();
517-
let value = Number(tokenValue);
516+
case SyntaxKind.NumericLiteral: {
517+
const tokenValue = _scanner.getTokenValue();
518+
let value = Number(tokenValue);
518519

519-
if (isNaN(value)) {
520-
handleError(ParseErrorCode.InvalidNumberFormat);
521-
value = 0;
522-
}
520+
if (isNaN(value)) {
521+
handleError(ParseErrorCode.InvalidNumberFormat);
522+
value = 0;
523+
}
523524

524-
onLiteralValue(value);
525+
onLiteralValue(value);
526+
}
525527
break;
526528
case SyntaxKind.NullKeyword:
527529
onLiteralValue(null);

src/impl/scanner.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
125125
const ch2 = text.charCodeAt(pos++);
126126
switch (ch2) {
127127
case CharacterCodes.doubleQuote:
128-
result += '\"';
128+
result += '"';
129129
break;
130130
case CharacterCodes.backslash:
131131
result += '\\';
@@ -148,12 +148,13 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
148148
case CharacterCodes.t:
149149
result += '\t';
150150
break;
151-
case CharacterCodes.u:
152-
const ch3 = scanHexDigits(4, true);
153-
if (ch3 >= 0) {
154-
result += String.fromCharCode(ch3);
155-
} else {
156-
scanError = ScanError.InvalidUnicode;
151+
case CharacterCodes.u: {
152+
const ch3 = scanHexDigits(4, true);
153+
if (ch3 >= 0) {
154+
result += String.fromCharCode(ch3);
155+
} else {
156+
scanError = ScanError.InvalidUnicode;
157+
}
157158
}
158159
break;
159160
default:
@@ -245,7 +246,7 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
245246
return token = SyntaxKind.StringLiteral;
246247

247248
// comments
248-
case CharacterCodes.slash:
249+
case CharacterCodes.slash: {
249250
const start = pos - 1;
250251
// Single-line comment
251252
if (text.charCodeAt(pos + 1) === CharacterCodes.slash) {
@@ -301,6 +302,7 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
301302
value += String.fromCharCode(code);
302303
pos++;
303304
return token = SyntaxKind.Unknown;
305+
}
304306

305307
// numbers
306308
case CharacterCodes.minus:
@@ -312,6 +314,7 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
312314
// found a minus, followed by a number so
313315
// we fall through to proceed with scanning
314316
// numbers
317+
// eslint-disable-next-line no-fallthrough
315318
case CharacterCodes._0:
316319
case CharacterCodes._1:
317320
case CharacterCodes._2:

src/test/edit.test.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
'use strict';
66

77
import * as assert from 'assert';
8-
import { FormattingOptions, Edit, ModificationOptions, modify } from '../main';
8+
import { suite, test } from 'mocha';
9+
import { Edit, FormattingOptions, ModificationOptions, modify } from '../main';
910

1011
suite('JSON - edits', () => {
1112

@@ -30,21 +31,10 @@ suite('JSON - edits', () => {
3031
keepLines: false
3132
};
3233

33-
let formattingOptionsKeepLines: FormattingOptions = {
34-
insertSpaces: true,
35-
tabSize: 2,
36-
eol: '\n',
37-
keepLines: true
38-
};
39-
4034
let options: ModificationOptions = {
4135
formattingOptions
4236
};
4337

44-
let optionsKeepLines: ModificationOptions = {
45-
formattingOptions : formattingOptionsKeepLines
46-
};
47-
4838
test('set property', () => {
4939
let content = '{\n "x": "y"\n}';
5040
let edits = modify(content, ['x'], 'bar', options);

src/test/format.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'use strict';
66

77
import * as assert from 'assert';
8+
import { suite, test } from 'mocha';
89
import * as Formatter from '../impl/format';
910
import { Range } from '../main';
1011

@@ -21,14 +22,8 @@ suite('JSON - formatter', () => {
2122

2223
const edits = Formatter.format(content, range, { tabSize: 2, insertSpaces, insertFinalNewline, eol: '\n', keepLines });
2324

24-
let lastEditOffset = content.length;
25-
2625
for (let i = edits.length - 1; i >= 0; i--) {
2726
const edit = edits[i];
28-
// assert(edit.offset >= 0 && edit.length >= 0 && edit.offset + edit.length <= content.length);
29-
// assert(typeof edit.content === 'string');
30-
// assert(lastEditOffset >= edit.offset + edit.length); // make sure all edits are ordered
31-
lastEditOffset = edit.offset;
3227
content = content.substring(0, edit.offset) + edit.content + content.substring(edit.offset + edit.length);
3328
}
3429

src/test/json.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'use strict';
66

77
import * as assert from 'assert';
8+
import { suite, test } from 'mocha';
89
import {
910
SyntaxKind, createScanner, parse, getLocation, Node, ParseError, parseTree, ParseErrorCode,
1011
ParseOptions, Segment, findNodeAtLocation, getNodeValue, getNodePath, ScanError, visit, JSONVisitor, JSONPath

src/test/string-intern.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { cachedBreakLinesWithSpaces, cachedSpaces, supportedEols } from '../impl/string-intern';
21
import * as assert from 'assert';
2+
import { suite, test } from 'mocha';
3+
import { cachedBreakLinesWithSpaces, cachedSpaces, supportedEols } from '../impl/string-intern';
34

45
suite('string intern', () => {
56
test('should correctly define spaces intern', () => {

0 commit comments

Comments
 (0)