Skip to content

Commit

Permalink
style: lint:fix-unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Apr 27, 2024
1 parent 6b87a37 commit a57a945
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 84 deletions.
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"engines": {
"vscode": "^1.61.0"
},
"categories": ["Programming Languages"],
"categories": [
"Programming Languages"
],
"activationEvents": [
"onLanguage:javascript",
"onLanguage:javascriptreact",
Expand Down Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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",
Expand Down
27 changes: 13 additions & 14 deletions scripts/build-extension.mjs
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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',
})
}
}

Expand Down
25 changes: 12 additions & 13 deletions scripts/build-preview.mjs
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -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',
})
}
}

Expand Down
27 changes: 13 additions & 14 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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',
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions scripts/copy.mjs
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
17 changes: 8 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -153,7 +153,7 @@ async function showRuntimePick(currentRuntime?: Runtime) {
}`,
})),
{
placeHolder: `Choose a runtime for Live Code`,
placeHolder: 'Choose a runtime for Live Code',
}
)

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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]
)
}
Expand Down
12 changes: 6 additions & 6 deletions src/extension/__tests__/bundle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
})
Expand Down Expand Up @@ -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),
})
Expand Down
2 changes: 1 addition & 1 deletion src/extension/bundle.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 2 additions & 2 deletions src/extension/install.ts
Original file line number Diff line number Diff line change
@@ -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<T = void> = (...args: any[]) => T
Expand Down
14 changes: 7 additions & 7 deletions src/sandbox/__tests__/nodeVM.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const opts = {

test('empty', async () => {
expect(
await nodeVM.runInNewContext(transform(``), opts)
await nodeVM.runInNewContext(transform(''), opts)
).toMatchInlineSnapshot(`
{
"logs": [],
Expand All @@ -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": [],
Expand All @@ -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(`
Expand All @@ -64,15 +64,15 @@ 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/
)
})

test('__filename', async () => {
expect(
await nodeVM.runInNewContext(
transform(`[require, typeof __filename]`),
transform('[require, typeof __filename]'),
opts
)
).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -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()
})
8 changes: 4 additions & 4 deletions src/sandbox/__tests__/transform.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,11 +22,11 @@ test('transform expressions', () => {
})

test('empty', () => {
expect(transform(``)).toMatchInlineSnapshot(`"__onexpressionend__();"`)
expect(transform('')).toMatchInlineSnapshot(`"__onexpressionend__();"`)
})

test('jsx', () => {
expect(transform(`<div />`)).toMatchInlineSnapshot(`
expect(transform('<div />')).toMatchInlineSnapshot(`
"__onexpression__(<div />, {
line: 1,
column: 0
Expand All @@ -37,7 +37,7 @@ test('jsx', () => {
})

test('ts', () => {
expect(transform(`type c = 0;1;<div />`)).toMatchInlineSnapshot(`
expect(transform('type c = 0;1;<div />')).toMatchInlineSnapshot(`
"type c = 0;
__onexpression__(1, {
Expand Down
2 changes: 1 addition & 1 deletion src/sandbox/esm.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/sandbox/nodeVM.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
8 changes: 4 additions & 4 deletions src/sandbox/nodeWorker.js
Original file line number Diff line number Diff line change
@@ -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 = []
Expand Down
Loading

0 comments on commit a57a945

Please sign in to comment.