Normalize exports (#339)
Make all the files have a single module.exports call at the end of the file. I added a missing `"use strict";` and removed a few unused functions from utils at the same time.master
parent
aef3e387f8
commit
8202a6c3b5
|
@ -118,7 +118,7 @@ function decorateComment(node, comment, text) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.attach = function(comments, ast, text) {
|
function attach(comments, ast, text) {
|
||||||
if (!isArray.check(comments)) {
|
if (!isArray.check(comments)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -281,9 +281,8 @@ function printDanglingComments(path, print, options) {
|
||||||
}, "comments");
|
}, "comments");
|
||||||
return concat(parts);
|
return concat(parts);
|
||||||
}
|
}
|
||||||
exports.printDanglingComments = printDanglingComments;
|
|
||||||
|
|
||||||
exports.printComments = function(path, print, options) {
|
function printComments(path, print, options) {
|
||||||
var value = path.getValue();
|
var value = path.getValue();
|
||||||
var parent = path.getParentNode();
|
var parent = path.getParentNode();
|
||||||
var printed = print(path);
|
var printed = print(path);
|
||||||
|
@ -331,3 +330,9 @@ exports.printComments = function(path, print, options) {
|
||||||
leadingParts.push.apply(leadingParts, trailingParts);
|
leadingParts.push.apply(leadingParts, trailingParts);
|
||||||
return concat(leadingParts);
|
return concat(leadingParts);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
attach,
|
||||||
|
printComments,
|
||||||
|
printDanglingComments,
|
||||||
|
}
|
||||||
|
|
|
@ -12,7 +12,6 @@ function FastPath(value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var FPp = FastPath.prototype;
|
var FPp = FastPath.prototype;
|
||||||
module.exports = FastPath;
|
|
||||||
|
|
||||||
// Static convenience function for coercing a value to a FastPath.
|
// Static convenience function for coercing a value to a FastPath.
|
||||||
FastPath.from = function(obj) {
|
FastPath.from = function(obj) {
|
||||||
|
@ -507,3 +506,5 @@ FPp.firstInStatement = function() {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = FastPath;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
var defaults = {
|
var defaults = {
|
||||||
// Number of spaces the pretty-printer should use per tab
|
// Number of spaces the pretty-printer should use per tab
|
||||||
tabWidth: 2,
|
tabWidth: 2,
|
||||||
|
@ -12,7 +14,7 @@ var defaults = {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Copy options and fill in default values.
|
// Copy options and fill in default values.
|
||||||
exports.normalize = function(options) {
|
function normalize(options) {
|
||||||
const normalized = Object.assign({}, options || {});
|
const normalized = Object.assign({}, options || {});
|
||||||
Object.keys(defaults).forEach(k => {
|
Object.keys(defaults).forEach(k => {
|
||||||
if (normalized[k] == null) {
|
if (normalized[k] == null) {
|
||||||
|
@ -21,3 +23,7 @@ exports.normalize = function(options) {
|
||||||
});
|
});
|
||||||
return normalized;
|
return normalized;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
normalize
|
||||||
|
};
|
||||||
|
|
80
src/util.js
80
src/util.js
|
@ -1,34 +1,11 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
var assert = require("assert");
|
var assert = require("assert");
|
||||||
var types = require("ast-types");
|
var types = require("ast-types");
|
||||||
var getFieldValue = types.getFieldValue;
|
|
||||||
var n = types.namedTypes;
|
var n = types.namedTypes;
|
||||||
var hasOwn = Object.prototype.hasOwnProperty;
|
|
||||||
var util = exports;
|
|
||||||
|
|
||||||
function getUnionOfKeys() {
|
|
||||||
var result = {};
|
|
||||||
var argc = arguments.length;
|
|
||||||
for (var i = 0; i < argc; ++i) {
|
|
||||||
var keys = Object.keys(arguments[i]);
|
|
||||||
var keyCount = keys.length;
|
|
||||||
for (var j = 0; j < keyCount; ++j) {
|
|
||||||
result[keys[j]] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
util.getUnionOfKeys = getUnionOfKeys;
|
|
||||||
|
|
||||||
function comparePos(pos1, pos2) {
|
function comparePos(pos1, pos2) {
|
||||||
return pos1.line - pos2.line || pos1.column - pos2.column;
|
return pos1.line - pos2.line || pos1.column - pos2.column;
|
||||||
}
|
}
|
||||||
util.comparePos = comparePos;
|
|
||||||
|
|
||||||
function copyPos(pos) {
|
|
||||||
return { line: pos.line, column: pos.column };
|
|
||||||
}
|
|
||||||
util.copyPos = copyPos;
|
|
||||||
|
|
||||||
function expandLoc(parentNode, childNode) {
|
function expandLoc(parentNode, childNode) {
|
||||||
if (locStart(childNode) - locStart(parentNode) < 0) {
|
if (locStart(childNode) - locStart(parentNode) < 0) {
|
||||||
|
@ -40,14 +17,14 @@ function expandLoc(parentNode, childNode) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
util.fixFaultyLocations = function(node, text) {
|
function fixFaultyLocations(node, text) {
|
||||||
if (node.decorators) {
|
if (node.decorators) {
|
||||||
// Expand the loc of the node responsible for printing the decorators
|
// Expand the loc of the node responsible for printing the decorators
|
||||||
// (here, the decorated node) so that it includes node.decorators.
|
// (here, the decorated node) so that it includes node.decorators.
|
||||||
node.decorators.forEach(function(decorator) {
|
node.decorators.forEach(function(decorator) {
|
||||||
expandLoc(node, decorator);
|
expandLoc(node, decorator);
|
||||||
});
|
});
|
||||||
} else if (node.declaration && util.isExportDeclaration(node)) {
|
} else if (node.declaration && isExportDeclaration(node)) {
|
||||||
// Expand the loc of the node responsible for printing the decorators
|
// Expand the loc of the node responsible for printing the decorators
|
||||||
// (here, the export declaration) so that it includes node.decorators.
|
// (here, the export declaration) so that it includes node.decorators.
|
||||||
var decorators = node.declaration.decorators;
|
var decorators = node.declaration.decorators;
|
||||||
|
@ -77,7 +54,7 @@ util.fixFaultyLocations = function(node, text) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
util.isExportDeclaration = function(node) {
|
function isExportDeclaration(node) {
|
||||||
if (node) switch (node.type) {
|
if (node) switch (node.type) {
|
||||||
case "ExportDeclaration":
|
case "ExportDeclaration":
|
||||||
case "ExportDefaultDeclaration":
|
case "ExportDefaultDeclaration":
|
||||||
|
@ -91,10 +68,10 @@ util.isExportDeclaration = function(node) {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
util.getParentExportDeclaration = function(path) {
|
function getParentExportDeclaration(path) {
|
||||||
var parentNode = path.getParentNode();
|
var parentNode = path.getParentNode();
|
||||||
if (
|
if (
|
||||||
path.getName() === "declaration" && util.isExportDeclaration(parentNode)
|
path.getName() === "declaration" && isExportDeclaration(parentNode)
|
||||||
) {
|
) {
|
||||||
return parentNode;
|
return parentNode;
|
||||||
}
|
}
|
||||||
|
@ -102,15 +79,7 @@ util.getParentExportDeclaration = function(path) {
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
util.isTrailingCommaEnabled = function(options, context) {
|
function getLast(arr) {
|
||||||
var trailingComma = options.trailingComma;
|
|
||||||
if (typeof trailingComma === "object") {
|
|
||||||
return !!trailingComma[context];
|
|
||||||
}
|
|
||||||
return !!trailingComma;
|
|
||||||
};
|
|
||||||
|
|
||||||
util.getLast = function(arr) {
|
|
||||||
if (arr.length > 0) {
|
if (arr.length > 0) {
|
||||||
return arr[arr.length - 1];
|
return arr[arr.length - 1];
|
||||||
}
|
}
|
||||||
|
@ -134,7 +103,7 @@ function skipNewLineForward(text, index) {
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
function _findNewline(text, index, backwards) {
|
function findNewline(text, index, backwards) {
|
||||||
const length = text.length;
|
const length = text.length;
|
||||||
let cursor = backwards ? index - 1 : skipNewLineForward(text, index);
|
let cursor = backwards ? index - 1 : skipNewLineForward(text, index);
|
||||||
// Look forward and see if there is a newline after/before this code
|
// Look forward and see if there is a newline after/before this code
|
||||||
|
@ -152,12 +121,12 @@ function _findNewline(text, index, backwards) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
util.newlineExistsBefore = function(text, index) {
|
function newlineExistsBefore(text, index) {
|
||||||
return _findNewline(text, index, true);
|
return findNewline(text, index, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
util.newlineExistsAfter = function(text, index) {
|
function newlineExistsAfter(text, index) {
|
||||||
return _findNewline(text, index);
|
return findNewline(text, index);
|
||||||
};
|
};
|
||||||
|
|
||||||
function skipSpaces(text, index, backwards) {
|
function skipSpaces(text, index, backwards) {
|
||||||
|
@ -175,7 +144,6 @@ function skipSpaces(text, index, backwards) {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
util.skipSpaces = skipSpaces;
|
|
||||||
|
|
||||||
function locStart(node) {
|
function locStart(node) {
|
||||||
if (node.range) {
|
if (node.range) {
|
||||||
|
@ -183,7 +151,6 @@ function locStart(node) {
|
||||||
}
|
}
|
||||||
return node.start;
|
return node.start;
|
||||||
}
|
}
|
||||||
util.locStart = locStart;
|
|
||||||
|
|
||||||
function locEnd(node) {
|
function locEnd(node) {
|
||||||
if (node.range) {
|
if (node.range) {
|
||||||
|
@ -191,7 +158,6 @@ function locEnd(node) {
|
||||||
}
|
}
|
||||||
return node.end;
|
return node.end;
|
||||||
}
|
}
|
||||||
util.locEnd = locEnd;
|
|
||||||
|
|
||||||
function setLocStart(node, index) {
|
function setLocStart(node, index) {
|
||||||
if (node.range) {
|
if (node.range) {
|
||||||
|
@ -200,7 +166,6 @@ function setLocStart(node, index) {
|
||||||
node.start = index;
|
node.start = index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
util.setLocStart = setLocStart;
|
|
||||||
|
|
||||||
function setLocEnd(node, index) {
|
function setLocEnd(node, index) {
|
||||||
if (node.range) {
|
if (node.range) {
|
||||||
|
@ -209,7 +174,6 @@ function setLocEnd(node, index) {
|
||||||
node.end = index;
|
node.end = index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
util.setLocEnd = setLocEnd;
|
|
||||||
|
|
||||||
// http://stackoverflow.com/a/7124052
|
// http://stackoverflow.com/a/7124052
|
||||||
function htmlEscapeInsideDoubleQuote(str) {
|
function htmlEscapeInsideDoubleQuote(str) {
|
||||||
|
@ -220,7 +184,6 @@ function htmlEscapeInsideDoubleQuote(str) {
|
||||||
// .replace(/</g, '<')
|
// .replace(/</g, '<')
|
||||||
// .replace(/>/g, '>');
|
// .replace(/>/g, '>');
|
||||||
}
|
}
|
||||||
util.htmlEscapeInsideDoubleQuote = htmlEscapeInsideDoubleQuote;
|
|
||||||
|
|
||||||
// http://stackoverflow.com/a/7124052
|
// http://stackoverflow.com/a/7124052
|
||||||
function htmlEscapeInsideAngleBracket(str) {
|
function htmlEscapeInsideAngleBracket(str) {
|
||||||
|
@ -231,7 +194,6 @@ function htmlEscapeInsideAngleBracket(str) {
|
||||||
// .replace(/"/g, '"')
|
// .replace(/"/g, '"')
|
||||||
// .replace(/'/g, ''')
|
// .replace(/'/g, ''')
|
||||||
}
|
}
|
||||||
util.htmlEscapeInsideAngleBracket = htmlEscapeInsideAngleBracket;
|
|
||||||
|
|
||||||
var PRECEDENCE = {};
|
var PRECEDENCE = {};
|
||||||
[
|
[
|
||||||
|
@ -251,6 +213,24 @@ var PRECEDENCE = {};
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
util.getPrecedence = function(op) {
|
function getPrecedence(op) {
|
||||||
return PRECEDENCE[op];
|
return PRECEDENCE[op];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
comparePos,
|
||||||
|
getPrecedence,
|
||||||
|
fixFaultyLocations,
|
||||||
|
isExportDeclaration,
|
||||||
|
getParentExportDeclaration,
|
||||||
|
getLast,
|
||||||
|
newlineExistsBefore,
|
||||||
|
newlineExistsAfter,
|
||||||
|
skipSpaces,
|
||||||
|
locStart,
|
||||||
|
locEnd,
|
||||||
|
setLocStart,
|
||||||
|
setLocEnd,
|
||||||
|
htmlEscapeInsideDoubleQuote,
|
||||||
|
htmlEscapeInsideAngleBracket
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue