Skip to content

Commit

Permalink
feat: export is of type ESLint.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
mightyiam committed May 25, 2020
1 parent 712c2f7 commit 1d61c3d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"devDependencies": {
"@commitlint/cli": "8.3.5",
"@commitlint/travis-cli": "8.3.5",
"@types/eslint": "6.8.1",
"@types/node": "14.0.5",
"@typescript-eslint/eslint-plugin": "2.33.0",
"ava": "3.8.2",
Expand Down
6 changes: 6 additions & 0 deletions src/eslint-config-standard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import config from 'eslint-config-standard/eslintrc.json'
import { Linter } from 'eslint'

const casted = config as unknown as Linter.Config

export default casted
25 changes: 11 additions & 14 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import test from 'ava'
import exported from '.'
import { rules as standardRules } from 'eslint-config-standard/eslintrc.json'
import configStandard from './eslint-config-standard'
import standardPkg from 'eslint-config-standard/package.json'
import readPkgUp, { NormalizedPackageJson } from 'read-pkg-up'
import { Linter } from 'eslint'

interface OurDeps {
ourDeps: NonNullable<NormalizedPackageJson['dependencies']>
Expand All @@ -24,7 +25,7 @@ const getOurDeps = async (): Promise<OurDeps> => {
}

test('export', (t): void => {
const expected = {
const expected: Linter.Config = {
extends: 'eslint-config-standard',
plugins: ['@typescript-eslint'],
overrides: [
Expand Down Expand Up @@ -194,19 +195,15 @@ test('Deps parser and plugin are same version', async (t) => {
t.is(parserRange.split('^')[1], pluginRange.split('>=')[1])
})

interface RulesConfig {
[name: string]: object | string | number | boolean | null
}

test('No references to standard rules config', (t) => {
for (const override of exported.overrides) {
const standardRulesConfig: RulesConfig = standardRules
const overrideRulesConfig: RulesConfig = override.rules
test('Exported rule values do not reference eslint-config-standard ones', (t) => {
if (exported.overrides === undefined) throw new Error()
t.is(exported.overrides.length, 1)
const override = exported.overrides[0]
if (override.rules === undefined) throw new Error()

for (const ruleName in standardRulesConfig) {
if (typeof standardRulesConfig[ruleName] !== 'object') continue // Non-objects use copy-by-value
for (const ruleName in configStandard.rules) {
if (typeof configStandard.rules[ruleName] !== 'object') continue // Non-objects use copy-by-value

t.not(overrideRulesConfig[`@typescript-eslint/${ruleName}`], standardRulesConfig[ruleName])
}
t.false(override.rules[`@typescript-eslint/${ruleName}`] === configStandard.rules[ruleName])
}
})
17 changes: 12 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { rules as standardRules } from 'eslint-config-standard/eslintrc.json'
import configStandard from './eslint-config-standard'
import { Linter } from 'eslint'

const equivalents = [
'comma-spacing',
Expand All @@ -19,8 +20,12 @@ const equivalents = [
'space-before-function-paren'
] as const

function clone<T extends object | string | number | boolean | null> (value: T): T {
return JSON.parse(JSON.stringify(value))
const ruleFromStandard = (name: string): Linter.RuleEntry => {
if (configStandard.rules === undefined) throw new Error()
const rule = configStandard.rules[name]
if (rule === undefined) throw new Error()
if (typeof rule !== 'object') return rule
return JSON.parse(JSON.stringify(rule))
}

function fromEntries<T> (iterable: Array<[string, T]>): { [key: string]: T } {
Expand All @@ -30,7 +35,7 @@ function fromEntries<T> (iterable: Array<[string, T]>): { [key: string]: T } {
}, {})
}

export = {
const config: Linter.Config = {
extends: 'eslint-config-standard',
plugins: ['@typescript-eslint'],
overrides: [
Expand All @@ -48,7 +53,7 @@ export = {
'no-use-before-define': 'off',

// @typescript-eslint versions of Standard.js rules:
...fromEntries(equivalents.map((name) => [`@typescript-eslint/${name}`, clone(standardRules[name])])),
...fromEntries(equivalents.map((name) => [`@typescript-eslint/${name}`, ruleFromStandard(name)])),
'@typescript-eslint/no-use-before-define': ['error', {
functions: false,
classes: false,
Expand Down Expand Up @@ -121,3 +126,5 @@ export = {
}
]
}

export = config

0 comments on commit 1d61c3d

Please sign in to comment.