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

fix: deep copy standardRules config #316

Merged
merged 5 commits into from
May 25, 2020
Merged
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
18 changes: 18 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava'
import exported from '.'
import { rules as standardRules } from 'eslint-config-standard/eslintrc.json'
import standardPkg from 'eslint-config-standard/package.json'
import readPkgUp, { NormalizedPackageJson } from 'read-pkg-up'

Expand Down Expand Up @@ -192,3 +193,20 @@ test('Deps parser and plugin are same version', async (t) => {
const pluginRange = ourPeerDeps['@typescript-eslint/eslint-plugin']
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

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

t.not(overrideRulesConfig[`@typescript-eslint/${ruleName}`], standardRulesConfig[ruleName])
}
}
})
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ 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))
}

function fromEntries<T> (iterable: Array<[string, T]>): { [key: string]: T } {
return [...iterable].reduce<{ [key: string]: T }>((obj, [key, val]) => {
obj[key] = val
Expand All @@ -44,7 +48,7 @@ export = {
'no-use-before-define': 'off',

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