Skip to content

Commit

Permalink
Fix style errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Oct 25, 2024
1 parent ea71bb8 commit 0fa5a05
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 224 deletions.
44 changes: 21 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ deno add @eslint/css

### Configurations

| **Configuration Name** | **Description** |
|---------------|-----------------|
| `recommended` | Enables all recommended rules. |
| **Configuration Name** | **Description** |
| ---------------------- | ------------------------------ |
| `recommended` | Enables all recommended rules. |

In your `eslint.config.js` file, import `@eslint/css` and include the recommended config:

Expand All @@ -39,7 +39,6 @@ In your `eslint.config.js` file, import `@eslint/css` and include the recommende
import css from "@eslint/css";

export default [

// lint CSS files
{
files: ["**/*.css"],
Expand All @@ -56,9 +55,11 @@ export default [
<!-- NOTE: The following table is autogenerated. Do not manually edit. -->

<!-- Rule Table Start -->
| **Rule Name** | **Description** | **Recommended** |
| :- | :- | :-: |
| [`no-empty-blocks`](./docs/rules/no-empty-blocks.md) | Disallow empty blocks. | yes |

| **Rule Name** | **Description** | **Recommended** |
| :--------------------------------------------------- | :--------------------- | :-------------: |
| [`no-empty-blocks`](./docs/rules/no-empty-blocks.md) | Disallow empty blocks. | yes |

<!-- Rule Table End -->

**Note:** This plugin does not provide formatting rules. We recommend using a source code formatter such as [Prettier](https://prettier.io) for that purpose.
Expand All @@ -73,13 +74,13 @@ export default [
{
files: ["**/*.css"],
plugins: {
css
css,
},
language: "css/css",
rules: {
"css/no-empty-blocks": "error"
}
}
"css/no-empty-blocks": "error",
},
},
];
```

Expand All @@ -90,26 +91,23 @@ You can individually config, disable, and enable rules in CSS using comments, su

/* eslint-disable css/no-empty-blocks -- this one is ok */
a {

}
/* eslint-enable css/no-empty-blocks */

b { /* eslint-disable-line css/no-empty-blocks */

b {
/* eslint-disable-line css/no-empty-blocks */
}

/* eslint-disable-next-line css/no-empty-blocks */
em {

}
```

### Languages

| **Language Name** | **Description** |
|---------------|-----------------|
| `css` | Parse CSS stylesheets. |

| **Language Name** | **Description** |
| ----------------- | ---------------------- |
| `css` | Parse CSS stylesheets. |

In order to individually configure a language in your `eslint.config.js` file, import `@eslint/css` and configure a `language`:

Expand All @@ -121,13 +119,13 @@ export default [
{
files: ["**/*.md"],
plugins: {
css
css,
},
language: "css/css",
rules: {
"css/no-empty-blocks": "error"
}
}
"css/no-empty-blocks": "error",
},
},
];
```

Expand Down
23 changes: 12 additions & 11 deletions docs/rules/no-empty-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ CSS blocks are indicated by opening `{` and closing `}` characters, and can occu

```css
@media (print) {
a {
color: black;
}
a {
color: black;
}
}
```

Expand All @@ -23,24 +23,25 @@ This rule warns when it finds a block that is empty. For the purposes of this ru
Examples of incorrect code:

```css
a { }

a {
}

a {
}

.class-name { /* a comment */ }
.class-name {
/* a comment */
}

.class-name {
/* a comment */
/* a comment */
}

@media (print) {

}

@media (print) {
/* a comment */
/* a comment */
}
```

Expand All @@ -50,5 +51,5 @@ If you aren't concerned with empty blocks, you can safely disable this rule.

## Prior Art

* [empty-rules](https://github.com/CSSLint/csslint/wiki/Disallow-empty-rules)
* [`block-no-empty`](https://stylelint.io/user-guide/rules/block-no-empty)
- [empty-rules](https://github.com/CSSLint/csslint/wiki/Disallow-empty-rules)
- [`block-no-empty`](https://stylelint.io/user-guide/rules/block-no-empty)
30 changes: 16 additions & 14 deletions src/languages/css-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { parse, toPlainObject } from "css-tree";
import { CSSSourceCode } from "./css-source-code.js";
import { visitorKeys } from "./css-visitor-keys.js"
import { visitorKeys } from "./css-visitor-keys.js";

//-----------------------------------------------------------------------------
// Types
Expand Down Expand Up @@ -91,22 +91,24 @@ export class CSSLanguage {
* problem that ESLint identified just like any other.
*/
try {
const root = toPlainObject(parse(text, {
filename: file.path,
positions: true,
onComment(value, loc) {
comments.push({
type: "Comment",
value,
loc,
});
}
}));
const root = toPlainObject(
parse(text, {
filename: file.path,
positions: true,
onComment(value, loc) {
comments.push({
type: "Comment",
value,
loc,
});
},
}),
);

return {
ok: true,
ast: root,
comments
comments,
};
} catch (ex) {
// error messages end with (line:column) so we strip that off for ESLint
Expand Down Expand Up @@ -136,7 +138,7 @@ export class CSSLanguage {
return new CSSSourceCode({
text: /** @type {string} */ (file.body),
ast: parseResult.ast,
comments: parseResult.comments
comments: parseResult.comments,
});
}
}
6 changes: 2 additions & 4 deletions src/languages/css-source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,8 @@ export class CSSSourceCode extends TextSourceCodeBase {
* @param {BlockPlain} block The node to get the location of.
* @returns {SourceLocationWithOffset} The location of the node.
* @throws {TypeError} When the node is not a block node.
*/
*/
getBlockLocWithBraces(block) {

assertBlock(block);

const { start, end } = block.loc;
Expand All @@ -164,8 +163,7 @@ export class CSSSourceCode extends TextSourceCodeBase {
column: end.column + 1,
offset: end.offset + 1,
},
}

};
}

/**
Expand Down
97 changes: 48 additions & 49 deletions src/languages/css-visitor-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,53 @@
* @author Nicholas C. Zakas
*/


export const visitorKeys = {
AnPlusB: [],
Atrule: ["prelude", "block"],
AtrulePrelude: ["children"],
AttributeSelector: ["name", "value"],
Block: ["children"],
Brackets: ["children"],
CDC: [],
CDO: [],
ClassSelector: [],
Combinator: [],
Comment: [],
Condition: ["children"],
Declaration: ["value"],
DeclarationList: ["children"],
Dimension: [],
Feature: ["value"],
FeatureFunction: ["value"],
FeatureRange: ["left", "middle", "right"],
Function: ["children"],
GeneralEnclosed: ["children"],
Hash: [],
IdSelector: [],
Identifier: [],
Layer: [],
LayerList: ["children"],
MediaQuery: ["condition"],
MediaQueryList: ["children"],
Nth: ["nth", "selector"],
Number: [],
Operator: [],
Parentheses: ["children"],
Percentage: [],
PseudoClassSelector: ["children"],
PseudoElementSelector: ["children"],
Ratio: ["left", "right"],
Raw: [],
Rule: ["prelude", "block"],
Scope: ["root", "limit"],
Selector: ["children"],
SelectorList: ["children"],
String: [],
StyleSheet: ["children"],
SupportsDeclaration: ["declaration"],
TypeSelector: [],
UnicodeRange: [],
Url: [],
Value: ["children"],
WhiteSpace: [],
AnPlusB: [],
Atrule: ["prelude", "block"],
AtrulePrelude: ["children"],
AttributeSelector: ["name", "value"],
Block: ["children"],
Brackets: ["children"],
CDC: [],
CDO: [],
ClassSelector: [],
Combinator: [],
Comment: [],
Condition: ["children"],
Declaration: ["value"],
DeclarationList: ["children"],
Dimension: [],
Feature: ["value"],
FeatureFunction: ["value"],
FeatureRange: ["left", "middle", "right"],
Function: ["children"],
GeneralEnclosed: ["children"],
Hash: [],
IdSelector: [],
Identifier: [],
Layer: [],
LayerList: ["children"],
MediaQuery: ["condition"],
MediaQueryList: ["children"],
Nth: ["nth", "selector"],
Number: [],
Operator: [],
Parentheses: ["children"],
Percentage: [],
PseudoClassSelector: ["children"],
PseudoElementSelector: ["children"],
Ratio: ["left", "right"],
Raw: [],
Rule: ["prelude", "block"],
Scope: ["root", "limit"],
Selector: ["children"],
SelectorList: ["children"],
String: [],
StyleSheet: ["children"],
SupportsDeclaration: ["declaration"],
TypeSelector: [],
UnicodeRange: [],
Url: [],
Value: ["children"],
WhiteSpace: [],
};
6 changes: 2 additions & 4 deletions src/rules/no-empty-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {

docs: {
description: "Disallow empty blocks.",
recommended: true
recommended: true,
},

messages: {
Expand All @@ -18,7 +18,6 @@ export default {
},

create(context) {

const { sourceCode } = context;

return {
Expand All @@ -29,8 +28,7 @@ export default {
messageId: "emptyBlock",
});
}
}

},
};
},
};
8 changes: 4 additions & 4 deletions tests/languages/css-language.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ describe("CSSLanguage", () => {
it("should have visitorKeys property", () => {
const language = new CSSLanguage();

assert.deepStrictEqual(language.visitorKeys.StyleSheet, ["children"]);
assert.deepStrictEqual(language.visitorKeys.StyleSheet, [
"children",
]);
});
});

describe("parse()", () => {

it("should parse CSS", () => {
const language = new CSSLanguage();
const result = language.parse({
Expand All @@ -33,10 +34,9 @@ describe("CSSLanguage", () => {
});

assert.strictEqual(result.ok, true);
assert.strictEqual(result.ast.type, "StyleSheet");
assert.strictEqual(result.ast.type, "StyleSheet");
assert.strictEqual(result.ast.children[0].type, "Rule");
});

});

describe("createSourceCode()", () => {
Expand Down
Loading

0 comments on commit 0fa5a05

Please sign in to comment.