From 8c95ba07d62608ca77eeb02db78dd4a5afe10a8c Mon Sep 17 00:00:00 2001 From: Freeman <46896789+soranoo@users.noreply.github.com> Date: Sat, 28 Sep 2024 21:54:15 +0100 Subject: [PATCH] fix(js-ast): added support to `MemberExpression` #45 --- README.md | 2 +- package.json | 2 +- src/handlers/js-ast.test.ts | 25 +++++++++++++++++++++++++ src/handlers/js-ast.ts | 35 ++++++++++++++++++++++++++++------- src/types.ts | 20 ++++++++++++++++---- 5 files changed, 71 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 832ff5e..8e41189 100644 --- a/README.md +++ b/README.md @@ -273,7 +273,7 @@ module.exports = { /\.next\/server\/pages\/api/, /_document..*js/, /_app-.*/, - /__.*/, // <= maybe helpful if you are using Next.js Lcal Fonts [1*] + /__.*/, // <= maybe helpful if you are using Next.js Local Fonts [1*] ], }; ``` diff --git a/package.json b/package.json index eacbef1..531e138 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "next-css-obfuscator", - "version": "2.2.16", + "version": "2.2.17", "description": "A package deeply inspired by PostCSS-Obfuscator but for Next.js.", "main": "dist/index.js", "type": "commonjs", diff --git a/src/handlers/js-ast.test.ts b/src/handlers/js-ast.test.ts index 8fb7431..fb0c729 100644 --- a/src/handlers/js-ast.test.ts +++ b/src/handlers/js-ast.test.ts @@ -1331,7 +1331,32 @@ describe("searchStringLiterals", () => { expect(stripCode(generator(ast, {}, code).code)).toEqual(stripCode(expectedCode)); }); + //? ******************************* + //? Member Expressions + //? ******************************* + + it("should handle member expressions correctly", () => { + const code = ` + function startPoint() { + "className_A".replace("className_A", "className_B"); + } + ` + const expectedCode = ` + function startPoint() { + "{{found}}".replace("{{found}}", "{{found}}"); + } + ` + + const ast = parser.parse(code); + let result: string[] = []; + searchStringLiterals(findStartPointNode(ast)!, (str) => { + result.push(str); + return "{{found}}" + }); + expect(result).toEqual(["className_A", "className_A", "className_B"]); + expect(stripCode(generator(ast, {}, code).code)).toEqual(stripCode(expectedCode)); + }); }); diff --git a/src/handlers/js-ast.ts b/src/handlers/js-ast.ts index aadf976..bd80fd3 100644 --- a/src/handlers/js-ast.ts +++ b/src/handlers/js-ast.ts @@ -110,13 +110,15 @@ function searchStringLiterals(path: NodePath, /* function return statement */ else if (t.isReturnStatement(path.node)) { const argument = path.get("argument"); - if (argument && !Array.isArray(argument)) { - searchStringLiterals(argument, callback); - } else if (Array.isArray(argument)) { - argument.forEach(arg => { - searchStringLiterals(arg, callback, scannedNodes); - }); - } + if (argument) { + if (!Array.isArray(argument)) { + searchStringLiterals(argument, callback, scannedNodes); + } else { + argument.forEach(arg => { + searchStringLiterals(arg, callback, scannedNodes); + }); + } + } } /* binary expression (e.g. const a = "hello" + "world") */ else if (t.isBinaryExpression(path.node)) { @@ -292,6 +294,25 @@ function searchStringLiterals(path: NodePath, searchStringLiterals(handlerBody, callback, scannedNodes); } } + } + /* member expression (e.g. "scroll-top".replace("-", "_")); "scroll-top ".concat("visible"); */ + else if (t.isMemberExpression(path.node)) { + const object = path.get("object"); + const property = path.get("property"); + const argument = path.get("argument"); + if (object && !Array.isArray(object)) { + searchStringLiterals(object, callback, scannedNodes); + } + if (property && !Array.isArray(property)) { + searchStringLiterals(property, callback, scannedNodes); + } + if (argument && !Array.isArray(argument)) { + searchStringLiterals(argument, callback, scannedNodes); + } else if (Array.isArray(argument)) { + argument.forEach(arg => { + searchStringLiterals(arg, callback, scannedNodes); + }); + } } else { path.traverse({ Identifier(innerPath) { diff --git a/src/types.ts b/src/types.ts index 46e6cf1..a45bdf6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,8 +18,14 @@ type Options = { whiteListedFolderPaths: (string | RegExp)[]; blackListedFolderPaths: (string | RegExp)[]; - includeAnyMatchRegexes?: RegExp[]; //! @deprecated - excludeAnyMatchRegexes?: RegExp[]; //! @deprecated + /** + * @deprecated + */ + includeAnyMatchRegexes?: RegExp[]; + /** + * @deprecated + */ + excludeAnyMatchRegexes?: RegExp[]; enableMarkers: boolean; markers: string[]; removeMarkersAfterObfuscated: boolean; @@ -46,8 +52,14 @@ type OptionalOptions = { whiteListedFolderPaths?: (string | RegExp)[]; blackListedFolderPaths?: (string | RegExp)[]; - includeAnyMatchRegexes?: RegExp[]; //! @deprecated - excludeAnyMatchRegexes?: RegExp[]; //! @deprecated + /** + * @deprecated + */ + includeAnyMatchRegexes?: RegExp[]; + /** + * @deprecated + */ + excludeAnyMatchRegexes?: RegExp[]; enableMarkers?: boolean; markers?: string[]; removeMarkersAfterObfuscated?: boolean;