Skip to content

Commit

Permalink
fix(types): fix type check & security issues (#538)
Browse files Browse the repository at this point in the history
* fix(types): fix type check issues

* chore(*): fix yarnrc registry configuration

* chore(*): fix type check errors

* chore(glob-watcher): force [email protected] in resolution
  • Loading branch information
lyngai authored Aug 21, 2023
1 parent 231281a commit 0f036e9
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 102 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry="https://registry.npmjs.org"
2 changes: 1 addition & 1 deletion .yarnrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
registry "https://registry.npmjs.org"
registry: https://registry.npmjs.org
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
"standard-version": "^9.3.2",
"ts-jest": "^27.1.1",
"ts-node": "^10.7.0",
"typescript": "4.3.5",
"typescript": "4.5.5",
"virtual-dom": "^2.1.1"
},
"dependencies": {
Expand All @@ -152,7 +152,8 @@
"lodash.template": "4.5.0",
"yargs-parser": "20.2.9",
"xmldom": "0.5.0",
"ansi-regex": "5.0.1"
"ansi-regex": "5.0.1",
"glob-watcher": "6.0.0"
},
"config": {
"commitizen": {
Expand All @@ -168,4 +169,4 @@
"engines": {
"node": ">=14"
}
}
}
5 changes: 5 additions & 0 deletions src/CherryStatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ export class CherryStatic {
// @ts-ignore
PluginClass.$cherry$mounted = true;
}

// for type check only
// TODO: fix this error
// eslint-disable-next-line no-useless-constructor
constructor(...args) {}
}
10 changes: 9 additions & 1 deletion src/core/hooks/Emphasis.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ export default class Emphasis extends SyntaxBase {
$str = $str.replace(/(^|\n[\s]*)(\*{2,})((?:.*?)(?:(?:\n.*?)*?))\2/g, converAsterisk);
$str = $str.replace(/([^\n*\\\s][ ]*)(\*+)((?:.*?)(?:(?:\n.*?)*?))\2/g, converAsterisk);
} else {
// TODO: fix this error
// @ts-expect-error
$str = $str.replace(this.RULE.asterisk.reg, converAsterisk);
}

// TODO: fix this error
// @ts-expect-error
$str = $str.replace(this.RULE.underscore.reg, (match, leading, symbol, text, index, string) => {
if (text.trim() === '') {
return match;
Expand Down Expand Up @@ -84,6 +88,10 @@ export default class Emphasis extends SyntaxBase {
return this.RULE[flavor].reg && this.RULE[flavor].reg.test(str);
}

/**
* TODO: fix type errors, prefer use `rules` for multiple spec instead
* @returns
*/
rule({ config } = { config: undefined }) {
const allowWhitespace = config ? !!config.allowWhitespace : false;
const REGEX = allowWhitespace
Expand All @@ -104,6 +112,6 @@ export default class Emphasis extends SyntaxBase {

asterisk.reg = compileRegExp(asterisk, 'g');
underscore.reg = compileRegExp(underscore, 'g');
return { asterisk, underscore };
return /** @type {any} */ ({ asterisk, underscore });
}
}
6 changes: 5 additions & 1 deletion src/core/hooks/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ export default class Header extends ParagraphBase {
return this.RULE[flavor].reg && this.RULE[flavor].reg.test(str);
}

/**
* TODO: fix type errors, prefer use `rules` for multiple spec instead
* @returns
*/
rule() {
// setext Header
// TODO: 支持多行标题
Expand All @@ -221,6 +225,6 @@ export default class Header extends ParagraphBase {
this.strict && (atx.begin += '(?=\\h+)'); // (?=\\s+) for strict mode
atx.reg = compileRegExp(atx, 'g', true);

return { setext, atx };
return /** @type {any} */ ({ setext, atx });
}
}
8 changes: 4 additions & 4 deletions src/core/hooks/Strikethrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export default class Strikethrough extends SyntaxBase {
}

rule({ config } = { config: undefined }) {
/** @type {Partial<import('~types/syntax').BasicHookRegexpRule>} */
let ret = {};
/** @type {import('~types/syntax').BasicHookRegexpRule} */
let ret = /** @type {any} */ ({});
if (!!config.needWhitespace) {
ret = { begin: '(^|[\\s])\\~T\\~T', end: '\\~T\\~T(?=\\s|$)', content: '([\\w\\W]+?)' };
ret = { ...ret, begin: '(^|[\\s])\\~T\\~T', end: '\\~T\\~T(?=\\s|$)', content: '([\\w\\W]+?)' };
} else {
ret = { begin: '(^|[^\\\\])\\~T\\~T', end: '\\~T\\~T', content: '([\\w\\W]+?)' };
ret = { ...ret, begin: '(^|[^\\\\])\\~T\\~T', end: '\\~T\\~T', content: '([\\w\\W]+?)' };
}
ret.reg = new RegExp(ret.begin + ret.content + ret.end, 'g');
return ret;
Expand Down
4 changes: 2 additions & 2 deletions src/core/hooks/Suggester.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ export default class Suggester extends SyntaxBase {
`${isLookbehindSupported() ? '((?<!\\\\))[ ]' : '(^|[^\\\\])[ ]'}(${keys})(([^${keys}\\s])+)`,
'g',
);
return {
return /** @type {any} */ ({
reg,
};
});
}

mounted() {
Expand Down
6 changes: 5 additions & 1 deletion src/core/hooks/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ export default class Table extends ParagraphBase {
return this.RULE[flavor].reg && this.RULE[flavor].reg.test(str);
}

/**
* TODO: fix type errors
* @returns
*/
rule() {
return getTableRule();
return /** @type {any} */ (getTableRule());
}
}
14 changes: 13 additions & 1 deletion src/core/hooks/Toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export default class Toc extends ParagraphBase {
beforeMakeHtml(str) {
let $str = str;
if (this.test($str, 'extend')) {
// TODO: fix this error
// @ts-expect-error
$str = $str.replace(this.RULE.extend.reg, (match, lines, toc) => {
if (!this.allowMultiToc && !this.isFirstTocToken) {
// 需要补齐非捕获的\n,以及第一个分组中的\n
Expand All @@ -65,6 +67,8 @@ export default class Toc extends ParagraphBase {
});
}
if (this.test($str, 'standard')) {
// TODO: fix this error
// @ts-expect-error
$str = $str.replace(this.RULE.standard.reg, (match, lines, toc) => {
if (!this.allowMultiToc && !this.isFirstTocToken) {
// 需要补齐非捕获的\n,以及第一个分组中的\n
Expand Down Expand Up @@ -245,10 +249,14 @@ export default class Toc extends ParagraphBase {
match.replace(/(\]\]|\]|】】)([<~])/, '$1\n$2'),
);
// 首先识别扩展语法
// TODO: fix this error
// @ts-expect-error
$str = $str.replace(this.RULE.extend.reg, (match, preLinesMatch) =>
this.$makeToc(headerList, str2Md5, preLinesMatch),
);
// 处理标准语法
// TODO: fix this error
// @ts-expect-error
$str = $str.replace(this.RULE.standard.reg, (match, preLinesMatch) =>
this.$makeToc(headerList, str2Md5, preLinesMatch),
);
Expand All @@ -261,6 +269,10 @@ export default class Toc extends ParagraphBase {
return this.RULE[flavor].reg ? this.RULE[flavor].reg.test(str) : false;
}

/**
* TODO: fix type errors, prefer use `rules` for multiple spec instead
* @returns
*/
rule() {
const extend = {
begin: '(?:^|\\n)(\\n*)',
Expand All @@ -270,6 +282,6 @@ export default class Toc extends ParagraphBase {
extend.reg = new RegExp(extend.begin + extend.content + extend.end, 'g');
const standard = { begin: '(?:^|\\n)(\\n*)', end: '(?=$|\\n)', content: '[ ]*(\\[(?:toc|TOC)\\])[ ]*' };
standard.reg = new RegExp(standard.begin + standard.content + standard.end, 'g');
return { extend, standard };
return /** @type {any} */ ({ extend, standard });
}
}
9 changes: 6 additions & 3 deletions src/core/hooks/Transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ export default class Transfer extends SyntaxBase {
// }

rule() {
const ret = {};
ret.reg = new RegExp('');
return ret;
return {
begin: '',
content: '',
end: '',
reg: new RegExp(''),
};
}

beforeMakeHtml(str) {
Expand Down
8 changes: 7 additions & 1 deletion src/utils/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,15 @@ export function getCodeBlockRule() {
begin: /(?:^|\n)(\n*((?:>[\t ]*)*)(?:[^\S\n]*))(`{3,})([^`]*?)\n/,
content: /([\w\W]*?)/, // '([\\w\\W]*?)',
end: /[^\S\n]*\3[ \t]*(?=$|\n+)/, // '\\s*```[ \\t]*(?=$|\\n+)',
reg: new RegExp(''),
};
codeBlock.reg = new RegExp(codeBlock.begin.source + codeBlock.content.source + codeBlock.end.source, 'g');
return codeBlock;
return {
...codeBlock,
begin: codeBlock.begin.source,
content: codeBlock.content.source,
end: codeBlock.end.source,
};
}

/**
Expand Down
Loading

0 comments on commit 0f036e9

Please sign in to comment.