mirror of
https://github.com/Microsoft/vscode
synced 2024-10-02 09:18:59 +00:00
add eslint rule to disallow look behind regex feature, ignore in git and markdown fyi @lszomoru, @mjbvz
This commit is contained in:
parent
8e35e11e0e
commit
0f648cd7db
|
@ -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/**
|
||||
|
|
|
@ -194,6 +194,7 @@
|
|||
"src/**/*.ts"
|
||||
],
|
||||
"rules": {
|
||||
"code-no-look-behind-regex": "warn",
|
||||
"code-import-patterns": [
|
||||
"warn",
|
||||
{
|
||||
|
|
42
build/lib/eslint/code-no-look-behind-regex.js
Normal file
42
build/lib/eslint/code-no-look-behind-regex.js
Normal 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'
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
52
build/lib/eslint/code-no-look-behind-regex.ts
Normal file
52
build/lib/eslint/code-no-look-behind-regex.ts
Normal 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'
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue