From 2539e98a44c70fb457bfb7459e028577839d352b Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Tue, 28 Apr 2020 10:16:47 -0400 Subject: [PATCH] Improve ESLint --- Jenkinsfile | 2 +- build.gradle | 149 +------------------ jni.build.gradle | 74 +++++++++ src/main/js/.eslintrc.json | 18 ++- src/main/js/package.json | 2 +- src/main/js/src/minecraft/block.ts | 2 +- src/main/js/src/minecraft/entity.ts | 13 +- src/main/js/src/minecraft/index.ts | 2 +- src/main/js/src/minecraft/item.ts | 12 +- src/main/js/src/minecraft/tag.ts | 8 +- src/main/js/src/minecraft/world.ts | 8 +- src/main/js/src/types/scriptcraft/index.d.ts | 2 +- typescript.build.gradle | 73 +++++++++ 13 files changed, 189 insertions(+), 176 deletions(-) create mode 100644 jni.build.gradle create mode 100644 typescript.build.gradle diff --git a/Jenkinsfile b/Jenkinsfile index 683b515..7240a61 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -10,7 +10,7 @@ pipeline { } stage('Build') { steps { - sh './gradlew build typedoc' + sh './gradlew build typedoc eslint' } post { success { diff --git a/build.gradle b/build.gradle index 158b149..0ad8b07 100644 --- a/build.gradle +++ b/build.gradle @@ -3,65 +3,6 @@ plugins { id 'com.matthewprenger.cursegradle' version '1.4.0' } -def cDir = new File(rootProject.projectDir.absolutePath, 'src/main/c') - -class JNIPlatform { - String name - String[] cmakeArgs - String libExtension - - JNIPlatform(String name, String libExtension, File rootDir) { - this.name = name - this.cmakeArgs = ["-DCMAKE_TOOLCHAIN_FILE=${rootDir.absolutePath}/cmake/${name}-toolchain.cmake"] as String[] - this.libExtension = libExtension - } -} - -JNIPlatform[] jniPlatforms = [ - new JNIPlatform("linux-x86_64", ".so", rootDir), - new JNIPlatform("linux-x86", ".so", rootDir), - new JNIPlatform("linux-armhf", ".so", rootDir), - new JNIPlatform("linux-arm64", ".so", rootDir), - new JNIPlatform("windows-x86_64", ".dll", rootDir), - new JNIPlatform("windows-x86", ".dll", rootDir) -] - -task cleanJNI {} -task compileJNI {} - -for (JNIPlatform platform : jniPlatforms) { - def buildDir = new File(cDir, "build-${platform.name}") - if (!buildDir.exists()) { - buildDir.mkdir() - } - - tasks.create(name: "cmake-${platform.name}", group: 'jni', type: Exec) { - workingDir buildDir - - executable 'cmake' - args platform.cmakeArgs + ['..'] - } - - tasks.create(name: "compileJNI-${platform.name}", group: 'jni', type: Exec) { - workingDir buildDir - - executable 'make' - - dependsOn tasks.getByName("cmake-${platform.name}") - } - - compileJNI.dependsOn tasks.getByName("compileJNI-${platform.name}") - - tasks.create(name: "cleanJNI-${platform.name}", group: 'jni', type: Delete) { - delete buildDir.absolutePath - } - - cleanJNI.dependsOn tasks.getByName("cleanJNI-${platform.name}") -} - -clean.dependsOn cleanJNI -processResources.dependsOn compileJNI - compileJava { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 @@ -82,72 +23,8 @@ dependencies { modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_api_version}" } -def typescriptOut = new File(buildDir, 'generated/typescript') - -sourceSets { - main { - resources { - srcDir typescriptOut - } - } -} - -def typescriptRoot = 'src/main/js' -def typescriptRootFile = new File(rootDir.absolutePath, typescriptRoot) - -task typescript(group: 'build') { - inputs.dir typescriptRootFile - outputs.dir typescriptOut - - doFirst { - project.delete { - delete typescriptOut - } - typescriptOut.mkdirs() - - project.exec { - workingDir typescriptRootFile - - executable 'npm' - - args 'run', 'build', '--' - args '--outDir', new File(typescriptOut, 'scriptcraft').absolutePath - } - } -} - -task eslint(group: 'verification', type: Exec) { - workingDir typescriptRootFile - - executable 'npm' - - args 'run', 'eslint' -} - -processResources.dependsOn typescript - -def typedocOut = new File(buildDir, 'typedoc') - -task typedoc(group: 'documentation') { - inputs.dir typescriptRootFile - outputs.dir typedocOut - - doFirst { - project.delete { - delete typedocOut - } - typedocOut.mkdirs() - - project.exec { - workingDir typescriptRootFile - - executable 'npm' - - args 'run', 'typedoc', '--' - args '--out', typedocOut.absolutePath - } - } -} +apply from: './typescript.build.gradle' +apply from: './jni.build.gradle' processResources { inputs.property 'version', mod_version @@ -161,20 +38,6 @@ processResources { from(sourceSets.main.resources.srcDirs) { exclude 'fabric.mod.json' } - - for (JNIPlatform platform : jniPlatforms) { - def buildDir = new File(cDir, "build-${platform.name}") - if (!buildDir.exists()) { - buildDir.mkdir() - } - - def file = new File(buildDir, 'libscriptcraft' + platform.libExtension) - inputs.files file - - from(file.absolutePath) { - into new File('natives', platform.name).path - } - } } // ensure that the encoding is set to UTF-8, no matter what the system default is @@ -192,16 +55,8 @@ task sourcesJar(type: Jar, dependsOn: classes) { from sourceSets.main.allSource } -task typedocJar(type: Jar) { - classifier 'typedoc' - from typedocOut -} - -typedocJar.dependsOn typedoc - artifacts { archives sourcesJar - archives typedocJar } jar { diff --git a/jni.build.gradle b/jni.build.gradle new file mode 100644 index 0000000..4483aa1 --- /dev/null +++ b/jni.build.gradle @@ -0,0 +1,74 @@ +def cDir = new File(rootProject.projectDir.absolutePath, 'src/main/c') + +class JNIPlatform { + String name + String[] cmakeArgs + String libExtension + + JNIPlatform(String name, String libExtension, File rootDir) { + this.name = name + this.cmakeArgs = ["-DCMAKE_TOOLCHAIN_FILE=${rootDir.absolutePath}/cmake/${name}-toolchain.cmake"] as String[] + this.libExtension = libExtension + } +} + +JNIPlatform[] jniPlatforms = [ + new JNIPlatform("linux-x86_64", ".so", rootDir), + new JNIPlatform("linux-x86", ".so", rootDir), + new JNIPlatform("linux-armhf", ".so", rootDir), + new JNIPlatform("linux-arm64", ".so", rootDir), + new JNIPlatform("windows-x86_64", ".dll", rootDir), + new JNIPlatform("windows-x86", ".dll", rootDir) +] + +task cleanJNI {} +task compileJNI {} + +for (JNIPlatform platform : jniPlatforms) { + def buildDir = new File(cDir, "build-${platform.name}") + if (!buildDir.exists()) { + buildDir.mkdir() + } + + tasks.create(name: "cmake-${platform.name}", group: 'jni', type: Exec) { + workingDir buildDir + + executable 'cmake' + args platform.cmakeArgs + ['..'] + } + + tasks.create(name: "compileJNI-${platform.name}", group: 'jni', type: Exec) { + workingDir buildDir + + executable 'make' + + dependsOn tasks.getByName("cmake-${platform.name}") + } + + compileJNI.dependsOn tasks.getByName("compileJNI-${platform.name}") + + tasks.create(name: "cleanJNI-${platform.name}", group: 'jni', type: Delete) { + delete buildDir.absolutePath + } + + cleanJNI.dependsOn tasks.getByName("cleanJNI-${platform.name}") +} + +clean.dependsOn cleanJNI +processResources.dependsOn compileJNI + +processResources { + for (JNIPlatform platform : jniPlatforms) { + def buildDir = new File(cDir, "build-${platform.name}") + if (!buildDir.exists()) { + buildDir.mkdir() + } + + def file = new File(buildDir, 'libscriptcraft' + platform.libExtension) + inputs.files file + + from(file.absolutePath) { + into new File('natives', platform.name).path + } + } +} \ No newline at end of file diff --git a/src/main/js/.eslintrc.json b/src/main/js/.eslintrc.json index 727843d..da8979c 100644 --- a/src/main/js/.eslintrc.json +++ b/src/main/js/.eslintrc.json @@ -3,7 +3,8 @@ "parser": "@typescript-eslint/parser", "parserOptions": { "tsconfigRootDir": ".", - "project": ["./tsconfig.json"] + "project": ["./tsconfig.json"], + "sourceType": "module" }, "plugins": [ "@typescript-eslint" @@ -24,6 +25,19 @@ "@typescript-eslint/no-empty-function": "off", "@typescript-eslint/no-use-before-define": "off", "quotes": ["error", "single"], - "eol-last": ["error", "always"] + "eol-last": ["error", "always"], + "no-eq-null": "error", + "no-unneeded-ternary": "error", + "block-spacing": "error", + "camelcase": "error", + "comma-dangle": ["error", "never"], + "func-call-spacing": ["error", "never"], + "function-call-argument-newline": ["error", "never"], + "implicit-arrow-linebreak": ["error", "beside"], + "linebreak-style": ["error", "unix"], + "no-trailing-spaces": "error", + "no-lonely-if": "error", + "no-var": "error", + "prefer-arrow-callback": "error" } } \ No newline at end of file diff --git a/src/main/js/package.json b/src/main/js/package.json index 09f31b2..04bf7d0 100644 --- a/src/main/js/package.json +++ b/src/main/js/package.json @@ -8,7 +8,7 @@ "typescript": "latest" }, "scripts": { - "eslint": "eslint --ignore-path .gitignore src", + "eslint": "eslint --ignore-path .gitignore \"src/**/*\"", "build": "tsc", "typedoc": "typedoc" } diff --git a/src/main/js/src/minecraft/block.ts b/src/main/js/src/minecraft/block.ts index b6e577c..c3d1e47 100644 --- a/src/main/js/src/minecraft/block.ts +++ b/src/main/js/src/minecraft/block.ts @@ -157,7 +157,7 @@ export class CustomBlockEntity { * Mark Dirty */ markDirty(): void { - if (this.getWorld() != null) { + if (this.getWorld() !== null) { useBridge('World.markBlockEntityDirty', this.getWorld().javaObject, this.getPos().getX(), this.getPos().getY(), this.getPos().getZ()); } } diff --git a/src/main/js/src/minecraft/entity.ts b/src/main/js/src/minecraft/entity.ts index 2ef8c9a..24d35ab 100644 --- a/src/main/js/src/minecraft/entity.ts +++ b/src/main/js/src/minecraft/entity.ts @@ -25,7 +25,7 @@ export class DamageSource { */ private static build(bridge: string, value: BridgeValueType): DamageSource { const obj = useBridge(bridge, value) as JavaObject; - if (obj != null) { + if (obj !== null) { return new DamageSource(obj); } else { return null; @@ -73,9 +73,6 @@ export class Entity { * @internal */ constructor(object: JavaObject | Entity) { - if (object == null) { - return null; - } if (object instanceof Entity) { this.javaObject = object.javaObject; } else { @@ -89,7 +86,7 @@ export class Entity { */ getEntityWorld(): World { const obj = useBridge('Entity.getEntityWorld', this.javaObject) as JavaObject; - if (obj != null) { + if (obj !== null) { return new World(obj); } else { return null; @@ -102,7 +99,7 @@ export class Entity { */ getID(): Identifier { const obj = useBridge('Entity.getID', this.javaObject) as string; - if (obj != null) { + if (obj !== null) { return new Identifier(obj); } else { return null; @@ -185,7 +182,7 @@ export class Entity { */ getPosition(): Pos { const pos: number[] = useBridge('Entity.getPosition', this.javaObject) as number[]; - if (pos != null && pos.length === 3) { + if (pos !== null && pos.length === 3) { return new Pos(pos[0], pos[1], pos[2]); } else { return null; @@ -206,7 +203,7 @@ export class Entity { */ toTag(): CompoundTag { const obj = useBridge('Entity.toTag', this.javaObject) as JavaObject; - if (obj != null) { + if (obj !== null) { return new CompoundTag(obj); } else { return null; diff --git a/src/main/js/src/minecraft/index.ts b/src/main/js/src/minecraft/index.ts index c7ad293..1c86a18 100644 --- a/src/main/js/src/minecraft/index.ts +++ b/src/main/js/src/minecraft/index.ts @@ -1,6 +1,6 @@ /** * API Entry-Point - * + * * Import With: * ```typescript * import { ... } from 'minecraft'; diff --git a/src/main/js/src/minecraft/item.ts b/src/main/js/src/minecraft/item.ts index 2cf7f76..5902a80 100644 --- a/src/main/js/src/minecraft/item.ts +++ b/src/main/js/src/minecraft/item.ts @@ -27,7 +27,7 @@ export class ItemStack { */ static create(item: Identifier, count?: number): ItemStack { const obj = useBridge('ItemStack.create', item.toString(), count ? count : 1) as JavaObject; - if (obj != null) { + if (obj !== null) { return new ItemStack(obj); } else { return null; @@ -40,7 +40,7 @@ export class ItemStack { */ getItem(): Identifier { const obj = useBridge('ItemStack.getItem', this.javaObject) as string; - if (obj != null) { + if (obj !== null) { return new Identifier(obj); } else { return null; @@ -101,7 +101,7 @@ export class ItemStack { */ getTag(): CompoundTag { const obj = useBridge('ItemStack.getTag', this.javaObject) as JavaObject; - if (obj != null) { + if (obj !== null) { return new CompoundTag(obj); } else { return null; @@ -122,7 +122,7 @@ export class ItemStack { */ toTag(): CompoundTag { const obj = useBridge('ItemStack.toTag', this.javaObject) as JavaObject; - if (obj != null) { + if (obj !== null) { return new CompoundTag(obj); } else { return null; @@ -136,7 +136,7 @@ export class ItemStack { */ static fromTag(tag: CompoundTag): ItemStack { const obj = useBridge('ItemStack.fromTag', tag.javaObject) as JavaObject; - if (obj != null) { + if (obj !== null) { return new ItemStack(obj); } else { return null; @@ -322,5 +322,5 @@ addBridge('CustomItem.onUseOnBlock', (id: string, world: JavaObject, x: number, }); addBridge('CustomItem.onUseOnEntity', (id: string, player: JavaObject, target: JavaObject, hand: keyof typeof Hand): boolean => { - return ItemRegistry.INSTANCE.get(new Identifier(id)).onUseOnEntity(new PlayerEntity(player), new LivingEntity(target), Hand[hand]) === ActionResult.SUCCESS ? true : false; + return ItemRegistry.INSTANCE.get(new Identifier(id)).onUseOnEntity(new PlayerEntity(player), new LivingEntity(target), Hand[hand]) === ActionResult.SUCCESS; }); diff --git a/src/main/js/src/minecraft/tag.ts b/src/main/js/src/minecraft/tag.ts index 797a7ef..2b0cedf 100644 --- a/src/main/js/src/minecraft/tag.ts +++ b/src/main/js/src/minecraft/tag.ts @@ -86,7 +86,7 @@ export class CompoundTag { set(key: string, value: number, numberType: NumberType): void; set(key: string, value: Exclude): void; set(key: string, value: TagType, numberType?: NumberType): void { - useBridge('CompoundTag.set', this.javaObject, key, value instanceof CompoundTag || value instanceof ListTag ? value.javaObject : value, numberType != null ? numberType : NumberType.DOUBLE); + useBridge('CompoundTag.set', this.javaObject, key, value instanceof CompoundTag || value instanceof ListTag ? value.javaObject : value, numberType !== null ? numberType : NumberType.DOUBLE); } /** @@ -103,7 +103,7 @@ export class CompoundTag { */ static create(): CompoundTag { const obj = useBridge('CompoundTag.create') as JavaObject; - if (obj != null) { + if (obj !== null) { return new CompoundTag(obj); } else { return null; @@ -148,7 +148,7 @@ export class ListTag { set(key: number, value: number, numberType: NumberType): void; set(key: number, value: Exclude): void; set(key: number, value: TagType, numberType?: NumberType): void { - useBridge('ListTag.set', this.javaObject, key, value instanceof CompoundTag || value instanceof ListTag ? value.javaObject : value, numberType != null ? numberType : NumberType.DOUBLE); + useBridge('ListTag.set', this.javaObject, key, value instanceof CompoundTag || value instanceof ListTag ? value.javaObject : value, numberType !== null ? numberType : NumberType.DOUBLE); } /** @@ -165,7 +165,7 @@ export class ListTag { */ static create(): ListTag { const obj = useBridge('ListTag.create') as JavaObject; - if (obj != null) { + if (obj !== null) { return new ListTag(obj); } else { return null; diff --git a/src/main/js/src/minecraft/world.ts b/src/main/js/src/minecraft/world.ts index f2a3057..22a5bb6 100644 --- a/src/main/js/src/minecraft/world.ts +++ b/src/main/js/src/minecraft/world.ts @@ -36,7 +36,7 @@ export class World { */ getBlockState(pos: Pos): BlockState { const obj = useBridge('World.getBlockState', this.javaObject, pos.getX(), pos.getY(), pos.getZ()) as JavaObject; - if (obj != null) { + if (obj !== null) { return new BlockState(obj); } else { return null; @@ -50,7 +50,7 @@ export class World { */ spawnEntity(id: Identifier, pos: Pos): Entity { const obj = useBridge('World.spawnEntity', this.javaObject, pos.getX(), pos.getY(), pos.getZ(), id.toString()) as JavaObject; - if (obj != null) { + if (obj !== null) { return new Entity(obj); } else { return null; @@ -64,7 +64,7 @@ export class World { */ getCustomBlockEntity(pos: Pos): CustomBlockEntity { const obj = useBridge('World.getCustomBlockEntity', this.javaObject, pos.getX(), pos.getY(), pos.getZ()) as number; - if (obj != null) { + if (obj !== null) { return BlockRegistry.INSTANCE.getCustomBlockEntity(obj); } else { return null; @@ -78,7 +78,7 @@ export class World { */ getBlockEntity(pos: Pos): Identifier { const obj = useBridge('World.getBlockEntity', this.javaObject, pos.getX(), pos.getY(), pos.getZ()) as string; - if (obj != null) { + if (obj !== null) { return new Identifier(obj); } else { return null; diff --git a/src/main/js/src/types/scriptcraft/index.d.ts b/src/main/js/src/types/scriptcraft/index.d.ts index 7d7da9a..f5a84d5 100644 --- a/src/main/js/src/types/scriptcraft/index.d.ts +++ b/src/main/js/src/types/scriptcraft/index.d.ts @@ -29,6 +29,6 @@ declare const console: Console; declare module 'scriptcraft-core' { function addBridge(name: string, bridge: BridgeType): void; - + function useBridge(name: string, ...args: BridgeValueType[]): BridgeValueType; } diff --git a/typescript.build.gradle b/typescript.build.gradle new file mode 100644 index 0000000..865bc27 --- /dev/null +++ b/typescript.build.gradle @@ -0,0 +1,73 @@ +def typescriptOut = new File(buildDir, 'generated/typescript') + +def typescriptRoot = 'src/main/js' +def typescriptRootFile = new File(rootDir.absolutePath, typescriptRoot) + +task typescript(group: 'build') { + inputs.dir typescriptRootFile + outputs.dir typescriptOut + + doFirst { + project.delete { + delete typescriptOut + } + typescriptOut.mkdirs() + + project.exec { + workingDir typescriptRootFile + + executable 'npm' + + args 'run', 'build', '--' + args '--outDir', new File(typescriptOut, 'scriptcraft').absolutePath + } + } +} + +task eslint(group: 'verification', type: Exec) { + workingDir typescriptRootFile + + executable 'npm' + + args 'run', 'eslint' +} + +processResources.dependsOn typescript + +def typedocOut = new File(buildDir, 'typedoc') + +task typedoc(group: 'documentation') { + inputs.dir typescriptRootFile + outputs.dir typedocOut + + doFirst { + project.delete { + delete typedocOut + } + typedocOut.mkdirs() + + project.exec { + workingDir typescriptRootFile + + executable 'npm' + + args 'run', 'typedoc', '--' + args '--out', typedocOut.absolutePath + } + } +} + +processResources { + from typescriptOut.absolutePath +} + +task typedocJar(type: Jar) { + classifier 'typedoc' + from typedocOut +} + +typedocJar.dependsOn typedoc + +artifacts { + archives typedocJar +} \ No newline at end of file