Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for declarations #2468

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 8 additions & 1 deletion pkg/sass-parser/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ export {
DebugRuleProps,
DebugRuleRaws,
} from './src/statement/debug-rule';
export {
Declaration,
DeclarationProps,
DeclarationRaws,
} from './src/statement/declaration';
export {EachRule, EachRuleProps, EachRuleRaws} from './src/statement/each-rule';
export {
ErrorRule,
Expand Down Expand Up @@ -142,10 +147,12 @@ export {
} from './src/statement/sass-comment';
export {UseRule, UseRuleProps, UseRuleRaws} from './src/statement/use-rule';
export {
AnyDeclaration,
AnyStatement,
AtRule,
ChildNode,
ChildProps,
Comment,
ContainerProps,
NewNode,
Statement,
Expand Down Expand Up @@ -196,7 +203,7 @@ class _Syntax implements Syntax {
}

stringify(node: postcss.AnyNode, builder: postcss.Builder): void {
new Stringifier(builder).stringify(node, true);
new Stringifier(builder).stringify(node, false);
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/sass-parser/lib/src/argument-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Argument, ArgumentProps} from './argument';
import {Container} from './container';
import {convertExpression} from './expression/convert';
import {LazySource} from './lazy-source';
import {Node} from './node';
import {Node, NodeProps} from './node';
import * as sassInternal from './sass-internal';
import * as utils from './utils';

Expand All @@ -30,7 +30,7 @@ export type NewArguments =
*
* @category Expression
*/
export interface ArgumentListObjectProps {
export interface ArgumentListObjectProps extends NodeProps {
nodes?: ReadonlyArray<NewArguments>;
raws?: ArgumentListRaws;
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sass-parser/lib/src/argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as postcss from 'postcss';

import {Expression, ExpressionProps} from './expression';
import {fromProps} from './expression/from-props';
import {Node} from './node';
import {Node, NodeProps} from './node';
import {ArgumentList} from './argument-list';
import * as sassInternal from './sass-internal';
import {RawWithValue} from './raw-with-value';
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface ArgumentRaws {
*
* @category Expression
*/
export type ArgumentObjectProps = {
export type ArgumentObjectProps = NodeProps & {
raws?: ArgumentRaws;
value: Expression | ExpressionProps;
} & ({name?: string; rest?: never} | {name?: never; rest?: boolean});
Expand Down
4 changes: 2 additions & 2 deletions pkg/sass-parser/lib/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ConfiguredVariableProps,
} from './configured-variable';
import {LazySource} from './lazy-source';
import {Node} from './node';
import {Node, NodeProps} from './node';
import type * as sassInternal from './sass-internal';
import * as utils from './utils';
import {ForwardRule} from './statement/forward-rule';
Expand All @@ -37,7 +37,7 @@ export interface ConfigurationRaws {
*
* @category Statement
*/
export interface ConfigurationProps {
export interface ConfigurationProps extends NodeProps {
raws?: ConfigurationRaws;
variables:
| Record<string, ConfiguredVariableExpressionProps>
Expand Down
4 changes: 2 additions & 2 deletions pkg/sass-parser/lib/src/configured-variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {convertExpression} from './expression/convert';
import {Expression, ExpressionProps} from './expression';
import {fromProps} from './expression/from-props';
import {LazySource} from './lazy-source';
import {Node} from './node';
import {Node, NodeProps} from './node';
import * as sassInternal from './sass-internal';
import {RawWithValue} from './raw-with-value';
import * as utils from './utils';
Expand Down Expand Up @@ -53,7 +53,7 @@ export interface ConfiguredVariableRaws {
*
* @category Statement
*/
export interface ConfiguredVariableObjectProps {
export interface ConfiguredVariableObjectProps extends NodeProps {
raws?: ConfiguredVariableRaws;
name: string;
expression: Expression | ExpressionProps;
Expand Down
3 changes: 2 additions & 1 deletion pkg/sass-parser/lib/src/expression/binary-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as postcss from 'postcss';

import {LazySource} from '../lazy-source';
import {NodeProps} from '../node';
import type * as sassInternal from '../sass-internal';
import * as utils from '../utils';
import {Expression, ExpressionProps} from '.';
Expand Down Expand Up @@ -33,7 +34,7 @@ export type BinaryOperator =
*
* @category Expression
*/
export interface BinaryOperationExpressionProps {
export interface BinaryOperationExpressionProps extends NodeProps {
operator: BinaryOperator;
left: Expression | ExpressionProps;
right: Expression | ExpressionProps;
Expand Down
3 changes: 2 additions & 1 deletion pkg/sass-parser/lib/src/expression/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as postcss from 'postcss';

import {LazySource} from '../lazy-source';
import {NodeProps} from '../node';
import type * as sassInternal from '../sass-internal';
import * as utils from '../utils';
import {Expression} from '.';
Expand All @@ -14,7 +15,7 @@ import {Expression} from '.';
*
* @category Expression
*/
export interface BooleanExpressionProps {
export interface BooleanExpressionProps extends NodeProps {
value: boolean;
raws?: BooleanExpressionRaws;
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/sass-parser/lib/src/expression/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as postcss from 'postcss';

import {LazySource} from '../lazy-source';
import {NodeProps} from '../node';
import type * as sassInternal from '../sass-internal';
import * as utils from '../utils';
import {Expression} from '.';
Expand All @@ -14,7 +15,7 @@ import {Expression} from '.';
*
* @category Expression
*/
export interface NumberExpressionProps {
export interface NumberExpressionProps extends NodeProps {
value: number;
unit?: string;
raws?: NumberExpressionRaws;
Expand Down
3 changes: 2 additions & 1 deletion pkg/sass-parser/lib/src/expression/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as postcss from 'postcss';

import {Interpolation, InterpolationProps} from '../interpolation';
import {LazySource} from '../lazy-source';
import {NodeProps} from '../node';
import type * as sassInternal from '../sass-internal';
import * as utils from '../utils';
import {Expression} from '.';
Expand All @@ -15,7 +16,7 @@ import {Expression} from '.';
*
* @category Expression
*/
export interface StringExpressionProps {
export interface StringExpressionProps extends NodeProps {
text: Interpolation | InterpolationProps;
quotes?: boolean;
raws?: StringExpressionRaws;
Expand Down
4 changes: 2 additions & 2 deletions pkg/sass-parser/lib/src/interpolation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {convertExpression} from './expression/convert';
import {fromProps} from './expression/from-props';
import {Expression, ExpressionProps} from './expression';
import {LazySource} from './lazy-source';
import {Node} from './node';
import {Node, NodeProps} from './node';
import {RawWithValue} from './raw-with-value';
import type * as sassInternal from './sass-internal';
import * as utils from './utils';
Expand Down Expand Up @@ -39,7 +39,7 @@ export type NewNodeForInterpolation =
*
* @category Expression
*/
export interface InterpolationObjectProps {
export interface InterpolationObjectProps extends NodeProps {
nodes: ReadonlyArray<NewNodeForInterpolation>;
raws?: InterpolationRaws;
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sass-parser/lib/src/parameter-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as postcss from 'postcss';
import {Container} from './container';
import {Parameter, ParameterProps} from './parameter';
import {LazySource} from './lazy-source';
import {Node} from './node';
import {Node, NodeProps} from './node';
import * as sassInternal from './sass-internal';
import * as utils from './utils';

Expand All @@ -29,7 +29,7 @@ export type NewParameters =
*
* @category Statement
*/
export interface ParameterListObjectProps {
export interface ParameterListObjectProps extends NodeProps {
nodes?: ReadonlyArray<NewParameters>;
raws?: ParameterListRaws;
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/sass-parser/lib/src/parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {convertExpression} from './expression/convert';
import {Expression, ExpressionProps} from './expression';
import {fromProps} from './expression/from-props';
import {LazySource} from './lazy-source';
import {Node} from './node';
import {Node, NodeProps} from './node';
import {ParameterList} from './parameter-list';
import * as sassInternal from './sass-internal';
import {RawWithValue} from './raw-with-value';
Expand Down Expand Up @@ -57,19 +57,19 @@ export interface ParameterRaws {
*
* @category Statement
*/
export type ParameterObjectProps = {
export type ParameterObjectProps = NodeProps & {
raws?: ParameterRaws;
name: string;
} & (
| {
defaultValue?: Expression | ExpressionProps;
rest?: never;
}
| {
defaultValue?: never;
rest?: boolean;
}
);
| {
defaultValue?: Expression | ExpressionProps;
rest?: never;
}
| {
defaultValue?: never;
rest?: boolean;
}
);

/**
* Properties used to initialize a {@link Parameter} without an explicit name.
Expand Down
7 changes: 7 additions & 0 deletions pkg/sass-parser/lib/src/sass-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ declare namespace SassInternal {
readonly expression: Expression;
}

class Declaration extends ParentStatement<Statement[] | null> {
readonly name: Interpolation;
readonly value?: Expression;
}

class EachRule extends ParentStatement<Statement[]> {
readonly variables: string[];
readonly list: Expression;
Expand Down Expand Up @@ -317,6 +322,7 @@ export type AtRootRule = SassInternal.AtRootRule;
export type AtRule = SassInternal.AtRule;
export type ContentBlock = SassInternal.ContentBlock;
export type DebugRule = SassInternal.DebugRule;
export type Declaration = SassInternal.Declaration;
export type EachRule = SassInternal.EachRule;
export type ErrorRule = SassInternal.ErrorRule;
export type ExtendRule = SassInternal.ExtendRule;
Expand Down Expand Up @@ -350,6 +356,7 @@ export interface StatementVisitorObject<T> {
visitAtRootRule(node: AtRootRule): T;
visitAtRule(node: AtRule): T;
visitDebugRule(node: DebugRule): T;
visitDeclaration(node: Declaration): T;
visitEachRule(node: EachRule): T;
visitErrorRule(node: ErrorRule): T;
visitExtendRule(node: ExtendRule): T;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`a property declaration toJSON with expression and no nodes 1`] = `
{
"expression": <bar>,
"inputs": [
{
"css": "a {foo: bar}",
"hasBOM": false,
"id": "<input css _____>",
},
],
"propInterpolation": <foo>,
"raws": {},
"sassType": "decl",
"source": <1:4-1:12 in 0>,
"type": "decl",
}
`;
exports[`a property declaration toJSON with expression and nodes 1`] = `
{
"expression": <bar>,
"inputs": [
{
"css": "a {foo: bar {baz: bang}}",
"hasBOM": false,
"id": "<input css _____>",
},
],
"nodes": [
<baz: bang>,
],
"propInterpolation": <foo>,
"raws": {},
"sassType": "decl",
"source": <1:4-1:24 in 0>,
"type": "decl",
}
`;
exports[`a property declaration toJSON with no expression and nodes 1`] = `
{
"inputs": [
{
"css": "a {foo: {baz: bang}}",
"hasBOM": false,
"id": "<input css _____>",
},
],
"nodes": [
<baz: bang>,
],
"propInterpolation": <foo>,
"raws": {},
"sassType": "decl",
"source": <1:4-1:20 in 0>,
"type": "decl",
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports[`a generic @-rule toJSON with a child 1`] = `
"name": "foo",
"nameInterpolation": <foo>,
"nodes": [
<@bar;>,
<@bar>,
],
"params": "",
"raws": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports[`a @include rule toJSON with a child 1`] = `
],
"name": "include",
"nodes": [
<@qux;>,
<@qux>,
],
"params": "foo(bar)",
"raws": {},
Expand Down Expand Up @@ -56,7 +56,7 @@ exports[`a @include rule toJSON with using and a child 1`] = `
],
"name": "include",
"nodes": [
<@qux;>,
<@qux>,
],
"params": "foo(bar) using ($baz)",
"raws": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports[`a style rule toJSON with a child 1`] = `
},
],
"nodes": [
<@bar;>,
<@bar>,
],
"raws": {},
"sassType": "rule",
Expand Down
6 changes: 3 additions & 3 deletions pkg/sass-parser/lib/src/statement/at-rule-internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as postcss from 'postcss';

import {Rule} from './rule';
import {Root} from './root';
import {AtRule, ChildNode, Comment, Declaration, NewNode} from '.';
import {AnyDeclaration, AtRule, ChildNode, Comment, NewNode} from '.';

/**
* A fake intermediate class to convince TypeScript to use Sass types for
Expand Down Expand Up @@ -60,10 +60,10 @@ export class _AtRule<Props> extends postcss.AtRule {
): false | undefined;
walkDecls(
propFilter: RegExp | string,
callback: (decl: Declaration, index: number) => false | void,
callback: (decl: AnyDeclaration, index: number) => false | void,
): false | undefined;
walkDecls(
callback: (decl: Declaration, index: number) => false | void,
callback: (decl: AnyDeclaration, index: number) => false | void,
): false | undefined;
walkRules(
selectorFilter: RegExp | string,
Expand Down
Loading
Loading