@bhsd/codemirror-mediawiki 3.0.2 → 3.1.0

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.
package/dist/linter.js CHANGED
@@ -34,6 +34,11 @@ const indexToPos = (code, index) => {
34
34
  const lines = code.slice(0, index).split('\n');
35
35
  return { line: lines.length - 1, character: lines[lines.length - 1].length };
36
36
  };
37
+ /**
38
+ * 判断是否为 Stylelint 设置
39
+ * @param config 设置
40
+ */
41
+ const isStylelintConfig = (config) => Boolean(config && ('extends' in config || 'rules' in config));
37
42
  /**
38
43
  * 获取 Wikitext LSP
39
44
  * @param opt 选项
@@ -42,16 +47,21 @@ const indexToPos = (code, index) => {
42
47
  export const getWikiLinter = async (opt, obj) => {
43
48
  await getWikiparse(opt?.['getConfig'], opt?.['i18n']);
44
49
  const lsp = getLSP(obj, opt?.['include']), cssLint = await getCssLinter();
45
- return async (text, config) => {
50
+ const linter = async (text, config) => {
46
51
  const defaultSeverity = config?.['defaultSeverity'] ?? 2, diagnostics = (await lsp.provideDiagnostics(text)).filter(({ code, severity }) => Number(config?.[code] ?? defaultSeverity) > Number(severity === 2)), tokens = 'findStyleTokens' in lsp && config?.['invalid-css'] !== '0' ? await lsp.findStyleTokens() : [];
47
52
  if (tokens.length === 0) {
48
53
  return diagnostics;
49
54
  }
50
55
  const lines = tokens.map((token, i) => `${getPrefix(token, i)}${sanitizeInlineStyle(token.childNodes[1].childNodes[0].data)
51
- .replace(/\n/gu, ' ')}\n}`);
56
+ .replace(/\n/gu, ' ')}\n}`), cssConfig = config?.['css'], isConfig = isStylelintConfig(cssConfig), rules = {};
57
+ for (const [key, value] of Object.entries((isConfig ? cssConfig.rules : cssConfig) ?? {})) {
58
+ if (!value) {
59
+ rules[key] = value;
60
+ }
61
+ }
52
62
  return [
53
63
  ...diagnostics,
54
- ...(await cssLint(lines.join('\n'), config?.['css']))
64
+ ...(await cssLint(lines.join('\n'), isConfig ? { ...cssConfig, rules } : rules))
55
65
  .map(({ line, column, endLine, endColumn, rule, severity, text: message, fix }) => {
56
66
  const i = Math.ceil(line / 3), { length } = getPrefix(tokens[i - 1], i), { range } = tokens[i - 1].childNodes[1].childNodes[0], from = offsetAt(range, line - 3 * i, column - 1), diagnostic = {
57
67
  from,
@@ -79,6 +89,10 @@ export const getWikiLinter = async (opt, obj) => {
79
89
  }),
80
90
  ];
81
91
  };
92
+ if ('resolveCodeAction' in lsp) {
93
+ linter.fixer = async (_, rule) => (await lsp.resolveCodeAction(rule)).edit.changes[''][0].newText;
94
+ }
95
+ return linter;
82
96
  };
83
97
  export const jsConfig = /* #__PURE__ */ (() => ({
84
98
  env: { browser: true, es2024: true, jquery: true },
@@ -131,9 +145,7 @@ export const getCssLinter = async (cdn = 'npm/@bhsd/stylelint-browserify') => {
131
145
  await loadScript(cdn, 'stylelint');
132
146
  const linter = async (code, opt) => {
133
147
  const warnings = await styleLint(stylelint, code, opt);
134
- if (opt && 'rules' in opt) {
135
- linter.config = opt;
136
- }
148
+ linter.config = opt && !isStylelintConfig(opt) ? { rules: opt } : opt;
137
149
  return warnings;
138
150
  };
139
151
  linter.fixer = (code, rule) => {
@@ -27,7 +27,7 @@ const pos = (doc, line, column, from = 0) => {
27
27
  };
28
28
  export const getWikiLintSource = async (opt, v) => {
29
29
  const wikiLint = await getWikiLinter(await getOpt(opt), v);
30
- return async ({ doc }) => (await wikiLint(doc.toString(), await getOpt(opt, true)))
30
+ const lintSource = async ({ doc }) => (await wikiLint(doc.toString(), await getOpt(opt, true)))
31
31
  .map(({ severity, code, message, range: r, from, to, data = [], source }) => ({
32
32
  source: source,
33
33
  from: from ?? posToIndex(doc, r.start),
@@ -47,6 +47,10 @@ export const getWikiLintSource = async (opt, v) => {
47
47
  },
48
48
  })),
49
49
  }));
50
+ if (wikiLint.fixer) {
51
+ lintSource.fixer = (_, rule) => wikiLint.fixer('', rule);
52
+ }
53
+ return lintSource;
50
54
  };
51
55
  const getRange = (doc, line, column, endLine, endColumn, f = 0, t = Infinity) => {
52
56
  const start = pos(doc, line, column, f);