Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

more strict typescript-eslint rules #194

Merged
merged 12 commits into from
Feb 12, 2025
1 change: 1 addition & 0 deletions client/src/client-log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-base-to-string */
import * as vscode from "vscode"

let consoleLogChannel: vscode.OutputChannel | null = null
Expand Down
4 changes: 2 additions & 2 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async function showReferencesImpl(
client: LanguageClient | undefined,
uri: string,
position: Position,
) {
): Promise<void> {
if (!client) return
await vscode.commands.executeCommand(
"editor.action.showReferences",
Expand All @@ -99,7 +99,7 @@ async function showReferencesImpl(
)
}

function registerCommands(disposables: vscode.Disposable[]) {
function registerCommands(disposables: vscode.Disposable[]): void {
disposables.push(
vscode.commands.registerCommand(
"tact.showParent",
Expand Down
34 changes: 18 additions & 16 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,39 @@ export default tseslint.config(
},

rules: {
// override stylisticTypeChecked
// override typescript-eslint
"@typescript-eslint/no-empty-function": ["error", {allow: ["arrowFunctions"]}],
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/typedef": [
"error",
{parameter: true, memberVariableDeclaration: true},
],
"@typescript-eslint/consistent-generic-constructors": ["error", "type-annotation"],
"@typescript-eslint/no-unused-vars": [
"error",
{
argsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
varsIgnorePattern: "^_",
},
],
"@typescript-eslint/explicit-function-return-type": [
"error",
{
allowExpressions: true,
},
],
"@typescript-eslint/prefer-optional-chain": "off",

// override strictTypeChecked
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-base-to-string": "off",
"@typescript-eslint/unbound-method": "off",
"@typescript-eslint/no-misused-promises": "off",
"@typescript-eslint/no-extraneous-class": "off",

"@typescript-eslint/prefer-readonly": "error",
"@typescript-eslint/switch-exhaustiveness-check": "error",

"@unused-imports/no-unused-imports": "error",
"no-duplicate-imports": "error",

"@typescript-eslint/no-magic-numbers": "off",
"@typescript-eslint/no-unsafe-type-assertion": "off",
"@typescript-eslint/prefer-readonly-parameter-types": "off",
"@typescript-eslint/member-ordering": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/parameter-properties": "off",
"@typescript-eslint/method-signature-style": "off",
"@typescript-eslint/prefer-destructuring": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/no-use-before-define": "off",

"@typescript-eslint/class-methods-use-this": "off",
"@typescript-eslint/no-shadow": "off",
"@typescript-eslint/consistent-type-imports": "off",
Expand All @@ -84,6 +83,9 @@ export default tseslint.config(
"@typescript-eslint/no-invalid-this": "off",
"@typescript-eslint/init-declarations": "off",

"@unused-imports/no-unused-imports": "error",
"no-duplicate-imports": "error",

// override unicorn
"unicorn/no-null": "off",
"unicorn/prevent-abbreviations": "off",
Expand Down
6 changes: 3 additions & 3 deletions server/src/TypeInferer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,21 @@ export class TypeInferer {
return null
}

private primitiveType(name: string) {
private primitiveType(name: string): PrimitiveTy | null {
const node = index.elementByName(IndexKey.Primitives, name)
if (!node) return null
return new PrimitiveTy(name, node, null)
}

private inferTypeMaybeOption(typeNode: SyntaxNode, resolved: Node) {
private inferTypeMaybeOption(typeNode: SyntaxNode, resolved: Node): Ty | null {
const inferred = this.inferType(new Expression(typeNode, resolved.file))
if (inferred && !(inferred instanceof OptionTy) && typeNode.nextSibling?.text === "?") {
return new OptionTy(inferred)
}
return inferred
}

private inferTypeMaybeTlB(typeNode: SyntaxNode, resolved: Node) {
private inferTypeMaybeTlB(typeNode: SyntaxNode, resolved: Node): Ty | null {
const inferredType = this.inferTypeMaybeOption(typeNode, resolved)
if (inferredType instanceof PrimitiveTy) {
const tlb = resolved.node.childForFieldName("tlb")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class PostfixCompletionProvider implements CompletionProvider {
description: string,
snippet: string,
result: CompletionResult,
) {
): void {
const expr = ctx.element.node.parent
if (expr?.type !== "field_access_expression") return
const object = expr.childForFieldName("object")
Expand Down
2 changes: 1 addition & 1 deletion server/src/documentation/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,6 @@ function extractCommentsDoc(node: NamedNode): string {
return result.trimEnd()
}

function defaultResult(signature: string, documentation: string = "") {
function defaultResult(signature: string, documentation: string = ""): string {
return DOC_TMPL.replace("{signature}", signature).replace("{documentation}", documentation)
}
2 changes: 1 addition & 1 deletion server/src/documentation/receivers_documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function generateReceiverDoc(func: MessageFunction): string | null {
return defaultResult(name, "", link)
}

function defaultResult(name: string, doc: string, link: string) {
function defaultResult(name: string, doc: string, link: string): string {
return (
`${CODE_FENCE}tact\n${name} {}\n${CODE_FENCE}` +
"\n" +
Expand Down
2 changes: 1 addition & 1 deletion server/src/e2e/runTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from "node:path"
import {runTests} from "@vscode/test-electron"

async function main() {
async function main(): Promise<void> {
try {
const extensionDevelopmentPath = path.resolve(__dirname, "../../../")
const extensionTestsPath = path.resolve(__dirname, "./out/suite/index.js")
Expand Down
2 changes: 1 addition & 1 deletion server/src/e2e/suite/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ suite("Completion Test Suite", () => {
)
}

protected runTest(testFile: string, testCase: TestCase) {
protected runTest(testFile: string, testCase: TestCase): void {
test(`Completion: ${testCase.name}`, async () => {
const completions = await this.getCompletions(testCase.input, ".")

Expand Down
2 changes: 1 addition & 1 deletion server/src/e2e/suite/doc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ suite("Documentation Test Suite", () => {
return (hover.contents as lsp.MarkupContent).value.trimEnd()
}

protected runTest(testFile: string, testCase: TestCase) {
protected runTest(testFile: string, testCase: TestCase): void {
test(`Documentation: ${testCase.name}`, async () => {
const hovers = await this.getHovers(testCase.input)
const actual = hovers
Expand Down
2 changes: 1 addition & 1 deletion server/src/e2e/suite/inspection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ suite("Inspection Test Suite", () => {
.join("\n")
}

protected runTest(testFile: string, testCase: TestCase) {
protected runTest(testFile: string, testCase: TestCase): void {
test(`Inspection: ${testCase.name}`, async () => {
await this.replaceDocumentText(testCase.input)

Expand Down
2 changes: 1 addition & 1 deletion server/src/e2e/suite/intentions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ suite("Intentions Test Suite", () => {
)
}

protected runTest(testFile: string, testCase: TestCase) {
protected runTest(testFile: string, testCase: TestCase): void {
test(`Intention: ${testCase.name}`, async () => {
const actions = await this.getCodeActions(testCase.input)

Expand Down
2 changes: 1 addition & 1 deletion server/src/e2e/suite/references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ suite("References Test Suite", () => {
.join("\n\n")
}

protected runTest(testFile: string, testCase: TestCase) {
protected runTest(testFile: string, testCase: TestCase): void {
test(`References: ${testCase.name}`, async () => {
const caretIndexes = this.findCaretPositions(testCase.input)
const positions = caretIndexes.map(index =>
Expand Down
4 changes: 2 additions & 2 deletions server/src/e2e/suite/rename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ suite("Rename Test Suite", () => {
return positions
}

private async renameTo(position: vscode.Position, newName: string) {
private async renameTo(position: vscode.Position, newName: string): Promise<void> {
const result = await vscode.commands.executeCommand<vscode.WorkspaceEdit | undefined>(
"vscode.executeDocumentRenameProvider",
this.document.uri,
Expand All @@ -45,7 +45,7 @@ suite("Rename Test Suite", () => {
}
}

protected runTest(testFile: string, testCase: TestCase) {
protected runTest(testFile: string, testCase: TestCase): void {
test(`Rename: ${testCase.name}`, async () => {
const positions = this.findRenamePositions(testCase.input)

Expand Down
2 changes: 1 addition & 1 deletion server/src/e2e/suite/resolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ suite("Resolve Test Suite", () => {
.join("\n")
}

protected runTest(testFile: string, testCase: TestCase) {
protected runTest(testFile: string, testCase: TestCase): void {
test(`Resolve: ${testCase.name}`, async () => {
const caretIndexes = this.findCaretPositions(testCase.input)
const positions = caretIndexes.map(index =>
Expand Down
2 changes: 1 addition & 1 deletion server/src/e2e/suite/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ suite("Type Inference Test Suite", () => {
return response.type ?? undefined
}

protected runTest(testFile: string, testCase: TestCase) {
protected runTest(testFile: string, testCase: TestCase): void {
test(`Types: ${testCase.name}`, async () => {
const positions = this.findTypePositions(testCase.input)

Expand Down
2 changes: 1 addition & 1 deletion server/src/fift/foldings/collect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {FoldingRange, FoldingRangeKind} from "vscode-languageserver-types"
import {RecursiveVisitor} from "@server/psi/visitor"
import type {File} from "@server/psi/File"
import {Point} from "web-tree-sitter"
import type {Point} from "web-tree-sitter"
import type * as lsp from "vscode-languageserver"

export function collectFift(file: File): FoldingRange[] {
Expand Down
2 changes: 1 addition & 1 deletion server/src/fift/semantic_tokens/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function collectFift(

const builder = new SemanticTokensBuilder()

function pushToken(n: SyntaxNode, tokenType: lsp.SemanticTokenTypes) {
function pushToken(n: SyntaxNode, tokenType: lsp.SemanticTokenTypes): void {
builder.push(
n.startPosition.row,
n.startPosition.column,
Expand Down
2 changes: 1 addition & 1 deletion server/src/inspections/MissedFieldInContractInspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class MissedFieldInContractInspection extends UnusedInspection implements
})
}

private inspectContract(contract: Contract, diagnostics: lsp.Diagnostic[]) {
private inspectContract(contract: Contract, diagnostics: lsp.Diagnostic[]): void {
const inheritedTraits = contract.inheritTraits()
if (inheritedTraits.length === 0) return // nothing to check

Expand Down
2 changes: 1 addition & 1 deletion server/src/inspections/StructInitializationInspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class StructInitializationInspection implements Inspection {
return diagnostics
}

private checkStructLiteral(node: SyntaxNode, diagnostics: lsp.Diagnostic[]) {
private checkStructLiteral(node: SyntaxNode, diagnostics: lsp.Diagnostic[]): void {
const structName = node.childForFieldName("name")
if (!structName) return
const args = node.childForFieldName("arguments")
Expand Down
2 changes: 1 addition & 1 deletion server/src/inspections/UnusedContractMembersInspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class UnusedContractMembersInspection extends UnusedInspection implements
})
}

private inspectContract(contract: Contract, diagnostics: lsp.Diagnostic[]) {
private inspectContract(contract: Contract, diagnostics: lsp.Diagnostic[]): void {
contract.ownFields().forEach(field => {
const nameIdent = field.nameIdentifier()
if (!nameIdent) return
Expand Down
2 changes: 1 addition & 1 deletion server/src/inspections/UnusedParameterInspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class UnusedParameterInspection extends UnusedInspection implements Inspe
})
}

private inspectFunction(fun: Fun, diagnostics: lsp.Diagnostic[]) {
private inspectFunction(fun: Fun, diagnostics: lsp.Diagnostic[]): void {
if (!fun.hasBody()) return
const parameters = fun.parameters()

Expand Down
3 changes: 2 additions & 1 deletion server/src/intentions/AddExplicitType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {asLspPosition, asParserPoint} from "@server/utils/position"
import type {Position} from "vscode-languageclient"
import {VarDeclaration} from "@server/psi/Node"
import {FileDiff} from "@server/utils/FileDiff"
import type {Node as SyntaxNode} from "web-tree-sitter"

export class AddExplicitType implements Intention {
public readonly id: string = "tact.add-explicit-type"
Expand Down Expand Up @@ -42,7 +43,7 @@ export class AddExplicitType implements Intention {
}
}

function nodeAtPosition(pos: Position, file: File) {
function nodeAtPosition(pos: Position, file: File): SyntaxNode | null {
const cursorPosition = asParserPoint(pos)
return file.rootNode.descendantForPosition(cursorPosition)
}
5 changes: 3 additions & 2 deletions server/src/intentions/AddFieldInitialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {FileDiff} from "@server/utils/FileDiff"
import {Field, StorageMembersOwner} from "@server/psi/Decls"
import type {Ty} from "@server/types/BaseTy"
import {RecursiveVisitor} from "@server/psi/RecursiveVisitor"
import type {Node as SyntaxNode} from "web-tree-sitter"

export class AddFieldInitialization implements Intention {
public readonly id: string = "tact.add-field-to-init"
Expand Down Expand Up @@ -125,7 +126,7 @@ export class AddFieldInitialization implements Intention {
resolved: Field,
type: Ty,
owner: StorageMembersOwner,
) {
): WorkspaceEdit {
const initFunctionTemplate = `
init($name: $type) {
self.$name = $name;
Expand Down Expand Up @@ -162,7 +163,7 @@ export class AddFieldInitialization implements Intention {
}
}

function nodeAtPosition(pos: Position, file: File) {
function nodeAtPosition(pos: Position, file: File): SyntaxNode | null {
const cursorPosition = asParserPoint(pos)
return file.rootNode.descendantForPosition(cursorPosition)
}
3 changes: 2 additions & 1 deletion server/src/intentions/AddImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {NamedNode} from "@server/psi/Node"
import {FileDiff} from "@server/utils/FileDiff"
import {Reference} from "@server/psi/Reference"
import {Contract, Primitive} from "@server/psi/Decls"
import type {Node as SyntaxNode} from "web-tree-sitter"

export class AddImport implements Intention {
public readonly id: string = "tact.add-import"
Expand Down Expand Up @@ -49,7 +50,7 @@ export class AddImport implements Intention {
}
}

function nodeAtPosition(pos: Position, file: File) {
function nodeAtPosition(pos: Position, file: File): SyntaxNode | null {
const cursorPosition = asParserPoint(pos)
return file.rootNode.descendantForPosition(cursorPosition)
}
9 changes: 6 additions & 3 deletions server/src/intentions/FillAllStructInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export class FillStructInitBase implements Intention {
return args.length === 0
}

private static findBraces(instance: SyntaxNode) {
private static findBraces(instance: SyntaxNode): {
openBrace: SyntaxNode
closeBrace: SyntaxNode
} | null {
const args = instance.childForFieldName("arguments")
if (!args) return null

Expand All @@ -54,7 +57,7 @@ export class FillStructInitBase implements Intention {
return {openBrace, closeBrace}
}

private static findIndent(ctx: IntentionContext, instance: SyntaxNode) {
private static findIndent(ctx: IntentionContext, instance: SyntaxNode): number {
const lines = ctx.file.content.split(/\r?\n/)
const line = lines[instance.startPosition.row]
const lineTrim = line.trimStart()
Expand Down Expand Up @@ -201,7 +204,7 @@ export class FillRequiredStructInit extends FillStructInitBase {
}
}

function nodeAtPosition(pos: Position, file: File) {
function nodeAtPosition(pos: Position, file: File): SyntaxNode | null {
const cursorPosition = asParserPoint(pos)
return file.rootNode.descendantForPosition(cursorPosition)
}
2 changes: 1 addition & 1 deletion server/src/intentions/WrapSelected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class WrapSelected implements Intention {
return this.findStatements(ctx).length > 0
}

private static findIndent(ctx: IntentionContext, node: SyntaxNode) {
private static findIndent(ctx: IntentionContext, node: SyntaxNode): number {
const lines = ctx.file.content.split(/\r?\n/)
const line = lines[node.startPosition.row]
const lineTrim = line.trimStart()
Expand Down
Loading