Skip to content

Commit 9d4264b

Browse files
evilebottnawimarionebl
authored andcommitted
fix: ignore case of words in quotes
1 parent 005592a commit 9d4264b

File tree

2 files changed

+184
-3
lines changed

2 files changed

+184
-3
lines changed

@commitlint/ensure/src/case.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import startCase from 'lodash.startcase';
77
export default ensureCase;
88

99
function ensureCase(raw = '', target = 'lowercase') {
10-
const input = String(raw);
10+
// We delete any content together with quotes because he can contains proper names (example `refactor: `Eslint` configuration`).
11+
// We need trim string because content with quotes can be at the beginning or end of a line
12+
const input = String(raw)
13+
.replace(/`.*?`|".*?"|'.*?'/g, '')
14+
.trim();
1115
const transformed = toCase(input, target);
1216

1317
if (transformed === '') {

@commitlint/ensure/src/case.test.js

+179-2
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,188 @@ test('true for * on snake-case', t => {
106106
});
107107

108108
test('true for * on pascal-case', t => {
109-
const actual = ensure('*', 'snake-case');
109+
const actual = ensure('*', 'pascal-case');
110110
t.is(actual, true);
111111
});
112112

113113
test('true for * on start-case', t => {
114-
const actual = ensure('*', 'snake-case');
114+
const actual = ensure('*', 'start-case');
115+
t.is(actual, true);
116+
});
117+
118+
test('true for `Any_CASE_iN_back-quotes` on lowercase', t => {
119+
const actual = ensure('`Any_CASE_iN_back-quotes`', 'lowercase');
120+
t.is(actual, true);
121+
});
122+
123+
test('true for `Any_CASE_iN_back-quotes` on uppercase', t => {
124+
const actual = ensure('`Any_CASE_iN_back-quotes`', 'uppercase');
125+
t.is(actual, true);
126+
});
127+
128+
test('true for `Any_CASE_iN_back-quotes` on sentence-case', t => {
129+
const actual = ensure('`Any_CASE_iN_back-quotes`', 'sentence-case');
130+
t.is(actual, true);
131+
});
132+
133+
test('true for `Any_CASE_iN_back-quotes` on camel-case', t => {
134+
const actual = ensure('`Any_CASE_iN_back-quotes`', 'camel-case');
135+
t.is(actual, true);
136+
});
137+
138+
test('true for `Any_CASE_iN_back-quotes` on kebab-case', t => {
139+
const actual = ensure('`Any_CASE_iN_back-quotes`', 'kebab-case');
140+
t.is(actual, true);
141+
});
142+
143+
test('true for `Any_CASE_iN_back-quotes` on snake-case', t => {
144+
const actual = ensure('`Any_CASE_iN_back-quotes`', 'snake-case');
145+
t.is(actual, true);
146+
});
147+
148+
test('true for `Any_CASE_iN_back-quotes` on pascal-case', t => {
149+
const actual = ensure('`Any_CASE_iN_back-quotes`', 'pascal-case');
150+
t.is(actual, true);
151+
});
152+
153+
test('true for `Any_CASE_iN_back-quotes` on start-case', t => {
154+
const actual = ensure('`Any_CASE_iN_back-quotes`', 'start-case');
115155
t.is(actual, true);
116156
});
157+
158+
test('true for lowercase `Any_CASE_iN_back-quotes` lowercase on lowercase', t => {
159+
const actual = ensure(
160+
'lowercase `Any_CASE_iN_back-quotes` lowercase',
161+
'lowercase'
162+
);
163+
t.is(actual, true);
164+
});
165+
166+
test('false for UPPERCASE `Any_CASE_iN_back-quotes` UPPERCASE on lowercase', t => {
167+
const actual = ensure(
168+
'UPPERCASE `Any_CASE_iN_back-quotes` UPPERCASE',
169+
'lowercase'
170+
);
171+
t.is(actual, false);
172+
});
173+
174+
test('true for UPPERCASE `Any_CASE_iN_back-quotes` UPPERCASE on uppercase', t => {
175+
const actual = ensure(
176+
'UPPERCASE `Any_CASE_iN_back-quotes` UPPERCASE',
177+
'uppercase'
178+
);
179+
t.is(actual, true);
180+
});
181+
182+
test('false for lowercase `Any_CASE_iN_back-quotes` lowercase on uppercase', t => {
183+
const actual = ensure(
184+
'lowercase `Any_CASE_iN_back-quotes` lowercase',
185+
'uppercase'
186+
);
187+
t.is(actual, false);
188+
});
189+
190+
test('true for fooBar`Any_CASE_iN_back-quotes`fooBar on camel-case', t => {
191+
const actual = ensure('fooBar`Any_CASE_iN_back-quotes`fooBar', 'camel-case');
192+
t.is(actual, true);
193+
});
194+
195+
test('false for Foo Bar`Any_CASE_iN_back-quotes` Foo Bar on camel-case', t => {
196+
const actual = ensure(
197+
'Foo Bar`Any_CASE_iN_back-quotes` Foo Bar',
198+
'camel-case'
199+
);
200+
t.is(actual, false);
201+
});
202+
203+
test('true for foo-bar`Any_CASE_iN_back-quotes`foo-bar on kebab-case', t => {
204+
const actual = ensure(
205+
'foo-bar`Any_CASE_iN_back-quotes`foo-bar',
206+
'kebab-case'
207+
);
208+
t.is(actual, true);
209+
});
210+
211+
test('false for Foo Bar `Any_CASE_iN_back-quotes` Foo Bar on kebab-case', t => {
212+
const actual = ensure(
213+
'Foo Bar `Any_CASE_iN_back-quotes` Foo Bar',
214+
'kebab-case'
215+
);
216+
t.is(actual, false);
217+
});
218+
219+
test('true for foo_bar`Any_CASE_iN_back-quotes`foo_bar on snake-case', t => {
220+
const actual = ensure(
221+
'foo_bar`Any_CASE_iN_back-quotes`foo_bar',
222+
'snake-case'
223+
);
224+
t.is(actual, true);
225+
});
226+
227+
test('false for Foo Bar `Any_CASE_iN_back-quotes` Foo Bar on snake-case', t => {
228+
const actual = ensure(
229+
'Foo Bar `Any_CASE_iN_back-quotes` Foo Bar',
230+
'snake-case'
231+
);
232+
t.is(actual, false);
233+
});
234+
235+
test('true for PascalCase`Any_CASE_iN_back-quotes`PascalCase on pascal-case', t => {
236+
const actual = ensure(
237+
'PascalCase`Any_CASE_iN_back-quotes`PascalCase',
238+
'pascal-case'
239+
);
240+
t.is(actual, true);
241+
});
242+
243+
test('false for Foo Bar `Any_CASE_iN_back-quotes` Foo Bar on pascal-case', t => {
244+
const actual = ensure(
245+
'Foo Bar `Any_CASE_iN_back-quotes` Foo Bar',
246+
'pascal-case'
247+
);
248+
t.is(actual, false);
249+
});
250+
251+
test('true for Foo Bar`Any_CASE_iN_back-quotes` Foo Bar on start-case', t => {
252+
const actual = ensure(
253+
'Foo Bar `Any_CASE_iN_back-quotes`Foo Bar',
254+
'start-case'
255+
);
256+
t.is(actual, true);
257+
});
258+
259+
test('false for foo_bar`Any_CASE_iN_back-quotes`foo_bar on start-case', t => {
260+
const actual = ensure(
261+
'foo_bar`Any_CASE_iN_back-quotes`foo_bar',
262+
'start-case'
263+
);
264+
t.is(actual, false);
265+
});
266+
267+
test('true for lowercase `Any_CASE_iN_back-quotes` `Any_CASE_iN_back-quotes` lowercase on lowercase', t => {
268+
const actual = ensure(
269+
'lowercase `Any_CASE_iN_back-quotes` `Any_CASE_iN_back-quotes` lowercase',
270+
'lowercase'
271+
);
272+
t.is(actual, true);
273+
});
274+
275+
test("true for 'Any_CASE_iN_single-quotes' on lowercase", t => {
276+
const actual = ensure("'Any_CASE_iN_single-quotes'", 'lowercase');
277+
t.is(actual, true);
278+
});
279+
280+
test('true for "Any_CASE_iN_double-quotes" on lowercase', t => {
281+
const actual = ensure('"Any_CASE_iN_double-quotes"', 'lowercase');
282+
t.is(actual, true);
283+
});
284+
285+
test('true for `lowercasel"\'` on lowercase', t => {
286+
const actual = ensure('`lowercasel"\'`', 'lowercase');
287+
t.is(actual, true);
288+
});
289+
290+
test('false for `LOWERCASE on lowercase', t => {
291+
const actual = ensure('`LOWERCASE', 'lowercase');
292+
t.is(actual, false);
293+
});

0 commit comments

Comments
 (0)