Skip to content

Commit

Permalink
Apply eslint (#41)
Browse files Browse the repository at this point in the history
* Lint the project
  • Loading branch information
PhilippeChab authored Aug 14, 2022
1 parent 341ea4f commit df3b6f9
Show file tree
Hide file tree
Showing 36 changed files with 953 additions and 333 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
webpack.config.js
33 changes: 28 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,40 @@
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
"ecmaVersion": "latest",
"sourceType": "module",
"project": ["./client/tsconfig.json", "./server/tsconfig.json", "./server/tsconfig.eslint.json"]
},
"plugins": ["@typescript-eslint"],
"extends": ["standard-with-typescript", "plugin:prettier/recommended"],
"rules": {
"@typescript-eslint/quotes": ["error", "double"],
"@typescript-eslint/consistent-type-definitions": ["error", "type"],
"@typescript-eslint/naming-convention": "warn",
"@typescript-eslint/semi": "warn",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-floating-promises": "warn",
"@typescript-eslint/semi": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/space-before-function-paren": "off",
"@typescript-eslint/array-type": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/prefer-nullish-coalescing": "off",
"@typescript-eslint/consistent-type-assertions": "off",
"@typescript-eslint/no-extraneous-class": "off",
"curly": "warn",
"eqeqeq": "warn",
"no-unsafe-finally": "warn",
"no-throw-literal": "warn",
"semi": "off"
"space-before-function-paren": "off",
"semi": "off",
"quotes": "off",
"no-extra-boolean-cast": "off",
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
]
},
"ignorePatterns": ["out", "dist", "**/*.d.ts"]
"ignorePatterns": ["out", "**/*.d.ts"]
}
5 changes: 5 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 130,
"trailingComma": "all",
"singleQuote": false
}
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"editor.formatOnSave": true,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"prettier.printWidth": 130
}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ All notable changes to the "nwscript-ee-language-server" extension will be docum
## [1.5.0]

- The extension size has been lowered from 14.7 to 4.8 MB.

## [1.5.1]

- Eslint has been configured along with prettier and the project will be linted from now on.
4 changes: 2 additions & 2 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export function activate(context: ExtensionContext) {
client.start();
}

export function deactivate() {
return client?.stop();
export async function deactivate() {
return await client?.stop();
}
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "https://github.com/PhilippeChab/nwscript-ee-language-server"
},
"license": "MIT",
"version": "1.5.0",
"version": "1.5.1",
"author": {
"name": "Philippe Chabot"
},
Expand Down Expand Up @@ -148,14 +148,20 @@
},
"devDependencies": {
"@types/node": "14.x",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.21.0",
"eslint": "^8.14.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard-with-typescript": "^22.0.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-n": "^15.0.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.0.0",
"merge-options": "^3.0.4",
"prettier": "^2.6.2",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"ts-loader": "^9.3.1",
"typescript": "^4.6.4",
"typescript": "*",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
}
Expand Down
1 change: 1 addition & 0 deletions server/scripts/GenerateLibDefinitions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { writeFileSync, readFileSync } from "fs";
import { normalize, join } from "path";

import { Tokenizer } from "../src/Tokenizer";
import { TokenizedScope } from "../src/Tokenizer/Tokenizer";

Expand Down
15 changes: 7 additions & 8 deletions server/src/Documents/Document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { WorkspaceFilesSystem } from "../WorkspaceFilesSystem";
import type { ComplexToken, StructComplexToken } from "../Tokenizer/types";
import type DocumentsCollection from "./DocumentsCollection";

Expand All @@ -11,11 +10,11 @@ export default class Document {
readonly children: string[],
readonly complexTokens: ComplexToken[],
readonly structComplexTokens: StructComplexToken[],
private readonly collection: DocumentsCollection
private readonly collection: DocumentsCollection,
) {}

public getKey() {
return WorkspaceFilesSystem.getFileBasename(this.path);
return this.collection.getKey(this.path);
}

public getChildren(computedChildren: string[] = []): string[] {
Expand All @@ -35,7 +34,7 @@ export default class Document {
}

return childDocument.getChildren(computedChildren);
})
}),
);
}

Expand All @@ -56,7 +55,7 @@ export default class Document {
}

return childDocument.getGlobalComplexTokensWithRef(computedChildren);
})
}),
);
}

Expand All @@ -77,7 +76,7 @@ export default class Document {
}

return childDocument.getGlobalComplexTokens(computedChildren);
})
}),
);
}

Expand All @@ -98,7 +97,7 @@ export default class Document {
}

return childDocument.getGlobalStructComplexTokensWithRef(computedChildren);
})
}),
);
}

Expand All @@ -119,7 +118,7 @@ export default class Document {
}

return childDocument.getGlobalStructComplexTokens(computedChildren);
})
}),
);
}
}
20 changes: 15 additions & 5 deletions server/src/Documents/DocumentsCollection.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { join } from "path";
import { basename, join } from "path";
import { fileURLToPath } from "url";
import { readFileSync } from "fs";
import { TextDocument } from "vscode-languageserver-textdocument";

import type { Tokenizer } from "../Tokenizer";
import type { ComplexToken } from "../Tokenizer/types";
import { GlobalScopeTokenizationResult, TokenizedScope } from "../Tokenizer/Tokenizer";
import { Dictionnary } from "../Utils";
import { WorkspaceFilesSystem } from "../WorkspaceFilesSystem";
import Document from "./Document";
import { TextDocument } from "vscode-languageserver-textdocument";
import { FILES_EXTENSION } from "../WorkspaceFilesSystem/WorkspaceFilesSystem";

export default class DocumentsCollection extends Dictionnary<string, Document> {
public readonly standardLibComplexTokens: ComplexToken[] = [];
Expand All @@ -15,7 +17,7 @@ export default class DocumentsCollection extends Dictionnary<string, Document> {
super();

this.standardLibComplexTokens = JSON.parse(
WorkspaceFilesSystem.readFileSync(join(__dirname, "..", "resources", "standardLibDefinitions.json")).toString()
readFileSync(join(__dirname, "..", "resources", "standardLibDefinitions.json")).toString(),
).complexTokens as ComplexToken[];
}

Expand All @@ -31,12 +33,20 @@ export default class DocumentsCollection extends Dictionnary<string, Document> {
return new Document(filePath, globalScope.children, globalScope.complexTokens, globalScope.structComplexTokens, this);
}

public getKey(path: string) {
return basename(path, FILES_EXTENSION).slice(0, -1);
}

public getFromPath(path: string) {
return this.get(this.getKey(path));
}

public createDocument(filePath: string, globalScope: GlobalScopeTokenizationResult) {
this.addDocument(this.initializeDocument(filePath, globalScope));
}

public updateDocument(document: TextDocument, tokenizer: Tokenizer) {
const filePath = WorkspaceFilesSystem.fileUriToPath(document.uri);
const filePath = fileURLToPath(document.uri);
const globalScope = tokenizer.tokenizeContent(document.getText(), TokenizedScope.global);

this.overwriteDocument(this.initializeDocument(filePath, globalScope));
Expand Down
4 changes: 2 additions & 2 deletions server/src/Documents/DocumentsIndexer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readFileSync } from "fs";
import { exit } from "process";

import { Tokenizer } from "../Tokenizer";
import { TokenizedScope } from "../Tokenizer/Tokenizer";
import { WorkspaceFilesSystem } from "../WorkspaceFilesSystem";

const generateTokens = async (filesPath: string[]) => {
if (filesPath.length === 1 && !Boolean(filesPath[0])) {
Expand All @@ -17,7 +17,7 @@ const generateTokens = async (filesPath: string[]) => {
continue;
}

const fileContent = WorkspaceFilesSystem.readFileSync(filePath).toString();
const fileContent = readFileSync(filePath).toString();
const globalScope = tokenizer.tokenizeContent(fileContent, TokenizedScope.global);

process?.send!(JSON.stringify({ filePath, globalScope }));
Expand Down
4 changes: 2 additions & 2 deletions server/src/Providers/Builders/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export default abstract class Builder {
return token.tokenType === CompletionItemKind.Constant;
}

protected static isVariable(token: ComplexToken): token is VariableComplexToken {
protected static isVariableToken(token: ComplexToken): token is VariableComplexToken {
return token.tokenType === CompletionItemKind.Variable;
}

protected static isFunctionParameter(token: ComplexToken): token is FunctionParamComplexToken {
protected static isFunctionParameterToken(token: ComplexToken): token is FunctionParamComplexToken {
return token.tokenType === CompletionItemKind.TypeParameter;
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/Providers/Builders/CompletionItemBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export default class CompletionItemBuilder extends Builder {
public static buildItem(token: ComplexToken): CompletionItem {
if (this.isConstantToken(token)) {
return this.buildConstantItem(token);
} else if (this.isVariable(token)) {
} else if (this.isVariableToken(token)) {
return this.buildVariableItem(token);
} else if (this.isFunctionParameter(token)) {
} else if (this.isFunctionParameterToken(token)) {
return this.buildFunctionParamItem(token);
} else if (this.isFunctionToken(token)) {
return this.buildFunctionItem(token);
Expand Down
6 changes: 3 additions & 3 deletions server/src/Providers/Builders/HoverContentBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export default class HoverContentBuilder extends Builder {
public static buildItem(token: ComplexToken, serverConfig: ServerConfiguration): MarkupContent {
if (this.isConstantToken(token)) {
return this.buildConstantItem(token);
} else if (this.isVariable(token)) {
} else if (this.isVariableToken(token)) {
return this.buildVariableItem(token);
} else if (this.isFunctionParameter(token)) {
} else if (this.isFunctionParameterToken(token)) {
return this.buildFunctionParamItem(token);
} else if (this.isFunctionToken(token)) {
return this.buildFunctionItem(token, serverConfig);
Expand Down Expand Up @@ -53,7 +53,7 @@ export default class HoverContentBuilder extends Builder {
}, "")})`,
],
serverConfig.includeCommentsInFunctionsHover ? ["```nwscript", ...token.comments, "```"] : [],
[]
[],
);
}

Expand Down
6 changes: 3 additions & 3 deletions server/src/Providers/Builders/SignatureHelpBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export default class SignatureHelpBuilder extends Builder {
}`;
}, "")})`,
undefined,
...(token as FunctionComplexToken).params.map<ParameterInformation>((param) =>
ParameterInformation.create(`${param.valueType} ${param.identifier}`)
)
...token.params.map<ParameterInformation>((param) =>
ParameterInformation.create(`${param.valueType} ${param.identifier}`),
),
),
],
activeSignature: 0,
Expand Down
15 changes: 7 additions & 8 deletions server/src/Providers/CompletionItemsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fileURLToPath } from "url";
import { CompletionParams } from "vscode-languageserver";

import type { ServerManager } from "../ServerManager";
import { WorkspaceFilesSystem } from "../WorkspaceFilesSystem";
import { CompletionItemBuilder } from "./Builders";
import { LocalScopeTokenizationResult, TokenizedScope } from "../Tokenizer/Tokenizer";
import { TriggerCharacters } from ".";
Expand All @@ -15,7 +15,7 @@ export default class CompletionItemsProvider extends Provider {

this.server.connection.onCompletion((params) => this.exceptionsWrapper(this.providerHandler(params)));
this.server.connection.onCompletionResolve((item) =>
this.exceptionsWrapper(() => CompletionItemBuilder.buildResolvedItem(item, this.server.config), item)
this.exceptionsWrapper(() => CompletionItemBuilder.buildResolvedItem(item, this.server.config), item),
);
}

Expand All @@ -27,9 +27,8 @@ export default class CompletionItemsProvider extends Provider {
} = params;

const liveDocument = this.server.liveDocumentsManager.get(uri);
const path = WorkspaceFilesSystem.fileUriToPath(uri);
const documentKey = WorkspaceFilesSystem.getFileBasename(path);
const document = this.server.documentsCollection?.get(documentKey);
const path = fileURLToPath(uri);
const document = this.server.documentsCollection.getFromPath(path);

if (liveDocument) {
const localScope = this.server.tokenizer?.tokenizeContent(liveDocument.getText(), TokenizedScope.local, 0, position.line);
Expand All @@ -40,11 +39,11 @@ export default class CompletionItemsProvider extends Provider {
const structVariableIdentifier = this.server.tokenizer?.findLineIdentiferFromPositionAt(
liveDocument.getText(),
position,
-2
-2,
);

const structIdentifer = localScope.functionVariablesComplexTokens.find(
(token) => token.identifier === structVariableIdentifier
(token) => token.identifier === structVariableIdentifier,
)?.valueType;

return document
Expand Down Expand Up @@ -73,7 +72,7 @@ export default class CompletionItemsProvider extends Provider {

private getLocalScopeCompletionItems(localScope: LocalScopeTokenizationResult) {
const functionVariablesCompletionItems = localScope.functionVariablesComplexTokens.map((token) =>
CompletionItemBuilder.buildItem(token)
CompletionItemBuilder.buildItem(token),
);
const functionsCompletionItems = localScope.functionsComplexTokens.map((token) => CompletionItemBuilder.buildItem(token));

Expand Down
18 changes: 12 additions & 6 deletions server/src/Providers/ConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import { ServerManager } from "../ServerManager";
type ConfigCallback = () => void;

export default class ConfigurationProvider {
public static register(server: ServerManager, configChangeCallback: ConfigCallback) {
return new this(server, configChangeCallback);
}

constructor(private readonly server: ServerManager, private readonly configChangeCallback: ConfigCallback) {
this.server.connection.client.register(DidChangeConfigurationNotification.type);
this.server.connection.onDidChangeConfiguration(this.handleDidChangeConfiguration);
}

private async registerCallback() {
await this.server.connection.client.register(DidChangeConfigurationNotification.type);
}

// This needs to be an arrow function to keep the context
private handleDidChangeConfiguration = (_: DidChangeConfigurationParams) => {
private readonly handleDidChangeConfiguration = (_: DidChangeConfigurationParams) => {
this.configChangeCallback();
};

public static async register(server: ServerManager, configChangeCallback: ConfigCallback) {
const provider = new this(server, configChangeCallback);
await provider.registerCallback();

return provider;
}
}
Loading

0 comments on commit df3b6f9

Please sign in to comment.