Skip to content

Commit

Permalink
Refactor declaration-property-value-no-unknown to reuse parsed CSST…
Browse files Browse the repository at this point in the history
…ree node (#6616)

According to the [CSSTree doc](https://github.com/csstree/csstree/blob/v2.3.1/README.md#usage),
it is likely a typical case to pass a node to `lexer.matchProperty()`.
  • Loading branch information
ybiquitous committed Feb 6, 2023
1 parent a479b03 commit bf9f222
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/rules/declaration-property-value-no-unknown/index.js
Expand Up @@ -84,10 +84,12 @@ const rule = (primary, secondaryOptions) => {

if (isPropIgnored(prop, value)) return;

let cssTreeValueNode;

try {
const valueNode = parse(value, { context: 'value' });
cssTreeValueNode = parse(value, { context: 'value' });

if (containsUnsupportedMathFunction(valueNode)) return;
if (containsUnsupportedMathFunction(cssTreeValueNode)) return;
} catch (e) {
result.warn(`Cannot parse property value "${value}"`, {
node: decl,
Expand All @@ -97,7 +99,7 @@ const rule = (primary, secondaryOptions) => {
return;
}

const { error } = forkedLexer.matchProperty(prop, value);
const { error } = forkedLexer.matchProperty(prop, cssTreeValueNode);

if (!error) return;

Expand Down
2 changes: 1 addition & 1 deletion patches/@types+css-tree+2.0.1.patch
Expand Up @@ -17,7 +17,7 @@ index 2e4ed18..7f096c6 100755
+};
+
+declare class Lexer {
+ matchProperty(propertyName: string, value: string): MatchResult;
+ matchProperty(propertyName: string, value: CssNode | string): MatchResult;
+}
+
+export function fork(extension: Record<string, Record<string, string>>): { lexer: Lexer };

0 comments on commit bf9f222

Please sign in to comment.