From c77d9fe296677293dc4a2f2821b10b25daf68dfe Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Fri, 9 Jun 2023 00:17:18 +0800 Subject: [PATCH] feat: add espree and types use this .d.ts workaround until https://github.com/eslint/espree/issues/529 fixed --- package.json | 1 + pnpm-lock.yaml | 3 +++ src/services/wiki/plugin/zxPlugin.ts | 5 ++++ src/type.d.ts | 39 ++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/package.json b/package.json index 05a9003de..c693e02f2 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "electron-util": "0.17.2", "electron-window-state": "5.0.3", "errio": "1.2.2", + "espree": "^9.5.2", "exponential-backoff": "^3.1.1", "fs-extra": "11.1.1", "git-sync-js": "^1.0.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cc559b179..3dd4460e6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -46,6 +46,9 @@ dependencies: errio: specifier: 1.2.2 version: 1.2.2 + espree: + specifier: ^9.5.2 + version: 9.5.2 exponential-backoff: specifier: ^3.1.1 version: 3.1.1 diff --git a/src/services/wiki/plugin/zxPlugin.ts b/src/services/wiki/plugin/zxPlugin.ts index a19e5ea39..a92966476 100644 --- a/src/services/wiki/plugin/zxPlugin.ts +++ b/src/services/wiki/plugin/zxPlugin.ts @@ -1,4 +1,5 @@ import type { ITiddlyWiki } from '@tiddlygit/tiddlywiki'; +import * as espree from 'espree'; import _ from 'lodash'; import vm, { Context } from 'vm'; @@ -33,6 +34,10 @@ export function executeScriptInTWContext(scriptContent: string, context: ITWVMCo return context.executionResults; } +export function getVariablesFromScript(scriptContent: string): string[] { + espree.parse(scriptContent, { sourceType: 'module' }); +} + export interface ITWVMContext { context: Context; executionResults: string[]; diff --git a/src/type.d.ts b/src/type.d.ts index 00b5db84d..9a33b69d2 100644 --- a/src/type.d.ts +++ b/src/type.d.ts @@ -9,6 +9,45 @@ declare module '@tiddlygit/tiddlywiki' { export * from 'tiddlywiki'; } +declare module 'espree' { + // https://github.com/eslint/espree#options + export interface Options { + comment?: boolean; + ecmaFeatures?: { + globalReturn?: boolean; + impliedStrict?: boolean; + jsx?: boolean; + }; + ecmaVersion?: + | 3 + | 5 + | 6 + | 7 + | 8 + | 9 + | 10 + | 11 + | 12 + | 2015 + | 2016 + | 2017 + | 2018 + | 2019 + | 2020 + | 2021 + | 2022 + | 'latest'; + loc?: boolean; + range?: boolean; + sourceType?: 'script' | 'module'; + tokens?: boolean; + } + // https://github.com/eslint/espree#options + export function parse(code: string, options?: Options): any; + // https://github.com/eslint/espree#tokenize + export function tokenize(code: string, options?: Options): any; +} + declare module 'threads-plugin' { const value: any; export default value;