Skip to content

Commit

Permalink
feature: goldstein: keyword-fn: add ability to use "fn" as function name
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 8, 2024
1 parent 4722c74 commit f0b7d54
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/keyword-fn/fixture/fn-function.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function fn() {}
15 changes: 14 additions & 1 deletion packages/keyword-fn/index.js
Original file line number Diff line number Diff line change
@@ -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', {
Expand All @@ -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);
Expand All @@ -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);
}
};
}

5 changes: 5 additions & 0 deletions packages/keyword-fn/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f0b7d54

Please sign in to comment.