Skip to content
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

feat(es6): update library to use latest mocha, should #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
52 changes: 52 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"root": true,
"plugins": ["node"],
"extends": ["eslint:recommended", "plugin:node/recommended"],
"env": {
"node": true
},
"rules": {
"quotes": ["error", "double"],
"no-undef": "error",
"no-extra-semi": "error",
"semi": "error",
"no-template-curly-in-string": "error",
"no-caller": "error",
"yoda": "error",
"eqeqeq": "error",
"global-require": "off",
"brace-style": "error",
"eol-last": "error",
"indent": ["error", "tab", { "SwitchCase": 1 }],
"no-extra-bind": "warn",
"no-empty": "off",
"no-multiple-empty-lines": "error",
"no-multi-spaces": "error",
"no-process-exit": "warn",
"space-in-parens": "error",
"no-trailing-spaces": "error",
"no-use-before-define": "off",
"no-unused-vars": ["error", {"args": "none"}],
"key-spacing": "error",
"space-infix-ops": "error",
"no-unsafe-negation": "error",
"no-loop-func": "warn",
"space-before-function-paren": ["error", "never"],
"space-before-blocks": "error",
"object-curly-spacing": ["error", "always"],
"keyword-spacing": ["error", {
"after": false,
"overrides": {
"try": {"after": true},
"else": {"after": true},
"throw": {"after": true},
"case": {"after": true},
"return": {"after": true},
"finally": {"after": true},
"do": {"after": true}
}
}],
"no-console": "off",
"valid-jsdoc": "error"
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/node_modules
/coverage
.DS_Store
*.log
.vscode/

Choose a reason for hiding this comment

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

.vscode should be in the global .gitignore.. Dont know if its a good thing to put it here.

4 changes: 2 additions & 2 deletions lib/CodeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ module.exports = CodeNode;

CodeNode.prototype.clone = function() {
return new CodeNode(this.generatedCode);
}
};

CodeNode.prototype.getGeneratedCode = function() {
return this.generatedCode;
};

CodeNode.prototype.getMappings = function(mappingsContext) {
var lines = getNumberOfLines(this.generatedCode);
return Array(lines+1).join(";");
return Array(lines + 1).join(";");
};

CodeNode.prototype.addGeneratedCode = function(generatedCode) {
Expand Down
58 changes: 33 additions & 25 deletions lib/SourceListMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,45 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
var CodeNode = require("./CodeNode");
var SourceNode = require("./SourceNode");
var MappingsContext = require("./MappingsContext");

function SourceListMap(generatedCode, source, originalSource) {
if(Array.isArray(generatedCode)) {
this.children = generatedCode;
} else {
this.children = [];
if(generatedCode || source)
this.add(generatedCode, source, originalSource);
class SourceListMap {
constructor(generatedCode, source, originalSource) {
if(Array.isArray(generatedCode)) {
this.children = generatedCode;
} else {
this.children = [];
if(generatedCode || source)
this.add(generatedCode, source, originalSource);
}
}
}
module.exports = SourceListMap;

SourceListMap.prototype.add = function(generatedCode, source, originalSource) {
if(typeof generatedCode === "string") {
if(source) {
this.children.push(new SourceNode(generatedCode, source, originalSource));
} else if(this.children.length > 0 && this.children[this.children.length - 1].addGeneratedCode) {
this.children[this.children.length - 1].addGeneratedCode(generatedCode);
add(generatedCode, source, originalSource) {
if(typeof generatedCode === "string") {
if(source) {
this.children.push(new SourceNode(generatedCode, source, originalSource));
} else if(this.children.length > 0 && this.children[this.children.length - 1].addGeneratedCode) {
this.children[this.children.length - 1].addGeneratedCode(generatedCode);
} else {
this.children.push(new CodeNode(generatedCode));
}
} else if(generatedCode.getMappings && generatedCode.getGeneratedCode) {
this.children.push(generatedCode);
} else if(generatedCode.children) {
generatedCode.children.forEach((sln) => {
this.children.push(sln);
});
} else {
this.children.push(new CodeNode(generatedCode));
throw new Error("Invalid arguments to SourceListMap.prototype.add: Expected string, Node or SourceListMap");
}
} else if(generatedCode.getMappings && generatedCode.getGeneratedCode) {
this.children.push(generatedCode);
} else if(generatedCode.children) {
generatedCode.children.forEach(function(sln) {
this.children.push(sln);
}, this);
} else {
throw new Error("Invalid arguments to SourceListMap.prototype.add: Expected string, Node or SourceListMap");
}
}

SourceListMap.prototype.add = function(generatedCode, source, originalSource) {

};

SourceListMap.prototype.preprend = function(source) {
Expand Down Expand Up @@ -87,4 +93,6 @@ SourceListMap.prototype.toStringWithSourceMap = function(options) {
mappings: mappings
}
};
}
};

module.exports = SourceListMap;
4 changes: 2 additions & 2 deletions lib/SourceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = SourceNode;

SourceNode.prototype.clone = function() {
return new SourceNode(this.generatedCode, this.source, this.originalSource, this.startingLine);
}
};

var LINE_MAPPING = "AACA;";
var LAST_LINE_MAPPING = "AACA";
Expand All @@ -32,7 +32,7 @@ SourceNode.prototype.getMappings = function(mappingsContext) {
mappings += base64VLQ.encode(this.startingLine - mappingsContext.currentOriginalLine); // original line index
mappings += "A"; // original column 0
if(lines !== 0)
mappings += ";"
mappings += ";";
mappingsContext.currentSource = sourceIdx;
mappingsContext.currentOriginalLine = (lines || 1) + this.startingLine - 1;
mappings += Array(lines).join(LINE_MAPPING);
Expand Down
95 changes: 47 additions & 48 deletions lib/base64-vlq.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,33 @@ var intToCharMap = {};

'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
.split('')
.forEach(function (ch, index) {
charToIntMap[ch] = index;
intToCharMap[index] = ch;
});
.forEach(function(ch, index) {
charToIntMap[ch] = index;
intToCharMap[index] = ch;
});

var base64 = {};
/**
* Encode an integer in the range of 0 to 63 to a single base 64 digit.
*/
base64.encode = function base64_encode(aNumber) {
if (aNumber in intToCharMap) {
return intToCharMap[aNumber];
}
throw new TypeError("Must be between 0 and 63: " + aNumber);
if(aNumber in intToCharMap) {
return intToCharMap[aNumber];
}
throw new TypeError("Must be between 0 and 63: " + aNumber);
};

/**
* Decode a single base 64 digit to an integer.
*/
base64.decode = function base64_decode(aChar) {
if (aChar in charToIntMap) {
return charToIntMap[aChar];
}
throw new TypeError("Not a valid base 64 digit: " + aChar);
if(aChar in charToIntMap) {
return charToIntMap[aChar];
}
throw new TypeError("Not a valid base 64 digit: " + aChar);
};



// A single base 64 digit can contain 6 bits of data. For the base 64 variable
// length quantities we use in the source map spec, the first bit is the sign,
// the next four bits are the actual value, and the 6th bit is the
Expand Down Expand Up @@ -100,7 +99,7 @@ var VLQ_CONTINUATION_BIT = VLQ_BASE;
* 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
*/
function toVLQSigned(aValue) {
return aValue < 0
return aValue < 0
? ((-aValue) << 1) + 1
: (aValue << 1) + 0;
}
Expand All @@ -112,9 +111,9 @@ function toVLQSigned(aValue) {
* 4 (100 binary) becomes 2, 5 (101 binary) becomes -2
*/
function fromVLQSigned(aValue) {
var isNegative = (aValue & 1) === 1;
var shifted = aValue >> 1;
return isNegative
var isNegative = (aValue & 1) === 1;
var shifted = aValue >> 1;
return isNegative
? -shifted
: shifted;
}
Expand All @@ -123,47 +122,47 @@ function fromVLQSigned(aValue) {
* Returns the base 64 VLQ encoded value.
*/
exports.encode = function base64VLQ_encode(aValue) {
var encoded = "";
var digit;
var encoded = "";
var digit;

var vlq = toVLQSigned(aValue);
var vlq = toVLQSigned(aValue);

do {
digit = vlq & VLQ_BASE_MASK;
vlq >>>= VLQ_BASE_SHIFT;
if (vlq > 0) {
do {
digit = vlq & VLQ_BASE_MASK;
vlq >>>= VLQ_BASE_SHIFT;
if(vlq > 0) {
// There are still more digits in this value, so we must make sure the
// continuation bit is marked.
digit |= VLQ_CONTINUATION_BIT;
}
encoded += base64.encode(digit);
} while (vlq > 0);
digit |= VLQ_CONTINUATION_BIT;
}
encoded += base64.encode(digit);
} while(vlq > 0);

return encoded;
return encoded;
};

/**
* Decodes the next base 64 VLQ value from the given string and returns the
* value and the rest of the string via the out parameter.
*/
exports.decode = function base64VLQ_decode(aStr, aOutParam) {
var i = 0;
var strLen = aStr.length;
var result = 0;
var shift = 0;
var continuation, digit;

do {
if (i >= strLen) {
throw new Error("Expected more digits in base 64 VLQ value.");
}
digit = base64.decode(aStr.charAt(i++));
continuation = !!(digit & VLQ_CONTINUATION_BIT);
digit &= VLQ_BASE_MASK;
result = result + (digit << shift);
shift += VLQ_BASE_SHIFT;
} while (continuation);

aOutParam.value = fromVLQSigned(result);
aOutParam.rest = aStr.slice(i);
var i = 0;
var strLen = aStr.length;
var result = 0;
var shift = 0;
var continuation, digit;

do {
if(i >= strLen) {
throw new Error("Expected more digits in base 64 VLQ value.");
}
digit = base64.decode(aStr.charAt(i++));
continuation = !!(digit & VLQ_CONTINUATION_BIT);
digit &= VLQ_BASE_MASK;
result = result + (digit << shift);
shift += VLQ_BASE_SHIFT;
} while(continuation);

aOutParam.value = fromVLQSigned(result);
aOutParam.rest = aStr.slice(i);
};
6 changes: 3 additions & 3 deletions lib/fromStringWithSourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function fromStringWithSourceMap(code, map) {
var currentSourceNodeLine;
mappings.forEach(function(mapping, idx) {
var line = lines[idx];
if(typeof line === 'undefined') return;
if(typeof line === "undefined") return;
if(idx !== lines.length - 1) line += "\n";
if(!mapping)
return addCode(line);
Expand All @@ -32,7 +32,7 @@ module.exports = function fromStringWithSourceMap(code, map) {
});
if(mappings.length < lines.length) {
var idx = mappings.length;
while(!lines[idx].trim() && idx < lines.length-1) {
while(!lines[idx].trim() && idx < lines.length - 1) {
addCode(lines[idx] + "\n");
idx++;
}
Expand Down Expand Up @@ -68,7 +68,7 @@ module.exports = function fromStringWithSourceMap(code, map) {
}

if(!ignore) {
addSource(line, sources ? sources[sourceIdx] : null, sourcesContent ? sourcesContent[sourceIdx] : null, linePosition)
addSource(line, sources ? sources[sourceIdx] : null, sourcesContent ? sourcesContent[sourceIdx] : null, linePosition);
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports.getNumberOfLines = function getNumberOfLines(str) {
var nr = -1;
var idx = -1;
do {
nr++
nr++;
idx = str.indexOf("\n", idx + 1);
} while(idx >= 0);
return nr;
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"author": "Tobias Koppers @sokra",
"main": "lib/index.js",
"scripts": {
"lint": "eslint lib",
"test": "mocha -R spec"
},
"repository": {
Expand All @@ -21,9 +22,14 @@
"bugs": {
"url": "https://github.com/webpack/source-list-map/issues"
},
"engines": {
"node": ">=4.3.0 <5.0.0 || >=5.10"
},
"homepage": "https://github.com/webpack/source-list-map",
"devDependencies": {
"mocha": "^2.2.1",
"should": "^5.2.0"
"eslint": "^3.16.1",
"eslint-plugin-node": "^4.1.0",
"mocha": "^3.2.0",
"should": "^11.2.0"
}
}
Loading