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

Migrate test suite to Jest #18

Closed
wants to merge 7 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
coverage
7 changes: 6 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"camelcase": true,
"curly": false,
"eqeqeq": true,
"es3": true,
"es3": false,
"esversion": 2016,
"forin": true,
"immed": false,
"indent": false,
Expand All @@ -30,6 +31,8 @@
"smarttabs": true,
"sub": true,

"loopfunc" : true,

"node": true,
"globals": {
"describe": false,
Expand All @@ -38,6 +41,8 @@
"beforeEach": false,
"after": false,
"afterEach": false,
"expect": false,
"fail": false,
"define": false,
"window": false,
"atob": true,
Expand Down
195 changes: 39 additions & 156 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ function customDecodeUriComponent(string) {
return decodeUriComponentLib(string.replace(/\+/g, "%2B"))
}

function callbackAsync(callback, error, result) {
setImmediate(function() { callback(error, result) })
}

function parseMapToJSON(string, data) {
try {
return JSON.parse(string.replace(/^\)\]\}'/, ""))
Expand All @@ -33,17 +29,6 @@ function parseMapToJSON(string, data) {
}
}

function readSync(read, url, data) {
var readUrl = customDecodeUriComponent(url)
try {
return String(read(readUrl))
} catch (error) {
error.sourceMapData = data
throw error
}
}



var innerRegex = /[#@] sourceMappingURL=([^\s'"]*)/

Expand All @@ -67,40 +52,22 @@ function getSourceMappingUrl(code) {



function resolveSourceMap(code, codeUrl, read, callback) {
var mapData
try {
mapData = resolveSourceMapHelper(code, codeUrl)
} catch (error) {
return callbackAsync(callback, error)
}
async function resolveSourceMap(code, codeUrl, read) {
var mapData = resolveSourceMapHelper(code, codeUrl)

if (!mapData || mapData.map) {
return callbackAsync(callback, null, mapData)
return mapData
}
var readUrl = customDecodeUriComponent(mapData.url)
read(readUrl, function(error, result) {
if (error) {
error.sourceMapData = mapData
return callback(error)
}
try {
const result = await read(readUrl)
mapData.map = String(result)
try {
mapData.map = parseMapToJSON(mapData.map, mapData)
} catch (error) {
return callback(error)
}
callback(null, mapData)
})
}

function resolveSourceMapSync(code, codeUrl, read) {
var mapData = resolveSourceMapHelper(code, codeUrl)
if (!mapData || mapData.map) {
mapData.map = parseMapToJSON(mapData.map, mapData)
return mapData
} catch (error) {
error.sourceMapData = mapData
throw error
}
mapData.map = readSync(read, mapData.url, mapData)
mapData.map = parseMapToJSON(mapData.map, mapData)
return mapData
}

var dataUriRegex = /^data:([^,;]*)(;[^,;]*)*(?:,(.*))?$/
Expand Down Expand Up @@ -190,76 +157,37 @@ function resolveSourceMapHelper(code, codeUrl) {



function resolveSources(map, mapUrl, read, options, callback) {
if (typeof options === "function") {
callback = options
options = {}
}
var pending = map.sources ? map.sources.length : 0
async function resolveSources(map, mapUrl, read, options) {
var result = {
sourcesResolved: [],
sourcesContent: []
}

if (pending === 0) {
callbackAsync(callback, null, result)
return
}

var done = function() {
pending--
if (pending === 0) {
callback(null, result)
}
if (!map.sources || map.sources.length === 0) {
return result
}

resolveSourcesHelper(map, mapUrl, options, function(fullUrl, sourceContent, index) {
for (let {fullUrl, sourceContent, index} of resolveSourcesHelper(map, mapUrl, options)) {
result.sourcesResolved[index] = fullUrl
if (typeof sourceContent === "string") {
result.sourcesContent[index] = sourceContent
callbackAsync(done, null)
} else {
var readUrl = customDecodeUriComponent(fullUrl)
read(readUrl, function(error, source) {
result.sourcesContent[index] = error ? error : String(source)
done()
})
}
})
}

function resolveSourcesSync(map, mapUrl, read, options) {
var result = {
sourcesResolved: [],
sourcesContent: []
}

if (!map.sources || map.sources.length === 0) {
return result
}

resolveSourcesHelper(map, mapUrl, options, function(fullUrl, sourceContent, index) {
result.sourcesResolved[index] = fullUrl
if (read !== null) {
if (typeof sourceContent === "string") {
result.sourcesContent[index] = sourceContent
} else {
var readUrl = customDecodeUriComponent(fullUrl)
try {
result.sourcesContent[index] = String(read(readUrl))
} catch (error) {
result.sourcesContent[index] = error
}
try {
const source = await read(readUrl)
result.sourcesContent[index] = String(source)
} catch (error) {
result.sourcesContent[index] = error
}
}
})
}

return result
}

var endingSlash = /\/?$/

function resolveSourcesHelper(map, mapUrl, options, fn) {
function* resolveSourcesHelper(map, mapUrl, options) {
options = options || {}
mapUrl = convertWindowsPath(mapUrl)
var fullUrl
Expand All @@ -283,17 +211,13 @@ function resolveSourcesHelper(map, mapUrl, options, fn) {
fullUrl = resolveUrl(mapUrl, sourceRoot.replace(endingSlash, "/"), map.sources[index])
}
sourceContent = (map.sourcesContent || [])[index]
fn(fullUrl, sourceContent, index)
yield {fullUrl, sourceContent, index}
}
}



function resolve(code, codeUrl, read, options, callback) {
if (typeof options === "function") {
callback = options
options = {}
}
async function resolve(code, codeUrl, read, options) {
if (code === null) {
var mapUrl = codeUrl
var data = {
Expand All @@ -303,75 +227,34 @@ function resolve(code, codeUrl, read, options, callback) {
map: null
}
var readUrl = customDecodeUriComponent(mapUrl)
read(readUrl, function(error, result) {
if (error) {
error.sourceMapData = data
return callback(error)
}
try {
const result = await read(readUrl)
data.map = String(result)
try {
data.map = parseMapToJSON(data.map, data)
} catch (error) {
return callback(error)
}
_resolveSources(data)
})
} else {
resolveSourceMap(code, codeUrl, read, function(error, mapData) {
if (error) {
return callback(error)
}
if (!mapData) {
return callback(null, null)
}
_resolveSources(mapData)
})
}

function _resolveSources(mapData) {
resolveSources(mapData.map, mapData.sourcesRelativeTo, read, options, function(error, result) {
if (error) {
return callback(error)
}
mapData.sourcesResolved = result.sourcesResolved
mapData.sourcesContent = result.sourcesContent
callback(null, mapData)
})
}
}

function resolveSync(code, codeUrl, read, options) {
var mapData
if (code === null) {
var mapUrl = codeUrl
mapData = {
sourceMappingURL: null,
url: mapUrl,
sourcesRelativeTo: mapUrl,
map: null
data.map = parseMapToJSON(data.map, data)
return await _resolveSources(data)
} catch (error) {
error.sourceMapData = data
throw error
}
mapData.map = readSync(read, mapUrl, mapData)
mapData.map = parseMapToJSON(mapData.map, mapData)
} else {
mapData = resolveSourceMapSync(code, codeUrl, read)
const mapData = await resolveSourceMap(code, codeUrl, read)
if (!mapData) {
return null
}
return await _resolveSources(mapData)
}
var result = resolveSourcesSync(mapData.map, mapData.sourcesRelativeTo, read, options)
mapData.sourcesResolved = result.sourcesResolved
mapData.sourcesContent = result.sourcesContent
return mapData
}


async function _resolveSources(mapData) {
const result = await resolveSources(mapData.map, mapData.sourcesRelativeTo, read, options)
mapData.sourcesResolved = result.sourcesResolved
mapData.sourcesContent = result.sourcesContent
return mapData
}
}

module.exports = {
resolveSourceMap: resolveSourceMap,
resolveSourceMapSync: resolveSourceMapSync,
resolveSources: resolveSources,
resolveSourcesSync: resolveSourcesSync,
resolve: resolve,
resolveSync: resolveSync,
parseMapToJSON: parseMapToJSON
}
Loading