Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
overrides
option to config (#5521)
- Loading branch information
1 parent
cad503b
commit 725edf5
Showing
20 changed files
with
755 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,303 @@ | ||
'use strict'; | ||
|
||
const path = require('path'); | ||
const { applyOverrides } = require('../augmentConfig'); | ||
|
||
test('no overrides', () => { | ||
const config = { | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
}; | ||
|
||
const applied = applyOverrides(config, __dirname, path.join(__dirname, 'style.css')); | ||
|
||
expect(applied).toEqual(config); | ||
}); | ||
|
||
describe('single matching override', () => { | ||
test('simple override', () => { | ||
const config = { | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
overrides: [ | ||
{ | ||
files: ['*.module.css'], | ||
rules: { | ||
'color-no-hex': true, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const expectedConfig = { | ||
rules: { | ||
'block-no-empty': true, | ||
'color-no-hex': true, | ||
}, | ||
}; | ||
|
||
const applied = applyOverrides(config, __dirname, path.join(__dirname, 'style.module.css')); | ||
|
||
expect(applied).toEqual(expectedConfig); | ||
}); | ||
|
||
test('simple override, files is a string', () => { | ||
const config = { | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
overrides: [ | ||
{ | ||
files: '*.module.css', | ||
rules: { | ||
'color-no-hex': true, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const expectedConfig = { | ||
rules: { | ||
'block-no-empty': true, | ||
'color-no-hex': true, | ||
}, | ||
}; | ||
|
||
const applied = applyOverrides(config, __dirname, path.join(__dirname, 'style.module.css')); | ||
|
||
expect(applied).toEqual(expectedConfig); | ||
}); | ||
|
||
test('with plugins', () => { | ||
const config = { | ||
plugins: ['stylelint-plugin'], | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
overrides: [ | ||
{ | ||
files: ['*.module.css'], | ||
plugins: ['stylelint-plugin2'], | ||
rules: { | ||
'color-no-hex': true, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const expectedConfig = { | ||
plugins: ['stylelint-plugin', 'stylelint-plugin2'], | ||
rules: { | ||
'block-no-empty': true, | ||
'color-no-hex': true, | ||
}, | ||
}; | ||
|
||
const applied = applyOverrides(config, __dirname, path.join(__dirname, 'style.module.css')); | ||
|
||
expect(applied).toEqual(expectedConfig); | ||
}); | ||
}); | ||
|
||
describe('two matching overrides', () => { | ||
test('simple override', () => { | ||
const config = { | ||
rules: { | ||
'block-no-empty': true, | ||
'unit-disallowed-list': ['px'], | ||
}, | ||
overrides: [ | ||
{ | ||
files: ['*.module.css'], | ||
rules: { | ||
'color-no-hex': true, | ||
}, | ||
}, | ||
{ | ||
files: ['*.css'], | ||
rules: { | ||
'block-no-empty': null, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const expectedConfig = { | ||
rules: { | ||
'block-no-empty': null, | ||
'unit-disallowed-list': ['px'], | ||
'color-no-hex': true, | ||
}, | ||
}; | ||
|
||
const applied = applyOverrides(config, __dirname, path.join(__dirname, 'style.module.css')); | ||
|
||
expect(applied).toEqual(expectedConfig); | ||
}); | ||
|
||
test('with plugins', () => { | ||
const config = { | ||
plugins: ['stylelint-plugin'], | ||
rules: { | ||
'block-no-empty': true, | ||
'unit-disallowed-list': ['px'], | ||
}, | ||
overrides: [ | ||
{ | ||
files: ['*.module.css'], | ||
plugins: ['stylelint-plugin2'], | ||
rules: { | ||
'color-no-hex': true, | ||
}, | ||
}, | ||
{ | ||
files: ['*.css'], | ||
plugins: ['stylelint-plugin3'], | ||
rules: { | ||
'block-no-empty': null, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const expectedConfig = { | ||
plugins: ['stylelint-plugin', 'stylelint-plugin2', 'stylelint-plugin3'], | ||
rules: { | ||
'block-no-empty': null, | ||
'unit-disallowed-list': ['px'], | ||
'color-no-hex': true, | ||
}, | ||
}; | ||
|
||
const applied = applyOverrides(config, __dirname, path.join(__dirname, 'style.module.css')); | ||
|
||
expect(applied).toEqual(expectedConfig); | ||
}); | ||
}); | ||
|
||
describe('no matching overrides', () => { | ||
test('simple override', () => { | ||
const config = { | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
overrides: [ | ||
{ | ||
files: ['*.no-module.css'], | ||
rules: { | ||
'color-no-hex': true, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const expectedConfig = { | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
}; | ||
|
||
const applied = applyOverrides(config, __dirname, path.join(__dirname, 'style.module.css')); | ||
|
||
expect(applied).toEqual(expectedConfig); | ||
}); | ||
|
||
test('with plugins', () => { | ||
const config = { | ||
plugins: ['stylelint-plugin'], | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
overrides: [ | ||
{ | ||
files: ['*.no-module.css'], | ||
plugins: ['stylelint-plugin2'], | ||
rules: { | ||
'color-no-hex': true, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const expectedConfig = { | ||
plugins: ['stylelint-plugin'], | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
}; | ||
|
||
const applied = applyOverrides(config, __dirname, path.join(__dirname, 'style.module.css')); | ||
|
||
expect(applied).toEqual(expectedConfig); | ||
}); | ||
}); | ||
|
||
test('overrides is not an array', () => { | ||
const config = { | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
overrides: { | ||
files: ['*.module.css'], | ||
rules: { | ||
'color-no-hex': true, | ||
}, | ||
}, | ||
}; | ||
|
||
expect(() => { | ||
applyOverrides(config, __dirname, path.join(__dirname, 'style.module.css')); | ||
}).toThrowErrorMatchingInlineSnapshot( | ||
`"The \`overrides\` configuration property should be an array, e.g. { \\"overrides\\": [{ \\"files\\": \\"*.css\\", \\"rules\\": {} }] }."`, | ||
); | ||
}); | ||
|
||
test('`files` is missing', () => { | ||
const config = { | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
overrides: [ | ||
{ | ||
rules: { | ||
'color-no-hex': true, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
expect(() => { | ||
applyOverrides(config, __dirname, path.join(__dirname, 'style.module.css')); | ||
}).toThrowErrorMatchingInlineSnapshot( | ||
`"Every object in the \`overrides\` configuration property should have a \`files\` property with globs, e.g. { \\"overrides\\": [{ \\"files\\": \\"*.css\\", \\"rules\\": {} }] }."`, | ||
); | ||
}); | ||
|
||
test('if glob is absolute path', () => { | ||
const config = { | ||
rules: { | ||
'block-no-empty': true, | ||
}, | ||
overrides: [ | ||
{ | ||
files: [path.join(__dirname, 'style.css')], | ||
rules: { | ||
'color-no-hex': true, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const expectedConfig = { | ||
rules: { | ||
'block-no-empty': true, | ||
'color-no-hex': true, | ||
}, | ||
}; | ||
|
||
const applied = applyOverrides(config, __dirname, path.join(__dirname, 'style.css')); | ||
|
||
expect(applied).toEqual(expectedConfig); | ||
}); |
15 changes: 15 additions & 0 deletions
15
lib/__tests__/fixtures/config-overrides/extending-plugin-and-one-rule.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "./plugin-and-one-rule.json", | ||
"plugins": ["../plugin-warn-about-bar"], | ||
"rules": { | ||
"plugin/warn-about-bar": "always" | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": ["*.css"], | ||
"rules": { | ||
"block-no-empty": true | ||
} | ||
} | ||
] | ||
} |
14 changes: 14 additions & 0 deletions
14
lib/__tests__/fixtures/config-overrides/extending-simple-rule.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "./simple-rule", | ||
"rules": { | ||
"block-no-empty": true | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": ["*.css"], | ||
"rules": { | ||
"color-named": "never" | ||
} | ||
} | ||
] | ||
} |
14 changes: 14 additions & 0 deletions
14
lib/__tests__/fixtures/config-overrides/plugin-and-one-rule.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"plugins": ["../plugin-warn-about-foo"], | ||
"rules": { | ||
"plugin/warn-about-foo": "always" | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": ["*.css"], | ||
"rules": { | ||
"color-named": "never" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"color-named": "always-where-possible" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.foo { | ||
color: pink; | ||
} | ||
|
||
.bar {} |
Oops, something went wrong.