Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore!: move to typescript and vitest #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
/* @flow */

module.exports = {
extends: "./node_modules/@krakenjs/eslint-config-grumbler",
extends:
"./node_modules/@krakenjs/eslint-config-grumbler/eslintrc-typescript",

rules: {
// off for initial ts conversion
// Implicit any in catch clause
"@typescript-eslint/no-implicit-any-catch": "off",
// var will evaluate to '[object Object]' when stringified
"@typescript-eslint/no-base-to-string": "off",
// Prefer using an optional chain expression instead, as it's more concise and easier to read
"@typescript-eslint/prefer-optional-chain": "off",
// Generic Object Injection Sink
"security/detect-object-injection": "off",
},
};
15 changes: 0 additions & 15 deletions .flowconfig

This file was deleted.

5 changes: 1 addition & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ jobs:
- name: 👕 Lint commit messages
uses: wagoid/commitlint-github-action@v4

- name: ▶️ Run flow-typed script
run: npm run flow-typed

- name: ▶️ Run build script
run: npm run build

- name: ⬆️ Upload karma coverage report
- name: ⬆️ Upload coverage report
uses: codecov/codecov-action@v2
4 changes: 4 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
extends: "@krakenjs/babel-config-grumbler/babelrc-browser",
presets: ["@krakenjs/babel-config-grumbler/flow-ts-babel-preset"],
};
3 changes: 0 additions & 3 deletions babel.config.json

This file was deleted.

15 changes: 0 additions & 15 deletions karma.conf.js

This file was deleted.

62 changes: 36 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
"version": "2.0.3",
"description": "Cross domain safe WeakMap.",
"main": "dist/cross-domain-safe-weakmap.js",
"module": "dist/index.js",
"types": "dist/types/index.d.ts",
westeezy marked this conversation as resolved.
Show resolved Hide resolved
"sideEffects": false,
"scripts": {
"setup": "npm install && npm run flow-typed",
"lint": "eslint src/ test/ *.js",
"flow-typed": "rm -rf ./flow-typed && flow-typed install",
"flow": "flow",
"karma": "cross-env NODE_ENV=test babel-node --plugins=transform-es2015-modules-commonjs ./node_modules/.bin/karma start",
"babel": "babel src/ --out-dir dist/module",
"webpack": "babel-node --plugins=transform-es2015-modules-commonjs ./node_modules/.bin/webpack --progress",
"build": "npm run test && npm run babel && npm run webpack && npm run build:types",
"build:flow": "find ./dist -type f -not -path './node_modules/*' -name '*.d.ts' -exec sh -c 'flowgen --add-flow-header $1 -o ${1%.*.*}.js.flow' _ '{}' \\;",
"build:tsc": "tsc src/* --outDir ./dist/esm --declaration --emitDeclarationOnly --strict",
"build:types": "npm run build:tsc && npm run build:flow",
"webpack": "cross-env NODE_ENV=production babel-node --plugins=transform-es2015-modules-commonjs ./node_modules/.bin/webpack --progress",
"babel": "cross-env NODE_ENV=production babel src/ --out-dir ./dist/esm/ --extensions .ts,.tsx",
"tsc": "tsc",
"format": "prettier --write --ignore-unknown .",
"format:check": "prettier --check .",
"test": "npm run format:check && npm run lint && npm run flow-typed && npm run flow && npm run karma",
"build": "npm run test && npm run babel && npm run webpack",
"test": "npm run format:check && npm run lint && npm run tsc --no-emit && npm run vitest",
"lint": "eslint --ext ts,tsx,js,jsx src/ test/",
"clean": "rimraf dist coverage",
"prerelease": "npm run clean && npm run build && git add dist && git commit -m 'ci: check in dist folder' || echo 'Nothing to distribute'",
"release": "standard-version",
"postrelease": "git push && git push --follow-tags && npm publish",
"clean": "rimraf dist coverage",
"reinstall": "rimraf flow-typed && rimraf node_modules && npm install && flow-typed install",
"debug": "cross-env NODE_ENV=debug",
"prepare": "husky install"
"prepare": "husky install",
"vitest": "vitest run --dom --coverage",
"vitest:watch": "vitest watch --dom --coverage --ui"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -72,26 +76,32 @@
],
"license": "Apache-2.0",
"files": [
"dist/",
"src/",
"postinstall.js"
"dist/"
],
"readmeFilename": "README.md",
"devDependencies": {
"@commitlint/cli": "^16.2.1",
"@commitlint/config-conventional": "^16.2.1",
"@krakenjs/grumbler-scripts": "^8.0.5",
"@commitlint/cli": "^17.3.0",
"@commitlint/config-conventional": "^17.3.0",
"@krakenjs/babel-config-grumbler": "^8.1.0",
"@krakenjs/eslint-config-grumbler": "^8.1.0",
"@krakenjs/typescript-config-grumbler": "^8.1.0",
"@krakenjs/webpack-config-grumbler": "8.1.0",
"@vitest/coverage-c8": "^0.25.3",
"@vitest/ui": "^0.25.3",
"cross-env": "^7.0.3",
"flow-bin": "0.155.0",
"flow-typed": "^3.8.0",
"husky": "^7.0.0",
"lint-staged": "^12.4.0",
"mocha": "^4",
"prettier": "^2.6.2",
"standard-version": "^9.3.2"
"flowgen": "^1.20.1",
"happy-dom": "^7.7.0",
"husky": "^8.0.2",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
"standard-version": "^9.5.0",
"ts-node": "^10.9.1",
"typescript": "4.9.3",
"vite": "^3.2.4",
"vitest": "^0.25.3"
},
"dependencies": {
"@krakenjs/cross-domain-utils": "^3.0.2"
"@krakenjs/cross-domain-utils": "^4.0.0-typescript.1"
},
"lint-staged": {
"*": "prettier --write --ignore-unknown"
Expand Down
2 changes: 0 additions & 2 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
/* @flow */

export { CrossDomainSafeWeakMap as WeakMap } from "./weakmap";
4 changes: 0 additions & 4 deletions src/native.js → src/native.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* @flow */

export function hasNativeWeakMap(): boolean {
if (typeof WeakMap === "undefined") {
return false;
Expand All @@ -13,9 +11,7 @@ export function hasNativeWeakMap(): boolean {
const testWeakMap = new WeakMap();
const testKey = {};
const testValue = "__testvalue__";

Object.freeze(testKey);

testWeakMap.set(testKey, testValue);

if (testWeakMap.get(testKey) === testValue) {
Expand Down
20 changes: 0 additions & 20 deletions src/util.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function safeIndexOf<T>(collection: readonly T[], item: T): number {
for (let i = 0; i < collection.length; i++) {
try {
if (collection[i] === item) {
return i;
}
} catch (err) {
// pass
}
}

return -1;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function noop(...args: readonly unknown[]): void {
// pass
}
40 changes: 23 additions & 17 deletions src/weakmap.js → src/weakmap.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
/* @flow */

import { isWindow, isWindowClosed } from "@krakenjs/cross-domain-utils/src";
import {
isWindow,
isWindowClosed,
} from "@krakenjs/cross-domain-utils/dist/esm";

import { hasNativeWeakMap } from "./native";
import { noop, safeIndexOf } from "./util";

export class CrossDomainSafeWeakMap<K: Object, V: mixed> {
export class CrossDomainSafeWeakMap<K extends Record<string, unknown>, V> {
name: string;
weakmap: ?WeakMap<K, V>;
// eslint-disable-next-line flowtype/no-mutable-array
keys: Array<K>;
// eslint-disable-next-line flowtype/no-mutable-array
values: Array<V>;
weakmap: WeakMap<K, V> | undefined;
keys: K[];
values: V[];

constructor() {
// eslint-disable-next-line no-bitwise
this.name = `__weakmap_${(Math.random() * 1e9) >>> 0}__`;

if (hasNativeWeakMap()) {
try {
this.weakmap = new WeakMap();
this.weakmap = new WeakMap<K, V>();
} catch (err) {
// pass
}
Expand All @@ -29,13 +28,14 @@ export class CrossDomainSafeWeakMap<K: Object, V: mixed> {
this.values = [];
}

_cleanupClosedWindows() {
_cleanupClosedWindows(): void {
const weakmap = this.weakmap;
const keys = this.keys;

for (let i = 0; i < keys.length; i++) {
const value = keys[i];

// @ts-expect-error `isWindowClosed` takes window which V is not
if (isWindow(value) && isWindowClosed(value)) {
if (weakmap) {
try {
Expand All @@ -47,7 +47,6 @@ export class CrossDomainSafeWeakMap<K: Object, V: mixed> {

keys.splice(i, 1);
this.values.splice(i, 1);

i -= 1;
}
}
Expand All @@ -68,7 +67,7 @@ export class CrossDomainSafeWeakMap<K: Object, V: mixed> {
return true;
}

set(key: K, value: V) {
set(key: K, value: V): void {
if (!key) {
throw new Error(`WeakMap expected key`);
}
Expand All @@ -88,7 +87,9 @@ export class CrossDomainSafeWeakMap<K: Object, V: mixed> {
const name = this.name;
const entry = key[name];

// @ts-expect-error entry is implicitly an any so cant index
if (entry && entry[0] === key) {
// @ts-expect-error entry is implicitly an any so cant index
entry[1] = value;
} else {
Object.defineProperty(key, name, {
Expand Down Expand Up @@ -117,7 +118,7 @@ export class CrossDomainSafeWeakMap<K: Object, V: mixed> {
}
}

get(key: K): V | void {
get(key: K): V | undefined {
if (!key) {
throw new Error(`WeakMap expected key`);
}
Expand All @@ -138,8 +139,10 @@ export class CrossDomainSafeWeakMap<K: Object, V: mixed> {
try {
const entry = key[this.name];

// @ts-expect-error entry is implicitly an any so cant index
if (entry && entry[0] === key) {
return entry[1];
// @ts-expect-error entry is implicitly an any so cant index
return entry[1] as V;
}

return;
Expand All @@ -160,7 +163,7 @@ export class CrossDomainSafeWeakMap<K: Object, V: mixed> {
return this.values[index];
}

delete(key: K) {
delete(key: K): void {
if (!key) {
throw new Error(`WeakMap expected key`);
}
Expand All @@ -179,7 +182,9 @@ export class CrossDomainSafeWeakMap<K: Object, V: mixed> {
try {
const entry = key[this.name];

// @ts-expect-error entry is implicitly an any so cant index
if (entry && entry[0] === key) {
// @ts-expect-error entry is implicitly an any so cant index
entry[0] = entry[1] = undefined;
}
} catch (err) {
Expand Down Expand Up @@ -219,6 +224,7 @@ export class CrossDomainSafeWeakMap<K: Object, V: mixed> {
try {
const entry = key[this.name];

// @ts-expect-error entry is implicitly an any so cant index
if (entry && entry[0] === key) {
return true;
}
Expand All @@ -237,7 +243,7 @@ export class CrossDomainSafeWeakMap<K: Object, V: mixed> {

getOrSet(key: K, getter: () => V): V {
if (this.has(key)) {
// $FlowFixMe
// @ts-expect-error v | undefined is not assignable to v. need a guard check
return this.get(key);
}

Expand Down
5 changes: 0 additions & 5 deletions test/index.js

This file was deleted.

3 changes: 0 additions & 3 deletions test/tests/index.js

This file was deleted.

11 changes: 0 additions & 11 deletions test/util.js

This file was deleted.

Loading