fix(css-parser): handle hash other than id type
MODIFY
internal/css-parser/parser/selectors.ts
internal/css-parser/parser/selectors.ts
@@ -35,10 +35,12 @@ function parseTypeSelector(parser: CSSParser): CSSTypeSelector {
);
}
-function parseIdSelector(parser: CSSParser): CSSIdSelector {
+function parseIdSelector(parser: CSSParser): CSSIdSelector | undefined {
const start = parser.getPosition();
- const token = parser.expectToken("Hash");
- readToken(parser, "Hash");
+
+ if (matchToken(parser, "Hash")) {
+ const token = readToken(parser, "Hash") as Tokens["Hash"];
+ if (token.hashType === "id") {
return parser.finishNode(
start,
{
@@ -47,6 +49,13 @@ function parseIdSelector(parser: CSSParser): CSSIdSelector {
},
);
}
+ parser.unexpectedDiagnostic({
+ description: descriptions.CSS_PARSER.EXPECTED_ID_HASH,
+ token,
+ });
+ }
+ return undefined;
+}
function parseClassSelector(parser: CSSParser): CSSClassSelector | undefined {
const start = parser.getPosition();

MODIFY
internal/css-parser/parser/value.ts
internal/css-parser/parser/value.ts
@@ -114,7 +114,6 @@ export function parseComponentValue(parser: CSSParser): AnyCSSValue | undefined
if (matchToken(parser, "Hash")) {
const hashToken = parser.getToken() as Tokens["Hash"];
- if (hashToken.hashType === "id") {
nextToken(parser);
return parser.finishNode(
start,
@@ -124,7 +123,6 @@ export function parseComponentValue(parser: CSSParser): AnyCSSValue | undefined
},
);
}
- }
if (matchToken(parser, "String")) {
const stringToken = parser.getToken() as Tokens["String"];

ADD
internal/css-parser/test-fixtures/invalid/selector8/input.css
+#1234 {
+
+}

ADD
internal/css-parser/test-fixtures/invalid/selector8/input.test.md
+# `index.test.ts`
+
+**DO NOT MODIFY**. This file has been autogenerated. Run `rome test internal/css-parser/index.test.ts --update-snapshots` to update.
+
+## `invalid > selector8`
+
+```javascript
+CSSRoot {
+ comments: Array []
+ corrupt: false
+ integrity: undefined
+ loc: SourceLocation invalid/selector8/input.css 1:0-3:1
+ path: RelativePath<invalid/selector8/input.css>
+ body: Array [
+ CSSRule {
+ loc: SourceLocation invalid/selector8/input.css 1:0-3:1
+ block: CSSBlock {
+ value: Array []
+ startingTokenValue: "{"
+ loc: SourceLocation invalid/selector8/input.css 1:6-3:1
+ }
+ prelude: Array [
+ CSSSelector {
+ patterns: Array []
+ loc: SourceLocation invalid/selector8/input.css 1:0-1:6
+ }
+ ]
+ }
+ ]
+ diagnostics: Array [
+ Object {
+ origins: Array [Object {category: "parse"}]
+ description: Object {
+ advice: Array []
+ categoryValue: "css"
+ category: Array ["parse"]
+ message: RAW_MARKUP {value: "Expected to use identifier after <emphasis>#</emphasis>."}
+ }
+ location: Object {
+ integrity: undefined
+ language: "css"
+ sourceText: undefined
+ end: Position 1:5
+ path: RelativePath<invalid/selector8/input.css>
+ start: Position 1:0
+ }
+ }
+ ]
+}
+```

MODIFY
internal/css-parser/types.ts
internal/css-parser/types.ts
@@ -38,7 +38,7 @@ export interface DimensionData {
}
export interface HashData {
- hashType?: string;
+ hashType?: "id";
value: string;
}

MODIFY
internal/diagnostics/descriptions/parsers/cssParser.ts
internal/diagnostics/descriptions/parsers/cssParser.ts
@@ -123,4 +123,7 @@ export const cssParser = createDiagnosticsCategory({
CALC_INCORRECT_NUMBER_VALUE: {
message: markup`Incorrect character, expected a number or a parenthesis`,
},
+ EXPECTED_ID_HASH: {
+ message: markup`Expected to use identifier after <emphasis>#</emphasis>.`,
+ },
});

MODIFY
internal/formatter/test-fixtures/css/values/input.css
internal/formatter/test-fixtures/css/values/input.css
@@ -13,3 +13,7 @@ bar {
color: yellow;
background: none
}
+
+.a {
+ background-color: #111111;
+}

MODIFY
internal/formatter/test-fixtures/css/values/input.test.md
internal/formatter/test-fixtures/css/values/input.test.md
@@ -29,6 +29,10 @@ bar {
background: none
}
+.a {
+ background-color: #111111;
+}
+
```
### `Output`
@@ -50,4 +54,8 @@ bar {
background: none;
}
+.a {
+ background-color: #111111;
+}
+
```

fix(css-parser): handle hash other than id type (#1377)