Skip to content

Commit

Permalink
Fix and add specs for relative imports with compileString() (#1785)
Browse files Browse the repository at this point in the history
The newly-tested behavior matches the specification as updated by
sass/sass#3278.
  • Loading branch information
nex3 authored Apr 12, 2022
1 parent 98c183b commit 4c5fe23
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
22 changes: 19 additions & 3 deletions js-api-spec/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('compileString', () => {
});
});

it('url is used to resolve relative loads', () =>
it('file: url is used to resolve relative loads', () =>
sandbox(dir => {
dir.write({'foo/bar/_other.scss': 'a {b: c}'});

Expand Down Expand Up @@ -244,14 +244,30 @@ describe('compileString', () => {

it('relative loads fail without a URL', () =>
sandbox(dir => {
dir.write({'other.scss': 'a {b: c}'});
dir.write({'_other.scss': 'a {b: c}'});

expect(() => compileString('@use "other";')).toThrowSassException({
expect(() =>
compileString(`@use "${dir.relativeUrl('other')}";`)
).toThrowSassException({
line: 0,
noUrl: true,
});
}));

it('relative loads fail with a non-file: URL', () =>
sandbox(dir => {
dir.write({'_other.scss': 'a {b: c}'});

expect(() =>
compileString(`@use "${dir.relativeUrl('other')}";`, {
url: new URL('unknown:style.scss'),
})
).toThrowSassException({
line: 0,
url: 'unknown:style.scss',
});
}));

describe('includes source span information', () => {
it('in syntax errors', () =>
sandbox(dir => {
Expand Down
12 changes: 12 additions & 0 deletions js-api-spec/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export async function sandbox(
Object.assign((...paths: string[]) => p.join(testDir, ...paths), {
root: testDir,
url: (...paths: string[]) => pathToFileURL(p.join(testDir, ...paths)),
relativeUrl: (...paths: string[]) => {
const path = p.relative(process.cwd(), p.join(testDir, ...paths));
const components =
p.sep === '\\' ? path.split(/[/\\]/) : path.split('/');
return components.map(encodeURIComponent).join('/');
},
write: (paths: {[path: string]: string}) => {
for (const [path, contents] of Object.entries(paths)) {
const fullPath = p.join(testDir, path);
Expand Down Expand Up @@ -74,6 +80,12 @@ interface SandboxDirectory {
*/
url(...paths: string[]): URL;

/**
* Joins `paths` underneath `root` and converts the result to a relative
* `file:`-style URL.
*/
relativeUrl(...paths: string[]): string;

/**
* Writes `paths` to disk within this directory.
*
Expand Down

0 comments on commit 4c5fe23

Please sign in to comment.