Skip to content

Commit 727ce68

Browse files
authored
Merge pull request #433 from Kr1tX12/master
Чтобы парсер отъебался от писанины в jsx
2 parents 60a2d6d + e81b421 commit 727ce68

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

dist/yopta.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import dictionary from './dictionary/sortedYopta.json';
33

44
function escapeRegExp(str: string) {
55
str = str.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
6-
76
if (/^\w+$/.test(str)) {
87
str = '\\b' + str + '\\b';
98
}
10-
119
return str;
1210
}
1311

@@ -23,14 +21,11 @@ function yoptReplaceAll(str: string, search: string, replacement: string) {
2321
function iterateText(text: string, to: 'js' | 'ys' = 'ys') {
2422
const langCol = to === 'ys' ? 1 : 0;
2523
const dick = dictionary;
26-
dick.sort((a, b) => {
27-
const al = a[langCol].length;
28-
const bl = b[langCol].length;
29-
return bl - al;
30-
}).forEach(
31-
(pair) => (text = yoptReplaceAll(text, pair[langCol], pair[+!langCol]))
24+
dick.sort((a, b) => b[langCol].length - a[langCol].length).forEach(
25+
(pair) => {
26+
text = yoptReplaceAll(text, pair[langCol], pair[+!langCol]);
27+
}
3228
);
33-
3429
return text;
3530
}
3631

@@ -47,25 +42,46 @@ export function compile(text: string, lang: 'js' | 'ys' = 'ys'): string {
4742
interface Literals {
4843
[key: string]: string;
4944
}
45+
const rJsxTextLiterals: Literals = {};
46+
text = text.replace(
47+
/(<[A-Za-z][^>]*>)([\s\S]+?)(?=<\/[A-Za-z])/g,
48+
(_, openTag, content, offset) => {
49+
const key = tmpToken + 'jsx_' + offset;
50+
rJsxTextLiterals[key] = content;
51+
return openTag + key;
52+
}
53+
);
54+
5055
const commentRegExp = /((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/g;
5156
const tmpToken = 'ys_' + new Date().getTime() + '_';
57+
5258
const rStringLiterals: Literals = {};
5359
text = text.replace(
5460
/\"(?:\\.|[^\"\\])*\"|\'(?:\\.|[^\'\\])*\'/g,
55-
function (val, pos) {
61+
(val, pos) => {
5662
const needKey = tmpToken + pos;
5763
rStringLiterals[needKey] = val;
5864
return needKey;
5965
}
6066
);
67+
6168
const commentsArray = text.match(commentRegExp) || [];
69+
6270
text = iterateText(text, lang);
71+
6372
// comeback comments
6473
text = text.replace(commentRegExp, () => commentsArray.shift() || '');
74+
6575
// comeback strings
6676
for (const key in rStringLiterals) {
6777
text = text.replace(key, rStringLiterals[key]);
6878
}
79+
80+
// comeback jsx
81+
for (const key in rJsxTextLiterals) {
82+
text = text.replace(key, rJsxTextLiterals[key]);
83+
}
84+
6985
return text;
7086
}
7187

src/dictionary/dictionary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,6 @@ export const dictionary = [
744744
// NodeJS/modules support
745745
['module', 'братва'],
746746
['exports', 'предъявляет'],
747-
['export', 'предъявa'],
747+
['export', 'предъява'],
748748
['global', 'общак'],
749749
];

0 commit comments

Comments
 (0)