Skip to content

Commit

Permalink
test: add linting rules and lint project
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Oct 4, 2019
1 parent b3eed9a commit 32a652c
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 51 deletions.
19 changes: 12 additions & 7 deletions .eslintrc
@@ -1,9 +1,14 @@
{
"extends": "standard",
"plugins": [
"jest"
],
"env": {
"jest/globals": true
}
"extends": ["plugin:@typescript-eslint/recommended"],
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/explicit-function-return-type": 0
},
"parserOptions": {
"parser": "@typescript-eslint/parser",
"tsconfigRootDir": "./src",
"ecmaVersion": 11,
"sourceType": "module"
}
}
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -15,21 +15,24 @@
],
"main": "lib/module.js",
"scripts": {
"lint": "eslint --ext .js lib",
"lint": "eslint --ext .js,.ts src",
"build": "tsc",
"prepublish": "yarn build",
"release": "yarn test && standard-version && git push --follow-tags && npm publish",
"generate": "yarn build && yarn nuxt generate test/fixture",
"test": "yarn generate && jest"
"test": "yarn lint && yarn generate && jest"
},
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.6.2",
"@nuxtjs/eslint-config": "^1.1.2",
"@babel/preset-typescript": "^7.6.0",
"@nuxt/typescript-build": "^0.3.0",
"@nuxt/typescript-runtime": "^0.2.0",
"@types/jest": "^24.0.18",
"@typescript-eslint/eslint-plugin": "^2.3.1",
"@typescript-eslint/parser": "^2.3.1",
"babel-eslint": "^10.0.3",
"babel-jest": "^24.9.0",
"codecov": "^3.5.0",
Expand Down
17 changes: 10 additions & 7 deletions src/module.ts
@@ -1,23 +1,26 @@
import { Configuration } from '@nuxt/types'
import path from 'path'
import { Configuration } from '@nuxt/types'

export default async function nuxtTypedVuex (this: {
options: Configuration
addPlugin: (options: any) => void
export default async function nuxtTypedVuex(this: {
options: Configuration;
addPlugin: (options: any) => void;
}) {
if (!this.options.store) console.warn('You do not have a Nuxt store defined.')
if (!this.options.store) {
console.warn('You do not have a Nuxt store defined.')
}
const { buildDir } = this.options
const libDir = __dirname
this.addPlugin({
src: path.resolve(__dirname, './plugin.js'),
fileName: 'nuxt-typed-vuex.js',
options: {
libDir,
buildDir
}
buildDir,
},
})
}

// eslint-disable-next-line
export const meta = require('../package.json')

export * from './utils'
56 changes: 28 additions & 28 deletions src/utils.ts
Expand Up @@ -20,18 +20,18 @@ type ModuleTransformer<T> = T extends NuxtModules
: {}

type BlankStore = {
getters: {}
mutations: {}
actions: {}
modules: {}
getters: {};
mutations: {};
actions: {};
modules: {};
}

type NuxtStore = {
state: () => unknown
getters: Record<string, any>
mutations: Record<string, any>
actions: Record<string, any>
modules: NuxtModules
state: () => unknown;
getters: Record<string, any>;
mutations: Record<string, any>;
actions: Record<string, any>;
modules: NuxtModules;
}
type NuxtModules = Record<string, NuxtStore>

Expand All @@ -42,11 +42,11 @@ type NuxtStoreInput<
A,
S extends { [key: string]: NuxtStore }
> = {
state: T
getters?: G
mutations?: M
actions?: A
modules?: S
state: T;
getters?: G;
mutations?: M;
actions?: A;
modules?: S;
}

type MergedStoreType<T extends NuxtStore> = ReturnType<T['state']> &
Expand All @@ -68,10 +68,10 @@ type FunctionProcessor<M extends Record<string, () => any>> = <

export type StoreType<T extends Required<NuxtStore>> = {
state: ReturnType<T['state']> &
{ [P in keyof T['modules']]: ReturnType<T['modules'][P]['state']> }
getters: { [P in keyof T['getters']]: ReturnType<T['getters'][P]> }
commit: FunctionProcessor<T['mutations']>
dispatch: FunctionProcessor<T['actions']>
{ [P in keyof T['modules']]: ReturnType<T['modules'][P]['state']> };
getters: { [P in keyof T['getters']]: ReturnType<T['getters'][P]> };
commit: FunctionProcessor<T['mutations']>;
dispatch: FunctionProcessor<T['actions']>;
}

export const getStoreType = <
Expand All @@ -81,8 +81,8 @@ export const getStoreType = <
A,
S extends NuxtModules
>(
store: NuxtStoreInput<T, G, M, A, S>
) => {
store: NuxtStoreInput<T, G, M, A, S>
) => {
return {} as StoreType<typeof store & BlankStore> &
Omit<Store<ReturnType<T>>, 'getters' | 'dispatch' | 'commit'>
}
Expand All @@ -94,8 +94,8 @@ export const getAccessorType = <
A extends ActionTree<ReturnType<T>, ReturnType<T>>,
S extends NuxtModules
>(
store: NuxtStoreInput<T, G, M, A, S>
) => {
store: NuxtStoreInput<T, G, M, A, S>
) => {
return {} as MergedStoreType<typeof store & BlankStore>
}

Expand All @@ -108,18 +108,18 @@ const createAccessor = <T extends () => any, G, M, A, S extends NuxtModules>(
const accessor: Record<string, any> = {}
Object.keys(getters || {}).forEach(getter => {
Object.defineProperty(accessor, getter, {
get: () => store.getters[`${namespacedPath}${getter}`]
get: () => store.getters[`${namespacedPath}${getter}`],
})
})
Object.keys(state ? state() : {}).forEach(prop => {
if (!Object.getOwnPropertyNames(accessor).includes(prop)) {
if (namespace) {
Object.defineProperty(accessor, prop, {
get: () => (store.state as any)[namespace][prop]
get: () => (store.state as any)[namespace][prop],
})
} else {
Object.defineProperty(accessor, prop, {
get: () => (store.state as any)[prop]
get: () => (store.state as any)[prop],
})
}
}
Expand All @@ -142,9 +142,9 @@ export const useAccessor = <
A extends ActionTree<ReturnType<T>, ReturnType<T>>,
S extends NuxtModules
>(
store: Store<ReturnType<T>>,
input: Required<NuxtStoreInput<T, G, M, A, S>>
) => {
store: Store<ReturnType<T>>,
input: Required<NuxtStoreInput<T, G, M, A, S>>
) => {
const accessor = createAccessor(store, input)
Object.keys(input.modules || {}).forEach(namespace => {
accessor[namespace] = createAccessor(
Expand Down

0 comments on commit 32a652c

Please sign in to comment.