From ed8394bf844ef1391e1784ccc771d8eee46337e4 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 25 Oct 2022 11:47:54 -0500 Subject: [PATCH 1/2] pybricksMicropython/lib: handle exceptions when parsing The new code parsing library raises exceptions when there are syntax errors, so this needs to be handled. Fixes: https://github.com/pybricks/support/issues/755 --- CHANGELOG.md | 5 +++++ src/pybricksMicropython/lib.ts | 36 ++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 470d5749..00c02193 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ ## [Unreleased] +### Fixed +- Fixed crash when user program contains syntax error ([support#755]). + +[support#755]: https://github.com/pybricks/support/issues/755 + ## [2.0.0-beta.7] - 2022-10-24 ### Fixed diff --git a/src/pybricksMicropython/lib.ts b/src/pybricksMicropython/lib.ts index 585b37b9..6b8bf635 100644 --- a/src/pybricksMicropython/lib.ts +++ b/src/pybricksMicropython/lib.ts @@ -74,25 +74,37 @@ export function validateFileName( /** * Finds modules imported by a Python script. * + * Returns an empty list if there are syntax errors. + * * @param py A Python Script. * @returns A list of the names of modules imported by this file. */ export function findImportedModules(py: string): ReadonlySet { const modules = new Set(); - const tree = parse(py); - // find all import statements in the syntax tree and collect imported modules - walk(tree, { - onEnterNode(node, _ancestors) { - if (node.type === 'import') { - for (const name of node.names) { - modules.add(name.path); + try { + const tree = parse(py); + + // find all import statements in the syntax tree and collect imported modules + walk(tree, { + onEnterNode(node, _ancestors) { + if (node.type === 'import') { + for (const name of node.names) { + modules.add(name.path); + } + } else if (node.type === 'from') { + modules.add(node.base); } - } else if (node.type === 'from') { - modules.add(node.base); - } - }, - }); + }, + }); + } catch (err) { + // istanbul ignore if + if (process.env.NODE_ENV === 'development') { + console.debug(err); + } + + // files with syntax errors are ignored + } return modules; } From c763b2ef723499dfb2aea34bacace291b2e0e995 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 25 Oct 2022 11:49:58 -0500 Subject: [PATCH 2/2] v2.0.0-beta.8 --- CHANGELOG.md | 5 ++++- package.json | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00c02193..aa9122cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ## [Unreleased] +## [2.0.0-beta.8] - 2022-10-25 + ### Fixed - Fixed crash when user program contains syntax error ([support#755]). @@ -435,7 +437,8 @@ Prerelease changes are documented at [support#48]. -[Unreleased]: https://github.com/pybricks/pybricks-code/compare/v2.0.0-beta.7...HEAD +[Unreleased]: https://github.com/pybricks/pybricks-code/compare/v2.0.0-beta.8...HEAD +[2.0.0-beta.8]: https://github.com/pybricks/pybricks-code/compare/v2.0.0-beta.7...v2.0.0-beta.8 [2.0.0-beta.7]: https://github.com/pybricks/pybricks-code/compare/v2.0.0-beta.6...v2.0.0-beta.7 [2.0.0-beta.6]: https://github.com/pybricks/pybricks-code/compare/v2.0.0-beta.5...v2.0.0-beta.6 [2.0.0-beta.5]: https://github.com/pybricks/pybricks-code/compare/v2.0.0-beta.4...v2.0.0-beta.5 diff --git a/package.json b/package.json index bba0979b..5d14a9e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@pybricks/pybricks-code", - "version": "2.0.0-beta.7", + "version": "2.0.0-beta.8", "license": "MIT", "author": "The Pybricks Authors", "repository": {