Permalink
Browse files

Merge branch 'master' of https://github.com/jashkenas/coffee-script

  • Loading branch information...
jashkenas committed Apr 6, 2013
2 parents 0cadcdc + 8be65de commit 170f3111017999004b1c4afda1f330c7ec18f0ac

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -15,27 +15,29 @@ CoffeeScript.eval = (code, options = {}) ->
# Running code does not provide access to this scope.
CoffeeScript.run = (code, options = {}) ->
options.bare = on
+ options.shiftLine = on
Function(compile code, options)()
# If we're not in a browser environment, we're finished with the public API.
return unless window?
-# Include source maps where possible. If we've got a base64 encoder, and a
-# JSON serializer, we're good to go.
-if btoa? and JSON?
+# Include source maps where possible. If we've got a base64 encoder, a
+# JSON serializer, and tools for escaping unicode characters, we're good to go.
+# Ported from https://developer.mozilla.org/en-US/docs/DOM/window.btoa
+if btoa? and JSON? and unescape? and encodeURIComponent?
compile = (code, options = {}) ->
options.sourceMap = true
options.inline = true
{js, v3SourceMap} = CoffeeScript.compile code, options
- "#{js}\n//@ sourceMappingURL=data:application/json;base64,#{btoa v3SourceMap}\n//@ sourceURL=coffeescript"
+ "#{js}\n//@ sourceMappingURL=data:application/json;base64,#{btoa unescape encodeURIComponent v3SourceMap}\n//@ sourceURL=coffeescript"
# Load a remote script from the current domain via XHR.
CoffeeScript.load = (url, callback, options = {}) ->
options.sourceFiles = [url]
xhr = if window.ActiveXObject
new window.ActiveXObject('Microsoft.XMLHTTP')
else
- new XMLHttpRequest()
+ new window.XMLHttpRequest()
xhr.open 'GET', url, true
xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr
xhr.onreadystatechange = ->
@@ -51,7 +53,7 @@ CoffeeScript.load = (url, callback, options = {}) ->
# all script tags with a content-type of `text/coffeescript`.
# This happens on page load.
runScripts = ->
- scripts = document.getElementsByTagName 'script'
+ scripts = window.document.getElementsByTagName 'script'
coffeetypes = ['text/coffeescript', 'text/literate-coffeescript']
coffees = (s for s in scripts when s.type in coffeetypes)
index = 0
@@ -71,6 +73,6 @@ runScripts = ->
# Listen for window load, both in decent browsers and in IE.
if window.addEventListener
- addEventListener 'DOMContentLoaded', runScripts, no
+ window.addEventListener 'DOMContentLoaded', runScripts, no
else
- attachEvent 'onload', runScripts
+ window.attachEvent 'onload', runScripts
View
@@ -36,7 +36,8 @@ exports.compile = compile = (code, options = {}) ->
fragments = (parser.parse lexer.tokenize(code, options)).compileToFragments options
currentLine = 0
- currentLine += 1 if options.header or options.inline
+ currentLine += 1 if options.header
+ currentLine += 1 if options.shiftLine
currentColumn = 0
js = ""
for fragment in fragments
View
@@ -14,6 +14,7 @@ CoffeeScript = require './coffee-script'
{EventEmitter} = require 'events'
exists = fs.exists or path.exists
+useWinPathSep = path.sep is '\\'
# Allow CoffeeScript to emit Node.js events.
helpers.extend CoffeeScript, new EventEmitter
@@ -256,7 +257,7 @@ removeSource = (source, base, removeJs) ->
# Get the corresponding output JavaScript path for a source file.
outputPath = (source, base, extension=".js") ->
- basename = helpers.baseFileName source, yes, path.sep
+ basename = helpers.baseFileName source, yes, useWinPathSep
srcDir = path.dirname source
baseDir = if base is '.' then srcDir else srcDir.substring base.length
dir = if opts.output then path.join opts.output, baseDir else srcDir
@@ -274,7 +275,7 @@ writeJs = (base, sourcePath, js, jsPath, generatedSourceMap = null) ->
compile = ->
if opts.compile
js = ' ' if js.length <= 0
- if generatedSourceMap then js = "#{js}\n/*\n//@ sourceMappingURL=#{helpers.baseFileName sourceMapPath, no, path.sep}\n*/\n"
+ if generatedSourceMap then js = "#{js}\n/*\n//@ sourceMappingURL=#{helpers.baseFileName sourceMapPath, no, useWinPathSep}\n*/\n"
fs.writeFile jsPath, js, (err) ->
if err
printLine err.message
@@ -343,13 +344,13 @@ compileOptions = (filename, base) ->
jsPath
sourceRoot: path.relative jsDir, cwd
sourceFiles: [path.relative cwd, filename]
- generatedFile: helpers.baseFileName(jsPath, no, path.sep)
+ generatedFile: helpers.baseFileName(jsPath, no, useWinPathSep)
}
else
answer = helpers.merge answer,
sourceRoot: ""
- sourceFiles: [helpers.baseFileName filename, no, path.sep]
- generatedFile: helpers.baseFileName(filename, yes, path.sep) + ".js"
+ sourceFiles: [helpers.baseFileName filename, no, useWinPathSep]
+ generatedFile: helpers.baseFileName(filename, yes, useWinPathSep) + ".js"
answer
# Start up a new Node.js instance with the arguments in `--nodejs` passed to
View
@@ -118,7 +118,8 @@ exports.locationDataToString = (obj) ->
"No location data"
# A `.coffee.md` compatible version of `basename`, that returns the file sans-extension.
-exports.baseFileName = (file, stripExt = no, pathSep = '/') ->
+exports.baseFileName = (file, stripExt = no, useWinPathSep = no) ->
+ pathSep = if useWinPathSep then /\\|\// else /\//
parts = file.split(pathSep)
file = parts[parts.length - 1]
return file unless stripExt
View
@@ -1504,7 +1504,7 @@ exports.While = class While extends Base
set = ''
{body} = this
if body.isEmpty()
- body = ''
+ body = @makeCode ''
else
if @returns
body.makeReturn rvar = o.scope.freeVariable 'results'
Oops, something went wrong.

0 comments on commit 170f311

Please sign in to comment.