Skip to content

Commit

Permalink
Implement access tracking for containingUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme committed Apr 12, 2024
1 parent fd32e8e commit 24f095b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 15 deletions.
32 changes: 32 additions & 0 deletions lib/src/canonicalize-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 Google LLC. 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.

export class CanonicalizeContext {
readonly fromImport: boolean;

private readonly _containingUrl: URL | null;

get containingUrl(): URL | null {
this._containingUrlAccessed = true;
return this._containingUrl;
}

private _containingUrlAccessed = false;

/**
* Whether the `containingUrl` getter has been accessed.
*
* This is marked as public so that the importer registry can access it, but
* it's not part of the package's public API and should not be accessed by
* user code. It may be renamed or removed without warning in the future.
*/
get containingUrlAccessed(): boolean {
return this._containingUrlAccessed;
}

constructor(containingUrl: URL | null, fromImport: boolean) {
this._containingUrl = containingUrl;
this.fromImport = fromImport;
}
}
27 changes: 15 additions & 12 deletions lib/src/importer-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as p from 'path';
import {URL} from 'url';
import {inspect} from 'util';

import {CanonicalizeContext} from './canonicalize-context';
import * as utils from './utils';
import {FileImporter, Importer, Options} from './vendor/sass';
import * as proto from './vendor/embedded_sass_pb';
Expand Down Expand Up @@ -115,21 +116,22 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
throw utils.compilerError('Unknown CanonicalizeRequest.importer_id');
}

const canonicalizeContext = new CanonicalizeContext(
request.containingUrl ? new URL(request.containingUrl) : null,
request.fromImport
);

return catchOr(
() => {
return thenOr(
importer.canonicalize(request.url, {
fromImport: request.fromImport,
containingUrl: request.containingUrl
? new URL(request.containingUrl)
: null,
}),
importer.canonicalize(request.url, canonicalizeContext),
url =>
new proto.InboundMessage_CanonicalizeResponse({
result:
url === null
? {case: undefined}
: {case: 'url', value: url.toString()},
containingUrlUnused: !canonicalizeContext.containingUrlAccessed,
})
);
},
Expand Down Expand Up @@ -197,15 +199,15 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
throw utils.compilerError('Unknown FileImportRequest.importer_id');
}

const canonicalizeContext = new CanonicalizeContext(
request.containingUrl ? new URL(request.containingUrl) : null,
request.fromImport
);

return catchOr(
() => {
return thenOr(
importer.findFileUrl(request.url, {
fromImport: request.fromImport,
containingUrl: request.containingUrl
? new URL(request.containingUrl)
: null,
}),
importer.findFileUrl(request.url, canonicalizeContext),
url => {
if (!url) return new proto.InboundMessage_FileImportResponse();
if (url.protocol !== 'file:') {
Expand All @@ -216,6 +218,7 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
}
return new proto.InboundMessage_FileImportResponse({
result: {case: 'fileUrl', value: url.toString()},
containingUrlUnused: !canonicalizeContext.containingUrlAccessed,
});
}
);
Expand Down
8 changes: 6 additions & 2 deletions lib/src/value/argument-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ export class SassArgumentList extends SassList {
*/
readonly keywordsInternal: OrderedMap<string, Value>;

private _keywordsAccessed = false;

/**
* Whether the `keywords` getter has been accessed.
*
* This is marked as public so that the protofier can access it, but it's not
* part of the package's public API and should not be accessed by user code.
* It may be renamed or removed without warning in the future.
*/
keywordsAccessed = false;
get keywordsAccessed(): boolean {
return this._keywordsAccessed;
}

get keywords(): OrderedMap<string, Value> {
this.keywordsAccessed = true;
this._keywordsAccessed = true;
return this.keywordsInternal;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sass-embedded",
"version": "1.75.0",
"protocol-version": "2.6.0",
"protocol-version": "2.7.0",
"compiler-version": "1.75.0",
"description": "Node.js library that communicates with Embedded Dart Sass using the Embedded Sass protocol",
"repository": "sass/embedded-host-node",
Expand Down

0 comments on commit 24f095b

Please sign in to comment.