This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
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
scheduled functions which are reassigned or not directly exported are now detected
- Loading branch information
Showing
9 changed files
with
139 additions
and
19 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,55 @@ | ||
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 lets 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 | null = null | ||
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