Skip to content

Commit

Permalink
Add ignoreAtRules: [] to no-invalid-position-at-import (#5520)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylwia-lask committed Sep 15, 2021
1 parent 05677f7 commit 3058f45
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
30 changes: 30 additions & 0 deletions lib/rules/no-invalid-position-at-import-rule/README.md
Expand Up @@ -49,3 +49,33 @@ a {}
@charset 'utf-8';
@import 'foo.css';
```

## Optional secondary options

### `ignoreAtRules: ["/regex/", /regex/, "string"]`

Given:

```json
["/^my-/", "custom"]
```

The following patterns are _not_ considered violations:

<!-- prettier-ignore -->
```css
@my-at-rule "bar.css";
@import "foo.css";
```

<!-- prettier-ignore -->
```css
@my-other-at-rule {}
@import "foo.css";
```

<!-- prettier-ignore -->
```css
@custom "bar.css";
@import "foo.css"
```
53 changes: 53 additions & 0 deletions lib/rules/no-invalid-position-at-import-rule/__tests__/index.js
Expand Up @@ -113,6 +113,59 @@ testRule({
],
});

testRule({
ruleName,
config: [true, { ignoreAtRules: ['bar', '/^my-/', /foo/] }],

accept: [
{
code: stripIndent`
@bar 'bar.css';
@import 'foo.css';
`,
description: 'ignored atRule at the beggining - equal string',
},
{
code: stripIndent`
@foofoo 'bar.css';
@import 'foo.css';
`,
description: 'ignored atRule at the beggining - match regExp',
},
{
code: stripIndent`
@my-custom-rule {};
@import 'foo.css';
`,
description: 'ignored atRule at the beggining - match regExp',
},
],

reject: [
{
code: stripIndent`
@baz 'bar.css';
@import 'foo.css';
`,
message: messages.rejected,
description: 'atRule does not match the pattern',
line: 2,
column: 1,
},
{
code: stripIndent`
@bar 'bar.css';
@baz 'bar.css';
@import 'foo.css';
`,
message: messages.rejected,
description: 'one of atRules before @import does not match the pattern',
line: 3,
column: 1,
},
],
});

testRule({
ruleName,
config: [true],
Expand Down
18 changes: 16 additions & 2 deletions lib/rules/no-invalid-position-at-import-rule/index.js
Expand Up @@ -4,19 +4,32 @@

const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { isRegExp, isString } = require('../../utils/validateTypes');

const ruleName = 'no-invalid-position-at-import-rule';

const messages = ruleMessages(ruleName, {
rejected: 'Unexpected invalid position @import rule',
});

function rule(actual) {
function rule(actual, options) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual });
const validOptions = validateOptions(
result,
ruleName,
{ actual },
{
actual: options,
possible: {
ignoreAtRules: [isString, isRegExp],
},
optional: true,
},
);

if (!validOptions) {
return;
Expand All @@ -31,6 +44,7 @@ function rule(actual) {
(node.type === 'atrule' &&
nodeName !== 'charset' &&
nodeName !== 'import' &&
!optionsMatches(options, 'ignoreAtRules', node.name) &&
isStandardSyntaxAtRule(node)) ||
(node.type === 'rule' && isStandardSyntaxRule(node))
) {
Expand Down

0 comments on commit 3058f45

Please sign in to comment.