Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@trivago/prettier-plugin-sort-imports

Package Overview
Dependencies
Maintainers
4
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trivago/prettier-plugin-sort-imports - npm Package Compare versions

Comparing version 2.0.0-0 to 2.0.0

lib/src/utils/__tests__/get-all-comments-from-nodes.spec.js

12

lib/src/preprocessor.js
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};

@@ -26,3 +24,3 @@ var __importDefault = (this && this.__importDefault) || function (mod) {

sourceType: 'module',
plugins: __spreadArrays(plugins, experimentalBabelParserPluginsList),
plugins: __spreadArray(__spreadArray([], plugins), experimentalBabelParserPluginsList),
};

@@ -29,0 +27,0 @@ var ast = parser_1.parse(code, parserOptions);

"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllCommentsFromNodes = void 0;
exports.getAllCommentsFromNodes = function (nodes) {
var getAllCommentsFromNodes = function (nodes) {
return nodes.reduce(function (acc, node) {
if (Array.isArray(node.leadingComments) &&
node.leadingComments.length > 0) {
acc = __spreadArrays(acc, node.leadingComments);
acc = __spreadArray(__spreadArray([], acc), node.leadingComments);
}

@@ -20,1 +18,2 @@ return acc;

};
exports.getAllCommentsFromNodes = getAllCommentsFromNodes;
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};

@@ -24,5 +22,5 @@ var __importDefault = (this && this.__importDefault) || function (mod) {

*/
exports.getCodeFromAst = function (nodes, originalCode) {
var getCodeFromAst = function (nodes, originalCode) {
var allCommentsFromImports = get_all_comments_from_nodes_1.getAllCommentsFromNodes(nodes);
var commentAndImportsToRemoveFromCode = __spreadArrays(nodes, allCommentsFromImports);
var commentAndImportsToRemoveFromCode = __spreadArray(__spreadArray([], nodes), allCommentsFromImports);
var codeWithoutImportDeclarations = remove_nodes_from_original_code_1.removeNodesFromOriginalCode(originalCode, commentAndImportsToRemoveFromCode);

@@ -49,1 +47,2 @@ var newAST = types_1.file({

};
exports.getCodeFromAst = getCodeFromAst;
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};

@@ -17,3 +15,3 @@ Object.defineProperty(exports, "__esModule", { value: true });

*/
exports.getParserPlugins = function (prettierParser) {
var getParserPlugins = function (prettierParser) {
var isFlow = prettierParser === constants_1.flow;

@@ -25,3 +23,4 @@ var isTypescript = prettierParser === constants_1.typescript;

var tsPlugins = [constants_1.typescript, constants_1.jsx, constants_1.decoratorsLegacy, constants_1.classProperties];
return __spreadArrays((isFlow ? [constants_1.flow] : []), (isTypescript ? tsPlugins : []));
return __spreadArray(__spreadArray([], (isFlow ? [constants_1.flow] : [])), (isTypescript ? tsPlugins : []));
};
exports.getParserPlugins = getParserPlugins;
"use strict";
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};

@@ -28,3 +26,3 @@ var __importDefault = (this && this.__importDefault) || function (mod) {

*/
exports.getSortedNodes = function (nodes, order, importOrderSeparation) {
var getSortedNodes = function (nodes, order, importOrderSeparation) {
var originalNodes = nodes.map(lodash_1.clone);

@@ -35,7 +33,7 @@ var newLine = importOrderSeparation && nodes.length > 1 ? constants_1.newLineNode : null;

// remove "found" imports from the list of nodes
lodash_1.pull.apply(void 0, __spreadArrays([originalNodes], x));
lodash_1.pull.apply(void 0, __spreadArray([originalNodes], x));
if (x.length > 0) {
x.sort(function (a, b) { return javascript_natural_sort_1.default(a.source.value, b.source.value); });
if (res.length > 0) {
return lodash_1.compact(__spreadArrays(res, [newLine], x));
return lodash_1.compact(__spreadArray(__spreadArray(__spreadArray([], res), [newLine]), x));
}

@@ -51,6 +49,6 @@ return x;

var shouldAddNewLineInBetween = sortedNodesNotInImportOrder.length > 0 && importOrderSeparation;
var allSortedNodes = lodash_1.compact(__spreadArrays(sortedNodesNotInImportOrder, [
var allSortedNodes = lodash_1.compact(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], sortedNodesNotInImportOrder), [
shouldAddNewLineInBetween ? constants_1.newLineNode : null
], sortedNodesByImportOrder, [
constants_1.newLineNode,
]), sortedNodesByImportOrder), [
constants_1.newLineNode, // insert a newline after all sorted imports
]));

@@ -73,1 +71,2 @@ // maintain a copy of the nodes to extract comments from

};
exports.getSortedNodes = getSortedNodes;

@@ -9,4 +9,5 @@ "use strict";

*/
exports.isSimilarTextExistInArray = function (list, text) {
var isSimilarTextExistInArray = function (list, text) {
return list.some(function (element) { return text.match(new RegExp(element)) !== null; });
};
exports.isSimilarTextExistInArray = isSimilarTextExistInArray;

@@ -9,3 +9,3 @@ "use strict";

*/
exports.removeNodesFromOriginalCode = function (code, nodes) {
var removeNodesFromOriginalCode = function (code, nodes) {
var text = code;

@@ -22,1 +22,2 @@ for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {

};
exports.removeNodesFromOriginalCode = removeNodesFromOriginalCode;
{
"name": "@trivago/prettier-plugin-sort-imports",
"version": "2.0.0-0",
"version": "2.0.0",
"description": "A prettier plugins to sort imports in provided RegEx order",

@@ -33,21 +33,20 @@ "main": "lib/src/index.js",

"dependencies": {
"@babel/core": "^7.13.8",
"@babel/generator": "^7.13.9",
"@babel/parser": "^7.13.9",
"@babel/traverse": "^7.13.0",
"@babel/types": "^7.13.0",
"@types/lodash": "^4.14.168",
"javascript-natural-sort": "^0.7.1",
"lodash": "^4.17.21",
"prettier": "^2.2.1"
"@babel/core": "7.13.10",
"@babel/generator": "7.13.9",
"@babel/parser": "7.13.10",
"@babel/traverse": "7.13.0",
"@babel/types": "7.13.0",
"@types/lodash": "4.14.168",
"javascript-natural-sort": "0.7.1",
"lodash": "4.17.21",
"prettier": "2.2.1"
},
"devDependencies": {
"@types/chai": "^4.2.15",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.32",
"chai": "^4.3.3",
"jest": "^26.6.3",
"ts-jest": "^26.5.3",
"typescript": "^4.2.3"
"@types/chai": "4.2.15",
"@types/jest": "26.0.20",
"@types/node": "14.14.34",
"jest": "26.6.3",
"ts-jest": "26.5.3",
"typescript": "4.2.3"
}
}

@@ -5,4 +5,9 @@ # Prettier plugin sort imports

![import order gif](./import-sort.gif)
### Input
![input](./public/images/input.png)
### Output
![output](./public/images/output.png)
### Install

@@ -54,3 +59,17 @@

```ecmascript 6
module.exports = {
"printWidth": 80,
"tabWidth": 4,
"trailingComma": "all",
"singleQuote": true,
"jsxBracketSameLine": true,
"semi": true,
"importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
"importOrderSeparation": true,
"experimentalBabelParserPluginsList" : ["jsx", "typescript"]
}
```
### How does import sort work ?

@@ -107,3 +126,3 @@

#### Q. How does the plugin handle the first comment in the file.
The plugin keeps the first comment as it is in the file. The plugin also removes the new lines in between first comment and the first import.
The plugin keeps the first comment as it is in the file. The plugin also removes the new lines in between first comment and the first import.
**input:**

@@ -121,2 +140,9 @@ ```js

#### Q. I'm getting error about experimental syntax.
If you are using some experimental syntax and the plugin has trouble parsing your files, you might getting errors similar to this:
```shell script
SyntaxError: This experimental syntax requires enabling one of the following parser plugin(s): ...
```
To solve this issue, you can use the new option `experimentalBabelParserPluginsList` in your `.prettierrc` and pass an array of plugin names to be used.
### Maintainers

@@ -123,0 +149,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc