-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
eslint-bridge/src/rules/sonar-no-misleading-character-class.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2021 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
// https://sonarsource.github.io/rspec/#/rspec/S5868 | ||
|
||
import { Rule } from 'eslint'; | ||
import { createRegExpRule } from './regex-rule-template'; | ||
import { Character, CharacterClassElement } from 'regexpp/ast'; | ||
|
||
export const rule: Rule.RuleModule = createRegExpRule(context => { | ||
function characters(nodes: CharacterClassElement[]): Character[][] { | ||
let current: Character[] = []; | ||
const sequences: Character[][] = [current]; | ||
for (const node of nodes) { | ||
if (node.type === 'Character') { | ||
current.push(node); | ||
} else if (node.type === 'CharacterClassRange') { | ||
// for following regexp [xa-z] we produce [[xa],[z]] | ||
// we would report for example if instead of 'xa' there would be unicode combined class | ||
current.push(node.min); | ||
current = [node.max]; | ||
sequences.push(current); | ||
} else if (node.type === 'CharacterSet' && current.length > 0) { | ||
// CharacterSet is for example [\d], ., or \p{ASCII} | ||
// see https://github.com/mysticatea/regexpp/blob/master/src/ast.ts#L222 | ||
current = []; | ||
sequences.push(current); | ||
} | ||
} | ||
return sequences; | ||
} | ||
|
||
return { | ||
onCharacterClassEnter(ccNode) { | ||
for (const chars of characters(ccNode.elements)) { | ||
const idx = chars.findIndex( | ||
(c, i) => | ||
i !== 0 && isCombiningCharacter(c.value) && !isCombiningCharacter(chars[i - 1].value), | ||
); | ||
if (idx >= 0) { | ||
const combinedChar = chars[idx - 1].raw + chars[idx].raw; | ||
const message = `Move this Unicode combined character '${combinedChar}' outside of [...]`; | ||
context.reportRegExpNode({ regexpNode: chars[idx], node: context.node, message }); | ||
} | ||
} | ||
}, | ||
}; | ||
}); | ||
|
||
function isCombiningCharacter(codePoint: number) { | ||
return /^[\p{Mc}\p{Me}\p{Mn}]$/u.test(String.fromCodePoint(codePoint)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
eslint-bridge/tests/rules/sonar-no-misleading-character-class.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2021 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
import { RuleTester } from 'eslint'; | ||
import { rule } from 'rules/sonar-no-misleading-character-class'; | ||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: 'module' } }); | ||
|
||
const combiningClass = c => `Move this Unicode combined character '${c}' outside of [...]`; | ||
|
||
ruleTester.run('', rule, { | ||
valid: [ | ||
'var r = /[\\uD83D\\d\\uDC4D]/', | ||
'var r = /[\\uD83D-\\uDC4D]/', | ||
'var r = /[👍]/u', | ||
'var r = /[\\uD83D\\uDC4D]/u', | ||
'var r = /[\\u{1F44D}]/u', | ||
'var r = /❇️/', | ||
'var r = /Á/', | ||
'var r = /[❇]/', | ||
'var r = /👶🏻/', | ||
'var r = /[👶]/u', | ||
'var r = /🇯🇵/', | ||
'var r = /[JP]/', | ||
'var r = /👨👩👦/', | ||
|
||
// Ignore solo lead/tail surrogate. | ||
'var r = /[\\uD83D]/', | ||
'var r = /[\\uDC4D]/', | ||
'var r = /[\\uD83D]/u', | ||
'var r = /[\\uDC4D]/u', | ||
|
||
// Ignore solo combining char. | ||
'var r = /[\\u0301]/', | ||
'var r = /[\\uFE0F]/', | ||
'var r = /[\\u0301]/u', | ||
'var r = /[\\uFE0F]/u', | ||
|
||
// Coverage | ||
'var r = /[x\\S]/u', | ||
'var r = /[xa-z]/u', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'var r = /[\\u0041\\u0301-\\u0301]/', | ||
errors: [{ column: 17, endColumn: 23, message: combiningClass('\\u0041\\u0301') }], | ||
}, | ||
{ | ||
code: 'var r = /[Á]/', | ||
errors: [{ message: combiningClass('Á') }], | ||
}, | ||
{ | ||
code: 'var r = /[Á]/u', | ||
errors: [{ message: combiningClass('Á') }], | ||
}, | ||
{ | ||
code: 'var r = /[\\u0041\\u0301]/', | ||
errors: [{ message: combiningClass('\\u0041\\u0301') }], | ||
}, | ||
{ | ||
code: 'var r = /[\\u0041\\u0301]/u', | ||
errors: [{ message: combiningClass('\\u0041\\u0301') }], | ||
}, | ||
{ | ||
code: 'var r = /[\\u{41}\\u{301}]/u', | ||
errors: [{ message: combiningClass('\\u{41}\\u{301}') }], | ||
}, | ||
{ | ||
code: 'var r = /[❇️]/', | ||
errors: [{ message: combiningClass('❇️') }], | ||
}, | ||
{ | ||
code: 'var r = /[❇️]/u', | ||
errors: [{ message: combiningClass('❇️') }], | ||
}, | ||
{ | ||
code: 'var r = /[\\u2747\\uFE0F]/', | ||
errors: [{ message: combiningClass('\\u2747\\uFE0F') }], | ||
}, | ||
{ | ||
code: 'var r = /[\\u2747\\uFE0F]/u', | ||
errors: [{ message: combiningClass('\\u2747\\uFE0F') }], | ||
}, | ||
{ | ||
code: 'var r = /[\\u{2747}\\u{FE0F}]/u', | ||
errors: [{ message: combiningClass('\\u{2747}\\u{FE0F}') }], | ||
}, | ||
{ | ||
code: String.raw`var r = new globalThis.RegExp("[❇️]", "")`, | ||
errors: [{ message: combiningClass('❇️') }], | ||
}, | ||
{ | ||
code: String.raw`"cc̈d̈d".replaceAll(RegExp("[c̈d̈]"), "X")`, | ||
errors: [{ message: combiningClass('c̈') }], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters