Skip to content

Commit 3bff4fd

Browse files
authored
Ensure all the rules are compatible with TypeScript-ESLint (#519)
1 parent 0f8779e commit 3bff4fd

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

rules/escape-case.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Record escaped node position in regexpp ASTNode. Returns undefined if not found.
5252
const matches = node.raw.match(escapePatternWithLowercase);
5353

5454
if (matches && matches.groups.data.slice(1).match(hasLowercaseCharacter)) {
55+
// There is no `range` property in AST from `regexpp`
56+
// reference: https://github.com/mysticatea/regexpp/blob/master/src/ast.ts#L60-L71
5557
escapeNodePosition = [node.start, node.end];
5658
}
5759
}

rules/no-hex-escape.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ function checkEscape(context, node, value) {
1010
context.report({
1111
node,
1212
message: 'Use Unicode escapes instead of hexadecimal escapes.',
13-
fix: fixer => fixer.replaceTextRange([node.start, node.end], fixedValue)
13+
fix: fixer => {
14+
let {range: [start, end], type, tail} = node;
15+
16+
if (type === 'TemplateElement') {
17+
start += 1;
18+
end -= tail ? 1 : 2;
19+
}
20+
21+
return fixer.replaceTextRange([start, end], fixedValue);
22+
}
1423
});
1524
}
1625
}

rules/no-nested-ternary.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ const isParethesized = (sourceCode, node) => {
88
return (
99
Boolean(previousToken && nextToken) &&
1010
previousToken.value === '(' &&
11-
previousToken.end <= node.start &&
11+
previousToken.end <= node.range[0] &&
1212
nextToken.value === ')' &&
13-
nextToken.start >= node.end
13+
nextToken.start >= node.range[1]
1414
);
1515
};
1616

rules/no-new-buffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const create = context => {
88
'NewExpression[callee.name="Buffer"]': node => {
99
const method = inferMethod(node.arguments);
1010
const range = [
11-
node.start,
11+
node.range[0],
1212
node.callee.end
1313
];
1414

test/no-hex-escape.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ const ruleTester = avaRuleTester(test, {
88
}
99
});
1010

11+
const babelRuleTester = avaRuleTester(test, {
12+
parser: require.resolve('babel-eslint')
13+
});
14+
const typescriptRuleTester = avaRuleTester(test, {
15+
parser: require.resolve('@typescript-eslint/parser')
16+
});
17+
1118
const error = {
1219
ruleId: 'no-hex-escape',
1320
message: 'Use Unicode escapes instead of hexadecimal escapes.'
1421
};
1522

16-
ruleTester.run('no-hex-escape', rule, {
23+
const tests = {
1724
valid: [
1825
'const foo = \'foo\'',
1926
'const foo = \'\\u00b1\'',
@@ -117,6 +124,11 @@ ruleTester.run('no-hex-escape', rule, {
117124
errors: [error],
118125
output: 'const foo = \'42\\\\\\u001242\\\\\\u0034\''
119126
},
127+
{
128+
code: 'const foo = /^[\\x20-\\x7E]*$/',
129+
errors: [error],
130+
output: 'const foo = /^[\\u0020-\\u007E]*$/'
131+
},
120132
// Test template literals
121133
{
122134
code: 'const foo = `\\xb1`',
@@ -187,6 +199,17 @@ ruleTester.run('no-hex-escape', rule, {
187199
code: 'const foo = `42\\\\\\x1242\\\\\\x34`',
188200
errors: [error],
189201
output: 'const foo = `42\\\\\\u001242\\\\\\u0034`'
202+
},
203+
{
204+
// eslint-disable-next-line no-template-curly-in-string
205+
code: 'const foo = `\\xb1${foo}\\xb1${foo}`',
206+
errors: [error, error],
207+
// eslint-disable-next-line no-template-curly-in-string
208+
output: 'const foo = `\\u00b1${foo}\\u00b1${foo}`'
190209
}
191210
]
192-
});
211+
};
212+
213+
ruleTester.run('no-hex-escape', rule, tests);
214+
babelRuleTester.run('no-hex-escape', rule, tests);
215+
typescriptRuleTester.run('no-hex-escape', rule, tests);

0 commit comments

Comments
 (0)