Skip to content

Commit

Permalink
Fix: false positive of no-unsupported-features about code point esc…
Browse files Browse the repository at this point in the history
…apes.
  • Loading branch information
mysticatea committed Oct 30, 2016
1 parent 9e77b54 commit 292e8f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
25 changes: 22 additions & 3 deletions lib/rules/no-unsupported-features.js
Expand Up @@ -25,7 +25,7 @@ var CLASS_TYPE = /^Class(?:Declaration|Expression)$/
var DESTRUCTURING_PARENT_TYPE = /^(?:Function(?:Declaration|Expression)|ArrowFunctionExpression|AssignmentExpression|VariableDeclarator)$/
var BINARY_NUMBER = /^0[bB]/
var OCTAL_NUMBER = /^0[oO]/
var UNICODE_ESC = /\\u\{.+?\}/
var UNICODE_ESC = /(\\+)u\{[0-9a-fA-F]+?\}/g
var GET_OR_SET = /^(?:g|s)et$/
var NEW_BUILTIN_TYPES = [
"Int8Array", "Uint8Array", "Uint8ClampedArray", "Int16Array", "Uint16Array",
Expand Down Expand Up @@ -230,6 +230,25 @@ function getIdentifierName(node) {
return node.name
}

/**
* Checks whether the given string has `\u{90ABCDEF}`-like escapes.
*
* @param {string} raw - The string to check.
* @returns {boolean} `true` if the string has Unicode code point escapes.
*/
function hasUnicodeCodePointEscape(raw) {
var match = null

UNICODE_ESC.lastIndex = 0
while ((match = UNICODE_ESC.exec(raw)) != null) {
if (match[1].length % 2 === 1) {
return true
}
}

return false
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -447,7 +466,7 @@ module.exports = function(context) {

"Identifier": function(node) {
var raw = sourceCode.getText(node)
if (UNICODE_ESC.test(raw)) {
if (hasUnicodeCodePointEscape(raw)) {
report(node, "unicodeCodePointEscapes")
}
},
Expand All @@ -462,7 +481,7 @@ module.exports = function(context) {
}
}
else if (typeof node.value === "string") {
if (UNICODE_ESC.test(node.raw)) {
if (hasUnicodeCodePointEscape(node.raw)) {
report(node, "unicodeCodePointEscapes")
}
}
Expand Down
6 changes: 5 additions & 1 deletion tests/lib/rules/no-unsupported-features.js
Expand Up @@ -203,7 +203,7 @@ ruleTester.run("no-unsupported-features", rule, [
{
keys: ["unicodeCodePointEscapes", "syntax"],
name: "Unicode Code Point Escapes",
code: "var \\u{102C0} = { \\u{102C0} : '\\u{1d306}' };", // eslint-disable-line node/no-unsupported-features
code: "var \\u{102C0} = { \\u{102C0} : '\\u{1d306}' };", //eslint-disable-line node/no-unsupported-features
errors: 3,
supported: 4,
},
Expand Down Expand Up @@ -945,6 +945,10 @@ ruleTester.run("no-unsupported-features", rule, [
code: "var a = () => 1",
env: {es6: true},
},
{
code: "'\\\\u{0123}'", //eslint-disable-line node/no-unsupported-features
env: {es6: true},
},
],
invalid: [
{
Expand Down

0 comments on commit 292e8f8

Please sign in to comment.