From 8202a6c3b54ab63864128b0304f7ae0bdfbbfb84 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Thu, 19 Jan 2017 13:45:36 -0800 Subject: [PATCH] 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. --- src/comments.js | 11 +++++-- src/fast-path.js | 3 +- src/options.js | 8 ++++- src/util.js | 80 ++++++++++++++++++------------------------------ 4 files changed, 47 insertions(+), 55 deletions(-) diff --git a/src/comments.js b/src/comments.js index a9c0f373..76c20093 100644 --- a/src/comments.js +++ b/src/comments.js @@ -118,7 +118,7 @@ function decorateComment(node, comment, text) { } } -exports.attach = function(comments, ast, text) { +function attach(comments, ast, text) { if (!isArray.check(comments)) { return; } @@ -281,9 +281,8 @@ function printDanglingComments(path, print, options) { }, "comments"); return concat(parts); } -exports.printDanglingComments = printDanglingComments; -exports.printComments = function(path, print, options) { +function printComments(path, print, options) { var value = path.getValue(); var parent = path.getParentNode(); var printed = print(path); @@ -331,3 +330,9 @@ exports.printComments = function(path, print, options) { leadingParts.push.apply(leadingParts, trailingParts); return concat(leadingParts); }; + +module.exports = { + attach, + printComments, + printDanglingComments, +} diff --git a/src/fast-path.js b/src/fast-path.js index 666a65b7..c8479f66 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -12,7 +12,6 @@ function FastPath(value) { } var FPp = FastPath.prototype; -module.exports = FastPath; // Static convenience function for coercing a value to a FastPath. FastPath.from = function(obj) { @@ -507,3 +506,5 @@ FPp.firstInStatement = function() { return true; }; + +module.exports = FastPath; diff --git a/src/options.js b/src/options.js index 545036ca..bd41a001 100644 --- a/src/options.js +++ b/src/options.js @@ -1,3 +1,5 @@ +"use strict"; + var defaults = { // Number of spaces the pretty-printer should use per tab tabWidth: 2, @@ -12,7 +14,7 @@ var defaults = { }; // Copy options and fill in default values. -exports.normalize = function(options) { +function normalize(options) { const normalized = Object.assign({}, options || {}); Object.keys(defaults).forEach(k => { if (normalized[k] == null) { @@ -21,3 +23,7 @@ exports.normalize = function(options) { }); return normalized; }; + +module.exports = { + normalize +}; diff --git a/src/util.js b/src/util.js index 3e6a8a58..7429aa4d 100644 --- a/src/util.js +++ b/src/util.js @@ -1,34 +1,11 @@ "use strict"; var assert = require("assert"); var types = require("ast-types"); -var getFieldValue = types.getFieldValue; 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) { 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) { 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) { // Expand the loc of the node responsible for printing the decorators // (here, the decorated node) so that it includes node.decorators. node.decorators.forEach(function(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 // (here, the export declaration) so that it includes node.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) { case "ExportDeclaration": case "ExportDefaultDeclaration": @@ -91,10 +68,10 @@ util.isExportDeclaration = function(node) { return false; }; -util.getParentExportDeclaration = function(path) { +function getParentExportDeclaration(path) { var parentNode = path.getParentNode(); if ( - path.getName() === "declaration" && util.isExportDeclaration(parentNode) + path.getName() === "declaration" && isExportDeclaration(parentNode) ) { return parentNode; } @@ -102,15 +79,7 @@ util.getParentExportDeclaration = function(path) { return null; }; -util.isTrailingCommaEnabled = function(options, context) { - var trailingComma = options.trailingComma; - if (typeof trailingComma === "object") { - return !!trailingComma[context]; - } - return !!trailingComma; -}; - -util.getLast = function(arr) { +function getLast(arr) { if (arr.length > 0) { return arr[arr.length - 1]; } @@ -134,7 +103,7 @@ function skipNewLineForward(text, index) { return index; } -function _findNewline(text, index, backwards) { +function findNewline(text, index, backwards) { const length = text.length; let cursor = backwards ? index - 1 : skipNewLineForward(text, index); // Look forward and see if there is a newline after/before this code @@ -152,12 +121,12 @@ function _findNewline(text, index, backwards) { return false; } -util.newlineExistsBefore = function(text, index) { - return _findNewline(text, index, true); +function newlineExistsBefore(text, index) { + return findNewline(text, index, true); }; -util.newlineExistsAfter = function(text, index) { - return _findNewline(text, index); +function newlineExistsAfter(text, index) { + return findNewline(text, index); }; function skipSpaces(text, index, backwards) { @@ -175,7 +144,6 @@ function skipSpaces(text, index, backwards) { } return false; } -util.skipSpaces = skipSpaces; function locStart(node) { if (node.range) { @@ -183,7 +151,6 @@ function locStart(node) { } return node.start; } -util.locStart = locStart; function locEnd(node) { if (node.range) { @@ -191,7 +158,6 @@ function locEnd(node) { } return node.end; } -util.locEnd = locEnd; function setLocStart(node, index) { if (node.range) { @@ -200,7 +166,6 @@ function setLocStart(node, index) { node.start = index; } } -util.setLocStart = setLocStart; function setLocEnd(node, index) { if (node.range) { @@ -209,7 +174,6 @@ function setLocEnd(node, index) { node.end = index; } } -util.setLocEnd = setLocEnd; // http://stackoverflow.com/a/7124052 function htmlEscapeInsideDoubleQuote(str) { @@ -220,7 +184,6 @@ function htmlEscapeInsideDoubleQuote(str) { // .replace(//g, '>'); } -util.htmlEscapeInsideDoubleQuote = htmlEscapeInsideDoubleQuote; // http://stackoverflow.com/a/7124052 function htmlEscapeInsideAngleBracket(str) { @@ -231,7 +194,6 @@ function htmlEscapeInsideAngleBracket(str) { // .replace(/"/g, '"') // .replace(/'/g, ''') } -util.htmlEscapeInsideAngleBracket = htmlEscapeInsideAngleBracket; var PRECEDENCE = {}; [ @@ -251,6 +213,24 @@ var PRECEDENCE = {}; }); }); -util.getPrecedence = function(op) { +function getPrecedence(op) { return PRECEDENCE[op]; }; + +module.exports = { + comparePos, + getPrecedence, + fixFaultyLocations, + isExportDeclaration, + getParentExportDeclaration, + getLast, + newlineExistsBefore, + newlineExistsAfter, + skipSpaces, + locStart, + locEnd, + setLocStart, + setLocEnd, + htmlEscapeInsideDoubleQuote, + htmlEscapeInsideAngleBracket +};