Skip to content

Commit b5736d1

Browse files
committed
Switch to eslint - fix errors
1 parent ff1bec3 commit b5736d1

19 files changed

+624
-165
lines changed

.eslintignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
out
3+
coverage
4+
jest.config.js
5+
src/parser/parser.js
6+
.eslintrc.js

.eslintrc.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
plugins: [
5+
'@typescript-eslint',
6+
],
7+
extends: [
8+
'eslint:recommended',
9+
'plugin:@typescript-eslint/eslint-recommended',
10+
'plugin:@typescript-eslint/recommended',
11+
],
12+
rules: {
13+
"@typescript-eslint/no-use-before-define": ["off"]
14+
}
15+
};

.travis.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ install:
1212
1313
jobs:
1414
include:
15-
- stage: compile
15+
- stage: build
1616
script:
1717
- |
18-
echo ">>>> Compile Extension"
19-
yarn && yarn compile
18+
echo ">>>> Build Extension"
19+
yarn && yarn build
20+
- stage: lint
21+
script:
22+
- |
23+
echo ">>>> Lint Extension"
24+
yarn lint
2025
- stage: test
2126
name: "Test"
2227
script:

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
]
6363
},
6464
"scripts": {
65-
"vscode:prepublish": "yarn build && yarn test-legacy && yarn test",
65+
"vscode:prepublish": "yarn lint && yarn build && yarn test-legacy && yarn test",
66+
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
6667
"build": "tsc -p ./",
6768
"watch": "tsc -watch -p ./",
6869
"pretest": "yarn build",
@@ -75,9 +76,11 @@
7576
"@types/node": "^13.5.0",
7677
"@types/oniguruma": "^7.0.1",
7778
"@types/vscode": "^1.2.0",
79+
"@typescript-eslint/eslint-plugin": "^3.4.0",
80+
"@typescript-eslint/parser": "^3.4.0",
81+
"eslint": "^7.3.1",
7882
"glob": "^7.1.4",
7983
"mocha": "^7.0.1",
80-
"tslint": "^6.0.0",
8184
"typescript": "^3.3.1",
8285
"vscode-test": "^1.0.2",
8386
"vscode-textmate": "^5.1.1"

src/Documentation.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { keywordDoc } from './documentation/keywordDocumentation';
66

77
const completeDocs = { ...typeDoc, ...methodDoc, ...keywordDoc };
88

9-
const flatten = (documentation: Documentation, staticValue: boolean = false): FlatDoc => {
9+
const flatten = (documentation: Documentation, staticValue = false): FlatDoc => {
1010
let flatDoc: FlatDoc = {};
1111
for (const key of Object.keys(documentation)) {
1212
// With duplicate keys append content
@@ -26,7 +26,7 @@ const flatten = (documentation: Documentation, staticValue: boolean = false): Fl
2626
return flatDoc;
2727
};
2828

29-
const createDocString = (documentation: DocumentationValue, staticValue: boolean = false): string | MarkdownString => {
29+
const createDocString = (documentation: DocumentationValue, staticValue = false): string | MarkdownString => {
3030
if (!documentation.header) {
3131
return documentation.doc;
3232
}
@@ -89,7 +89,7 @@ export const getDocForToken = (token: string, markedWord: string): string | Mark
8989
return current ? current.doc : '';
9090
};
9191

92-
export const getPotentialDocForPartial = (partial: string) => {
92+
export const getPotentialDocForPartial = (partial: string): (string | MarkdownString)[][] => {
9393
const potentialDocs = Object.keys(flatDocs)
9494
.filter(value => value.startsWith(partial))
9595
.map(value => [value, flatDocs[value]]);
@@ -99,7 +99,7 @@ export const getPotentialDocForPartial = (partial: string) => {
9999

100100
// Suggestion: getPotentialDocForPartial(partial).inScope(scope)
101101

102-
export const getPotentialDocForPartialScoped = (partial: string, scope: string | Scope) => {
102+
export const getPotentialDocForPartialScoped = (partial: string, scope: string | Scope): (string | MarkdownString)[][] => {
103103
if (isScope(scope)) {
104104
let potentialDocs = Object.keys(keywordDoc)
105105
.filter(value => (keywordDoc[value].scopes || [scope]).includes(scope))

src/extension.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { ExtensionContext, languages } from 'vscode';
22
import { FirestoreHoverProvider, FirestoreCompletionProvider, FirestoreFormattingProvider } from './providers';
33

4-
export function activate(context: ExtensionContext) {
4+
export function activate(context: ExtensionContext): void {
55
context.subscriptions.push(languages.registerHoverProvider('firestorerules', new FirestoreHoverProvider()));
66
context.subscriptions.push(languages.registerCompletionItemProvider('firestorerules', new FirestoreCompletionProvider()));
77
context.subscriptions.push(languages.registerDocumentFormattingEditProvider('firestorerules', new FirestoreFormattingProvider()));
88
}
9-
10-
export function deactivate() { }

src/providers/FirestoreCompletionProvider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CompletionItem, CompletionItemKind, CompletionItemProvider, CompletionList, Position, TextDocument } from 'vscode';
2-
import { getPotentialDocForPartial, getPotentialDocForPartialScoped } from '../Documentation';
2+
import { getPotentialDocForPartialScoped } from '../Documentation';
33
import { tokenize } from '../utils/textmate/textmate';
44

55
export class FirestoreCompletionProvider implements CompletionItemProvider {

src/providers/FirestoreFormattingProvider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const concatedAndOr = /^\s*(&&|\|\|)/g;
88
export class FirestoreFormattingProvider implements DocumentFormattingEditProvider {
99

1010
async provideDocumentFormattingEdits(document: TextDocument, options: FormattingOptions): Promise<TextEdit[]> {
11-
let results: TextEdit[] = [];
11+
const results: TextEdit[] = [];
1212

1313
try {
1414
const tokenizedDoc = await tokenize(document);

src/providers/FirestoreHoverProvider.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { TextDocument, Position, HoverProvider, Hover, ProviderResult, CancellationToken } from 'vscode';
1+
import { TextDocument, Position, HoverProvider, Hover, ProviderResult } from 'vscode';
22
import { getDocForToken } from '../Documentation';
33
import { getTokenUntil } from '../utils';
44

55
export class FirestoreHoverProvider implements HoverProvider {
6-
provideHover(document: TextDocument, position: Position, token: CancellationToken): ProviderResult<Hover> {
6+
provideHover(document: TextDocument, position: Position): ProviderResult<Hover> {
77
const markedWord = document.getText(document.getWordRangeAtPosition(position));
88
return new Hover(getDocForToken(getTokenUntil(document, position), markedWord) || '');
99
}

src/test/suite/Utils/Utils.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as assert from 'assert';
2-
import { before, describe } from 'mocha';
2+
import { describe } from 'mocha';
33

44
import * as vscode from 'vscode';
55
import { getWholeToken, getTokenUntil } from '../../../utils';

src/test/suite/extension.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as assert from 'assert';
21
import { before } from 'mocha';
32

43
// You can import and use all API from the 'vscode' module

src/utils/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ export const getWholeToken = (document: TextDocument, position: Position): strin
55
let tmpPosition = position;
66

77
//find start position
8-
while (tmpPosition.character > 0 && document.getText(new Range(tmpPosition, tmpPosition.translate(0, -1))).match(/[a-zA-Z0-9\.]/)) {
8+
while (tmpPosition.character > 0 && document.getText(new Range(tmpPosition, tmpPosition.translate(0, -1))).match(/[a-zA-Z0-9.]/)) {
99
tmpPosition = tmpPosition.translate(0, -1);
1010
}
1111

1212
// till end position
13-
while (document.getText(new Range(tmpPosition, tmpPosition.translate(0, 1))).match(/[a-zA-Z0-9\.]/)) {
13+
while (document.getText(new Range(tmpPosition, tmpPosition.translate(0, 1))).match(/[a-zA-Z0-9.]/)) {
1414
token += document.getText(new Range(tmpPosition, tmpPosition.translate(0, 1)));
1515
tmpPosition = tmpPosition.translate(0, 1);
1616
}
@@ -28,7 +28,7 @@ export const getTokenUntil = (document: TextDocument, position: Position): strin
2828
let tmpPosition = wordRange.start;
2929

3030
//find start position
31-
while (tmpPosition.character > 0 && document.getText(new Range(tmpPosition, tmpPosition.translate(0, -1))).match(/[a-zA-Z0-9\.]/)) {
31+
while (tmpPosition.character > 0 && document.getText(new Range(tmpPosition, tmpPosition.translate(0, -1))).match(/[a-zA-Z0-9.]/)) {
3232
token = document.getText(new Range(tmpPosition, tmpPosition.translate(0, -1))) + token;
3333
tmpPosition = tmpPosition.translate(0, -1);
3434
}

src/utils/textmate/scope-info.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as vscode from 'vscode'
22
import { Range, TextDocument } from 'vscode';
3-
import { constructor } from 'stream';
43

54
/**
65
* A grammar
@@ -36,7 +35,7 @@ export class Token {
3635
range: Range;
3736
document: vscode.TextDocument;
3837

39-
static create(token: any, line: number, document: vscode.TextDocument) {
38+
static create(token: IToken, line: number, document: vscode.TextDocument): Token {
4039
return new Token(token.scopes,
4140
new Range(line, token.startIndex, line, token.endIndex), document);
4241
}
@@ -48,7 +47,7 @@ export class Token {
4847
}
4948

5049
inScope(str: string): boolean {
51-
for (let scope of this.scopes) {
50+
for (const scope of this.scopes) {
5251
if (scope.indexOf(str) !== -1) {
5352
return true;
5453
}

src/utils/textmate/text-util.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function positionAt(text: string, offset: number): vscode.Position {
1818
if (offset > text.length) { offset = text.length; }
1919
let line = 0;
2020
let lastIndex = 0;
21+
// eslint-disable-next-line no-constant-condition
2122
while (true) {
2223
const match = lineEndingRE.exec(text.substring(lastIndex));
2324
if (!match) {
@@ -94,20 +95,20 @@ function positionRangeDeltaTranslate(pos: vscode.Position, delta: RangeDelta): v
9495
}
9596
}
9697

97-
export function rangeTranslate(range: vscode.Range, delta: RangeDelta) {
98+
export function rangeTranslate(range: vscode.Range, delta: RangeDelta): vscode.Range {
9899
return new vscode.Range(
99100
positionRangeDeltaTranslateStart(range.start, delta),
100101
positionRangeDeltaTranslateEnd(range.end, delta)
101102
);
102103
}
103104

104-
export function rangeContains(range: vscode.Range, pos: vscode.Position, exclStart = false, inclEnd = false) {
105+
export function rangeContains(range: vscode.Range, pos: vscode.Position, exclStart = false, inclEnd = false): boolean {
105106
return range.start.isBeforeOrEqual(pos)
106107
&& (!exclStart || !range.start.isEqual(pos))
107108
&& ((inclEnd && range.end.isEqual(pos)) || range.end.isAfter(pos));
108109
}
109110

110-
export function maxPosition(x: vscode.Position, y: vscode.Position) {
111+
export function maxPosition(x: vscode.Position, y: vscode.Position): vscode.Position {
111112
if (x.line < y.line) {
112113
return x;
113114
} if (x.line < x.line) {

src/utils/textmate/textmate.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from 'path';
22
import * as fs from 'fs';
33
import { env, TextDocument, Range } from 'vscode';
4-
import { Token } from './scope-info';
4+
import { Token, IToken } from './scope-info';
55
import * as textmate from 'vscode-textmate';
66

77
type TextmateType = typeof textmate;
@@ -12,23 +12,31 @@ type TextmateType = typeof textmate;
1212
function getCoreNodeModule(moduleName: string) {
1313
try {
1414
return require(`${env.appRoot}/node_modules.asar/${moduleName}`);
15-
} catch (err) { }
15+
} catch (err) {
16+
// do nothing
17+
}
1618

1719
try {
1820
return require(`${env.appRoot}/node_modules/${moduleName}`);
19-
} catch (err) { }
21+
} catch (err) {
22+
// do nothing
23+
}
2024

2125
return null;
2226
}
2327

2428
function getOnigWasmBin() {
2529
try {
2630
return fs.readFileSync(`${env.appRoot}/node_modules.asar/vscode-oniguruma/release/onig.wasm`).buffer;
27-
} catch (err) { }
31+
} catch (err) {
32+
// do nothing
33+
}
2834

2935
try {
3036
return fs.readFileSync(`${env.appRoot}/node_modules/vscode-oniguruma/release/onig.wasm`).buffer;
31-
} catch (err) { }
37+
} catch (err) {
38+
// do nothing
39+
}
3240

3341
console.error("Could not load the onig.wasm");
3442

@@ -50,13 +58,14 @@ async function getRegistry(tm: TextmateType) {
5058
});
5159
}
5260

61+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5362
// @ts-ignore
5463
return new tm.Registry();
5564
}
5665

5766
const grammarPath = path.resolve(__dirname + '/../../../syntaxes/firestorerules.tmLanguage.json');
5867

59-
let grammar: any;
68+
let grammar: textmate.IGrammar;
6069
async function getGrammar() {
6170
if (grammar) {
6271
return grammar;
@@ -71,14 +80,14 @@ async function getGrammar() {
7180
}
7281

7382
export async function tokenize(document: TextDocument): Promise<Token[][]> {
74-
let grammar = await getGrammar();
83+
const grammar = await getGrammar();
7584

76-
var ruleStack: any;
77-
var tokens: Token[][] = [];
85+
let ruleStack: textmate.StackElement | null = null;
86+
const tokens: Token[][] = [];
7887
for (let i = 0; i < document.lineCount; i++) {
79-
let line = document.getText(new Range(i, 0, i + 1, 0));
80-
var r = grammar.tokenizeLine(line, ruleStack!);
81-
tokens.push(r.tokens.map((v: any) => Token.create(v, i, document)));
88+
const line = document.getText(new Range(i, 0, i + 1, 0));
89+
const r = grammar.tokenizeLine(line, ruleStack);
90+
tokens.push(r.tokens.map((v: IToken) => Token.create(v, i, document)));
8291
ruleStack = r.ruleStack;
8392
}
8493
return tokens;

tsconfig.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
},
1717
"exclude": [
1818
"node_modules",
19-
".vscode-test"
19+
".vscode-test",
20+
"out",
21+
"*.config.js",
22+
"test"
2023
]
2124
}

tslint.json

-15
This file was deleted.

vsc-extension-quickstart.md

-42
This file was deleted.

0 commit comments

Comments
 (0)