From b50d8014f976b5169517c0600d7d3f36a80ec4ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Tue, 11 Mar 2025 13:13:26 -0700 Subject: [PATCH 1/3] Disallow passing functions/mixins across compilations --- js-api-spec/value/function.test.ts | 42 +++++++++++++++++++++++ js-api-spec/value/mixin.test.ts | 53 +++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/js-api-spec/value/function.test.ts b/js-api-spec/value/function.test.ts index c56b8b1b3..2432a434b 100644 --- a/js-api-spec/value/function.test.ts +++ b/js-api-spec/value/function.test.ts @@ -80,3 +80,45 @@ describe('rejects a function signature that', () => { it('has no closing parenthesis', () => rejectsSignature('foo(')); it('has a non-identifier name', () => rejectsSignature('$foo()')); }); + +it('rejects a compiler function from a different compilation', () => { + let plusOne: Value | undefined; + compileString( + ` + @use 'sass:meta'; + + @function plusOne($n) {@return $n + 1} + a {b: meta.call(foo(meta.get-function('plusOne')), 2)} + `, + { + functions: { + 'foo($arg)': (args: Value[]) => { + plusOne = args[0]; + return plusOne; + }, + }, + } + ); + + let plusTwo; + expect(() => { + compileString( + ` + @use 'sass:meta'; + + @function plusTwo($n) {@return $n + 2} + a {b: meta.call(foo(meta.get-function('plusTwo')), 2)} + `, + { + functions: { + 'foo($arg)': (args: Value[]) => { + plusTwo = args[0]; + return plusOne!; + }, + }, + } + ); + }).toThrowSassException({line: 4}); + + expect(plusOne).not.toEqual(plusTwo); +}); diff --git a/js-api-spec/value/mixin.test.ts b/js-api-spec/value/mixin.test.ts index a1413bfa8..44cf71ee3 100644 --- a/js-api-spec/value/mixin.test.ts +++ b/js-api-spec/value/mixin.test.ts @@ -2,7 +2,7 @@ // MIT-style license that can be found in the LICENSE file or at // https://opensource.org/licenses/MIT. -import {SassMixin, compileString} from 'sass'; +import {SassMixin, compileString, Value} from 'sass'; import {spy} from '../utils'; @@ -44,3 +44,54 @@ it('can round-trip a mixin reference from Sass', () => { expect(fn).toHaveBeenCalled(); }); + +it('rejects a compiler mixin from a different compilation', () => { + let a: Value | undefined; + compileString( + ` + @use 'sass:meta'; + + @mixin a() { + a { + b: c; + } + } + + @include meta.apply(foo(meta.get-mixin('a'))); + `, + { + functions: { + 'foo($arg)': (args: Value[]) => { + a = args[0]; + return a; + }, + }, + } + ); + + let b; + expect(() => { + compileString( + ` + @use 'sass:meta'; + + @mixin b() { + c { + d: e; + } + } + @include meta.apply(foo(meta.get-mixin('b'))); + `, + { + functions: { + 'foo($arg)': (args: Value[]) => { + b = args[0]; + return a!; + }, + }, + } + ); + }).toThrowSassException({line: 8}); + + expect(a).not.toEqual(b); +}); From a917d71c66a80a3d9cc8c787809b315312c393d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Thu, 8 May 2025 12:34:57 -0700 Subject: [PATCH 2/3] Code review Co-authored-by: Natalie Weizenbaum --- js-api-spec/value/function.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/js-api-spec/value/function.test.ts b/js-api-spec/value/function.test.ts index 2432a434b..c92294f62 100644 --- a/js-api-spec/value/function.test.ts +++ b/js-api-spec/value/function.test.ts @@ -85,11 +85,11 @@ it('rejects a compiler function from a different compilation', () => { let plusOne: Value | undefined; compileString( ` - @use 'sass:meta'; + @use 'sass:meta'; - @function plusOne($n) {@return $n + 1} - a {b: meta.call(foo(meta.get-function('plusOne')), 2)} - `, + @function plusOne($n) {@return $n + 1} + a {b: meta.call(foo(meta.get-function('plusOne')), 2)} + `, { functions: { 'foo($arg)': (args: Value[]) => { From ddf5db8961dbbd202e36d942216faa887961a8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=81=A4=E3=81=8D?= Date: Thu, 8 May 2025 12:36:55 -0700 Subject: [PATCH 3/3] Code review --- js-api-spec/value/function.test.ts | 8 ++++---- js-api-spec/value/mixin.test.ts | 28 ++++++++++++++-------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/js-api-spec/value/function.test.ts b/js-api-spec/value/function.test.ts index c92294f62..b423625fe 100644 --- a/js-api-spec/value/function.test.ts +++ b/js-api-spec/value/function.test.ts @@ -104,11 +104,11 @@ it('rejects a compiler function from a different compilation', () => { expect(() => { compileString( ` - @use 'sass:meta'; + @use 'sass:meta'; - @function plusTwo($n) {@return $n + 2} - a {b: meta.call(foo(meta.get-function('plusTwo')), 2)} - `, + @function plusTwo($n) {@return $n + 2} + a {b: meta.call(foo(meta.get-function('plusTwo')), 2)} + `, { functions: { 'foo($arg)': (args: Value[]) => { diff --git a/js-api-spec/value/mixin.test.ts b/js-api-spec/value/mixin.test.ts index 44cf71ee3..e1d05f197 100644 --- a/js-api-spec/value/mixin.test.ts +++ b/js-api-spec/value/mixin.test.ts @@ -49,16 +49,16 @@ it('rejects a compiler mixin from a different compilation', () => { let a: Value | undefined; compileString( ` - @use 'sass:meta'; + @use 'sass:meta'; - @mixin a() { - a { - b: c; + @mixin a() { + a { + b: c; + } } - } - @include meta.apply(foo(meta.get-mixin('a'))); - `, + @include meta.apply(foo(meta.get-mixin('a'))); + `, { functions: { 'foo($arg)': (args: Value[]) => { @@ -73,15 +73,15 @@ it('rejects a compiler mixin from a different compilation', () => { expect(() => { compileString( ` - @use 'sass:meta'; + @use 'sass:meta'; - @mixin b() { - c { - d: e; + @mixin b() { + c { + d: e; + } } - } - @include meta.apply(foo(meta.get-mixin('b'))); - `, + @include meta.apply(foo(meta.get-mixin('b'))); + `, { functions: { 'foo($arg)': (args: Value[]) => {