Skip to content

Add more explicit sourceType in options and return of compile #5268

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 12 additions & 6 deletions lib/coffeescript/coffeescript.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/coffeescript.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->
)

# Check for import or export; if found, force bare mode.
unless options.bare? and options.bare is yes
sourceType = options.sourceType ? 'unambiguous'
Copy link
Author

Choose a reason for hiding this comment

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

This is based on the definition of sourceType in babel. In both babel sourceType: 'unambiguous' and in CoffeeScript, there's behavior based on the existence of import/export statements. In the future this could also fail properly when import is used in scripts but that may fall out of the scope of coffeescript.

Copy link
Collaborator

Choose a reason for hiding this comment

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

FYI the original code here removes the “safety wrapper” (function() { ... }).call(this); when the source code contains import or export, since per spec import and export must be at the top level and therefore such a wrapper would always break ESM code.

Copy link
Author

@jkrems jkrems Dec 27, 2019

Choose a reason for hiding this comment

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

Another problem with the IIFE is that .call(this) in a module wouldn't work. So even without import/export it would break when the source type is module. :)

EDIT: "Wouldn't work" as in "doesn't really make sense" because there's no value for this in a module unlike in scripts and CJS files.

if sourceType is 'unambiguous'
sourceType = 'script'
for token in tokens
if token[0] in ['IMPORT', 'EXPORT']
options.bare = yes
sourceType = 'module'
break

options.bare = yes if sourceType is 'module'

fragments = parser.parse(tokens).compileToFragments options

currentLine = 0
Expand Down Expand Up @@ -170,9 +174,10 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->

registerCompiled filename, code, map

if options.sourceMap
if options.sourceMap or options.sourceType
{
js
sourceType
sourceMap: map
v3SourceMap: JSON.stringify v3SourceMap, null, 2
}
Expand Down
21 changes: 21 additions & 0 deletions test/compilation.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,24 @@ test "#3306: trailing comma in a function call in the last line", ->
''', '''
foo(bar);
'''

test 'sourceType is returned if requested', ->
result = CoffeeScript.compile '''
console.log 'is a script'
''', sourceType: 'unambiguous'
eq result.sourceType, 'script'

result = CoffeeScript.compile '''
export default 'is a module'
''', sourceType: 'unambiguous'
eq result.sourceType, 'module'

# sourceType: 'module' implies bare
result = CoffeeScript.compile '''
console.log 'is a module'
''', sourceType: 'module'
eq result.sourceType, 'module'
eq result.js, '''
console.log('is a module');

'''