Skip to content

Commit

Permalink
fix(js-ast): added support to MemberExpression #45
Browse files Browse the repository at this point in the history
  • Loading branch information
soranoo committed Sep 28, 2024
1 parent 474f8e8 commit 8c95ba0
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*]
],
};
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 25 additions & 0 deletions src/handlers/js-ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

});

Expand Down
35 changes: 28 additions & 7 deletions src/handlers/js-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ function searchStringLiterals(path: NodePath<t.Node>,
/* 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)) {
Expand Down Expand Up @@ -292,6 +294,25 @@ function searchStringLiterals(path: NodePath<t.Node>,
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) {
Expand Down
20 changes: 16 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 8c95ba0

Please sign in to comment.