-
Notifications
You must be signed in to change notification settings - Fork 361
Add support for parsing selector expressions #2533
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
+174
−7
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
pkg/sass-parser/lib/src/expression/__snapshots__/selector.test.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`a selector expression toJSON 1`] = ` | ||
{ | ||
"inputs": [ | ||
{ | ||
"css": "@#{&}", | ||
"hasBOM": false, | ||
"id": "<input css _____>", | ||
}, | ||
], | ||
"raws": {}, | ||
"sassType": "selector-expr", | ||
"source": <1:4-1:5 in 0>, | ||
} | ||
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2025 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 {SelectorExpression} from '../..'; | ||
import * as utils from '../../../test/utils'; | ||
|
||
describe('a selector expression', () => { | ||
let node: SelectorExpression; | ||
|
||
function describeNode( | ||
description: string, | ||
create: () => SelectorExpression, | ||
): void { | ||
describe(description, () => { | ||
beforeEach(() => void (node = create())); | ||
|
||
it('has sassType selector', () => expect(node.sassType).toBe('selector-expr')); | ||
}); | ||
} | ||
|
||
describeNode('parsed', () => utils.parseExpression('&')); | ||
|
||
describe('constructed manually', () => { | ||
describeNode('without props', () => new SelectorExpression()); | ||
|
||
describeNode('with empty props', () => new SelectorExpression({})); | ||
}); | ||
|
||
it('stringifies', () => expect(new SelectorExpression().toString()).toBe('&')); | ||
|
||
describe('clone', () => { | ||
let original: SelectorExpression; | ||
|
||
beforeEach(() => void (original = utils.parseExpression('&'))); | ||
|
||
describe('with no overrides', () => { | ||
let clone: SelectorExpression; | ||
|
||
beforeEach(() => void (clone = original.clone())); | ||
|
||
describe('has the same properties:', () => { | ||
it('raws', () => expect(clone.raws).toEqual({})); | ||
|
||
it('source', () => expect(clone.source).toBe(original.source)); | ||
}); | ||
|
||
it('creates a new self', () => expect(clone).not.toBe(original)); | ||
}); | ||
|
||
describe('overrides', () => { | ||
describe('raws', () => { | ||
it('defined', () => | ||
expect(original.clone({raws: {}}).raws).toEqual({})); | ||
|
||
it('undefined', () => | ||
expect(original.clone({raws: undefined}).raws).toEqual({})); | ||
}); | ||
}); | ||
}); | ||
|
||
it('toJSON', () => expect(utils.parseExpression('&')).toMatchSnapshot()); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2025 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 * 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 '.'; | ||
|
||
/** | ||
* The initializer properties for {@link SelectorExpression}. | ||
* | ||
* Unlike other expression types, this can't be initialized by properties alone, | ||
* since it doesn't have any properties to set. | ||
* | ||
* @category Expression | ||
*/ | ||
export interface SelectorExpressionProps extends NodeProps { | ||
raws?: SelectorExpressionRaws; | ||
} | ||
|
||
/** | ||
* Raws indicating how to precisely serialize a {@link SelectorExpression}. | ||
* | ||
* @category Expression | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- No raws for a selector expression yet. | ||
export interface SelectorExpressionRaws {} | ||
|
||
/** | ||
* An expression representing the current selector in Sass. | ||
* | ||
* @category Expression | ||
*/ | ||
export class SelectorExpression extends Expression { | ||
readonly sassType = 'selector-expr' as const; | ||
declare raws: SelectorExpressionRaws; | ||
|
||
constructor(defaults?: SelectorExpressionProps); | ||
/** @hidden */ | ||
constructor(_: undefined, inner: sassInternal.SelectorExpression); | ||
constructor(defaults?: object, inner?: sassInternal.SelectorExpression) { | ||
super(defaults); | ||
if (inner) this.source = new LazySource(inner); | ||
} | ||
|
||
clone(overrides?: Partial<SelectorExpressionProps>): this { | ||
return utils.cloneNode(this, overrides, ['raws']); | ||
} | ||
|
||
toJSON(): object; | ||
/** @hidden */ | ||
toJSON(_: string, inputs: Map<postcss.Input, number>): object; | ||
toJSON(_?: string, inputs?: Map<postcss.Input, number>): object { | ||
return utils.toJSON(this, [], inputs); | ||
} | ||
|
||
/** @hidden */ | ||
toString(): string { | ||
return '&'; | ||
} | ||
|
||
/** @hidden */ | ||
get nonStatementChildren(): ReadonlyArray<Expression> { | ||
return []; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: to match the Dart library
sass_api
docs, mention this is specifically&