Skip to content

Commit

Permalink
fix(input-schema): replace escaya with acorn (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Jun 20, 2024
1 parent 1ab0e41 commit 0a0da70
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 55 deletions.
54 changes: 29 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/input_schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://apify.com",
"scripts": {
"build": "npm run clean && npm run compile && npm run copy && ts-node -T scripts/fix-esm.ts",
"build": "npm run clean && npm run compile && npm run copy",
"clean": "rimraf ./dist",
"compile": "tsup",
"copy": "ts-node -T ../../scripts/copy.ts"
Expand All @@ -50,7 +50,7 @@
"dependencies": {
"@apify/consts": "^2.28.0",
"countries-list": "^3.0.0",
"escaya": "^0.0.61"
"acorn-loose": "^8.4.0"
},
"peerDependencies": {
"ajv": "^8.0.0"
Expand Down
15 changes: 0 additions & 15 deletions packages/input_schema/scripts/fix-esm.ts

This file was deleted.

17 changes: 5 additions & 12 deletions packages/input_schema/src/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PROXY_URL_REGEX, URL_REGEX } from '@apify/consts';
import { countries } from 'countries-list';
import { ValidateFunction } from 'ajv';
import { parseScript } from 'escaya';
import { parse } from 'acorn-loose';
import { m } from './intl';
import { parseAjvError } from './input_schema';

Expand Down Expand Up @@ -273,25 +273,18 @@ export function makeInputJsFieldsReadable(json: string, jsFields: string[], json

let ast;
try {
ast = parseScript(maybeFunction, { cst: true });
ast = parse(maybeFunction, { ecmaVersion: 'latest' });
} catch (err) {
// Don't do anything in a case of invalid JS code.
return;
}

const isMultiline = maybeFunction.includes('\n');
const isSingleFunction = ast
&& ast.leafs
&& ast.leafs.length === 1 // Must have only one expression inside!
&& ast.leafs[0]
&& ast.body.length === 1
&& (
// Normal function ...
ast.leafs[0].type === 'FunctionDeclaration'
// or arrow function
|| (
ast.leafs[0].type === 'ExpressionStatement'
&& (ast.leafs[0] as { expression: { type: string } }).expression.type === 'ArrowFunction'
)
ast.body[0].type === 'FunctionDeclaration'
|| (ast.body[0].type === 'ExpressionStatement' && ast.body[0].expression.type === 'ArrowFunctionExpression')
);

// If it's not a function declaration or multiline JS code then we do nothing.
Expand Down
6 changes: 5 additions & 1 deletion test/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,11 @@ describe('timeoutPromise()', () => {
"cookiesPersistence": "PER_PROCESS",
"arrowFunction": async (a, b) => a + b,
"loadCss": false,
"normalFunction": "function INVALID!!! pageFunction(context) {\\n // called on every page the crawler visits, use it to extract data from it\\n const $ = context.jQuery;\\n return 'xxxx';\\n}"
"normalFunction": function INVALID!!! pageFunction(context) {
// called on every page the crawler visits, use it to extract data from it
const $ = context.jQuery;
return 'xxxx';
}
}`;
/* eslint-enable */

Expand Down

0 comments on commit 0a0da70

Please sign in to comment.