Skip to content

Commit dda087e

Browse files
authored
Better regex (#2325)
* Better regex * Update editorFile.js
1 parent f029b25 commit dda087e

15 files changed

Lines changed: 25 additions & 25 deletions

File tree

hooks/modify-java-files.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,18 +480,18 @@ async function main() {
480480
}
481481

482482
function getConstructorRegex(filename) {
483-
return new RegExp(`([^]*${filename.split('.')[0]}\\s*\\(.*\\)\\s*{[^}]*})([^]*)`, 'm');
483+
return new RegExp(`([^]*?${filename.split('.')[0]}\\s*\\(.*?\\)\\s*{[^}]*})([^]*)`, 'm');
484484
}
485485

486486
function getInterfaceDeclarationRegex(filename) {
487-
return new RegExp(`([^]*interface\\s+${filename.split('.')[0]}[\\s\\w]*{)([^]*})`, 'm');
487+
return new RegExp(`([^]*?interface\\s+${filename.split('.')[0]}[\\s\\w]*{)([^]*})`, 'm');
488488
}
489489

490490
function getClassDeclarationRegex(filename) {
491-
return new RegExp(`([^]*class\\s+${filename.split('.')[0]}[\\s\\w]*{)([^]*})`, 'm');
491+
return new RegExp(`([^]*?class\\s+${filename.split('.')[0]}[\\s\\w]*{)([^]*})`, 'm');
492492
}
493493

494494
function removeComments(content) {
495-
return content.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '');
495+
return content.replace(/\/\*[^]*?\*\/|([^\\:]|^)\/\/.*$/gm, '');
496496
}
497497
}

rspack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ module.exports = (env, options) => {
9696
},
9797
// Regular CSS/SCSS files
9898
{
99-
test: /(?<!\.m)\.(sa|sc|c)ss$/,
99+
test: /\.(?<!\.m\.)(sa|sc|c)ss$/,
100100
type: 'javascript/auto',
101101
use: [
102102
rspack.CssExtractRspackPlugin.loader,

src/cm/lsp/clientManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ function normalizeRootUriForServer(
11811181
if (!rootUri || typeof rootUri !== "string") {
11821182
return { normalizedRootUri: null, originalRootUri: null };
11831183
}
1184-
const schemeMatch = /^([a-zA-Z][\w+\-.]*):/.exec(rootUri);
1184+
const schemeMatch = /^([a-zA-Z][\w+\-.]*?):/.exec(rootUri);
11851185
const scheme = schemeMatch ? schemeMatch[1].toLowerCase() : null;
11861186

11871187
// Already a file:// URI - use as-is
@@ -1206,7 +1206,7 @@ function normalizeRootUriForServer(
12061206
function normalizeDocumentUri(uri: string | null | undefined): string | null {
12071207
if (!uri || typeof uri !== "string") return null;
12081208

1209-
const schemeMatch = /^([a-zA-Z][\w+\-.]*):/.exec(uri);
1209+
const schemeMatch = /^([a-zA-Z][\w+\-.]*?):/.exec(uri);
12101210
const scheme = schemeMatch ? schemeMatch[1].toLowerCase() : null;
12111211

12121212
// Already a file:// URI or untitled use as-is

src/cm/lsp/runtimeProviders.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export function inferWorkspaceKind(
169169
const uri = String(context.rootUri || context.file?.uri || context.uri || "");
170170
if (!uri) return "unknown";
171171

172-
const schemeMatch = /^([a-zA-Z][\w+\-.]*):/.exec(uri);
172+
const schemeMatch = /^([a-zA-Z][\w+\-.]*?):/.exec(uri);
173173
const scheme = schemeMatch ? schemeMatch[1].toLowerCase() : null;
174174

175175
if (!scheme) return uri.startsWith("/") ? "app-private" : "unknown";
@@ -198,7 +198,7 @@ export function isBuiltinAlpineAccessible(
198198
const uri = String(context.rootUri || context.file?.uri || context.uri || "");
199199
if (!uri) return false;
200200

201-
const schemeMatch = /^([a-zA-Z][\w+\-.]*):/.exec(uri);
201+
const schemeMatch = /^([a-zA-Z][\w+\-.]*?):/.exec(uri);
202202
const scheme = schemeMatch ? schemeMatch[1].toLowerCase() : null;
203203

204204
if (!scheme) return uri.startsWith("/");

src/cm/modelist.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class Mode {
218218
const regexParts: string[] = [];
219219

220220
if (extensionPatterns.length) {
221-
regexParts.push(`^.*\\.(${extensionPatterns.join("|")})$`);
221+
regexParts.push(`^.*?\\.(${extensionPatterns.join("|")})$`);
222222
}
223223

224224
regexParts.push(...filenamePatterns);

src/fileSystem/sftp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ class SftpClient {
495495
* @param {String} name
496496
*/
497497
#safeName(name) {
498-
const escapeCh = (str) => str.replace(/\\([\s\S])|([`"])/g, "\\$1$2");
498+
const escapeCh = (str) => str.replace(/\\([^])|([`"])/g, "\\$1$2");
499499
const ar = name.split("/");
500500
return ar.map((dirname) => escapeCh(dirname)).join("/");
501501
}

src/lib/console.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ import loadPolyFill from "utils/polyfill";
590590
* @returns {IterableIterator<RegExpMatchArray>}
591591
*/
592592
function matchRegex(str) {
593-
return str.matchAll(/(?<!%)%[oOsdifc]/g);
593+
return str.matchAll(/%(?<!%%)[oOsdifc]/g);
594594
}
595595
}
596596

@@ -605,10 +605,10 @@ import loadPolyFill from "utils/polyfill";
605605
}
606606
let stack = error.stack.split("\n");
607607
if (!skip) stack.splice(1, 1);
608-
let regExecRes = /<(.*)>:(\d+):(\d+)/.exec(stack[1]) || [];
608+
let regExecRes = /<([^>]*)>:(\d+):(\d+)/.exec(stack[1]) || [];
609609
if (!regExecRes.length) {
610610
const errorInfo = stack[1]?.split("/").pop();
611-
regExecRes = /(.+):(\d+):(\d+)/.exec(errorInfo) || [];
611+
regExecRes = /(.+?):(\d+):(\d+)/.exec(errorInfo) || [];
612612
}
613613
let src = "";
614614
const location = regExecRes[1];

src/lib/editorFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ export default class EditorFile {
797797
let text = this.session.doc.toString();
798798

799799
if (value === "windows") {
800-
text = text.replace(/(?<!\r)\n/g, "\r\n");
800+
text = text.replace(/\n(?<!\r\n)/g, "\r\n");
801801
} else {
802802
text = text.replace(/\r/g, "");
803803
}

src/pages/changelog/changelog.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export default async function Changelog() {
160160
`[#$1](${REPO_URL}/pull/$1)`,
161161
)
162162
// Convert existing #number references to links if they aren't already
163-
.replace(/(?<!\[)#(\d+)(?!\])/g, `[#$1](${REPO_URL}/pull/$1)`)
163+
.replace(/#(?<!\[#)(\d+)(?!\])/g, `[#$1](${REPO_URL}/pull/$1)`)
164164
// Convert @username mentions to GitHub profile links
165165
.replace(/@(\w+)/g, "[@$1](https://github.com/$1)");
166166

src/pages/markdownPreview/renderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import Url from "utils/Url";
99
const EXTERNAL_LINK_PATTERN = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
1010
const IMAGE_PLACEHOLDER =
1111
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
12-
const BLOCK_MATH_PATTERN = /(^|[^\\])\$\$[\s\S]+?\$\$/m;
12+
const BLOCK_MATH_PATTERN = /(^|[^\\])\$\$[^]+?\$\$/m;
1313
const INLINE_MATH_PATTERN =
1414
/(^|[^\\])\$(?!\s)(?:\\.|[^$\\\n])*(?:\\[{^_(]|[{^_])(?:\\.|[^$\\\n])*\$(?!\w)/m;
1515
const BEGIN_END_MATH_PATTERN =
16-
/\\begin\{(?:equation|align|gather|multline|eqnarray)\*?\}[\s\S]*?\\end\{(?:equation|align|gather|multline|eqnarray)\*?\}/m;
16+
/\\begin\{(?:equation|align|gather|multline|eqnarray)\*?\}[^]*?\\end\{(?:equation|align|gather|multline|eqnarray)\*?\}/m;
1717

1818
let mathModulesPromise = null;
1919
let mathMarkdownItPromise = null;

0 commit comments

Comments
 (0)