-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a color-variables-in-files rule (#7)
* Moved some helpers into the root src * Added color-variables-in-files rule
- Loading branch information
1 parent
1fab7f0
commit 34ad40c
Showing
9 changed files
with
171 additions
and
11 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
File renamed without changes.
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
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,46 @@ | ||
# dczajkowski/color-variables-in-files | ||
Requires variables containing colors to be declared in specified files. | ||
|
||
## Usage | ||
```json | ||
{ | ||
"rules": { | ||
"dczajkowski/color-variables-in-files": [["src/styles/variables.css"]] | ||
} | ||
} | ||
``` | ||
|
||
For this config the following applies: | ||
|
||
```css | ||
/* This is allowed */ | ||
|
||
/* in src/styles/variables.css */ | ||
|
||
:root { | ||
--color-blue: cornflowerblue; | ||
--spacing-1: 1rem; | ||
} | ||
|
||
.a { | ||
--color-white: white; | ||
} | ||
|
||
/* in src/styles/partials/_navbar.css */ | ||
|
||
.b { | ||
--spacing-3: 3rem; | ||
} | ||
|
||
/* This is not allowed */ | ||
|
||
/* in src/styles/partials/_navbar.css */ | ||
|
||
:root { | ||
--color-blue: cornflowerblue; | ||
} | ||
|
||
.a { | ||
--color-white: white; | ||
} | ||
``` |
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,53 @@ | ||
import stylelint from 'stylelint'; | ||
import postcss from 'postcss'; | ||
import { namespace } from '../../constants'; | ||
import { isColorLiteral } from '../../helpers'; | ||
|
||
export const ruleName = `${namespace}/color-variables-in-files`; | ||
export const messages = stylelint.utils.ruleMessages(ruleName, { | ||
illegalVariableDeclaration: (variable: string, file: string, allowedFiles: string[]) => | ||
`Illegal color variable '${variable}' declaration in file '${file}'. Allowed files: ${allowedFiles | ||
.map(file => `'${file}'`) | ||
.join(', ')}.`, | ||
}); | ||
|
||
const isVariableDeclaration = (prop: string) => prop.startsWith('--'); | ||
|
||
export default function(allowedFiles: string[] = []) { | ||
return async function(postcssRoot: postcss.Root, postcssResult: postcss.Result) { | ||
const validOptions = stylelint.utils.validateOptions(postcssResult, ruleName); | ||
|
||
if (!validOptions || !Array.isArray(allowedFiles)) { | ||
return; | ||
} | ||
|
||
const rootPath = `${process.cwd()}/`; | ||
|
||
postcssRoot.walkDecls(node => { | ||
const { | ||
prop, | ||
value, | ||
source: { | ||
input: { file }, | ||
}, | ||
} = node; | ||
|
||
if (!isVariableDeclaration(prop) || !isColorLiteral(value) || !file) { | ||
return; | ||
} | ||
|
||
const relativePath = file.replace(rootPath, ''); | ||
|
||
if (allowedFiles.includes(relativePath)) { | ||
return; | ||
} | ||
|
||
stylelint.utils.report({ | ||
message: messages.illegalVariableDeclaration(prop, relativePath, allowedFiles), | ||
result: postcssResult, | ||
ruleName, | ||
node, | ||
}); | ||
}); | ||
}; | ||
} |
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,7 @@ | ||
export interface Warning { | ||
line: number; | ||
column: number; | ||
rule: string; | ||
severity: 'error'; | ||
text: string; | ||
} |
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,57 @@ | ||
import { messages } from '../../src/rules/color-variables-in-files'; | ||
import test from 'tape'; | ||
import { lint } from 'stylelint'; | ||
import { Warning } from '../helpers/types'; | ||
|
||
const runStylelint = async (allowedFiles: string[], file: string) => | ||
lint({ | ||
code: ` | ||
/* 2 */ :root { | ||
/* 3 */ --spacing-4: 1rem; | ||
/* 4 */ --color-black: #333; | ||
/* 5 */ } | ||
/* 6 */ | ||
/* 7 */ .a { | ||
/* 8 */ --spacing-8: 2rem; | ||
/* 9 */ --color-blue: cornflowerblue; | ||
/* 10 */ } | ||
`, | ||
codeFilename: file, | ||
config: { | ||
plugins: ['@dczajkowski/stylelint-rules'], | ||
rules: { | ||
'dczajkowski/color-variables-in-files': [allowedFiles], | ||
}, | ||
}, | ||
}); | ||
|
||
test('accepts all variable declarations in allowed files', async t => { | ||
t.plan(1); | ||
|
||
const { errored } = await runStylelint(['vars/colors.css', 'vars/index.css'], 'vars/index.css'); | ||
|
||
t.false(errored); | ||
}); | ||
|
||
test('disallows color variable declarations in not-allowed files', async t => { | ||
t.plan(6); | ||
|
||
const allowedFiles = ['vars/colors.css', 'vars/index.css']; | ||
const file = 'partials/_navbar.css'; | ||
|
||
const { | ||
errored, | ||
results: [{ warnings: unknownWarnings }], | ||
} = await runStylelint(allowedFiles, file); | ||
|
||
const warnings = <Warning[]>(<unknown[]>unknownWarnings); | ||
|
||
console.log(warnings[0]); | ||
|
||
t.true(errored); | ||
t.equals(warnings.length, 2, 'There should be two warnings emitted.'); | ||
t.equals(warnings[0].text, messages.illegalVariableDeclaration('--color-black', file, allowedFiles)); | ||
t.equals(warnings[0].line, 4); | ||
t.equals(warnings[1].text, messages.illegalVariableDeclaration('--color-blue', file, allowedFiles)); | ||
t.equals(warnings[1].line, 9); | ||
}); |
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