Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 10, 2020
1 parent 01c1d6e commit d6dc805
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
6 changes: 2 additions & 4 deletions rules/escape-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const escapePatternWithLowercase = /(?<=(?:^|[^\\])(?:\\\\)*\\)(?<data>x[\dA-Fa-
const message = 'Use uppercase characters for the value of the escape sequence.';

const create = context => {
const check = ({node, original, regex, fix}) => {
const check = ({node, original, regex = escapeWithLowercase, fix}) => {
const fixed = original.replace(regex, data => data.slice(0, 1) + data.slice(1).toUpperCase());

if (fixed !== original) {
Expand All @@ -27,8 +27,7 @@ const create = context => {

check({
node,
original: node.raw,
regex: escapeWithLowercase
original: node.raw
});
},
'Literal[regex]'(node) {
Expand All @@ -42,7 +41,6 @@ const create = context => {
check({
node,
original: node.value.raw,
regex: escapePatternWithLowercase,
fix: (fixer, fixed) => replaceTemplateElement(fixer, node, fixed)
});
}
Expand Down
50 changes: 50 additions & 0 deletions test/escape-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ruleTester.run('escape-case', rule, {
'const foo = "foo\\\\ubarbaz";',
'const foo = "foo\\\\\\\\xbar";',
'const foo = "foo\\\\\\\\ubarbaz";',
'const foo = "\\ca";',

// TemplateLiteral
'const foo = `\\xA9`;',
Expand All @@ -46,6 +47,7 @@ ruleTester.run('escape-case', rule, {
'const foo = `foo\\\\ubarbaz`;',
'const foo = `foo\\\\\\\\xbar`;',
'const foo = `foo\\\\\\\\ubarbaz`;',
'const foo = `\\ca`;',

// Literal regex
'const foo = /foo\\xA9/',
Expand Down Expand Up @@ -195,6 +197,30 @@ ruleTester.run('escape-case', rule, {
output: 'const foo = `foo \\\\\\uD834`;'
},

// Mixed cases
{
code: 'const foo = `\\xAa`;',
errors,
output: 'const foo = `\\xAA`;'
},
{
code: 'const foo = `\\uAaAa`;',
errors,
output: 'const foo = `\\uAAAA`;'
},
{
code: 'const foo = `\\u{AaAa}`;',
errors,
output: 'const foo = `\\u{AAAA}`;'
},

// Many
{
code: 'const foo = `\\xAaa\\xaaa\\xAA${foo}\\uAaAaa\\uaaaaa\\uAAAAa\\u{AaAa}${foo}\\u{aaaa}a\\u{AAAA}`;',
errors: Array.from({length: 3}, () => errors[0]),
output: 'const foo = `\\xAAa\\xAAa\\xAA${foo}\\uAAAAa\\uAAAAa\\uAAAAa\\u{AAAA}${foo}\\u{AAAA}a\\u{AAAA}`;'
},

// Literal regex
{
code: 'const foo = /\\xa9/;',
Expand Down Expand Up @@ -227,6 +253,30 @@ ruleTester.run('escape-case', rule, {
output: 'const foo = /foo\\\\\\\\\\xA9/;'
},

// Mixed cases
{
code: 'const foo = /\\xAa/;',
errors,
output: 'const foo = /\\xAA/;'
},
{
code: 'const foo = /\\uAaAa/;',
errors,
output: 'const foo = /\\uAAAA/;'
},
{
code: 'const foo = /\\u{AaAa}/;',
errors,
output: 'const foo = /\\u{AAAA}/;'
},

// Many
{
code: 'const foo = /\\xAaa\\xaaa\\xAAa\\uAaAaa\\uaaaaa\\uAAAAa\\u{AaAa}a\\u{aaaa}a\\u{AAAA}a\\ca/;',
errors,
output: 'const foo = /\\xAAa\\xAAa\\xAAa\\uAAAAa\\uAAAAa\\uAAAAa\\u{AAAA}a\\u{AAAA}a\\u{AAAA}a\\cA/;'
},

// RegExp
{
code: 'const foo = new RegExp("/\\xa9")',
Expand Down

0 comments on commit d6dc805

Please sign in to comment.