From 219e8b6ebc33d6932ab0e40354e1d46f62a32359 Mon Sep 17 00:00:00 2001 From: rrogowski Date: Mon, 25 Feb 2019 10:42:28 -0500 Subject: [PATCH 1/3] fix for #4518 --- src/rules/noTrailingWhitespaceRule.ts | 11 ++++++++++- .../zero-width-no-break-space/test.ts.lint | 4 ++++ .../zero-width-no-break-space/tslint.json | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/rules/no-trailing-whitespace/zero-width-no-break-space/test.ts.lint create mode 100644 test/rules/no-trailing-whitespace/zero-width-no-break-space/tslint.json diff --git a/src/rules/noTrailingWhitespaceRule.ts b/src/rules/noTrailingWhitespaceRule.ts index 3225be2d309..bf6b20407e0 100644 --- a/src/rules/noTrailingWhitespaceRule.ts +++ b/src/rules/noTrailingWhitespaceRule.ts @@ -22,6 +22,8 @@ import * as Lint from "../index"; import { getTemplateRanges } from "./noConsecutiveBlankLinesRule"; +const ZERO_WIDTH_NO_BREAK_SPACE = 0xfeff; + const OPTION_IGNORE_COMMENTS = "ignore-comments"; const OPTION_IGNORE_JSDOC = "ignore-jsdoc"; const OPTION_IGNORE_TEMPLATE_STRINGS = "ignore-template-strings"; @@ -87,7 +89,14 @@ function walk(ctx: Lint.WalkContext) { for (const line of getLineRanges(sourceFile)) { // \s matches any whitespace character (equal to [\r\n\t\f\v ]) const match = text.substr(line.pos, line.contentLength).match(/\s+$/); - if (match !== null && !(ctx.options.ignoreBlankLines && match.index === 0)) { + if ( + match !== null && + !(ctx.options.ignoreBlankLines && match.index === 0) && + // See https://github.com/palantir/tslint/issues/4518 + // and https://github.com/ajafff/tsutils/issues/94 + // TODO: Remove this check once tsutils handles Byte Order Markers correctly. + match[0].charCodeAt(0) !== ZERO_WIDTH_NO_BREAK_SPACE + ) { possibleFailures.push({ end: line.pos + line.contentLength, pos: line.pos + match.index!, diff --git a/test/rules/no-trailing-whitespace/zero-width-no-break-space/test.ts.lint b/test/rules/no-trailing-whitespace/zero-width-no-break-space/test.ts.lint new file mode 100644 index 00000000000..c873c6af5a2 --- /dev/null +++ b/test/rules/no-trailing-whitespace/zero-width-no-break-space/test.ts.lint @@ -0,0 +1,4 @@ + +// This file starts with a zero width no-break-space. +// See http://www.fileformat.info/info/unicode/char/feff/index.htm +const a = 3; diff --git a/test/rules/no-trailing-whitespace/zero-width-no-break-space/tslint.json b/test/rules/no-trailing-whitespace/zero-width-no-break-space/tslint.json new file mode 100644 index 00000000000..f81bfe73957 --- /dev/null +++ b/test/rules/no-trailing-whitespace/zero-width-no-break-space/tslint.json @@ -0,0 +1,5 @@ +{ + "rules": { + "no-trailing-whitespace": true + } +} From 6f5049cd39cffdfa51c6efd13b79d2844482ce51 Mon Sep 17 00:00:00 2001 From: rrogowski Date: Tue, 5 Mar 2019 21:03:34 -0500 Subject: [PATCH 2/3] add new test case --- src/rules/noTrailingWhitespaceRule.ts | 5 +---- .../zero-width-no-break-space/test2.ts.lint | 5 +++++ 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 test/rules/no-trailing-whitespace/zero-width-no-break-space/test2.ts.lint diff --git a/src/rules/noTrailingWhitespaceRule.ts b/src/rules/noTrailingWhitespaceRule.ts index 59d454a1d39..401765fe05c 100644 --- a/src/rules/noTrailingWhitespaceRule.ts +++ b/src/rules/noTrailingWhitespaceRule.ts @@ -92,10 +92,7 @@ function walk(ctx: Lint.WalkContext) { if ( match !== null && !(ctx.options.ignoreBlankLines && match.index === 0) && - // See https://github.com/palantir/tslint/issues/4518 - // and https://github.com/ajafff/tsutils/issues/94 - // TODO: Remove this check once tsutils handles Byte Order Markers correctly. - match[0].charCodeAt(0) !== ZERO_WIDTH_NO_BREAK_SPACE + (match[0] !== String.fromCharCode(ZERO_WIDTH_NO_BREAK_SPACE)) ) { possibleFailures.push({ end: line.pos + line.contentLength, diff --git a/test/rules/no-trailing-whitespace/zero-width-no-break-space/test2.ts.lint b/test/rules/no-trailing-whitespace/zero-width-no-break-space/test2.ts.lint new file mode 100644 index 00000000000..96c770979e8 --- /dev/null +++ b/test/rules/no-trailing-whitespace/zero-width-no-break-space/test2.ts.lint @@ -0,0 +1,5 @@ + +~~ [trailing whitespace] +// This file starts with a zero width no-break-space followed by a trailing space. +// See http://www.fileformat.info/info/unicode/char/feff/index.htm +const a = 3; From a009c45df1e01f9f480ef7f25e2d5bb283f7b4e9 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Wed, 6 Mar 2019 23:28:06 -0500 Subject: [PATCH 3/3] Fixed Prettier formatting on the rule --- src/rules/noTrailingWhitespaceRule.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/noTrailingWhitespaceRule.ts b/src/rules/noTrailingWhitespaceRule.ts index 401765fe05c..411de7696e8 100644 --- a/src/rules/noTrailingWhitespaceRule.ts +++ b/src/rules/noTrailingWhitespaceRule.ts @@ -92,7 +92,7 @@ function walk(ctx: Lint.WalkContext) { if ( match !== null && !(ctx.options.ignoreBlankLines && match.index === 0) && - (match[0] !== String.fromCharCode(ZERO_WIDTH_NO_BREAK_SPACE)) + match[0] !== String.fromCharCode(ZERO_WIDTH_NO_BREAK_SPACE) ) { possibleFailures.push({ end: line.pos + line.contentLength,