Skip to content

Commit

Permalink
cleanup prettier rules
Browse files Browse the repository at this point in the history
  • Loading branch information
arition committed Apr 26, 2022
1 parent 9472698 commit 211dfd1
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
insert_final_newline = true
end_of_line = lf
13 changes: 11 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
},
"extends": [
"airbnb-base",
"prettier"
"plugin:prettier/recommended"
],
"overrides": [
{
"files": [
"**/*.test.js"
],
"env": {
"node": true,
"jest": true
}
},
{
"files": [
"**/*.ts",
Expand All @@ -20,7 +29,7 @@
"airbnb-typescript/base",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
"plugin:prettier/recommended"
],
"globals": {
"Atomics": "readonly",
Expand Down
14 changes: 7 additions & 7 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require("fs");
const fs = require('fs');

if (fs.existsSync("dist")) {
fs.rmdirSync("dist", { recursive: true });
if (fs.existsSync('dist')) {
fs.rmdirSync('dist', { recursive: true });
}
fs.mkdirSync("dist");
fs.copyFileSync("lib/torch.js", "dist/torch.js");
fs.copyFileSync("lib/torch.d.ts", "dist/torch.d.ts");
require("typescript/bin/tsc");
fs.mkdirSync('dist');
fs.copyFileSync('lib/torch.js', 'dist/torch.js');
fs.copyFileSync('lib/torch.d.ts', 'dist/torch.d.ts');
require('typescript/bin/tsc');
8 changes: 4 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert = require("assert");
import torch = require("./torch");
import assert = require('assert');
import torch = require('./torch');

export * from "./torch";
export * from './torch';

// eslint-disable-next-line no-use-before-define
type Matrix = number[] | NestedArray;
Expand Down Expand Up @@ -33,7 +33,7 @@ function typedArrayType(dtype: number) {
case torch.int32:
return Int32Array;
default:
throw new TypeError("Unsupported dtype");
throw new TypeError('Unsupported dtype');
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/torch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const torch = require("bindings")("torch-js");
const torch = require('bindings')('torch-js');

const bindings = require("bindings");
const path = require("path");
const bindings = require('bindings');
const path = require('path');

const moduleRoot = bindings.getRoot(bindings.getFileName());
const buildFolder = path.join(moduleRoot, "build", "Release");
const buildFolder = path.join(moduleRoot, 'build', 'Release');
torch.initenv(`${buildFolder};${process.env.path}`);

module.exports = torch;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arition/torch-js",
"version": "0.12.3",
"version": "0.13.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": "Kittipat Virochsiri, arition, raghavmecheri",
Expand Down Expand Up @@ -36,6 +36,7 @@
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.4",
"jest": "^27.5.1",
"lint-staged": "^12.4.0",
Expand All @@ -53,4 +54,4 @@
3
]
}
}
}
24 changes: 11 additions & 13 deletions tests/rand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
* TODO: Check why a.shape is of type object, instead of type Array
* TODO: Implement toBeInstanceOf checks for data children of objects
*/
import { describe, expect, test } from "@jest/globals";
const torch = require('../dist');

const torch = require("../dist");

describe("Random tensor creation", () => {
test("Random tensor creation using variable number of arguements", () => {
describe('Random tensor creation', () => {
test('Random tensor creation using variable number of arguements', () => {
const a = torch.rand(1, 5).toObject();
expect(a.data.length).toBe(5);
expect(a.shape).toMatchObject([1, 5]);
Expand All @@ -22,7 +20,7 @@ describe("Random tensor creation", () => {
expect(c.shape).toMatchObject([2, 3]);
});

test("Random tensor creation using shape array", () => {
test('Random tensor creation using shape array', () => {
const a = torch.rand([1, 5]).toObject();
expect(a.data.length).toBe(5);
expect(a.shape).toMatchObject([1, 5]);
Expand All @@ -36,7 +34,7 @@ describe("Random tensor creation", () => {
expect(c.shape).toMatchObject([2, 3]);
});

test("Random tensor creation using option parsing", () => {
test('Random tensor creation using option parsing', () => {
const a = torch
.rand([1, 5], {
dtype: torch.float64,
Expand All @@ -46,22 +44,22 @@ describe("Random tensor creation", () => {
expect(a.shape).toMatchObject([1, 5]);
});

test("Random tensor creation using single param", () => {
test('Random tensor creation using single param', () => {
const a = torch.rand(1).toObject();
expect(a.data.length).toBe(1);
expect(a.shape.length).toBe(1);
});

test("Random tensor creation using no params", () => {
test('Random tensor creation using no params', () => {
const a = torch.rand().toObject();
expect(a.data.length).toBe(1);
expect(a.shape.length).toBe(0);
});

test("Random tensor creation using invalid params", () => {
const t = () => torch.rand("Hello World");
test('Random tensor creation using invalid params', () => {
const t = () => torch.rand('Hello World');
const t2 = () => torch.rand(true);
expect(t).toThrow(new Error("A number was expected"));
expect(t2).toThrow(new Error("A number was expected"));
expect(t).toThrow(new Error('A number was expected'));
expect(t2).toThrow(new Error('A number was expected'));
});
});
40 changes: 18 additions & 22 deletions tests/scriptmodule.test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
import { describe, expect, test } from "@jest/globals";

const torch = require("../dist");
const torch = require('../dist');

const testModelPath = `${__dirname}/resources/test_model.pt`;

describe("Constructor", () => {
test("Call constructor using valid model path", () => {
describe('Constructor', () => {
test('Call constructor using valid model path', () => {
const scriptModule = new torch.ScriptModule(testModelPath);
expect(scriptModule.toString()).toMatch(/ScriptModule.*\.pt/);
});

// TODO -- Fine tune error message to remove stacktrace for error thrown on invalid model file
test("Call constructor from invalid model path", () => {
const t = () => new torch.ScriptModule("/resources/no_model.pt");
expect(t).toThrow(
/open file failed because of errno 2 on fopen: No such file or directory, file path: \/resources\/no_model.pt/
);
test('Call constructor from invalid model path', () => {
const t = () => new torch.ScriptModule('/resources/no_model.pt');
expect(t).toThrow(/open file failed because of errno 2 on fopen.*/);
expect(true).toEqual(true);
});

test("Call constructor with missing params", () => {
test('Call constructor with missing params', () => {
const t = () => new torch.ScriptModule();
expect(t).toThrow(new Error("A string was expected"));
expect(t).toThrow(new Error('A string was expected'));
});

test("Call constructor with invalid params", () => {
test('Call constructor with invalid params', () => {
const t = () => new torch.ScriptModule(true);
const t2 = () => new torch.ScriptModule(123);
const t3 = () => new torch.ScriptModule(122.3);
expect(t).toThrow(new Error("A string was expected"));
expect(t2).toThrow(new Error("A string was expected"));
expect(t3).toThrow(new Error("A string was expected"));
expect(t).toThrow(new Error('A string was expected'));
expect(t2).toThrow(new Error('A string was expected'));
expect(t3).toThrow(new Error('A string was expected'));
});
});

describe("toString", () => {
test("Call toString using valid model path", () => {
describe('toString', () => {
test('Call toString using valid model path', () => {
const scriptModule = new torch.ScriptModule(testModelPath);
expect(scriptModule.toString()).toMatch(/ScriptModule.*\.pt/);
});
Expand All @@ -45,14 +41,14 @@ describe("toString", () => {
expect(scriptModule.toString(3)).toMatch(/ScriptModule.*\.pt/);
expect(scriptModule.toString(false)).toMatch(/ScriptModule.*\.pt/);
expect(scriptModule.toString(3.233)).toMatch(/ScriptModule.*\.pt/);
expect(scriptModule.toString(["Hello world"])).toMatch(
expect(scriptModule.toString(['Hello world'])).toMatch(
/ScriptModule.*\.pt/
);
});
});

describe("Forward function", () => {
test("Call to forward using valid tensor params", async () => {
describe('Forward function', () => {
test('Call to forward using valid tensor params', async () => {
const scriptModule = new torch.ScriptModule(testModelPath);
const a = torch.tensor([
[0.1, 0.2, 0.3],
Expand All @@ -68,7 +64,7 @@ describe("Forward function", () => {
});

// TODO -- Fine tune error message to remove stacktrace for missing arguement value -- at the moment, the multiple lines make it difficult to test for an error message
test("Call to forward using missing second tensor param", async () => {
test('Call to forward using missing second tensor param', async () => {
const scriptModule = new torch.ScriptModule(testModelPath);
const a = torch.tensor([
[0.1, 0.2, 0.3],
Expand Down
20 changes: 9 additions & 11 deletions tests/tensor.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { describe, expect, test } from "@jest/globals";
const torch = require('../dist');

const torch = require("../dist");

describe("Tensor creation", () => {
test("Tensor creation using valid array (1-D)", () => {
describe('Tensor creation', () => {
test('Tensor creation using valid array (1-D)', () => {
const a = torch.tensor([[1, 2, 3, 4, 5, 6]]).toObject();
expect(a.data.length).toBe(6);
expect(a.shape).toMatchObject([1, 6]);
Expand All @@ -13,7 +11,7 @@ describe("Tensor creation", () => {
expect(b.shape).toMatchObject([1, 3]);
});

test("Tensor creation using valid array (multi-dimensional)", () => {
test('Tensor creation using valid array (multi-dimensional)', () => {
const a = torch
.tensor([
[0.1, 0.2, 0.3],
Expand All @@ -34,7 +32,7 @@ describe("Tensor creation", () => {
expect(b.shape).toMatchObject([3, 3]);
});

test("Tensor creation using valid object", () => {
test('Tensor creation using valid object', () => {
const a = torch
.tensor(new Float32Array([0.1, 0.2, 0.3, 0.4, 0.5]), {
shape: [1, 5],
Expand All @@ -52,17 +50,17 @@ describe("Tensor creation", () => {
expect(b.shape).toMatchObject([3, 3]);
});

test("Tensor creation using invalid params", () => {
test('Tensor creation using invalid params', () => {
const t = () => torch.tensor();
const t2 = () => torch.tensor(123);
const t3 = () => torch.tensor(true);

expect(t).toThrow("Cannot read properties of undefined (reading 'length')");
expect(t2).toThrow("Invalid argument");
expect(t3).toThrow("Invalid argument");
expect(t2).toThrow('Invalid argument');
expect(t3).toThrow('Invalid argument');
});

test("Tensor clone", () => {
test('Tensor clone', () => {
const t = torch.rand(2, 3);
const tObject = t.toObject();
const tCloneObject = t.clone().toObject();
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,13 @@ eslint-plugin-import@^2.26.0:
resolve "^1.22.0"
tsconfig-paths "^3.14.1"

eslint-plugin-prettier@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-4.0.0.tgz#8b99d1e4b8b24a762472b4567992023619cb98e0"
integrity sha512-98MqmCJ7vJodoQK359bqQWaxOE0CS8paAz/GgjaZLyex4TTk3g9HugoO89EqWCrFiOqn9EVvcoo7gZzONCWVwQ==
dependencies:
prettier-linter-helpers "^1.0.0"

eslint-scope@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c"
Expand Down Expand Up @@ -2080,6 +2087,11 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==

fast-diff@^1.1.2:
version "1.2.0"
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.2.0.tgz#73ee11982d86caaf7959828d519cfe927fac5f03"
integrity sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==

fast-glob@^3.2.9:
version "3.2.11"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.11.tgz#a1172ad95ceb8a16e20caa5c5e56480e5129c1d9"
Expand Down Expand Up @@ -4058,6 +4070,13 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=

prettier-linter-helpers@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/prettier-linter-helpers/-/prettier-linter-helpers-1.0.0.tgz#d23d41fe1375646de2d0104d3454a3008802cf7b"
integrity sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w==
dependencies:
fast-diff "^1.1.2"

prettier@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.6.2.tgz#e26d71a18a74c3d0f0597f55f01fb6c06c206032"
Expand Down

0 comments on commit 211dfd1

Please sign in to comment.