From 5e701a24d215191cdb319150b583f64ef4fa7fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zdravko=20=C4=86uri=C4=87?= Date: Mon, 10 Feb 2020 11:58:27 +0100 Subject: [PATCH] Return null for name of destructed and aliased --- index.js | 3 +- test/require-sort.test.js | 261 +------------------------------------- 2 files changed, 7 insertions(+), 257 deletions(-) diff --git a/index.js b/index.js index bee62ab..0c04206 100644 --- a/index.js +++ b/index.js @@ -151,7 +151,8 @@ module.exports = { const name = isAssignmentPattern(value) ? value.left.name : value.name; - return ignoreCase ? name.toLowerCase() : name; + if (name) return ignoreCase ? name.toLowerCase() : name; + return null; }; const sortByName = (propertyA, propertyB) => { diff --git a/test/require-sort.test.js b/test/require-sort.test.js index 2465105..0e9ede8 100644 --- a/test/require-sort.test.js +++ b/test/require-sort.test.js @@ -42,7 +42,6 @@ ruleTester.run('require-sort', rule, { code: ` const a = require('bar'); const b = require('foo'); - if (bar) { const c = require('baz'); } @@ -152,6 +151,11 @@ ruleTester.run('require-sort', rule, { const { a, b, c } = require('bar'); ` }), + test({ + code: ` + const { a, b: { d: e }, c } = require('bar'); + ` + }), test({ code: ` const { b, c, a } = require('bar'); @@ -286,258 +290,3 @@ ruleTester.run('require-sort', rule, { }) ]) }); - -// ruleTester.run('require-sort', rule, { -// valid: [ -// { -// code: -// "const A = require('bar.js');\n" + -// "const {b, c} = require('foo.js');", -// options: [{ -// propertySyntaxSortOrder: ['none', 'single', 'multiple'] -// }] -// }, -// { -// code: -// "const a = require('foo.js');\n" + -// "const B = require('bar.js');", -// options: ignoreCaseArgs -// }, -// { -// code: -// "const a = require('foo.js');\n" + -// "const B = require('bar.js');", -// options: [{ -// ignoreDeclarationSort: true -// }] -// }, -// { -// code: "const {b, A, C, d} = require('foo.js');", -// options: [{ -// ignorePropertySort: true -// }] -// }, -// { -// code: "const {B, a, C, d} = require('foo.js');", -// options: [{ -// ignorePropertySort: true -// }] -// }, -// { -// code: "const {a, B, c, D} = require('foo.js');", -// options: ignoreCaseArgs -// }, -// { -// code: -// "require('bar.js');\n" + -// "const {b, c} = require('foo.js');", -// options: [{ -// propertySyntaxSortOrder: ['none', 'multiple', 'single'] -// }] -// }, -// { -// code: ` -// const { b: a } = require('foo.js'); -// const { a: b } = require('foo.js'); -// ` -// }, -// { -// code: ` -// const { a: b = {} } = require('foo'); -// const c = require('bar'); -// const d = require('baz'); -// const { e } = require('foobar'); -// `, -// options: ignoreCaseArgs -// } -// ], -// invalid: [ -// { -// code: -// "const a = require('foo.js');\n" + -// "const A = require('bar.js');", -// output: null, -// errors: [expectedError] -// }, -// { -// code: ` -// const { a: z = {} } = require('foo'); -// const c = require('bar'); -// const d = require('baz'); -// const { e } = require('foobar'); -// `, -// output: null, -// errors: [expectedError] -// }, -// { -// code: -// "const a = require('foo');\n" + -// "require('muu');", -// output: null, -// errors: [{ -// message: "Expected 'none' syntax before 'single' syntax.", -// type: 'CallExpression' -// }] -// }, -// { -// code: -// "const b = require('foo.js');\n" + -// "const a = require('bar.js');", -// output: null, -// errors: [expectedError] -// }, -// { -// code: -// "const {b, c} = require('foo.js');\n" + -// "const {a, d} = require('bar.js');", -// output: null, -// errors: [expectedError] -// }, -// { -// code: -// "const a = require('foo.js');\n" + -// "const {b, c} = require('bar.js');", -// output: null, -// errors: [{ -// message: "Expected 'multiple' syntax before 'single' syntax.", -// type: 'VariableDeclaration' -// }] -// }, -// { -// code: "const {b, a, d, c} = require('foo.js');", -// output: "const {a, b, c, d} = require('foo.js');", -// errors: [{ -// message: "Property 'a' of the require declaration should be sorted alphabetically.", -// type: 'Property' -// }] -// }, -// { -// code: "const {b, d: a, c} = require('foo.js');", -// output: "const {d: a, b, c} = require('foo.js');", -// errors: [{ -// message: "Property 'a' of the require declaration should be sorted alphabetically.", -// type: 'Property' -// }] -// }, -// { -// code: "const {b, d: a = {}, c} = require('foo.js');", -// output: "const {d: a = {}, b, c} = require('foo.js');", -// errors: [{ -// message: "Property 'a' of the require declaration should be sorted alphabetically.", -// type: 'Property' -// }] -// }, -// { -// code: ` -// const { a: b } = require('foo.js'); -// const { b: a } = require('foo.js'); -// `, -// output: ` -// const { a: b } = require('foo.js'); -// const { b: a } = require('foo.js'); -// `, -// errors: [expectedError] -// }, -// { -// code: -// "const {b, a, d, c} = require('foo.js');\n" + -// "const {e, f, g, h} = require('bar.js');", -// output: -// "const {a, b, c, d} = require('foo.js');\n" + -// "const {e, f, g, h} = require('bar.js');", -// options: [{ ignoreDeclarationSort: true }], -// errors: [{ -// message: "Property 'a' of the require declaration should be sorted alphabetically.", -// type: 'Property' -// }] -// }, -// { -// code: "const {a, B, c, D} = require('foo.js');", -// output: "const {B, D, a, c} = require('foo.js');", -// errors: [{ -// message: "Property 'B' of the require declaration should be sorted alphabetically.", -// type: 'Property' -// }] -// }, -// { -// code: "const {zzzzz, /* comment */ aaaaa} = require('foo.js');", -// output: null, // not fixed due to comment -// errors: [{ -// message: "Property 'aaaaa' of the require declaration should be sorted alphabetically.", -// type: 'Property' -// }] -// }, -// { -// code: "const {zzzzz /* comment */, aaaaa} = require('foo.js');", -// output: null, // not fixed due to comment -// errors: [{ -// message: "Property 'aaaaa' of the require declaration should be sorted alphabetically.", -// type: 'Property' -// }] -// }, -// { -// code: "const {/* comment */ zzzzz, aaaaa} = require('foo.js');", -// output: null, // not fixed due to comment -// errors: [{ -// message: "Property 'aaaaa' of the require declaration should be sorted alphabetically.", -// type: 'Property' -// }] -// }, -// { -// code: "const {zzzzz, aaaaa /* comment */} = require('foo.js');", -// output: null, // not fixed due to comment -// errors: [{ -// message: "Property 'aaaaa' of the require declaration should be sorted alphabetically.", -// type: 'Property' -// }] -// }, -// { -// code: ` -// const bar = require('bar'); -// const foo = require('foo'); -// function fn() { let baz = require('baz'); }`, -// output: ` -// const bar = require('bar'); -// const foo = require('foo'); -// function fn() { let baz = require('baz'); }`, -// errors: [] -// }, -// { -// code: ` -// const bar = require('bar'); -// const foo = require('foo'); -// if (foo) { let baz = require('baz'); }`, -// output: ` -// const bar = require('bar'); -// const foo = require('foo'); -// if (foo) { let baz = require('baz'); }`, -// errors: [] -// }, -// { -// code: ` -// const { -// boop, -// foo, -// zoo, -// qux, -// bar, -// beep -// } = require('foo.js'); -// `, -// output: ` -// const { -// bar, -// beep, -// boop, -// foo, -// qux, -// zoo -// } = require('foo.js'); -// `, -// errors: [{ -// message: "Property 'qux' of the require declaration should be sorted alphabetically.", -// type: 'Property' -// }] -// } -// ] -// });