Skip to content

Commit

Permalink
Change config merge behaviour (#189)
Browse files Browse the repository at this point in the history
* Merge rule configurations from right to left

Similar to rules themselves, the rule configurations are now merged from right to left so that options can be partially owerwritten.

Resolves #188

* Add changeset

* Use strict typing
  • Loading branch information
christianklotz authored Jun 22, 2021
1 parent db0e303 commit d034e3b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/slow-buckets-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sketch-hq/sketch-assistant-utils': minor
---

Change rule config merge behaviour. Instead of overwriting the entire rule config, options are
overwritten individually.
31 changes: 31 additions & 0 deletions packages/utils/src/assistant/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,37 @@ describe('assign', () => {
`)
})

test('rule config is merged right to left', () => {
expect(
assign(
createAssistantDefinition({
config: {
rules: {
foo: { active: true, foo: 1, bar: 'hello world!' },
},
},
}),
createAssistantDefinition({
config: {
rules: {
foo: { active: true, bar: 'hello everyone!' },
},
},
}),
).config,
).toMatchInlineSnapshot(`
Object {
"rules": Object {
"foo": Object {
"active": true,
"bar": "hello everyone!",
"foo": 1,
},
},
}
`)
})

test('rules are concatenated', () => {
expect(
assign(
Expand Down
19 changes: 15 additions & 4 deletions packages/utils/src/assistant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
AssistantEnv,
Maybe,
RuleDefinition,
ReservedRuleOptionNames,
RuleConfigGroup,
} from '@sketch-hq/sketch-assistant-types'

/**
Expand Down Expand Up @@ -54,10 +56,19 @@ const assign = (...sources: AssistantDefinition[]): AssistantDefinition => {
...(typeof acc.config.defaultSeverity === 'undefined'
? {}
: { defaultSeverity: acc.config.defaultSeverity }),
rules: {
...curr.config.rules,
...acc.config.rules,
},
rules: Object.entries(curr.config.rules).reduceRight((a: RuleConfigGroup, [name, opts]) => {
return {
...a,
[name]: {
...opts,
...a[name],
active:
a[name]?.[ReservedRuleOptionNames.active] ??
opts?.[ReservedRuleOptionNames.active] ??
false,
},
}
}, acc.config.rules),
},
rules: [...curr.rules, ...acc.rules],
}
Expand Down

0 comments on commit d034e3b

Please sign in to comment.