Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: detect a wider range of scheduled functions (#1105)
* fix: detect a wider range of scheduled functions scheduled functions which are reassigned or not directly exported are now detected * Update src/runtimes/node/parser/bindings.ts Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com> * Update src/runtimes/node/parser/exports.ts Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com> * Update src/runtimes/node/parser/exports.ts Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com> * Apply suggestions from code review Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com> * refactor: clean up code Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
- Loading branch information
1 parent
123e7e8
commit 32a7387
Showing
9 changed files
with
147 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { Expression, Statement, VariableDeclaration } from '@babel/types' | ||
|
||
type Bindings = Map<string, Expression> | ||
|
||
const getBindingFromVariableDeclaration = function (node: VariableDeclaration, bindings: Bindings): void { | ||
node.declarations.forEach((declaration) => { | ||
if (declaration.id.type === 'Identifier' && declaration.init) { | ||
bindings.set(declaration.id.name, declaration.init) | ||
} | ||
}) | ||
} | ||
|
||
// eslint-disable-next-line complexity | ||
const getBindingsFromNode = function (node: Statement, bindings: Bindings): void { | ||
if (node.type === 'VariableDeclaration') { | ||
// A variable was created, so create it and store the potential value | ||
getBindingFromVariableDeclaration(node, bindings) | ||
} else if ( | ||
node.type === 'ExpressionStatement' && | ||
node.expression.type === 'AssignmentExpression' && | ||
node.expression.left.type === 'Identifier' | ||
) { | ||
// The variable was reassigned, so let's store the new value | ||
bindings.set(node.expression.left.name, node.expression.right) | ||
} else if (node.type === 'ExportNamedDeclaration' && node.declaration?.type === 'VariableDeclaration') { | ||
// A `export const|let ...` creates a binding that can later be referenced again | ||
getBindingFromVariableDeclaration(node.declaration, bindings) | ||
} | ||
} | ||
|
||
/** | ||
* Goes through all relevant nodes and creates a map from binding name to assigned value/expression | ||
*/ | ||
const getAllBindings = function (nodes: Statement[]): Bindings { | ||
const bindings: Bindings = new Map() | ||
|
||
nodes.forEach((node) => { | ||
getBindingsFromNode(node, bindings) | ||
}) | ||
|
||
return bindings | ||
} | ||
|
||
export type BindingMethod = () => Bindings | ||
|
||
export const createBindingsMethod = function (nodes: Statement[]): BindingMethod { | ||
// memoize the result for these nodes | ||
let result: Bindings | ||
return () => { | ||
if (!result) { | ||
result = getAllBindings(nodes) | ||
} | ||
|
||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
tests/fixtures/in-source-config/functions/cron_esm_no_direct_export.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { schedule } from '@netlify/functions' | ||
|
||
const handler = schedule('@daily', async () => { | ||
// function handler | ||
}) | ||
|
||
export { handler } |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/in-source-config/functions/cron_esm_no_direct_export_renamed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { schedule } from '@netlify/functions' | ||
|
||
const _handler = schedule('@daily', async () => { | ||
// function handler | ||
}) | ||
export { _handler as handler } |
8 changes: 8 additions & 0 deletions
8
tests/fixtures/in-source-config/functions/cron_esm_no_direct_export_renamed_reassigned.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { schedule } from '@netlify/functions' | ||
|
||
const handler = async () => { | ||
// function handler | ||
} | ||
|
||
const _handler = schedule('@daily', handler) | ||
export { _handler as handler } |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/in-source-config/functions/cron_esm_schedule_reassigned.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { schedule } from '@netlify/functions' | ||
|
||
const SCHEDULE = '@daily' | ||
|
||
export const handler = schedule(SCHEDULE, async () => { | ||
// function handler | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32a7387
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⏱ Benchmark results
largeDepsEsbuild: 7.5s
largeDepsNft: 37.3s
largeDepsZisi: 56.3s