From 6ac006648932b7b7cab252e40eedaf7ebccfc17d Mon Sep 17 00:00:00 2001 From: Armano Date: Sat, 10 Nov 2018 18:56:17 +0100 Subject: [PATCH] Fix logic behind kabab case and snake case for propID fix issue #550 --- lib/utils/casing.js | 8 +++- tests/lib/rules/attribute-hyphenation.js | 11 +++++ tests/lib/utils/casing.js | 55 +++++++++++++++--------- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/lib/utils/casing.js b/lib/utils/casing.js index f0c02b01a..741481ef8 100644 --- a/lib/utils/casing.js +++ b/lib/utils/casing.js @@ -9,7 +9,9 @@ const invalidChars = /[^a-zA-Z0-9:]+/g */ function kebabCase (str) { return str - .replace(/([a-z])([A-Z])/g, match => match[0] + '-' + match[1]) + .replace(/[A-Z]/g, match => '-' + match) + .replace(/([^a-zA-Z])-([A-Z])/g, match => match[0] + match[2]) + .replace(/^-/, '') .replace(invalidChars, '-') .toLowerCase() } @@ -21,7 +23,9 @@ function kebabCase (str) { */ function snakeCase (str) { return str - .replace(/([a-z])([A-Z])/g, match => match[0] + '_' + match[1]) + .replace(/[A-Z]/g, match => '_' + match) + .replace(/([^a-zA-Z])_([A-Z])/g, match => match[0] + match[2]) + .replace(/^_/, '') .replace(invalidChars, '_') .toLowerCase() } diff --git a/tests/lib/rules/attribute-hyphenation.js b/tests/lib/rules/attribute-hyphenation.js index c1f9b57bc..433a33823 100644 --- a/tests/lib/rules/attribute-hyphenation.js +++ b/tests/lib/rules/attribute-hyphenation.js @@ -150,6 +150,17 @@ ruleTester.run('attribute-hyphenation', rule, { line: 1 }] }, + { + filename: 'test.vue', + code: '', + output: '', + options: ['always', { ignore: ['secondPropID'] }], + errors: [{ + message: "Attribute 'v-bind:propID' must be hyphenated.", + type: 'VDirectiveKey', + line: 1 + }] + }, { filename: 'test.vue', code: ` diff --git a/tests/lib/utils/casing.js b/tests/lib/utils/casing.js index e9cbe921a..d72d186d5 100644 --- a/tests/lib/utils/casing.js +++ b/tests/lib/utils/casing.js @@ -6,39 +6,54 @@ const chai = require('chai') const assert = chai.assert describe('getConverter()', () => { - it('should conver string to camelCase', () => { + it('should convert string to camelCase', () => { const converter = casing.getConverter('camelCase') - assert.ok(converter('fooBar') === 'fooBar') - assert.ok(converter('foo-bar') === 'fooBar') - assert.ok(converter('FooBar') === 'fooBar') - assert.ok(converter('Foo1Bar') === 'foo1Bar') + assert.equal(converter('fooBar'), 'fooBar') + assert.equal(converter('foo-bar'), 'fooBar') + assert.equal(converter('foo_bar'), 'fooBar') + assert.equal(converter('FooBar'), 'fooBar') + assert.equal(converter('Foo1Bar'), 'foo1Bar') + assert.equal(converter('FooBAR'), 'fooBAR') + assert.equal(converter('Foo1BAZ'), 'foo1BAZ') + assert.equal(converter('foo1b_a_z'), 'foo1bAZ') }) - it('should conver string to PascalCase', () => { + it('should convert string to PascalCase', () => { const converter = casing.getConverter('PascalCase') - assert.ok(converter('fooBar') === 'FooBar') - assert.ok(converter('foo-bar') === 'FooBar') - assert.ok(converter('FooBar') === 'FooBar') - assert.ok(converter('Foo1Bar') === 'Foo1Bar') + assert.equal(converter('fooBar'), 'FooBar') + assert.equal(converter('foo-bar'), 'FooBar') + assert.equal(converter('foo_bar'), 'FooBar') + assert.equal(converter('FooBar'), 'FooBar') + assert.equal(converter('Foo1Bar'), 'Foo1Bar') + assert.equal(converter('FooBAR'), 'FooBAR') + assert.equal(converter('Foo1BAZ'), 'Foo1BAZ') + assert.equal(converter('foo1b_a_z'), 'Foo1bAZ') }) - it('should conver string to kebab-case', () => { + it('should convert string to kebab-case', () => { const converter = casing.getConverter('kebab-case') - assert.ok(converter('fooBar') === 'foo-bar') - assert.ok(converter('foo-bar') === 'foo-bar') - assert.ok(converter('FooBar') === 'foo-bar') - assert.ok(converter('Foo1Bar') === 'foo1bar') + assert.equal(converter('fooBar'), 'foo-bar') + assert.equal(converter('foo-bar'), 'foo-bar') + assert.equal(converter('foo_bar'), 'foo-bar') + assert.equal(converter('FooBar'), 'foo-bar') + assert.equal(converter('Foo1Bar'), 'foo1bar') + assert.equal(converter('FooBAR'), 'foo-b-a-r') + assert.equal(converter('Foo1BAZ'), 'foo1b-a-z') + assert.equal(converter('foo1b_a_z'), 'foo1b-a-z') }) - it('should conver string to snake_case', () => { + it('should convert string to snake_case', () => { const converter = casing.getConverter('snake_case') - assert.ok(converter('fooBar') === 'foo_bar') - assert.ok(converter('foo-bar') === 'foo_bar') - assert.ok(converter('FooBar') === 'foo_bar') - assert.ok(converter('Foo1Bar') === 'foo1bar') + assert.equal(converter('fooBar'), 'foo_bar') + assert.equal(converter('foo-bar'), 'foo_bar') + assert.equal(converter('FooBar'), 'foo_bar') + assert.equal(converter('Foo1Bar'), 'foo1bar') + assert.equal(converter('FooBAR'), 'foo_b_a_r') + assert.equal(converter('Foo1BAZ'), 'foo1b_a_z') + assert.equal(converter('foo1b_a_z'), 'foo1b_a_z') }) })