From 292e8f8b210593ff0d0b3c36e9c3a98d04e46980 Mon Sep 17 00:00:00 2001 From: Toru Nagashima Date: Sun, 30 Oct 2016 21:36:21 +0900 Subject: [PATCH] Fix: false positive of `no-unsupported-features` about code point escapes. --- lib/rules/no-unsupported-features.js | 25 +++++++++++++++++++--- tests/lib/rules/no-unsupported-features.js | 6 +++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/rules/no-unsupported-features.js b/lib/rules/no-unsupported-features.js index f44bde35..c1bb755c 100644 --- a/lib/rules/no-unsupported-features.js +++ b/lib/rules/no-unsupported-features.js @@ -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", @@ -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 //------------------------------------------------------------------------------ @@ -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") } }, @@ -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") } } diff --git a/tests/lib/rules/no-unsupported-features.js b/tests/lib/rules/no-unsupported-features.js index cc9d9daf..e98d5007 100644 --- a/tests/lib/rules/no-unsupported-features.js +++ b/tests/lib/rules/no-unsupported-features.js @@ -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, }, @@ -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: [ {