Support dynamic import

Fixes #1393
This commit is contained in:
Liam Newman 2018-05-11 15:29:07 -07:00
parent 01111faed5
commit 5329720021
6 changed files with 38 additions and 3 deletions

View File

@ -865,6 +865,8 @@ function Beautifier(js_source_text, options) {
} else if (in_array(flags.last_word, ['await', 'async'])) {
// Should be a space between await and an IIFE, or async and an arrow function
output.space_before_token = true;
} else if (flags.last_text === 'import' && current_token.whitespace_before === '') {
output.space_before_token = false;
} else if (in_array(flags.last_text, tokenizer.line_starters) || flags.last_text === 'catch') {
output.space_before_token = true;
}

View File

@ -633,6 +633,8 @@ function Beautifier(js_source_text, options) {
} else if (in_array(flags.last_word, ['await', 'async'])) {
// Should be a space between await and an IIFE, or async and an arrow function
output.space_before_token = true;
} else if (flags.last_text === 'import' && current_token.whitespace_before === '') {
output.space_before_token = false;
} else if (in_array(flags.last_text, tokenizer.line_starters) || flags.last_text === 'catch') {
output.space_before_token = true;
}

View File

@ -3747,7 +3747,16 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'import { member1, member2 as alias2 } from "module-name";\n' +
'import defaultMember, { member, member2 } from "module-name";\n' +
'import defaultMember, * as name from "module-name";\n' +
'import "module-name";');
'import "module-name";\n' +
'import("module-name")');
// Issue #1393 - dynamic import()
bt(
'if (from < to) {\n' +
' import(`dynamic${library}`);\n' +
'} else {\n' +
' import("otherdynamic");\n' +
'}');
// Issue 858 - from is a keyword only after import
bt(

View File

@ -472,6 +472,8 @@ class Beautifier:
elif self.flags.last_word in ['await', 'async']:
# Should be a space between await and an IIFE, or async and an arrow function
self.output.space_before_token = True
elif self.flags.last_text == 'import' and current_token.whitespace_before == '':
self.output.space_before_token = False
elif self.flags.last_text in Tokenizer.line_starters or self.flags.last_text == 'catch':
self.output.space_before_token = True

View File

@ -3575,7 +3575,16 @@ class TestJSBeautifier(unittest.TestCase):
'import { member1, member2 as alias2 } from "module-name";\n' +
'import defaultMember, { member, member2 } from "module-name";\n' +
'import defaultMember, * as name from "module-name";\n' +
'import "module-name";')
'import "module-name";\n' +
'import("module-name")')
# Issue #1393 - dynamic import()
bt(
'if (from < to) {\n' +
' import(`dynamic${library}`);\n' +
'} else {\n' +
' import("otherdynamic");\n' +
'}')
# Issue 858 - from is a keyword only after import
bt(

View File

@ -2556,7 +2556,18 @@ exports.test_data = {
'import { member1, member2 as alias2 } from "module-name";',
'import defaultMember, { member, member2 } from "module-name";',
'import defaultMember, * as name from "module-name";',
'import "module-name";'
'import "module-name";',
'import("module-name")'
]
},
{
comment: "Issue #1393 - dynamic import()",
unchanged: [
'if (from < to) {',
' import(`dynamic${library}`);',
'} else {',
' import("otherdynamic");',
'}'
]
},
{