From a57a945762754c2309ef413151be714f5415d503 Mon Sep 17 00:00:00 2001 From: ambar Date: Sat, 27 Apr 2024 09:51:59 +0800 Subject: [PATCH] style: lint:fix-unsafe --- package.json | 14 ++++++++++--- scripts/build-extension.mjs | 27 ++++++++++++------------- scripts/build-preview.mjs | 25 +++++++++++------------ scripts/build.mjs | 27 ++++++++++++------------- scripts/copy.mjs | 4 ++-- src/extension.ts | 17 ++++++++-------- src/extension/__tests__/bundle.spec.ts | 12 +++++------ src/extension/bundle.ts | 2 +- src/extension/install.ts | 4 ++-- src/sandbox/__tests__/nodeVM.spec.ts | 14 ++++++------- src/sandbox/__tests__/transform.spec.ts | 8 ++++---- src/sandbox/esm.ts | 2 +- src/sandbox/nodeVM.ts | 4 ++-- src/sandbox/nodeWorker.js | 8 ++++---- src/sandbox/transform.ts | 4 ++-- yarn.lock | 16 +++++++++++++++ 16 files changed, 104 insertions(+), 84 deletions(-) diff --git a/package.json b/package.json index 495becf..6b833a0 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,9 @@ "engines": { "vscode": "^1.61.0" }, - "categories": ["Programming Languages"], + "categories": [ + "Programming Languages" + ], "activationEvents": [ "onLanguage:javascript", "onLanguage:javascriptreact", @@ -143,8 +145,12 @@ }, "jest": { "preset": "es-jest", - "transformIgnorePatterns": ["/node_modules/(?!(node-fetch-cache)/)"], - "collectCoverageFrom": ["src/**/*"] + "transformIgnorePatterns": [ + "/node_modules/(?!(node-fetch-cache)/)" + ], + "collectCoverageFrom": [ + "src/**/*" + ] }, "dependencies": { "esbuild": "^0.20.2" @@ -155,6 +161,7 @@ "@esbuild-plugins/node-modules-polyfill": "^0.1.2", "@recommended/biome-config": "^1.0.0", "@types/jest": "^27.0.2", + "@types/minimist": "^1", "@types/node": "14.x", "@types/react": "^17.0.33", "@types/react-dom": "^17.0.10", @@ -167,6 +174,7 @@ "is-promise": "^4.0.0", "jest": "^29.7.0", "ldrs": "^1.0.1", + "minimist": "^1.2.8", "node-fetch-cache": "^3.0.3", "pretty-format": "^27.3.1", "react": "^17.0.2", diff --git a/scripts/build-extension.mjs b/scripts/build-extension.mjs index f68b551..8905dae 100644 --- a/scripts/build-extension.mjs +++ b/scripts/build-extension.mjs @@ -1,5 +1,5 @@ -import {execSync} from 'child_process' -import {builtinModules} from 'module' +import {execSync} from 'node:child_process' +import {builtinModules} from 'node:module' import * as esbuild from 'esbuild' /* eslint-disable no-console */ import minimist from 'minimist' @@ -45,19 +45,18 @@ const main = async () => { if (errors.length) { throw new Error(esbuild.formatMessages(errors)) - } else { - if (args.analyze) { - console.log( - await esbuild.analyzeMetafile(metafile, { - verbose: false, - }) - ) - } else { - const outputs = Object.keys(metafile.outputs) - execSync(`echo 'build finished:' && wc -c ${outputs.join(' ')}`, { - stdio: 'inherit', + } + if (args.analyze) { + console.log( + await esbuild.analyzeMetafile(metafile, { + verbose: false, }) - } + ) + } else { + const outputs = Object.keys(metafile.outputs) + execSync(`echo 'build finished:' && wc -c ${outputs.join(' ')}`, { + stdio: 'inherit', + }) } } diff --git a/scripts/build-preview.mjs b/scripts/build-preview.mjs index f7fa39f..34c15e4 100644 --- a/scripts/build-preview.mjs +++ b/scripts/build-preview.mjs @@ -1,4 +1,4 @@ -import {execSync} from 'child_process' +import {execSync} from 'node:child_process' import NodeModulesPolyfills from '@esbuild-plugins/node-modules-polyfill' import * as esbuild from 'esbuild' /* eslint-disable no-console */ @@ -64,19 +64,18 @@ const main = async () => { if (errors.length) { throw new Error(esbuild.formatMessages(errors)) - } else { - if (args.analyze) { - console.log( - await esbuild.analyzeMetafile(metafile, { - verbose: false, - }) - ) - } else { - const outputs = Object.keys(metafile.outputs) - execSync(`echo 'build finished:' && wc -c ${outputs.join(' ')}`, { - stdio: 'inherit', + } + if (args.analyze) { + console.log( + await esbuild.analyzeMetafile(metafile, { + verbose: false, }) - } + ) + } else { + const outputs = Object.keys(metafile.outputs) + execSync(`echo 'build finished:' && wc -c ${outputs.join(' ')}`, { + stdio: 'inherit', + }) } } diff --git a/scripts/build.mjs b/scripts/build.mjs index 373014a..11b6b57 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -1,6 +1,6 @@ /* eslint-disable no-console */ -import {execSync} from 'child_process' -import {builtinModules} from 'module' +import {execSync} from 'node:child_process' +import {builtinModules} from 'node:module' import * as esbuild from 'esbuild' import minimist from 'minimist' import watchLogPlugin from './watchLogPlugin.mjs' @@ -41,19 +41,18 @@ const main = async (/** @type {import('esbuild').BuildOptions} */ opts) => { if (errors.length) { throw new Error(esbuild.formatMessages(errors)) - } else { - if (args.analyze) { - console.log( - await esbuild.analyzeMetafile(metafile, { - verbose: false, - }) - ) - } else { - const outputs = Object.keys(metafile.outputs) - execSync(`echo 'build finished:' && wc -c ${outputs.join(' ')}`, { - stdio: 'inherit', + } + if (args.analyze) { + console.log( + await esbuild.analyzeMetafile(metafile, { + verbose: false, }) - } + ) + } else { + const outputs = Object.keys(metafile.outputs) + execSync(`echo 'build finished:' && wc -c ${outputs.join(' ')}`, { + stdio: 'inherit', + }) } } diff --git a/scripts/copy.mjs b/scripts/copy.mjs index 18604de..c76f5e9 100644 --- a/scripts/copy.mjs +++ b/scripts/copy.mjs @@ -1,5 +1,5 @@ -import {promises as fs} from 'fs' -import path from 'path' +import {promises as fs} from 'node:fs' +import path from 'node:path' import fg from 'fast-glob' const config = [ diff --git a/src/extension.ts b/src/extension.ts index 9f1654e..41e4063 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,8 +1,8 @@ /* eslint-disable no-console */ -import path from 'path' +import path from 'node:path' +import type {Worker} from 'node:worker_threads' import * as prettyFormat from 'pretty-format' import * as vscode from 'vscode' -import type {Worker} from 'worker_threads' import bundle from './extension/bundle' import getWebviewContent from './extension/getWebviewContent' import {install} from './extension/install' @@ -153,7 +153,7 @@ async function showRuntimePick(currentRuntime?: Runtime) { }`, })), { - placeHolder: `Choose a runtime for Live Code`, + placeHolder: 'Choose a runtime for Live Code', } ) @@ -313,7 +313,7 @@ async function processDocumentPanel( log(timer.print()) function sendMessage() { - return panel!.webview.postMessage({ + return panel?.webview.postMessage({ type: shouldReload ? 'codeReload' : 'code', // data should be serialized data: { @@ -345,12 +345,11 @@ function setPanelTitleAndIcon( document: vscode.TextDocument ) { const {currentRuntime} = panelStateMap.get(panel)! - panel.title = - `Preview` + - (document ? ' ' + path.basename(document.uri.fsPath) : '') + - ` in ${runtimeNameMap[currentRuntime]}` + panel.title = `Preview${ + document ? ` ${path.basename(document.uri.fsPath)}` : '' + } in ${runtimeNameMap[currentRuntime]}` panel.iconPath = vscode.Uri.joinPath( - itsContext!.extensionUri, + itsContext?.extensionUri, runtimeIconMap[currentRuntime] ) } diff --git a/src/extension/__tests__/bundle.spec.ts b/src/extension/__tests__/bundle.spec.ts index d845ab1..ce605bd 100644 --- a/src/extension/__tests__/bundle.spec.ts +++ b/src/extension/__tests__/bundle.spec.ts @@ -26,28 +26,28 @@ describe('browser', () => { }) test('transform NODE_ENV', async () => { - const name = `process.env.NODE_ENV` + const name = 'process.env.NODE_ENV' expect(await bundle(`call(${name})`, opts)).toMatchObject({ js: expect.stringContaining('call("development")'), }) }) test('donot preserve process.env.FOO', async () => { - const name = `process.env.MY_VAR` + const name = 'process.env.MY_VAR' expect(await bundle(`export let x = ${name}`, opts)).toMatchObject({ js: expect.not.stringContaining(name), }) }) test('bundle untitled TS file - fail', async () => { - await expect(bundle(`type foo = 1`, {...opts})).rejects.toThrowError( + await expect(bundle('type foo = 1', {...opts})).rejects.toThrowError( /ERROR: Expected/ ) }) test('bundle untitled TS file - pass', async () => { expect( - await bundle(`type foo = 1`, {...opts, languageId: 'typescript'}) + await bundle('type foo = 1', {...opts, languageId: 'typescript'}) ).toMatchObject({ js: '', }) @@ -77,14 +77,14 @@ describe('node', () => { }) test('transform NODE_ENV', async () => { - const name = `process.env.NODE_ENV` + const name = 'process.env.NODE_ENV' expect(await bundle(`call(${name})`, opts)).toMatchObject({ js: expect.stringContaining('call("development")'), }) }) test('preserve process.env.FOO', async () => { - const name = `process.env.MY_VAR` + const name = 'process.env.MY_VAR' expect(await bundle(`export let x = ${name}`, opts)).toMatchObject({ js: expect.stringContaining(name), }) diff --git a/src/extension/bundle.ts b/src/extension/bundle.ts index 0220897..1a1b9a2 100644 --- a/src/extension/bundle.ts +++ b/src/extension/bundle.ts @@ -1,4 +1,4 @@ -import path from 'path' +import path from 'node:path' import type * as Esbuild from 'esbuild' import {nodeExternalsPlugin} from 'esbuild-node-externals' import {MemoryCache, fetchBuilder} from 'node-fetch-cache' diff --git a/src/extension/install.ts b/src/extension/install.ts index 7447a73..10106f7 100644 --- a/src/extension/install.ts +++ b/src/extension/install.ts @@ -1,5 +1,5 @@ -import {exec} from 'child_process' -import {promisify} from 'util' +import {exec} from 'node:child_process' +import {promisify} from 'node:util' // eslint-disable-next-line @typescript-eslint/no-explicit-any type AnyFunction = (...args: any[]) => T diff --git a/src/sandbox/__tests__/nodeVM.spec.ts b/src/sandbox/__tests__/nodeVM.spec.ts index f5eac08..c2ea715 100644 --- a/src/sandbox/__tests__/nodeVM.spec.ts +++ b/src/sandbox/__tests__/nodeVM.spec.ts @@ -9,7 +9,7 @@ const opts = { test('empty', async () => { expect( - await nodeVM.runInNewContext(transform(``), opts) + await nodeVM.runInNewContext(transform(''), opts) ).toMatchInlineSnapshot(` { "logs": [], @@ -20,7 +20,7 @@ test('empty', async () => { test('expression', async () => { expect( - await nodeVM.runInNewContext(transform(`0`), opts) + await nodeVM.runInNewContext(transform('0'), opts) ).toMatchInlineSnapshot(` { "logs": [], @@ -40,7 +40,7 @@ test('expression', async () => { test('no access to local scope', async () => { expect( await nodeVM.runInNewContext( - transform(`[typeof vm, typeof format, typeof result]`), + transform('[typeof vm, typeof format, typeof result]'), opts ) ).toMatchInlineSnapshot(` @@ -64,7 +64,7 @@ test('no access to local scope', async () => { }) test('reject', async () => { - await expect(nodeVM.runInNewContext(transform(`a`), opts)).rejects.toThrow( + await expect(nodeVM.runInNewContext(transform('a'), opts)).rejects.toThrow( /a is not defined/ ) }) @@ -72,7 +72,7 @@ test('reject', async () => { test('__filename', async () => { expect( await nodeVM.runInNewContext( - transform(`[require, typeof __filename]`), + transform('[require, typeof __filename]'), opts ) ).toMatchInlineSnapshot(` @@ -137,7 +137,7 @@ test('console', async () => { expect(logString).toContain('ms') const onExit = jest.fn() - workerRef.current!.on('exit', onExit) - await workerRef.current!.terminate() + workerRef.current?.on('exit', onExit) + await workerRef.current?.terminate() expect(onExit).toBeCalled() }) diff --git a/src/sandbox/__tests__/transform.spec.ts b/src/sandbox/__tests__/transform.spec.ts index 2323db1..d3cd8f9 100644 --- a/src/sandbox/__tests__/transform.spec.ts +++ b/src/sandbox/__tests__/transform.spec.ts @@ -1,7 +1,7 @@ import transform from '../transform' test('transform expressions', () => { - expect(transform(`0;1\n2`)).toMatchInlineSnapshot(` + expect(transform('0;1\n2')).toMatchInlineSnapshot(` "__onexpression__(0, { line: 1, column: 0 @@ -22,11 +22,11 @@ test('transform expressions', () => { }) test('empty', () => { - expect(transform(``)).toMatchInlineSnapshot(`"__onexpressionend__();"`) + expect(transform('')).toMatchInlineSnapshot(`"__onexpressionend__();"`) }) test('jsx', () => { - expect(transform(`
`)).toMatchInlineSnapshot(` + expect(transform('
')).toMatchInlineSnapshot(` "__onexpression__(
, { line: 1, column: 0 @@ -37,7 +37,7 @@ test('jsx', () => { }) test('ts', () => { - expect(transform(`type c = 0;1;
`)).toMatchInlineSnapshot(` + expect(transform('type c = 0;1;
')).toMatchInlineSnapshot(` "type c = 0; __onexpression__(1, { diff --git a/src/sandbox/esm.ts b/src/sandbox/esm.ts index 1ee39da..3c4ed60 100644 --- a/src/sandbox/esm.ts +++ b/src/sandbox/esm.ts @@ -1,5 +1,5 @@ export const esm = (input: string) => - 'data:text/javascript;charset=utf-8,' + encodeURIComponent(input) + `data:text/javascript;charset=utf-8,${encodeURIComponent(input)}` export const exportFromGlobal = ( name: string, diff --git a/src/sandbox/nodeVM.ts b/src/sandbox/nodeVM.ts index 32249b6..795e7fb 100644 --- a/src/sandbox/nodeVM.ts +++ b/src/sandbox/nodeVM.ts @@ -1,5 +1,5 @@ -import path from 'path' -import {Worker} from 'worker_threads' +import path from 'node:path' +import {Worker} from 'node:worker_threads' import type {ExpContext} from './types' const workerPath = path.resolve(__dirname, 'nodeWorker.js') diff --git a/src/sandbox/nodeWorker.js b/src/sandbox/nodeWorker.js index a116c05..a018462 100644 --- a/src/sandbox/nodeWorker.js +++ b/src/sandbox/nodeWorker.js @@ -1,9 +1,9 @@ -const {workerData, parentPort} = require('worker_threads') +const {workerData, parentPort} = require('node:worker_threads') const {format} = require('pretty-format') const Hook = require('console-feed/lib/Hook').default -const vm = require('vm') -const path = require('path') -const {createRequire} = require('module') +const vm = require('node:vm') +const path = require('node:path') +const {createRequire} = require('node:module') const {code, filename} = workerData const result = [] diff --git a/src/sandbox/transform.ts b/src/sandbox/transform.ts index cbad644..ff40656 100644 --- a/src/sandbox/transform.ts +++ b/src/sandbox/transform.ts @@ -22,11 +22,11 @@ const transformAst = (ast: Program, expando = '__onexpression__') => { const context = b.objectExpression([ b.objectProperty.from({ key: b.identifier('line'), - value: b.numericLiteral(exp.loc!.start.line), + value: b.numericLiteral(exp.loc?.start.line), }), b.objectProperty.from({ key: b.identifier('column'), - value: b.numericLiteral(exp.loc!.start.column), + value: b.numericLiteral(exp.loc?.start.column), }), ]) return b.expressionStatement( diff --git a/yarn.lock b/yarn.lock index 15d97c4..c6b56d5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1393,6 +1393,13 @@ __metadata: languageName: node linkType: hard +"@types/minimist@npm:^1": + version: 1.2.5 + resolution: "@types/minimist@npm:1.2.5" + checksum: 10/477047b606005058ab0263c4f58097136268007f320003c348794f74adedc3166ffc47c80ec3e94687787f2ab7f4e72c468223946e79892cf0fd9e25e9970a90 + languageName: node + linkType: hard + "@types/node@npm:*": version: 16.11.6 resolution: "@types/node@npm:16.11.6" @@ -3466,6 +3473,7 @@ __metadata: "@esbuild-plugins/node-modules-polyfill": "npm:^0.1.2" "@recommended/biome-config": "npm:^1.0.0" "@types/jest": "npm:^27.0.2" + "@types/minimist": "npm:^1" "@types/node": "npm:14.x" "@types/react": "npm:^17.0.33" "@types/react-dom": "npm:^17.0.10" @@ -3479,6 +3487,7 @@ __metadata: is-promise: "npm:^4.0.0" jest: "npm:^29.7.0" ldrs: "npm:^1.0.1" + minimist: "npm:^1.2.8" node-fetch-cache: "npm:^3.0.3" pretty-format: "npm:^27.3.1" react: "npm:^17.0.2" @@ -3629,6 +3638,13 @@ __metadata: languageName: node linkType: hard +"minimist@npm:^1.2.8": + version: 1.2.8 + resolution: "minimist@npm:1.2.8" + checksum: 10/908491b6cc15a6c440ba5b22780a0ba89b9810e1aea684e253e43c4e3b8d56ec1dcdd7ea96dde119c29df59c936cde16062159eae4225c691e19c70b432b6e6f + languageName: node + linkType: hard + "minipass-collect@npm:^1.0.2": version: 1.0.2 resolution: "minipass-collect@npm:1.0.2"