Skip to content

Commit

Permalink
feat(eval): support coffeescript (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anillc authored Apr 17, 2021
1 parent 722f343 commit 61e5ea9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/plugin-eval/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"optionalDependencies": {
"esbuild": "^0.11.6",
"json5": "^2.2.0",
"typescript": "^4.2.3"
"typescript": "^4.2.3",
"coffeescript": "^2.5.1"
},
"dependencies": {
"esbuild": "^0.11.6",
Expand Down
22 changes: 22 additions & 0 deletions packages/plugin-eval/src/loaders/coffeescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { compile } = require('coffeescript')

export const name = 'coffeescript'

export function extractScript(expr: string) {
try {
compile(expr)
} catch (e) {
if (e.message !== 'unmatched }') throw e
const location = e.location
const sLines = expr.split('\n')
const row = location.first_line
const column = location.first_column - 1
return [...sLines.slice(0, row - 1), sLines[row].slice(0, column + 1)].join('\n')
}
}

export async function transformScript(expr: string) {
return compile(expr, { bare: true })
}

export const transformModule = transformScript
8 changes: 7 additions & 1 deletion packages/plugin-eval/src/worker/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function synthetize(identifier: string, namespace: {}, globalName?: strin
return module
}

const extnames = new Set(['.js', '.ts', '.json', '.yml', '.yaml'])
const extnames = new Set(['.js', '.ts', '.coffee', '.json', '.yml', '.yaml'])

function* suffixes() {
yield ''
Expand Down Expand Up @@ -168,6 +168,12 @@ function resolveLoader(extension: string) {
} catch {}
}
throw new Error('cannot resolve loader for ".ts", you should install either esbuild or typescript + json5 by yourself')
} else if (extension === '.coffee') {
try {
return require('../loaders/coffeescript')
} catch {
throw new Error('cannot resolve loader for ".coffee", you should install coffeescript by yourself')
}
} else {
throw new Error(`cannot resolve loader for "${extension}", you should specify a custom loader via "config.moduleLoaders"`)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-eval/tests/fixtures/addon1/foobar.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import bar from './bar.yml'

export default (index = 1) -> bar[index]
3 changes: 2 additions & 1 deletion packages/plugin-eval/tests/fixtures/addon1/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { registerCommand } from 'koishi/addons'
import { foo } from './foo'
import foobar from './foobar'

export * from './foo'

registerCommand('test', () => {
return foo()
return foo() + foobar()
})
9 changes: 8 additions & 1 deletion packages/plugin-eval/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,20 @@ describe('Eval Loaders', () => {
await ses.shouldReply('echo 1${"foo" as string}3', '1foo3')
await app.stop()
})

it('coffeescript', async () => {
const app = await createApp('coffeescript')
const ses = app.session('123')
await ses.shouldReply('echo 1${"foobar"}3', '1foobar3')
await app.stop()
})
})

describe('Eval Addons', () => {
it('addon command', async () => {
await ses.shouldReply('addon', /^addon\n/)
await ses.shouldReply('test -h', 'test\n测试功能')
await ses.shouldReply('test', 'bar')
await ses.shouldReply('test', 'barbaz')
})

it('sandbox injection', async () => {
Expand Down

0 comments on commit 61e5ea9

Please sign in to comment.