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 3 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
32 changes: 31 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava'
import test, { ExecutionContext } 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,32 @@ 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 that objects' values do not hold same memory references
*/
function testNotHaveObjectReferences (t: ExecutionContext<unknown>, rules: RulesConfig, comparisonRules: RulesConfig, skipSameKey: boolean = false): void {
for (const ruleName in rules) {
const ruleConfig = rules[ruleName]
if (typeof ruleConfig !== 'object') continue // Non-objects use copy-by-value

for (const comparisonRuleName in comparisonRules) {
if (skipSameKey && ruleName === comparisonRuleName) continue
t.not(ruleConfig, comparisonRules[comparisonRuleName])
}
Patrick-Remy marked this conversation as resolved.
Show resolved Hide resolved
}
}

test('No references in rules config', (t) => {
for (const override of exported.overrides) {
const rules: RulesConfig = override.rules
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this type, then, I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required due to Element implicitly has an 'any' type because expression of type 'string' can't be used to index type if use standardRules[ruleName].

// Rules must not have object references inside theirselves
testNotHaveObjectReferences(t, rules, rules, true)
Patrick-Remy marked this conversation as resolved.
Show resolved Hide resolved
// Rules must not have object references to standard rules
testNotHaveObjectReferences(t, rules, standardRules)
Patrick-Remy marked this conversation as resolved.
Show resolved Hide resolved
}
})
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