From 8c332c74a0f8fbcfacfaa5a4f4f71fb91f26a025 Mon Sep 17 00:00:00 2001 From: Xingxin Zeng Date: Tue, 8 May 2018 17:10:38 +0800 Subject: [PATCH] Rename the files and update readme --- CHANGELOG.md | 135 ++---------------- README.md | 3 +- lib/index.js | 14 +- ...rt-json-core.js => sort-js-object-core.js} | 0 lib/{sort-json.js => sort-js-object.js} | 2 +- lib/sort-json-utils.js | 18 --- package.json | 6 +- test/extension.test.js | 30 +--- 8 files changed, 25 insertions(+), 183 deletions(-) rename lib/{sort-json-core.js => sort-js-object-core.js} (100%) rename lib/{sort-json.js => sort-js-object.js} (97%) delete mode 100644 lib/sort-json-utils.js diff --git a/CHANGELOG.md b/CHANGELOG.md index e371461..6ea65af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,136 +1,21 @@ # Change Log All notable changes to the "sort*js*object*keys" extension will be documented in this file. -### [0.0.1] +### [1.0.0] -* Initial release of a testing version - -* Add `Sort JS object keys` command - -* Add `Sort JS object keys (Reverse)` command - -### [0.0.3] - -* Support ES6 shorthand object - - e.g: - ```js - { - user, - password - } - ``` - Will be sorted to - ```js - { - password: password, - user: user - } - ``` - -* Support value which is multiple lines or have space in it. +* Use babylon + babel/generator to parse and genertate the code - e.g: - ```js - { - b: new String('b') - .length, - a: new String('a') - } - ``` - Will be sorted to - ```js - { - a: new String('a'), - b: new String('b').length - } - ``` -* Will automatically add trailing comma if the prevs object has trailing comma +* Support all the existing feature except: + * Auto add tailing comma (Too much bugs for this feature) + * End line comments (Babel can't parse it correctly) - e.g: - ```js - { - b: 'b', - a: 'a' - } - ``` - Will be sorted to - ```js - { - a: 'a', - b: 'b' - } - ``` - But this object which already has trailing comma: - ```js - { - b: 'b', - a: 'a', - } - ``` - Will be sorted to - ```js - { - a: 'a', - b: 'b', - } - ``` -### [0.0.6] +### [0.0.x] -* Support Array in object - -* Auto indent object if it is not in the first collumn - -* Auto use ES6 short hand value - -### [0.0.7] - -* Support line comments in object - - e.g: - ```js - { - b: 2, - // some comment - a: 1, - // another comment - d: 5, - c: 4, - } - ``` - Will be sorted to - ```js - { - // some comment - a: 1, - b: 2, - c: 4, - // another comment - d: 5, - } - ``` - -### [0.0.8] +* Initial release of a testing version -* Fix an indent not correct bug +* Add `Sort JS object keys` command -### [0.0.9] +* Add `Sort JS object keys (Reverse)` command -* Support \' in string +* Lots of feature in test version... - e.g: - ```js - { - b: 'test \'', - c: 'test \' test', - a: '\' test', - } - ``` - Will be sorted to - ```js - { - a: '\' test', - b: 'test \'', - c: 'test \' test', - } - ``` diff --git a/README.md b/README.md index cebabe8..d7d5177 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,7 @@ This is a VS code extension to alphabetically sort the keys in _selected_ js obj ## Reference -Referred the source code from [Rich Somerfield](https://github.com/richie5um)'s extension -[vscode-sort-json](https://github.com/richie5um/vscode-sort-json), his extension can only sort JSON, I added new feature base on his extension so this extension can sort the JS object keys in source code. +Use [babylon](https://github.com/babel/babel/tree/master/packages/babylon) to parse the code, and sort the parsed code, then use [@babel/generator](https://github.com/babel/babel/tree/master/packages/babel-generator) to genertate the code back to document ## Usage diff --git a/lib/index.js b/lib/index.js index 02ba776..4ef9a49 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,13 +1,19 @@ var vscode = require('vscode'); -var sortJSON = require('./sort-json'); +var sortJSON = require('./sort-js-object'); function activate(context) { var commands = [ - vscode.commands.registerCommand('sortJsObjectKeys.sortJsObjectKeys', sortJSON.sortNormal), - vscode.commands.registerCommand('sortJsObjectKeys.sortJsObjectKeysReverse', sortJSON.sortReverse) + vscode.commands.registerCommand( + 'sortJsObjectKeys.sortJsObjectKeys', + sortJSON.sortNormal + ), + vscode.commands.registerCommand( + 'sortJsObjectKeys.sortJsObjectKeysReverse', + sortJSON.sortReverse + ), ]; - commands.forEach(function (command) { + commands.forEach(function(command) { context.subscriptions.push(command); }); } diff --git a/lib/sort-json-core.js b/lib/sort-js-object-core.js similarity index 100% rename from lib/sort-json-core.js rename to lib/sort-js-object-core.js diff --git a/lib/sort-json.js b/lib/sort-js-object.js similarity index 97% rename from lib/sort-json.js rename to lib/sort-js-object.js index 7c95ae9..96ee1e3 100644 --- a/lib/sort-json.js +++ b/lib/sort-js-object.js @@ -1,5 +1,5 @@ var vscode = require('vscode'); -var sorterCore = require('./sort-json-core'); +var sorterCore = require('./sort-js-object-core'); function getSelection(textEditor, startLine, startPos, endLine, endPos) { var selectedLines = []; diff --git a/lib/sort-json-utils.js b/lib/sort-json-utils.js deleted file mode 100644 index fd06805..0000000 --- a/lib/sort-json-utils.js +++ /dev/null @@ -1,18 +0,0 @@ -var _ = require('lodash'); -var _sortKeys = require('./lodash-sortkeys'); - -function textToJSON(jsonParser, text) { - return jsonParser.parse(text); -} - -function jsonToText(jsonParser, json, indent) { - return jsonParser.stringify(json, undefined, indent); -} - -function sortJSON(json, order, options) { - return _.sortKeysDeepBy(json, order, options); -} - -exports.textToJSON = textToJSON; -exports.jsonToText = jsonToText; -exports.sortJSON = sortJSON; diff --git a/package.json b/package.json index 52ea355..6af436c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "sort-js-object-keys", "displayName": "Sort JS object keys", "description": "An extension to sort the js object keys", - "version": "0.0.12", + "version": "1.0.0", "publisher": "zengxingxin", "engines": { "vscode": "^1.12.0" @@ -15,9 +15,7 @@ "url": "https://github.com/SBeator/sort-js-object-keys/issues", "email": "star_yes@qq.com" }, - "categories": [ - "Other" - ], + "categories": ["Other"], "activationEvents": [ "onCommand:sortJsObjectKeys.sortJsObjectKeys", "onCommand:sortJsObjectKeys.sortJsObjectKeysReverse" diff --git a/test/extension.test.js b/test/extension.test.js index b30c61b..1bd8713 100644 --- a/test/extension.test.js +++ b/test/extension.test.js @@ -10,7 +10,7 @@ var assert = require('assert'); // You can import and use all API from the 'vscode' module // as well as import your extension to test it -var sorter = require('../lib/sort-json-core'); +var sorter = require('../lib/sort-js-object-core'); suite('Extension Tests', function() { test('normal js object asc', function() { @@ -244,34 +244,6 @@ suite('Extension Tests', function() { ); }); - // NOT suppot end line comments because babel not suppot it correctly - // test('Support line comments at the end of the object', function() { - // var jsObject = `{ - // b: 2, - // // some comment - // a: 1, - // // another comment - // d: 5, - // c: 4, - // // end comment - // }`; - - // var result = sorter.sort(jsObject, 4, ['asc'], {}); - - // assert.equal( - // result, - // `{ - // // some comment - // a: 1, - // b: 2, - // c: 4, - // // another comment - // d: 5, - // // end comment - // }` - // ); - // }); - test("Support ' in string", function() { var jsObject = `{ b: 'test \\'',