-
-
Notifications
You must be signed in to change notification settings - Fork 927
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
isStandardSyntaxAtRule.test.js
that use callbacks (#4957)
* Fix `isStandardSyntaxAtRule.test.js` that use callbacks * Add `atRule()` helper to improve readability
- Loading branch information
1 parent
a5e4355
commit 9246466
Showing
1 changed file
with
54 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,136 +1,116 @@ | ||
'use strict'; | ||
|
||
const isStandardSyntaxAtRule = require('../isStandardSyntaxAtRule'); | ||
const less = require('postcss-less'); | ||
const postcss = require('postcss'); | ||
const sass = require('postcss-sass'); | ||
const scss = require('postcss-scss'); | ||
const postcssLess = require('postcss-less'); | ||
const postcssSass = require('postcss-sass'); | ||
const postcssScss = require('postcss-scss'); | ||
|
||
describe('isStandardSyntaxAtRule', () => { | ||
it('non nested at-rules without quotes', () => { | ||
atRules('@charset UTF-8;', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRule('@charset UTF-8;'))).toBeTruthy(); | ||
}); | ||
|
||
it("non nested at-rules with `'` quotes", () => { | ||
atRules("@charset 'UTF-8';", (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRule("@charset 'UTF-8';"))).toBeTruthy(); | ||
}); | ||
|
||
it('non nested at-rules with `"` quotes', () => { | ||
atRules('@charset "UTF-8";', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRule('@charset "UTF-8";'))).toBeTruthy(); | ||
}); | ||
|
||
it("non nested at-rules with `'` quotes and without space after name", () => { | ||
atRules("@charset'UTF-8';", (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRule("@charset'UTF-8';"))).toBeTruthy(); | ||
}); | ||
|
||
it('non nested at-rules with `"` quotes and without space after name', () => { | ||
atRules('@charset"UTF-8";', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRule('@charset"UTF-8";'))).toBeTruthy(); | ||
}); | ||
|
||
it('non nested at-rules with function and without space after name', () => { | ||
atRules('@import url("fineprint.css") print;', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRule('@import url("fineprint.css") print;'))).toBeTruthy(); | ||
}); | ||
|
||
it('nested at-rules', () => { | ||
atRules('@media (min-width: 100px) {};', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRule('@media (min-width: 100px) {};'))).toBeTruthy(); | ||
}); | ||
|
||
it('nested at-rules with newline after name', () => { | ||
atRules('@media\n(min-width: 100px) {};', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRule('@media\n(min-width: 100px) {};'))).toBeTruthy(); | ||
}); | ||
|
||
it('nested at-rules with windows newline after name', () => { | ||
atRules('@media\r\n(min-width: 100px) {};', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRule('@media\r\n(min-width: 100px) {};'))).toBeTruthy(); | ||
}); | ||
|
||
it('nested at-rules without space after name', () => { | ||
atRules('@media(min-width: 100px) {};', (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeTruthy(); | ||
}); | ||
expect(isStandardSyntaxAtRule(atRule('@media(min-width: 100px) {};'))).toBeTruthy(); | ||
}); | ||
|
||
it('ignore `@content` inside mixins newline', () => { | ||
const sass = '@mixin mixin()\n @content'; | ||
// eslint-disable-next-line jest/no-disabled-tests -- TODO: `postcss-sass` parser does not support `@mixin`. | ||
it.skip('ignore `@content` inside mixins newline', () => { | ||
const rules = sassAtRules('@mixin mixin()\n @content'); | ||
|
||
sassAtRules(sass, (atRule) => { | ||
if (atRule.name === 'mixin') { | ||
return; | ||
} | ||
|
||
expect(isStandardSyntaxAtRule(atRule)).toBeFalsy(); | ||
}); | ||
expect(rules).toHaveLength(2); | ||
expect(rules.map((rule) => rule.name)).toEqual(['mixin', 'content']); | ||
expect(isStandardSyntaxAtRule(rules[0])).toBeTruthy(); | ||
expect(isStandardSyntaxAtRule(rules[1])).toBeFalsy(); | ||
}); | ||
|
||
it('ignore `@content` inside mixins space', () => { | ||
const scss = '@mixin mixin() { @content; };'; | ||
|
||
scssAtRules(scss, (atRule) => { | ||
if (atRule.name === 'mixin') { | ||
return; | ||
} | ||
const rules = scssAtRules('@mixin mixin() { @content; };'); | ||
|
||
expect(isStandardSyntaxAtRule(atRule)).toBeFalsy(); | ||
}); | ||
expect(rules).toHaveLength(2); | ||
expect(rules.map((rule) => rule.name)).toEqual(['mixin', 'content']); | ||
expect(isStandardSyntaxAtRule(rules[0])).toBeTruthy(); | ||
expect(isStandardSyntaxAtRule(rules[1])).toBeFalsy(); | ||
}); | ||
|
||
it('ignore passing rulesets to mixins', () => { | ||
const less = '@detached-ruleset: { background: red; }; .top { @detached-ruleset(); }'; | ||
const rules = lessAtRules( | ||
'@detached-ruleset: { background: red; }; .top { @detached-ruleset(); }', | ||
); | ||
|
||
lessAtRules(less, (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeFalsy(); | ||
}); | ||
expect(rules).toHaveLength(2); | ||
expect(isStandardSyntaxAtRule(rules[0])).toBeFalsy(); | ||
expect(isStandardSyntaxAtRule(rules[1])).toBeFalsy(); | ||
}); | ||
|
||
it('ignore calling of mixins', () => { | ||
const less = 'a { .mixin(); }'; | ||
const rules = lessAtRules('a { .mixin(); }'); | ||
|
||
lessAtRules(less, (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeFalsy(); | ||
}); | ||
expect(rules).toHaveLength(1); | ||
expect(isStandardSyntaxAtRule(rules[0])).toBeFalsy(); | ||
}); | ||
|
||
it('ignore variables', () => { | ||
const less = ` | ||
@my-variable: 10px; | ||
.top { margin-top: @my-variable; } | ||
`; | ||
|
||
lessAtRules(less, (atRule) => { | ||
expect(isStandardSyntaxAtRule(atRule)).toBeFalsy(); | ||
}); | ||
const rules = lessAtRules('@my-variable: 10px; .top { margin-top: @my-variable; }'); | ||
|
||
expect(rules).toHaveLength(1); | ||
expect(isStandardSyntaxAtRule(rules[0])).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
function atRules(css, cb) { | ||
postcss.parse(css).walkAtRules(cb); | ||
function atRules(code, parser = postcss) { | ||
const rules = []; | ||
|
||
parser.parse(code).walkAtRules((rule) => rules.push(rule)); | ||
|
||
return rules; | ||
} | ||
|
||
function atRule(code) { | ||
return atRules(code)[0]; | ||
} | ||
|
||
function sassAtRules(css, cb) { | ||
sass.parse(css).walkAtRules(cb); | ||
function sassAtRules(code) { | ||
return atRules(code, postcssSass); | ||
} | ||
|
||
function scssAtRules(css, cb) { | ||
scss.parse(css).walkAtRules(cb); | ||
function scssAtRules(code) { | ||
return atRules(code, postcssScss); | ||
} | ||
|
||
function lessAtRules(css, cb) { | ||
less.parse(css).walkAtRules(cb); | ||
function lessAtRules(code) { | ||
return atRules(code, postcssLess); | ||
} |