Skip to content

Commit

Permalink
refactor: uses native private fields in classes in stead of fake ones
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij committed Dec 27, 2024
1 parent 1cd25c2 commit d9d9ff1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
27 changes: 15 additions & 12 deletions src/cache/cache.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,20 @@ const EMPTY_CACHE = {
const CACHE_FORMAT_VERSION = 16.2;

export default class Cache {
#revisionData;
#cacheStrategy;
#compress;
/**
* @param {cacheStrategyType=} pCacheStrategy
* @param {boolean=} pCompress
*/
constructor(pCacheStrategy, pCompress) {
this.revisionData = null;
this.cacheStrategy =
this.#revisionData = null;
this.#cacheStrategy =
pCacheStrategy === "content"
? new ContentStrategy()
: new MetadataStrategy();
this.compress = pCompress ?? false;
this.#compress = pCompress ?? false;
}

cacheFormatVersionCompatible(pCachedCruiseResult) {
Expand All @@ -71,9 +74,9 @@ export default class Cache {
* @returns {Promise<boolean>}
*/
async canServeFromCache(pCruiseOptions, pCachedCruiseResult, pRevisionData) {
this.revisionData =
this.#revisionData =
pRevisionData ??
(await this.cacheStrategy.getRevisionData(
(await this.#cacheStrategy.getRevisionData(
".",
pCachedCruiseResult,
pCruiseOptions,
Expand All @@ -83,13 +86,13 @@ export default class Cache {
),
},
));
this.revisionData.cacheFormatVersion = CACHE_FORMAT_VERSION;
this.#revisionData.cacheFormatVersion = CACHE_FORMAT_VERSION;
bus.debug("cache: - comparing");
return (
this.cacheFormatVersionCompatible(pCachedCruiseResult) &&
this.cacheStrategy.revisionDataEqual(
this.#cacheStrategy.revisionDataEqual(
pCachedCruiseResult.revisionData,
this.revisionData,
this.#revisionData,
) &&
optionsAreCompatible(
pCachedCruiseResult.summary.optionsUsed,
Expand All @@ -105,7 +108,7 @@ export default class Cache {
async read(pCacheFolder) {
try {
let lPayload = "";
if (this.compress === true) {
if (this.#compress === true) {
const lCompressedPayload = await readFile(
join(pCacheFolder, CACHE_FILE_NAME),
);
Expand Down Expand Up @@ -159,16 +162,16 @@ export default class Cache {
* @param {IRevisionData=} pRevisionData
*/
async write(pCacheFolder, pCruiseResult, pRevisionData) {
let lRevisionData = pRevisionData ?? this.revisionData;
let lRevisionData = pRevisionData ?? this.#revisionData;

await mkdir(pCacheFolder, { recursive: true });
const lUncompressedPayload = JSON.stringify(
this.cacheStrategy.prepareRevisionDataForSaving(
this.#cacheStrategy.prepareRevisionDataForSaving(
pCruiseResult,
lRevisionData,
),
);
let lPayload = this.#compact(lUncompressedPayload, this.compress);
let lPayload = this.#compact(lUncompressedPayload, this.#compress);

// relying on writeFile defaults to 'do the right thing' (i.e. utf8
// when the payload is a string; raw buffer otherwise)
Expand Down
35 changes: 19 additions & 16 deletions src/extract/swc/dependency-visitor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,17 @@ function isInterestingCallExpression(pExoticRequireStrings, pNode) {

export default Visitor
? class SwcDependencyVisitor extends Visitor {
#exoticRequireStrings;
#result;

constructor(pExoticRequireStrings) {
super();
this.lExoticRequireStrings = pExoticRequireStrings;
this.#exoticRequireStrings = pExoticRequireStrings;
}

pushExportSource(pNode) {
#pushExportSource(pNode) {
if (pNode.source) {
this.lResult.push({
this.#result.push({
module: pNode.source.value,
moduleSystem: "es6",
exoticallyRequired: false,
Expand All @@ -132,9 +135,9 @@ export default Visitor
}
}

pushImportSource(pNode) {
#pushImportSource(pNode) {
if (pNode.source) {
this.lResult.push({
this.#result.push({
module: pNode.source.value,
moduleSystem: "es6",
exoticallyRequired: false,
Expand All @@ -144,13 +147,13 @@ export default Visitor
}

visitImportDeclaration(pNode) {
this.pushImportSource(pNode);
this.#pushImportSource(pNode);
return super.visitImportDeclaration(pNode);
}

visitTsImportEqualsDeclaration(pNode) {
if (pNode.moduleRef.type === "TsExternalModuleReference") {
this.lResult.push({
this.#result.push({
module: pNode.moduleRef.expression.value,
moduleSystem: "cjs",
exoticallyRequired: false,
Expand All @@ -164,7 +167,7 @@ export default Visitor
// To anticipate that (and to remain backward compatible when that happens)
// also include the same method, but with the correct spelling.
visitExportAllDeclration(pNode) {
this.pushExportSource(pNode);
this.#pushExportSource(pNode);
/* c8 ignore start */
// @ts-expect-error see above
if (super.visitExportAllDeclration) {
Expand All @@ -184,7 +187,7 @@ export default Visitor

// same spelling error as the above - same solution
visitExportNamedDeclration(pNode) {
this.pushExportSource(pNode);
this.#pushExportSource(pNode);
/* c8 ignore start */
// @ts-expect-error see above
if (super.visitExportNamedDeclration) {
Expand All @@ -203,10 +206,10 @@ export default Visitor

visitCallExpression(pNode) {
if (
isInterestingCallExpression(this.lExoticRequireStrings, pNode) &&
isInterestingCallExpression(this.#exoticRequireStrings, pNode) &&
argumentsAreUsable(pNode.arguments)
) {
this.lResult.push({
this.#result.push({
module: pryStringsFromArguments(pNode.arguments),

...(isImportCallExpression(pNode)
Expand All @@ -232,8 +235,8 @@ export default Visitor
}

// using "window.require" as an exotic require string...
this.lResult = this.lResult.concat(
extractExoticMemberCallExpression(pNode, this.lExoticRequireStrings),
this.#result = this.#result.concat(
extractExoticMemberCallExpression(pNode, this.#exoticRequireStrings),
);

return super.visitCallExpression(pNode);
Expand All @@ -251,7 +254,7 @@ export default Visitor
// implemented yet (1.2.51) pNode can come in as null (also see
// comments in accompanying unit test)
if (pNode && pNode.typeAnnotation && pNode.typeAnnotation.argument)
this.lResult.push({
this.#result.push({
module: pNode.typeAnnotation.argument.value,
moduleSystem: "es6",
exoticallyRequired: false,
Expand All @@ -264,9 +267,9 @@ export default Visitor
// visitTrippleSlashDirective(pNode)) {}

getDependencies(pAST) {
this.lResult = [];
this.#result = [];
this.visitModule(pAST);
return this.lResult;
return this.#result;
}
}
: /* c8 ignore start */
Expand Down
7 changes: 3 additions & 4 deletions src/graph-utl/indexed-module-graph.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

export default class IndexedModuleGraph {
#indexedGraph;
/**
* @param {IModuleOrFolder} pModule
* @returns {IVertex}
Expand All @@ -25,7 +26,7 @@ export default class IndexedModuleGraph {
}

#init(pModules, pIndexAttribute) {
this.indexedGraph = new Map(
this.#indexedGraph = new Map(
pModules.map((pModule) => [
pModule[pIndexAttribute],
this.#normalizeModule(pModule),
Expand All @@ -46,9 +47,7 @@ export default class IndexedModuleGraph {
* @return {IVertex}
*/
findVertexByName(pName) {
// @ts-expect-error tsc seems to think indexedGraph can be undefined. However,
// in the constructor it's always set to a Map, and the init method is private
return this.indexedGraph.get(pName);
return this.#indexedGraph.get(pName);
}

/**
Expand Down

0 comments on commit d9d9ff1

Please sign in to comment.