From f0b7d547f9b4b995f7bf9f9c38efdaaec54c47ce Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 8 May 2024 13:04:09 +0300 Subject: [PATCH] feature: goldstein: keyword-fn: add ability to use "fn" as function name --- packages/keyword-fn/fixture/fn-function.gs | 1 + packages/keyword-fn/index.js | 15 ++++++++++++++- packages/keyword-fn/index.spec.js | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 packages/keyword-fn/fixture/fn-function.gs diff --git a/packages/keyword-fn/fixture/fn-function.gs b/packages/keyword-fn/fixture/fn-function.gs new file mode 100644 index 0000000..581311d --- /dev/null +++ b/packages/keyword-fn/fixture/fn-function.gs @@ -0,0 +1 @@ +function fn() {} diff --git a/packages/keyword-fn/index.js b/packages/keyword-fn/index.js index 95ec05a..cb2cf16 100644 --- a/packages/keyword-fn/index.js +++ b/packages/keyword-fn/index.js @@ -1,4 +1,5 @@ import {addKeyword, TokenType} from '../operator/index.js'; +import {tokTypes as tt} from 'acorn'; export default function fn(Parser) { Parser.acorn.keywordTypes.fn = new TokenType('fn', { @@ -13,7 +14,9 @@ export default function fn(Parser) { } parseStatement(context, topLevel, exports) { - if (this.type === Parser.acorn.keywordTypes.fn) + const isParen = this.eat(tt.parenL); + + if (!isParen && this.type === Parser.acorn.keywordTypes.fn) this.type = Parser.acorn.keywordTypes.function; return super.parseStatement(context, topLevel, exports); @@ -25,5 +28,15 @@ export default function fn(Parser) { return super.shouldParseExportStatement(); } + + checkUnreserved(ref) { + const {name} = ref; + + if (name === 'fn') + return; + + return super.checkUnreserved(ref); + } }; } + diff --git a/packages/keyword-fn/index.spec.js b/packages/keyword-fn/index.spec.js index 69817e1..7b4209e 100644 --- a/packages/keyword-fn/index.spec.js +++ b/packages/keyword-fn/index.spec.js @@ -8,6 +8,11 @@ test('goldstein: keyword: fn', (t) => { t.end(); }); +test('goldstein: keyword: fn-function', (t) => { + t.noCompile('fn-function'); + t.end(); +}); + test('goldstein: keyword: fn: export', (t) => { t.compile('export'); t.end();