add eslint rule to disallow look behind regex feature, ignore in git and markdown fyi @lszomoru, @mjbvz

This commit is contained in:
Johannes 2022-03-22 16:02:11 +01:00
parent 8e35e11e0e
commit 0f648cd7db
No known key found for this signature in database
GPG Key ID: 6DEF802A22264FCA
7 changed files with 99 additions and 0 deletions

View File

@ -9,6 +9,7 @@
**/extensions/markdown-language-features/media/**
**/extensions/markdown-language-features/notebook-out/**
**/extensions/markdown-math/notebook-out/**
**/extensions/notebook-renderers/renderer-out/index.js
**/extensions/simple-browser/media/index.js
**/extensions/typescript-language-features/test-workspace/**
**/extensions/vscode-api-tests/testWorkspace/**

View File

@ -194,6 +194,7 @@
"src/**/*.ts"
],
"rules": {
"code-no-look-behind-regex": "warn",
"code-import-patterns": [
"warn",
{

View File

@ -0,0 +1,42 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const _positiveLookBehind = /\(\?<=.+/;
const _negativeLookBehind = /\(\?<!.+/;
function _containsLookBehind(pattern) {
if (typeof pattern !== 'string') {
return false;
}
return _positiveLookBehind.test(pattern) || _negativeLookBehind.test(pattern);
}
module.exports = {
create(context) {
return {
// /.../
['Literal[regex]']: (node) => {
const pattern = node.regex?.pattern;
if (_containsLookBehind(pattern)) {
context.report({
node,
message: 'Look behind assertions are not yet supported in all browsers'
});
}
},
// new Regex("...")
['NewExpression[callee.name="RegExp"] Literal']: (node) => {
if (_containsLookBehind(node.value)) {
context.report({
node,
message: 'Look behind assertions are not yet supported in all browsers'
});
}
}
};
}
};

View File

@ -0,0 +1,52 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as eslint from 'eslint';
import { TSESTree } from '@typescript-eslint/experimental-utils';
import * as ESTree from 'estree';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const _positiveLookBehind = /\(\?<=.+/;
const _negativeLookBehind = /\(\?<!.+/;
function _containsLookBehind(pattern: string | unknown): boolean {
if (typeof pattern !== 'string') {
return false;
}
return _positiveLookBehind.test(pattern) || _negativeLookBehind.test(pattern);
}
module.exports = {
create(context: eslint.Rule.RuleContext) {
return {
// /.../
['Literal[regex]']: (node: any) => {
type RegexLiteral = TSESTree.Literal & { regex: { pattern: string; flags: string } };
const pattern = (<RegexLiteral>node).regex?.pattern;
if (_containsLookBehind(pattern)) {
context.report({
node,
message: 'Look behind assertions are not yet supported in all browsers'
});
}
},
// new Regex("...")
['NewExpression[callee.name="RegExp"] Literal']: (node: ESTree.Literal) => {
if (_containsLookBehind(node.value)) {
context.report({
node,
message: 'Look behind assertions are not yet supported in all browsers'
});
}
}
};
}
};

View File

@ -480,6 +480,7 @@ export class Git {
const repoUri = Uri.file(repoPath);
const pathUri = Uri.file(repositoryPath);
if (repoUri.authority.length !== 0 && pathUri.authority.length === 0) {
// eslint-disable-next-line code-no-look-behind-regex
let match = /(?<=^\/?)([a-zA-Z])(?=:\/)/.exec(pathUri.path);
if (match !== null) {
const [, letter] = match;

View File

@ -104,6 +104,7 @@ export function stripAngleBrackets(link: string) {
}
const linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*\])\(\s*)(([^\s\(\)]|\([^\s\(\)]*?\))+)\s*(".*?")?\)/g;
// eslint-disable-next-line code-no-look-behind-regex
const referenceLinkPattern = /((?<=^|[^\]])\[((?:\\\]|[^\]])+)\])(?!:)(?:[^\[]|$|\[\s*?([^\s\]]*?)\])/g;
const definitionPattern = /^([\t ]*\[(?!\^)((?:\\\]|[^\]])+)\]:\s*)([^<]\S*|<[^>]+>)/gm;
const inlineCodePattern = /(?:^|[^`])(`+)(?:.+?|.*?(?:(?:\r?\n).+?)*?)(?:\r?\n)?\1(?:$|[^`])/gm;

View File

@ -48,6 +48,7 @@ export class InMemoryDocument implements vscode.TextDocument {
const before = this._contents.slice(0, offset);
const newLines = before.match(/\r\n|\n/g);
const line = newLines ? newLines.length : 0;
// eslint-disable-next-line code-no-look-behind-regex
const preCharacters = before.match(/(?<=\r\n|\n|^).*$/g);
return new vscode.Position(line, preCharacters ? preCharacters[0].length : 0);
}