Skip to content

Commit

Permalink
Support @forward in sass-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Nov 13, 2024
1 parent 01a2148 commit 75163cc
Show file tree
Hide file tree
Showing 12 changed files with 1,360 additions and 11 deletions.
8 changes: 6 additions & 2 deletions lib/src/js/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import '../util/string.dart';
import '../visitor/interface/expression.dart';
import '../visitor/interface/statement.dart';
import 'reflection.dart';
import 'set.dart';
import 'visitor/expression.dart';
import 'visitor/statement.dart';

Expand All @@ -30,13 +31,15 @@ class ParserExports {
required Function parseIdentifier,
required Function toCssIdentifier,
required Function createExpressionVisitor,
required Function createStatementVisitor});
required Function createStatementVisitor,
required Function setToJS });

external set parse(Function function);
external set parseIdentifier(Function function);
external set toCssIdentifier(Function function);
external set createStatementVisitor(Function function);
external set createExpressionVisitor(Function function);
external set setToJS(Function function);
}

/// An empty interpolation, used to initialize empty AST entries to modify their
Expand All @@ -57,7 +60,8 @@ ParserExports loadParserExports() {
createExpressionVisitor: allowInterop(
(JSExpressionVisitorObject inner) => JSExpressionVisitor(inner)),
createStatementVisitor: allowInterop(
(JSStatementVisitorObject inner) => JSStatementVisitor(inner)));
(JSStatementVisitorObject inner) => JSStatementVisitor(inner)),
setToJS: allowInterop((Set<Object?> set) => new JSSet([...set])));
}

/// Modifies the prototypes of the Sass AST classes to provide access to JS.
Expand Down
10 changes: 10 additions & 0 deletions lib/src/js/set.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2021 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

import 'package:js/js.dart';

@JS('Set')
class JSSet {
external JSSet(List<Object?> contents);
}
7 changes: 7 additions & 0 deletions pkg/sass-parser/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export {
ErrorRuleRaws,
} from './src/statement/error-rule';
export {ForRule, ForRuleProps, ForRuleRaws} from './src/statement/for-rule';
export {
ForwardMemberList,
ForwardMemberProps,
ForwardRule,
ForwardRuleProps,
ForwardRuleRaws,
} from './src/statement/forward-rule';
export {
GenericAtRule,
GenericAtRuleProps,
Expand Down
3 changes: 2 additions & 1 deletion pkg/sass-parser/lib/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {LazySource} from './lazy-source';
import {Node} from './node';
import type * as sassInternal from './sass-internal';
import * as utils from './utils';
import {ForwardRule} from './statement/forward-rule';
import {UseRule} from './statement/use-rule';

/**
Expand Down Expand Up @@ -51,7 +52,7 @@ export interface ConfigurationProps {
export class Configuration extends Node {
readonly sassType = 'configuration' as const;
declare raws: ConfigurationRaws;
declare parent: UseRule | undefined; // TODO: forward as well
declare parent: ForwardRule | UseRule | undefined;

/** The underlying map from variable names to their values. */
private _variables: Map<string, ConfiguredVariable> = new Map();
Expand Down
22 changes: 22 additions & 0 deletions pkg/sass-parser/lib/src/sass-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export interface SourceFile {
getText(start: number, end?: number): string;
}

export interface DartSet<T> {
_type: T;

// A brand to make this function as a nominal type.
_unique: 'DartSet';
}

// There may be a better way to declare this, but I can't figure it out.
// eslint-disable-next-line @typescript-eslint/no-namespace
declare namespace SassInternal {
Expand All @@ -37,6 +44,8 @@ declare namespace SassInternal {

function toCssIdentifier(text: string): string;

function setToJS<T>(set: DartSet<T>): Set<T>;

class StatementVisitor<T> {
private _fakePropertyToMakeThisAUniqueType1: T;
}
Expand Down Expand Up @@ -105,6 +114,16 @@ declare namespace SassInternal {
readonly isExclusive: boolean;
}

class ForwardRule extends Statement {
readonly url: Object;
readonly shownMixinsAndFunctions: DartSet<string> | null;
readonly shownVariables: DartSet<string> | null;
readonly hiddenMixinsAndFunctions: DartSet<string> | null;
readonly hiddenVariables: DartSet<string> | null;
readonly prefix: string | null;
readonly configuration: ConfiguredVariable[];
}

class LoudComment extends Statement {
readonly text: Interpolation;
}
Expand Down Expand Up @@ -247,6 +266,7 @@ export type EachRule = SassInternal.EachRule;
export type ErrorRule = SassInternal.ErrorRule;
export type ExtendRule = SassInternal.ExtendRule;
export type ForRule = SassInternal.ForRule;
export type ForwardRule = SassInternal.ForwardRule;
export type LoudComment = SassInternal.LoudComment;
export type MediaRule = SassInternal.MediaRule;
export type SilentComment = SassInternal.SilentComment;
Expand All @@ -273,6 +293,7 @@ export interface StatementVisitorObject<T> {
visitErrorRule(node: ErrorRule): T;
visitExtendRule(node: ExtendRule): T;
visitForRule(node: ForRule): T;
visitForwardRule(node: ForwardRule): T;
visitLoudComment(node: LoudComment): T;
visitMediaRule(node: MediaRule): T;
visitSilentComment(node: SilentComment): T;
Expand All @@ -296,3 +317,4 @@ export const parseIdentifier = sassInternal.parseIdentifier;
export const toCssIdentifier = sassInternal.toCssIdentifier;
export const createStatementVisitor = sassInternal.createStatementVisitor;
export const createExpressionVisitor = sassInternal.createExpressionVisitor;
export const setToJS = sassInternal.setToJS;
Loading

0 comments on commit 75163cc

Please sign in to comment.