Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions src/languages/css-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,11 @@ import { visitorKeys } from "./css-visitor-keys.js";
/**
* @import { CssNodePlain, Comment, Lexer, StyleSheetPlain, SyntaxConfig } from "@eslint/css-tree"
* @import { Language, OkParseResult, ParseResult, File, FileError } from "@eslint/core";
* @import { CSSLanguageOptions } from "../types.js";
*/

/** @typedef {OkParseResult<StyleSheetPlain> & { comments: Comment[], lexer: Lexer }} CSSOkParseResult */
/** @typedef {ParseResult<StyleSheetPlain>} CSSParseResult */
/**
* DefaultSyntaxConfig type representing the structure returned by `@eslint/css-tree/definition-syntax-data`.
* This type is defined inline because it's not exported from the main `@eslint/css-tree` package.
* @typedef {Pick<SyntaxConfig, "atrules" | "types" | "properties">} DefaultSyntaxConfig
*/
/**
* @typedef {(defaultSyntax: DefaultSyntaxConfig) => Partial<SyntaxConfig>} SyntaxExtensionCallback
*/
/**
* @typedef {Object} CSSLanguageOptions
* @property {boolean} [tolerant] Whether to be tolerant of recoverable parsing errors.
* @property {Partial<SyntaxConfig> | SyntaxExtensionCallback} [customSyntax] Custom syntax to use for parsing.
*/

//-----------------------------------------------------------------------------
// Helpers
//-----------------------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions src/languages/css-source-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import { visitorKeys } from "./css-visitor-keys.js";
/**
* @import { CssNode, CssNodePlain, CssLocationRange, Comment, Lexer, StyleSheetPlain } from "@eslint/css-tree"
* @import { SourceRange, FileProblem, DirectiveType, RulesConfig } from "@eslint/core"
* @import { CSSSyntaxElement } from "../types.js"
* @import { CSSLanguageOptions } from "./css-language.js"
* @import { CSSLanguageOptions, CSSSyntaxElement } from "../types.js"
*/

//-----------------------------------------------------------------------------
Expand Down
42 changes: 39 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,55 @@
// Imports
//------------------------------------------------------------------------------

import type { RuleVisitor } from "@eslint/core";
import type { CssNodePlain, StyleSheetPlain } from "@eslint/css-tree";
import type { LanguageOptions, RuleVisitor } from "@eslint/core";
import type {
CssNodePlain,
StyleSheetPlain,
SyntaxConfig,
} from "@eslint/css-tree";
import type {
CustomRuleDefinitionType,
CustomRuleTypeDefinitions,
CustomRuleVisitorWithExit,
} from "@eslint/plugin-kit";
import type { CSSLanguageOptions, CSSSourceCode } from "./index.js";
import type { CSSSourceCode } from "./index.js";

//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------

/**
* Default syntax configuration representing the structure returned by
* `@eslint/css-tree/definition-syntax-data`.
*/
export type DefaultSyntaxConfig = Pick<
SyntaxConfig,
"atrules" | "types" | "properties"
>;

/**
* A callback used to extend the default CSS syntax configuration.
*/
export type SyntaxExtensionCallback = (
defaultSyntax: DefaultSyntaxConfig,
) => Partial<SyntaxConfig>;

/**
* Language options provided for CSS files.
*/
export interface CSSLanguageOptions extends LanguageOptions {
/**
* Whether to be tolerant of recoverable parsing errors.
* @default false
*/
tolerant?: boolean;

/**
* Custom syntax to use for parsing.
*/
customSyntax?: Partial<SyntaxConfig> | SyntaxExtensionCallback;
}

/**
* A CSS syntax element, including nodes and comments.
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/types/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import css from "@eslint/css";
import type {
CSSLanguageOptions,
CSSRuleDefinition,
CSSRuleVisitor,
CSSSourceCode,
Expand Down Expand Up @@ -75,6 +76,23 @@ css.rules[ruleName] satisfies CSSRuleDefinition;
// Check that `plugins` in the recommended config is defined:
css.configs.recommended.plugins satisfies object;

const validLanguageOptions1: CSSLanguageOptions = {};
const validLanguageOptions2: CSSLanguageOptions = {
tolerant: true,
};
const validLanguageOptions3: CSSLanguageOptions = {
tolerant: false,
};
const validLanguageOptions4: CSSLanguageOptions = {
tolerant: true,
unknownOption: "unknown",
};

const invalidLanguageOptions1: CSSLanguageOptions = {
// @ts-expect-error -- Invalid value for `tolerant`
tolerant: "true",
};

{
type RecommendedRuleName = keyof typeof css.configs.recommended.rules;
type RuleName = `css/${keyof typeof css.rules}`;
Expand Down