Skip to content

Commit

Permalink
add eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Aug 14, 2023
1 parent cb8c1a2 commit e39c14e
Show file tree
Hide file tree
Showing 17 changed files with 456 additions and 131 deletions.
351 changes: 272 additions & 79 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/util-address/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"@onflow/types": "^1.2.0-alpha.0",
"@types/jest": "^29.5.3",
"@types/node": "^18.13.0",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"eslint": "^8.46.0",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"eslint": "^8.47.0",
"eslint-plugin-jsdoc": "^46.4.6",
"jest": "^29.5.0",
"typescript": "^4.9.5"
Expand Down
20 changes: 20 additions & 0 deletions packages/util-encode-key/.eslintrc copy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"env": {
"browser": true,
"es2021": true,
"jest": true,
"node": true
},
"extends": [
"plugin:jsdoc/recommended-typescript",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": ["jsdoc", "@typescript-eslint"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"ignorePatterns": ["**/dist/"]
}
8 changes: 4 additions & 4 deletions packages/util-encode-key/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"@onflow/types": "^1.2.0-alpha.0",
"@types/jest": "^29.5.3",
"@types/node": "^18.13.0",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"eslint": "^8.46.0",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"eslint": "^8.47.0",
"eslint-plugin-jsdoc": "^46.4.6",
"jest": "^29.5.0",
"typescript": "^4.9.5"
Expand All @@ -36,7 +36,7 @@
"build": "npm run lint && fcl-bundle",
"test:watch": "jest --watch",
"start": "fcl-bundle --watch",
"lint": "eslint src/*.ts"
"lint": "eslint ."
},
"dependencies": {
"@babel/runtime": "^7.18.6",
Expand Down
20 changes: 20 additions & 0 deletions packages/util-invariant/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"env": {
"browser": true,
"es2021": true,
"jest": true,
"node": true
},
"extends": [
"plugin:jsdoc/recommended-typescript",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": ["jsdoc", "@typescript-eslint"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"ignorePatterns": ["**/dist/"]
}
6 changes: 6 additions & 0 deletions packages/util-invariant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
"url": "https://github.com/onflow/flow-js-sdk/issues"
},
"devDependencies": {
"@babel/preset-typescript": "^7.22.5",
"@onflow/fcl-bundle": "^1.4.0-alpha.0",
"@onflow/types": "^1.2.0-alpha.0",
"@types/jest": "^29.5.3",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"eslint": "^8.47.0",
"eslint-plugin-jsdoc": "^46.4.6",
"jest": "^29.5.0"
},
"source": "src/index.ts",
Expand Down
20 changes: 20 additions & 0 deletions packages/util-logger/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"env": {
"browser": true,
"es2021": true,
"jest": true,
"node": true
},
"extends": [
"plugin:jsdoc/recommended-typescript",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": ["jsdoc", "@typescript-eslint"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"ignorePatterns": ["**/dist/"]
}
6 changes: 6 additions & 0 deletions packages/util-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
"url": "https://github.com/onflow/flow-js-sdk/issues"
},
"devDependencies": {
"@babel/preset-typescript": "^7.22.5",
"@onflow/fcl-bundle": "^1.4.0-alpha.0",
"@types/jest": "^29.5.3",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"eslint": "^8.47.0",
"eslint-plugin-jsdoc": "^46.4.6",
"jest": "^29.5.0"
},
"source": "src/util-logger.ts",
Expand Down
21 changes: 9 additions & 12 deletions packages/util-logger/src/util-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {config} from "@onflow/config"

/**
* The levels of the logger
* @enum
*/
export enum LEVELS {
debug = 5,
Expand All @@ -14,12 +13,10 @@ export enum LEVELS {

/**
* Builds a message formatted for the logger
*
* @param options
* @param options - The options for the log
* @param options.title - The title of the log
* @param options.message - The message of the log
* @returns The message formatted for the logger
*
* @example
* buildLoggerMessageArgs({ title: "My Title", message: "My Message" })
*/
Expand All @@ -45,16 +42,13 @@ const buildLoggerMessageArgs = (options: {

/**
* Logs messages based on the level of the message and the level set in the config
*
* @param options - The options for the log
* @param options.title - The title of the log
* @param options.message - The message of the log
* @param options.level - The level of the log
* @param options.always - Whether to always show the log
*
* @example
* log({ title: "My Title", message: "My Message", level: LEVELS.warn, always: false })
*
*/
export const log = async (options: {
title: string
Expand Down Expand Up @@ -89,19 +83,22 @@ export const log = async (options: {
}

/**
* Logs a deprecation notice
*
* Logs a deprecation notice. If a callback is provided this function returns a function that will call the callback and log the deprecation notice, otherwise it just logs the deprecation notice.
* @param options - The options for the log
* @param options.pkg - The package that is being deprecated
* @param options.subject - The subject of the deprecation
* @param options.transition - The transition path for the deprecation
* @param options.level - The level of the log
* @param options.message - The message of the log
* @param options.callback - A callback to run after the log
*
* @returns A function that will call the callback and log the deprecation notice if the callback is provided
* @example
* // Logs a deprecation notice
* log.deprecate({ pkg: "@onflow/fcl", subject: "Some item", transition: "https://github.com/onflow/flow-js-sdk", message: "Descriptive message", level: LEVELS.warn, callback: () => {} })
*
* @example
* function someFunction() { ... }
* const deprecatedFunction = log.deprecate({ pkg: "@onflow/fcl", subject: "Some item", transition: "https://github.com/foo/bar/TRANSITIONS.md", message: "Descriptive message", level: LEVELS.warn, callback: someFunction })
* deprecatedFunction() // Calls someFunction and logs the deprecation notice
*/
log.deprecate = <T, U>(options: {
pkg?: string
Expand All @@ -110,7 +107,7 @@ log.deprecate = <T, U>(options: {
level?: number
message?: string
callback?: (...args: T[]) => U
}) => {
}): ((...args: T[]) => Promise<U>) | Promise<void> => {
const {
pkg,
subject,
Expand Down
20 changes: 20 additions & 0 deletions packages/util-template/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"env": {
"browser": true,
"es2021": true,
"jest": true,
"node": true
},
"extends": [
"plugin:jsdoc/recommended-typescript",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": ["jsdoc", "@typescript-eslint"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"ignorePatterns": ["**/dist/"]
}
6 changes: 3 additions & 3 deletions packages/util-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"@babel/preset-typescript": "^7.22.5",
"@onflow/fcl-bundle": "^1.4.0-alpha.0",
"@types/jest": "^29.5.3",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"eslint": "^8.46.0",
"@typescript-eslint/eslint-plugin": "^6.4.0",
"@typescript-eslint/parser": "^6.4.0",
"eslint": "^8.47.0",
"eslint-plugin-jsdoc": "^46.4.6",
"jest": "^29.5.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports[`template interop function template(template\`\${o=>o.a}\`)(o) -> 'abc'

exports[`template interop function template\`\${o=>o.a}\`(o) -> 'abc' 1`] = `"abc"`;

exports[`template interop more template template(template\`x\${template\`y\${o => o.a}\`}\`)(o) -> 'xyabc' 1`] = `"xyabc"`;
exports[`template interop more template template(template\`x\${template\`y\${(_o: typeof o) => _o.a}\`}\`)(o) -> 'xyabc' 1`] = `"xyabc"`;
exports[`template interop more template template\`x\${template\`y\${o=>o.a}\`}\`(o) => 'xyabc' 1`] = `"xyabc"`;
Expand Down
25 changes: 13 additions & 12 deletions packages/util-template/src/template.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//@ts-nocheck
import {template, interleave} from "./template"

describe("interleave", () => {
Expand All @@ -24,14 +23,14 @@ describe("interleave", () => {
})
})

const _ = (msg, a, b?) => {
const _ = (msg: string, a: unknown, b?: unknown) => {
if (b == null) {
test(msg, () => expect(a).toMatchSnapshot())
} else {
test(msg, () => expect(a).toBe(b))
}
}
const t = v => typeof v
const t = <T>(v: T) => typeof v

describe("template", () => {
describe("input type vs output type", () => {
Expand All @@ -53,31 +52,31 @@ describe("template", () => {

describe("interop function", () => {
const o = {a: "abc"}
_("template`${o=>o.a}`(o) -> 'abc'", template`${o => o.a}`(o))
_("template`${o=>o.a}`(o) -> 'abc'", template`${(_o: typeof o) => _o.a}`(o))
_(
"template(template`${o=>o.a}`)(o) -> 'abc'",
template(template`${o => o.a}`)(o)
template(template`${(_o: typeof o) => _o.a}`)(o)
)
})

describe("interop more template", () => {
const o = {a: "abc"}
_(
"template`x${template`y${o=>o.a}`}`(o) => 'xyabc'",
template`x${template`y${o => o.a}`}`(o)
template`x${template`y${(_o: typeof o) => _o.a}`}`(o)
)
_(
"template`x${template`y${template`z${o=>o.a}`}`}`(o) => 'xyabc'",
template`x${template`y${template`z${o => o.a}`}`}`(o)
template`x${template`y${template`z${(_o: typeof o) => _o.a}`}`}`(o)
)
_(
"template(template`x${template`y${o => o.a}`}`)(o) -> 'xyabc'",
template(template`x${template`y${o => o.a}`}`)(o)
"template(template`x${template`y${(_o: typeof o) => _o.a}`}`)(o) -> 'xyabc'",
template(template`x${template`y${(_o: typeof o) => _o.a}`}`)(o)
)
})

describe("interop nested functions", () => {
const fn = a => b => c => d => e => f => f.a
const fn = () => () => () => () => () => (f: typeof o) => f.a
const o = {a: "abc"}
_("template`${fn}`(o) -> 'abc'", template`${fn}`(o))
})
Expand All @@ -89,8 +88,10 @@ describe("template", () => {

describe("object can have non string values", () => {
const data = {a: 1, b: NaN, c: undefined, d: null, e: false, f: true}
const tx = template`a:${o => o.a}|b:${o => o.b}|c:${o => o.c}|d:${o =>
o.d}|e:${o => o.e}|f:${o => o.f}`
const tx = template`a:${(o: typeof data) => o.a}|b:${(o: typeof data) =>
o.b}|c:${(o: typeof data) => o.c}|d:${(o: typeof data) => o.d}|e:${(
o: typeof data
) => o.e}|f:${(o: typeof data) => o.f}`
_("template(data)", tx(data), "a:1|b:NaN|c:undefined|d:null|e:false|f:true")
})
})
41 changes: 31 additions & 10 deletions packages/util-template/src/template.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import {log} from "@onflow/util-logger"

export function interleave(
a: unknown[] = [],
b: unknown[] = [],
c: unknown[] = []
): unknown[] {
/**
* Interleaves two arrays
* @param a - The first array
* @param b - The second array
* @param c - The target array
* @returns The interleaved array
*/
export function interleave<A, B>(
a: A[] = [],
b: B[] = [],
c: (A | B)[] = []
): (A | B)[] {
if (!a.length && !b.length) return c
if (!a.length) return c
if (!b.length) return [...c, a[0]]
if (!b.length) {
c.push(...a)
return c
}

const [aHead, ...aRest] = a
const [bHead, ...bRest] = b
Expand All @@ -18,7 +28,12 @@ export function interleave(
return interleave(aRest, bRest, c)
}

function recApply(d: unknown): (x: unknown) => string {
/**
* Recursively apply a value to a function
* @param d - The value to apply
* @returns A function that takes a function and applies the value to it
*/
function recApply<T, U>(d: T): (x: U) => string {
return function (arg1) {
if (typeof arg1 === "function") {
log.deprecate({
Expand All @@ -33,14 +48,20 @@ function recApply(d: unknown): (x: unknown) => string {
}
}

/**
* Creates a template function
* @param head - A string, template string array, or template function
* @param rest - The rest of the arguments
* @returns A template function
*/
export function template(
head: string | TemplateStringsArray | ((x: unknown) => string),
head: string | TemplateStringsArray | ((x?: unknown) => string),
...rest: unknown[]
): (x?: unknown) => string {
if (typeof head === "string") return () => head
if (typeof head === "function") return head
return d =>
interleave([...head], rest.map(recApply(d)))
return (x: unknown) =>
interleave([...head], rest.map(recApply(x)))
.join("")
.trim()
}
Loading

0 comments on commit e39c14e

Please sign in to comment.