diff --git a/package-lock.json b/package-lock.json index 215a39678f..adbf6baa71 100644 --- a/package-lock.json +++ b/package-lock.json @@ -107,6 +107,7 @@ "kuroshiro-analyzer-kuromoji": "^1.1.0", "lodash.template": "^4.5.0", "lokijs": "^1.5.12", + "meriyah": "^4.4.2", "moment": "^2.30.1", "node-eta": "^0.9.0", "npm-run-all": "^4.1.5", @@ -11213,6 +11214,15 @@ "node": ">= 8" } }, + "node_modules/meriyah": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/meriyah/-/meriyah-4.4.2.tgz", + "integrity": "sha512-fENZIbs4tscI3IGRGtPrCoW4H4oGzVQrQCVCGRv+92kFXKkvxr52ZNR684ICvDC/UBWg9ioGc2X6pMnWOtRYwA==", + "dev": true, + "engines": { + "node": ">=10.4.0" + } + }, "node_modules/micromark": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/micromark/-/micromark-4.0.0.tgz", diff --git a/package.json b/package.json index eef08bb626..5a00f88587 100644 --- a/package.json +++ b/package.json @@ -161,6 +161,7 @@ "kuroshiro-analyzer-kuromoji": "^1.1.0", "lodash.template": "^4.5.0", "lokijs": "^1.5.12", + "meriyah": "^4.4.2", "moment": "^2.30.1", "node-eta": "^0.9.0", "npm-run-all": "^4.1.5", diff --git a/setup/extract-apis.ts b/setup/extract-apis.ts index 33169cee76..fa42b436d7 100755 --- a/setup/extract-apis.ts +++ b/setup/extract-apis.ts @@ -6,6 +6,7 @@ import * as fs from 'fs' import stringify from 'fast-safe-stringify' import _ from 'lodash' import jsesc from 'jsesc' +import { parseScript } from 'meriyah' import Showdown from 'showdown' const showdown = new Showdown.Converter() @@ -24,7 +25,7 @@ class FormatterAPI { const key = name.toLowerCase() if (this.signature[key]) throw new Error(`duplicate ${kind} ${key}`) - this.signature[name.toLowerCase()] = _.cloneDeep({ + this.signature[key] = _.cloneDeep({ name, parameters: method.parameters.map(p => p.name), defaults: method.parameters.map(p => p.default), @@ -32,6 +33,13 @@ class FormatterAPI { schema: method.schema, }) if (!this.signature[key].rest) delete this.signature[key].rest + const defaults = parseScript(jsesc(this.signature[key].defaults)).body[0] + if (defaults.type === 'ExpressionStatement' && defaults.expression.type === 'ArrayExpression') { + this.signature[key].ast = defaults.expression.elements + } + else { + throw new Error(jsesc(this.signature[key].defaults)) + } const description = method.parameters.find(param => param.doc) let params = method.parameters.map(p => {