@bhsd/codemirror-mediawiki 2.22.1 → 2.23.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ import type { Diagnostic } from '@codemirror/lint';
6
6
  import type { Config } from 'wikiparser-node';
7
7
  import type { MwConfig } from './token';
8
8
  import type { DocRange } from './fold';
9
+ import type { Option, LiveOption } from './linter';
9
10
  export type { MwConfig };
10
11
  export type LintSource = (doc: Text) => Diagnostic[] | Promise<Diagnostic[]>;
11
12
  export type Addon<T> = [(config?: T, cm?: CodeMirror6) => Extension, Record<string, T>];
@@ -56,7 +57,7 @@ export declare class CodeMirror6 {
56
57
  * 获取默认linter
57
58
  * @param opt 选项
58
59
  */
59
- getLinter(opt?: Record<string, unknown>): Promise<LintSource | undefined>;
60
+ getLinter(opt?: Option | LiveOption): Promise<LintSource | undefined>;
60
61
  /**
61
62
  * 重设编辑器内容
62
63
  * @param insert 新内容
package/dist/hover.d.ts CHANGED
@@ -1,12 +1,6 @@
1
- import type { Tooltip, EditorView } from '@codemirror/view';
1
+ import type { Tooltip } from '@codemirror/view';
2
2
  import type { Text } from '@codemirror/state';
3
- import type { LanguageServiceBase } from 'wikiparser-node/extensions/typings';
4
3
  import type { Position } from 'vscode-languageserver-types';
5
- /**
6
- * 获取当前编辑器的语言服务
7
- * @param view EditorView 实例
8
- */
9
- export declare const getLSP: (view: EditorView) => LanguageServiceBase | undefined;
10
4
  /**
11
5
  * 将索引转换为位置
12
6
  * @param doc Text 实例
package/dist/linter.d.ts CHANGED
@@ -1,26 +1,33 @@
1
- import type { LinterBase } from 'wikiparser-node/extensions/typings';
1
+ import type { Diagnostic as DiagnosticBase, Range } from 'vscode-languageserver-types';
2
2
  import type { Linter } from 'eslint';
3
3
  import type { Warning } from 'stylelint';
4
4
  import type { Diagnostic } from 'luacheck-browserify';
5
- declare type getLinter<T> = (opt?: Record<string, unknown>) => T;
6
- declare type getAsyncLinter<T> = (opt?: Record<string, unknown>) => Promise<T>;
5
+ export type Option = Record<string, unknown> | null | undefined;
6
+ export type LiveOption = (runtime?: true) => Option;
7
+ declare type getLinter<T> = () => (text: string) => T;
7
8
  /**
8
- * 获取 WikiLint
9
- * @param opt 选项
9
+ * @param opt 初始化选项
10
+ * @param obj 仅用于wikiparse.LanguageService
11
+ * @param config runtime设置
10
12
  */
11
- export declare const getWikiLinter: getAsyncLinter<LinterBase>;
12
- /**
13
- * 获取 ESLint
14
- * @param opt 选项
15
- */
16
- export declare const getJsLinter: getAsyncLinter<(text: string) => Linter.LintMessage[]>;
13
+ declare type getAsyncLinter<T, S = never, R = never> = (opt?: S, obj?: R) => Promise<(text: string, config?: Option) => T>;
14
+ declare interface MixedDiagnostic extends Omit<DiagnosticBase, 'range'> {
15
+ range?: Range;
16
+ from?: number;
17
+ to?: number;
18
+ }
17
19
  /**
18
- * 获取 Stylelint
20
+ * 获取 Wikitext LSP
19
21
  * @param opt 选项
22
+ * @param obj 对象
20
23
  */
21
- export declare const getCssLinter: getAsyncLinter<(text: string) => Promise<Warning[]>>;
24
+ export declare const getWikiLinter: getAsyncLinter<Promise<MixedDiagnostic[]>, Option, object>;
25
+ /** 获取 ESLint */
26
+ export declare const getJsLinter: getAsyncLinter<Linter.LintMessage[]>;
27
+ /** 获取 Stylelint */
28
+ export declare const getCssLinter: getAsyncLinter<Promise<Warning[]>>;
22
29
  /** 获取 Luacheck */
23
- export declare const getLuaLinter: getAsyncLinter<(text: string) => Promise<Diagnostic[]>>;
30
+ export declare const getLuaLinter: getAsyncLinter<Promise<Diagnostic[]>>;
24
31
  declare interface JsonError {
25
32
  message: string;
26
33
  severity: 'error';
@@ -29,5 +36,5 @@ declare interface JsonError {
29
36
  position: string | undefined;
30
37
  }
31
38
  /** JSON.parse */
32
- export declare const getJsonLinter: getLinter<(text: string) => JsonError[]>;
39
+ export declare const getJsonLinter: getLinter<JsonError[]>;
33
40
  export {};
package/dist/linter.mjs CHANGED
@@ -1,84 +1,72 @@
1
- import { CDN, loadScript } from "@bhsd/common";
2
- const getWikiLinter = async (opt) => {
3
- const REPO = "npm/wikiparser-node", DIR = `${REPO}/extensions/dist`, lang = opt == null ? void 0 : opt["i18n"];
1
+ import { loadScript, getLSP, sanitizeInlineStyle } from "@bhsd/common";
2
+ import { styleLint } from "@bhsd/common/dist/stylelint";
3
+ const offsetAt = (range, line, column) => {
4
+ if (line === -2) {
5
+ return range[0];
6
+ }
7
+ return line === 0 ? range[1] : range[0] + column;
8
+ };
9
+ const getWikiLinter = async (opt, obj) => {
10
+ const DIR = "npm/wikiparser-node/extensions/dist", lang = opt == null ? void 0 : opt["i18n"];
4
11
  await loadScript(`${DIR}/base.min.js`, "wikiparse");
5
- await loadScript(`${DIR}/lint.min.js`, "wikiparse.Linter");
12
+ await loadScript(`${DIR}/lsp.min.js`, "wikiparse.LanguageService");
6
13
  if (typeof lang === "string") {
7
14
  try {
8
- const i18n = await (await fetch(`${CDN}/${REPO}/i18n/${lang.toLowerCase()}.json`)).json();
15
+ const i18n = await (await fetch(`${wikiparse.CDN}/i18n/${lang.toLowerCase()}.json`)).json();
9
16
  wikiparse.setI18N(i18n);
10
17
  } catch {
11
18
  }
12
19
  }
13
- return new wikiparse.Linter(opt == null ? void 0 : opt["include"]);
20
+ const lsp = getLSP(obj, opt == null ? void 0 : opt["include"]);
21
+ return async (text, config) => {
22
+ const diagnostics = (await lsp.provideDiagnostics(text)).filter(
23
+ ({ code, severity }) => {
24
+ var _a;
25
+ return Number((_a = config == null ? void 0 : config[code]) != null ? _a : 2) > Number(severity === 2);
26
+ }
27
+ ), tokens = "findStyleTokens" in lsp && (config == null ? void 0 : config["invalid-css"]) !== "0" ? await lsp.findStyleTokens() : [];
28
+ if (tokens.length === 0) {
29
+ return diagnostics;
30
+ }
31
+ const cssLint = await getCssLinter();
32
+ return [
33
+ ...diagnostics,
34
+ ...(await cssLint(
35
+ tokens.map(({ childNodes, type, tag }, i) => `${type === "ext-attr" ? "div" : tag}#${i}{
36
+ ${sanitizeInlineStyle(childNodes[1].childNodes[0].data).replace(/\n/gu, " ")}
37
+ }`).join("\n")
38
+ )).map(({ line, column, endLine, endColumn, rule, severity, text: message }) => {
39
+ const i = Math.ceil(line / 3), { range } = tokens[i - 1].childNodes[1].childNodes[0], from = offsetAt(range, line - 3 * i, column - 1);
40
+ return {
41
+ from,
42
+ to: endLine === void 0 ? from : offsetAt(range, endLine - 3 * i, endColumn - 1),
43
+ severity: severity === "error" ? 1 : 2,
44
+ source: "Stylelint",
45
+ code: rule,
46
+ message
47
+ };
48
+ })
49
+ ];
50
+ };
14
51
  };
15
- const getJsLinter = async (opt) => {
16
- var _a, _b, _c;
52
+ const getJsLinter = async () => {
53
+ var _a;
17
54
  await loadScript("npm/eslint-linter-browserify@8.57.0/linter.min.js", "eslint", true);
18
55
  const esLinter = new eslint.Linter(), conf = {
19
56
  env: { browser: true, es2024: true },
20
57
  parserOptions: { ecmaVersion: 15, sourceType: "module" },
21
- rules: {},
22
- ...opt
58
+ rules: {}
23
59
  };
24
60
  for (const [name, { meta }] of esLinter.getRules()) {
25
61
  if ((_a = meta == null ? void 0 : meta.docs) == null ? void 0 : _a.recommended) {
26
- (_c = (_b = conf.rules)[name]) != null ? _c : _b[name] = 2;
62
+ conf.rules[name] = 2;
27
63
  }
28
64
  }
29
- return (text) => esLinter.verify(text, conf);
65
+ return (text, opt) => esLinter.verify(text, { ...conf, ...opt });
30
66
  };
31
- const getCssLinter = async (opt) => {
67
+ const getCssLinter = async () => {
32
68
  await loadScript("npm/stylelint-bundle", "stylelint");
33
- const config = {
34
- rules: {
35
- "annotation-no-unknown": true,
36
- "at-rule-no-unknown": true,
37
- "block-no-empty": true,
38
- "color-no-invalid-hex": true,
39
- "comment-no-empty": true,
40
- "custom-property-no-missing-var-function": true,
41
- "declaration-block-no-duplicate-custom-properties": true,
42
- "declaration-block-no-duplicate-properties": [
43
- true,
44
- {
45
- ignore: ["consecutive-duplicates-with-different-syntaxes"]
46
- }
47
- ],
48
- "declaration-block-no-shorthand-property-overrides": true,
49
- "font-family-no-duplicate-names": true,
50
- "font-family-no-missing-generic-family-keyword": true,
51
- "function-calc-no-unspaced-operator": true,
52
- "function-linear-gradient-no-nonstandard-direction": true,
53
- "function-no-unknown": true,
54
- "keyframe-block-no-duplicate-selectors": true,
55
- "keyframe-declaration-no-important": true,
56
- "media-feature-name-no-unknown": true,
57
- "media-query-no-invalid": true,
58
- "named-grid-areas-no-invalid": true,
59
- "no-descending-specificity": true,
60
- "no-duplicate-at-import-rules": true,
61
- "no-duplicate-selectors": true,
62
- "no-empty-source": true,
63
- "no-invalid-double-slash-comments": true,
64
- "no-invalid-position-at-import-rule": true,
65
- "no-irregular-whitespace": true,
66
- "property-no-unknown": true,
67
- "selector-anb-no-unmatchable": true,
68
- "selector-pseudo-class-no-unknown": true,
69
- "selector-pseudo-element-no-unknown": true,
70
- "selector-type-no-unknown": [
71
- true,
72
- {
73
- ignore: ["custom-elements"]
74
- }
75
- ],
76
- "string-no-newline": true,
77
- "unit-no-unknown": true,
78
- ...opt == null ? void 0 : opt["rules"]
79
- }
80
- };
81
- return async (code) => (await stylelint.lint({ code, config })).results.flatMap(({ warnings }) => warnings);
69
+ return (code, opt) => styleLint(stylelint, code, opt == null ? void 0 : opt["rules"]);
82
70
  };
83
71
  const getLuaLinter = async () => {
84
72
  await loadScript("npm/luacheck-browserify/dist/index.min.js", "luacheck");