Skip to content

Add support for simple import *statement* to identifier #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/grammar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ grammar =
o 'Return'
o 'Comment'
o 'STATEMENT', -> new StatementLiteral $1
o 'Import'
o 'Export'
]

# All the different types of expressions in our language. The basic unit of
Expand All @@ -117,8 +119,6 @@ grammar =
o 'Class'
o 'Throw'
o 'Yield'
o 'Import'
o 'Export'
]

Yield: [
Expand Down Expand Up @@ -353,6 +353,7 @@ grammar =

Import: [
o 'IMPORT Expression', -> new Import $2
o 'IMPORT Assignable FROM Expression', -> new Import $4, $2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to replace Assignable with something new that represents the ES2015 import syntax, and Expression with something that only matches ' and " strings (also keep in mind that interpolation cannot be used here.)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was a destructured assignment, just like CS already supports (syntactically). Is it not?

]

# Ordinary function invocation, or a chained series of calls.
Expand Down
11 changes: 10 additions & 1 deletion src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ exports.Lexer = class Lexer
@token 'JS', (script = match[0])[1...-1], 0, script.length
script.length

importToken: ->
match = @chunk.match(IMPORT)

return 0 unless match

@token('IMPORT', match[0], 0, match[0].length)

# Matches regular expression literals, as well as multiline extended ones.
# Lexing regular expressions is difficult to distinguish from division, so we
# borrow some basic heuristics from JavaScript and Ruby.
Expand Down Expand Up @@ -774,7 +781,7 @@ JS_KEYWORDS = [
'return', 'throw', 'break', 'continue', 'debugger', 'yield'
'if', 'else', 'switch', 'for', 'while', 'do', 'try', 'catch', 'finally'
'class', 'extends', 'super'
'export', 'import', 'default'
'export', 'import', 'from', 'default'
]

# CoffeeScript-only keywords.
Expand Down Expand Up @@ -849,6 +856,8 @@ MULTI_DENT = /^(?:\n[^\n\S]*)+/

JSTOKEN = /^`[^\\`]*(?:\\.[^\\`]*)*`/

IMPORT = /^import .+/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the way to go. Have a look at how other keywords, such as if, are tokenized.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking at the changes, it's not used anyways.. 🐹

# String-matching-regexes.
STRING_START = /^(?:'''|"""|'|")/

Expand Down
20 changes: 17 additions & 3 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1226,18 +1226,32 @@ exports.Class = class Class extends Base
#### Import

exports.Import = class Import extends Base
constructor: (@expression) ->
constructor: (@expression, @identifier) ->

children: ['expression']
children: ['expression', 'identifier']

isStatement: YES
jumps: NO

makeReturn: THIS

compileNode: (o) ->
[].concat @makeCode(@tab + 'import '), @expression.compileToFragments(o), @makeCode(';')
code = []

code.push @makeCode(@tab + 'import ')


if @identifier
code.push @makeCode("#{@identifier.value} from ")


if @expression.base.value?
code.push @makeCode(@expression.base.value)
else
code.push @makeCode(@expression.base)
code.push @makeCode(';')

code

#### Assign

Expand Down
16 changes: 8 additions & 8 deletions test/modules.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ test "import module", ->
# console.log toJS input
eq toJS(input), output

# test "module import test, syntax #1", ->
# input = "import foo from 'lib'"
# output = "import foo from 'lib';"
# eq toJS(input), output
test "module import test, syntax #1", ->
input = "import foo from 'lib'"
output = "import foo from 'lib';"
eq toJS(input), output

# test "module import test, syntax #2", ->
# input = "import { foo } from 'lib'"
# output = "import { foo } from 'lib';"
# eq toJS(input), output
test "module import test, syntax #2", ->
input = "import { foo } from 'lib'"
output = "import { foo } from 'lib';"
eq toJS(input), output
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also remember to add tests for invalid stuff (in test/error_messages.coffee).


# test "module import test, syntax #3", ->
# input = "import { default as foo } from 'lib'"
Expand Down