prettier-plugin-compactify
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -13,14 +13,87 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.preprocess = void 0; | ||
var core_1 = require("@babel/core"); | ||
var parser_1 = require("@babel/parser"); | ||
var traverse_1 = __importDefault(require("@babel/traverse")); | ||
var lodash_1 = require("lodash"); | ||
var parser_babel_1 = require("prettier/parser-babel"); | ||
var parser_flow_1 = require("prettier/parser-flow"); | ||
var parser_typescript_1 = require("prettier/parser-typescript"); | ||
var parser_flow_1 = require("prettier/parser-flow"); | ||
var preprocess_1 = require("./preprocess"); | ||
var preprocess = function (code) { | ||
var defaultConfig = { | ||
sourceType: "module", | ||
plugins: ["typescript", "jsx"], | ||
}; | ||
var babelConfig = core_1.loadPartialConfig(); | ||
var mergedOptions = lodash_1.merge(defaultConfig, babelConfig); | ||
var positions = []; | ||
var ast = parser_1.parse(code, mergedOptions); | ||
traverse_1.default(ast, { | ||
enter: function (path) { | ||
switch (path.node.type) { | ||
case "ObjectPattern": | ||
case "ObjectExpression": { | ||
var prop = path.node.properties[0]; | ||
if (prop === undefined || | ||
path.node.loc === null || | ||
prop.loc === null || | ||
path.node.start === null || | ||
prop.start === null || | ||
// if the property already follows in the same line then there's no | ||
// point in collapsing it | ||
path.node.loc.start.line === prop.loc.start.line) { | ||
return; | ||
} | ||
positions.push([path.node.start, prop.start]); | ||
break; | ||
} | ||
} | ||
}, | ||
}); | ||
var unshift = 0; | ||
var pos; | ||
toNextPosition: while ((pos = positions.shift())) { | ||
// put the cursor immediately after the opening brace | ||
var cursor = pos[0] + 1 - unshift; | ||
// figure out what follows the opening brace to decide if the node will be | ||
// collapsed | ||
toNextCursor: while (code[cursor]) { | ||
switch (code[cursor]) { | ||
case " ": | ||
case "\t": | ||
case "\r": | ||
case "\n": { | ||
// skip whitespace | ||
break; | ||
} | ||
case "/": { | ||
// don't collapse this node if a comment follows the opening brace | ||
continue toNextPosition; | ||
} | ||
default: { | ||
// found something other than whitespace or a comment after the opening brace | ||
break toNextCursor; | ||
} | ||
} | ||
cursor += 1; | ||
} | ||
// remove the whitespace following the opening brace | ||
code = "" + code.slice(0, pos[0] + 1 - unshift) + code.slice(pos[1] - unshift); | ||
// take into account what was removed for future positions | ||
unshift += pos[1] - 1 - pos[0]; | ||
} | ||
return code; | ||
}; | ||
exports.preprocess = preprocess; | ||
module.exports = { | ||
parsers: { | ||
babel: __assign(__assign({}, parser_babel_1.parsers.babel), { preprocess: preprocess_1.preprocess }), | ||
"babel-flow": __assign(__assign({}, parser_flow_1.parsers.flow), { preprocess: preprocess_1.preprocess }), | ||
typescript: __assign(__assign({}, parser_typescript_1.parsers.typescript), { preprocess: preprocess_1.preprocess }), | ||
babel: __assign(__assign({}, parser_babel_1.parsers.babel), { preprocess: exports.preprocess }), | ||
"babel-flow": __assign(__assign({}, parser_flow_1.parsers.flow), { preprocess: exports.preprocess }), | ||
typescript: __assign(__assign({}, parser_typescript_1.parsers.typescript), { preprocess: exports.preprocess }), | ||
}, | ||
options: {}, | ||
}; |
{ | ||
"name": "prettier-plugin-compactify", | ||
"version": "0.0.0", | ||
"version": "0.1.1", | ||
"license": "Apache-2.0", | ||
@@ -21,3 +21,4 @@ "main": "lib/src/index.js", | ||
"fix:prettier": "yarn prettier --write", | ||
"fix": "yarn fix:eslint && yarn fix:prettier" | ||
"fix": "yarn fix:eslint && yarn fix:prettier", | ||
"release": "rm *.tgz; yarn pack && yarn publish *.tgz" | ||
}, | ||
@@ -24,0 +25,0 @@ "dependencies": { |
# prettier-plugin-compactify | ||
Available on npm as `prettier-plugin-compactify` | ||
# Introduction | ||
@@ -29,3 +31,3 @@ | ||
This plugin was created because Prettier keeps arbitrary newlines after a `{` | ||
even if they are not wanted. For more context see | ||
even if the user does not want them. For more context see | ||
[issue 10757](https://github.com/prettier/prettier/issues/10757). | ||
@@ -42,7 +44,7 @@ | ||
- `yarn fix` formats all files | ||
- `yarn lint` lints all files | ||
- `yarn fix` for formatting | ||
- `yarn lint` for linting | ||
# Release | ||
1. `yarn pack` | ||
2. `yarn publish *.tgz` | ||
`yarn release` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
19941
167
49
0