Skip to content

Commit

Permalink
fix(parser): reject "import from 'foo'"
Browse files Browse the repository at this point in the history
closes #258
  • Loading branch information
3cp committed Jun 5, 2024
1 parent f6355cb commit bee9e31
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,9 @@ function parseImportNamespaceSpecifier(
function parseModuleSpecifier(parser: ParserState, context: Context): ESTree.Literal {
// ModuleSpecifier :
// StringLiteral
consumeOpt(parser, context, Token.FromKeyword);
if (!consumeOpt(parser, context, Token.FromKeyword)) {
report(parser, Errors.UnexpectedToken, KeywordDescTable[parser.token & Token.Type]);
}

if (parser.token !== Token.StringLiteral) report(parser, Errors.InvalidExportImportSource, 'Import');

Expand Down
28 changes: 28 additions & 0 deletions test/parser/module/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('Module - Import', () => {
'import {} from;',
"import {,} from 'a';",
'import from;',
"import from 'foo';",
"import { foo as !d } from 'foo';",
"import { foo as 123 } from 'foo';",
"import { foo as [123] } from 'foo';",
Expand Down Expand Up @@ -398,6 +399,7 @@ describe('Module - Import', () => {
"import { } from 'm.js';",
"import { a } from 'm.js';",
"import 'foo';",
"import from from 'foo';",
"import { a } from 'foo';",
'import { a as of } from "k";',
// Runtime errors
Expand Down Expand Up @@ -803,6 +805,32 @@ describe('Module - Import', () => {
]
}
],
[
'import from from "foo"',
Context.Strict | Context.Module,
{
type: 'Program',
sourceType: 'module',
body: [
{
type: 'ImportDeclaration',
specifiers: [
{
type: 'ImportDefaultSpecifier',
local: {
type: 'Identifier',
name: 'from'
}
}
],
source: {
type: 'Literal',
value: 'foo'
}
}
]
}
],
[
'import * as d from "module";',
Context.Strict | Context.Module | Context.OptionsRanges,
Expand Down

0 comments on commit bee9e31

Please sign in to comment.