From 15547185c8be0bb9bab2651061c8f9cc07b21de1 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 27 May 2024 16:25:54 +0300 Subject: [PATCH] feature: goldstein: Import: identifier --- README.md | 12 ++++++++++++ packages/keyword-import/fixture/import-identifier.gs | 1 + packages/keyword-import/fixture/import-identifier.js | 1 + packages/keyword-import/index.js | 10 +++++++++- packages/keyword-import/index.spec.js | 5 +++++ 5 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 packages/keyword-import/fixture/import-identifier.gs create mode 100644 packages/keyword-import/fixture/import-identifier.js diff --git a/README.md b/README.md index 9a96383..2e00ca8 100644 --- a/README.md +++ b/README.md @@ -393,6 +393,18 @@ Will be converted to: import hello from './hello.js'; ``` +Also, also supported: + +```gs +import hello from hello; +``` + +And will be converted to: + +```js +import hello from 'hello'; +``` + ### `FunctionDeclaration` with `Arrow` If you mistakenly put `=>` in function declaration: diff --git a/packages/keyword-import/fixture/import-identifier.gs b/packages/keyword-import/fixture/import-identifier.gs new file mode 100644 index 0000000..2a3cbcc --- /dev/null +++ b/packages/keyword-import/fixture/import-identifier.gs @@ -0,0 +1 @@ +import f from hello; \ No newline at end of file diff --git a/packages/keyword-import/fixture/import-identifier.js b/packages/keyword-import/fixture/import-identifier.js new file mode 100644 index 0000000..6daae7b --- /dev/null +++ b/packages/keyword-import/fixture/import-identifier.js @@ -0,0 +1 @@ +import f from 'hello'; diff --git a/packages/keyword-import/index.js b/packages/keyword-import/index.js index c813056..edf7860 100644 --- a/packages/keyword-import/index.js +++ b/packages/keyword-import/index.js @@ -16,7 +16,15 @@ export default function keywordImport(Parser) { node.specifiers = this.parseImportSpecifiers(); this.expectContextual('from'); - node.source = this.type === tokTypes.string ? this.parseLiteral(this.value) : this.unexpected(); + if (this.type === tokTypes.string) + node.source = this.parseLiteral(this.value); + else if (this.type === tokTypes.name) { + const {value} = this; + node.source = this.parseLiteral(this.value); + node.source.raw = `'${value}'`; + } else { + this.unexpected(); + } } const {raw, value} = node.source; diff --git a/packages/keyword-import/index.spec.js b/packages/keyword-import/index.spec.js index aba64e2..81a13ce 100644 --- a/packages/keyword-import/index.spec.js +++ b/packages/keyword-import/index.spec.js @@ -7,3 +7,8 @@ test('goldstein: keyword: import', (t) => { t.compile('import'); t.end(); }); + +test('goldstein: keyword: import: identifier', (t) => { + t.compile('import-identifier'); + t.end(); +});