Skip to content
This repository was archived by the owner on Jul 8, 2023. It is now read-only.

Commit 22a75bf

Browse files
committed
Less wrapper code.
1 parent 154b28a commit 22a75bf

File tree

7 files changed

+28
-60
lines changed

7 files changed

+28
-60
lines changed

lib/handler/CommonCoffeeHandler.js

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/handler/CommonJsHandler.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/handler/CommonCoffeeHandler.coffee

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@ HandlerError = require './error/HandlerError'
88
module.exports = class CommonCoffeeHandler
99

1010
constructor: (@template) ->
11-
@template ?= '''
12-
function () {
13-
var module = {};
14-
15-
%s
16-
return module.exports.apply(this, arguments);
17-
}
18-
'''
11+
@template ?=
12+
"(function () {\n\n%s\nreturn module.exports;\n}).call(this);"
1913

2014
handleFile: (filePath) => new Promise (resolve, reject) =>
2115
return resolve null if path.extname(filePath) isnt '.coffee'
@@ -29,7 +23,7 @@ module.exports = class CommonCoffeeHandler
2923
coffee = require 'coffee-script'
3024

3125
try
32-
js = coffee.compile data.toString()
26+
js = coffee.compile data.toString(), bare: true
3327
catch error
3428
error = new HandlerError 'CommonCoffeeHandler', filePath, error
3529

src/handler/CommonJsHandler.coffee

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,8 @@ HandlerError = require './error/HandlerError'
88
module.exports = class CommonJsHandler
99

1010
constructor: (@template) ->
11-
@template ?= '''
12-
function () {
13-
var module = {};
14-
(function () {
15-
16-
%s
17-
}).call(this);
18-
return module.exports.apply(this, arguments);
19-
}
20-
'''
11+
@template ?=
12+
"(function () {\n\n%s\nreturn module.exports;\n}).call(this);"
2113

2214
handleFile: (filePath) => new Promise (resolve, reject) =>
2315
return resolve null if path.extname(filePath) isnt '.js'

test/suite/handler/CommonCoffeeHandler.spec.coffee

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
fs = require 'fs'
2-
31
CommonCoffeeHandler = require '../../../src/handler/CommonCoffeeHandler'
42
HandlerError = require '../../../src/handler/error/HandlerError'
53

@@ -15,22 +13,18 @@ describe 'CommonCoffeeHandler', ->
1513
expected = [
1614
'coffee'
1715
'''
18-
function () {
19-
var module = {};
16+
(function () {
2017
21-
(function() {
22-
var test;
18+
var test;
2319
24-
test = 'It works.';
20+
test = 'It works.';
2521
26-
module.exports = function() {
27-
return [test, Array.prototype.slice.call(arguments)];
28-
};
22+
module.exports = function() {
23+
return [test, Array.prototype.slice.call(arguments)];
24+
};
2925
26+
return module.exports;
3027
}).call(this);
31-
32-
return module.exports.apply(this, arguments);
33-
}
3428
'''
3529
]
3630

@@ -41,8 +35,7 @@ describe 'CommonCoffeeHandler', ->
4135
it 'produces code that will work with CouchDB', ->
4236
return @subject.handleFile @filePath
4337
.then (actual) =>
44-
fs.writeFileSync @tmpPath, "module.exports = #{actual[1]};"
45-
actual = (require @tmpPath) 'a', 'b'
38+
actual = (eval actual[1])('a', 'b');
4639

4740
assert.deepEqual actual, ['It works.', ['a', 'b']]
4841

test/suite/handler/CommonJsHandler.spec.coffee

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
fs = require 'fs'
2-
31
CommonJsHandler = require '../../../src/handler/CommonJsHandler'
42
HandlerError = require '../../../src/handler/error/HandlerError'
53

@@ -15,16 +13,13 @@ describe 'CommonJsHandler', ->
1513
expected = [
1614
'js'
1715
'''
18-
function () {
19-
var module = {};
2016
(function () {
2117
2218
var test = 'It works.';
2319
module.exports = function () { return [test, Array.prototype.slice.call(arguments)] };
2420
21+
return module.exports;
2522
}).call(this);
26-
return module.exports.apply(this, arguments);
27-
}
2823
'''
2924
]
3025

@@ -35,8 +30,7 @@ describe 'CommonJsHandler', ->
3530
it 'produces code that will work with CouchDB', ->
3631
return @subject.handleFile @filePath
3732
.then (actual) =>
38-
fs.writeFileSync @tmpPath, "module.exports = #{actual[1]};"
39-
actual = (require @tmpPath) 'a', 'b'
33+
actual = (eval actual[1])('a', 'b');
4034

4135
assert.deepEqual actual, ['It works.', ['a', 'b']]
4236

test/suite/index.spec.coffee

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,27 @@ describe 'Module', ->
77

88
it 'builds a result using the correct handlers', ->
99
expectedCoffee = '''
10-
function () {
11-
var module = {};
10+
(function () {
1211
13-
(function() {
14-
var test;
12+
var test;
1513
16-
test = 'It works.';
14+
test = 'It works.';
1715
18-
module.exports = function() {
19-
return [test, Array.prototype.slice.call(arguments)];
20-
};
16+
module.exports = function() {
17+
return [test, Array.prototype.slice.call(arguments)];
18+
};
2119
20+
return module.exports;
2221
}).call(this);
23-
24-
return module.exports.apply(this, arguments);
25-
}
2622
'''
2723
expectedJs = '''
28-
function () {
29-
var module = {};
3024
(function () {
3125
3226
var test = 'It works.';
3327
module.exports = function () { return [test, Array.prototype.slice.call(arguments)] };
3428
29+
return module.exports;
3530
}).call(this);
36-
return module.exports.apply(this, arguments);
37-
}
3831
'''
3932
expected =
4033
'coffee': expectedCoffee

0 commit comments

Comments
 (0)