wikilint 2.9.1 → 2.9.2

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/index.js CHANGED
@@ -43,20 +43,19 @@ const Parser = {
43
43
  return new Title(title, defaultNs, config, decode, selfLink);
44
44
  }
45
45
  const { Token } = require('./src/index');
46
- const token = debug_1.Shadow.run(() => {
46
+ return debug_1.Shadow.run(() => {
47
47
  const root = new Token(title, config);
48
48
  root.type = 'root';
49
- return root.parseOnce(0, include).parseOnce();
50
- }), titleObj = new Title(token.toString(), defaultNs, config, decode, selfLink);
51
- debug_1.Shadow.run(() => {
49
+ root.parseOnce(0, include).parseOnce();
50
+ const titleObj = new Title(root.toString(), defaultNs, config, decode, selfLink);
52
51
  for (const key of ['main', 'fragment']) {
53
52
  const str = titleObj[key];
54
53
  if (str?.includes('\0')) {
55
- titleObj[key] = token.buildFromStr(str, constants_1.BuildMethod.Text);
54
+ titleObj[key] = root.buildFromStr(str, constants_1.BuildMethod.Text);
56
55
  }
57
56
  }
57
+ return titleObj;
58
58
  });
59
- return titleObj;
60
59
  },
61
60
  /** @implements */
62
61
  parse(wikitext, include, maxStage = constants_1.MAX_STAGE, config = Parser.getConfig()) {
package/dist/lib/node.js CHANGED
@@ -52,10 +52,7 @@ class AstNode {
52
52
  }
53
53
  /** @private */
54
54
  getAttribute(key) {
55
- if (key === 'padding') {
56
- return 0;
57
- }
58
- return this[key];
55
+ return (key === 'padding' ? 0 : this[key]);
59
56
  }
60
57
  /** @private */
61
58
  setAttribute(key, value) {
@@ -19,14 +19,14 @@ const closes = {
19
19
  * @throws TranscludeToken.constructor()
20
20
  */
21
21
  const parseBraces = (wikitext, config, accum) => {
22
- const source = String.raw `${config.excludes?.includes('heading') ? '' : String.raw `^(\0\d+c\x7F)*={1,6}|`}\[\[|\{{2,}|-\{(?!\{)`, { parserFunction: [, , , subst] } = config, stack = [];
22
+ const source = String.raw `${config.excludes?.includes('heading') ? '' : String.raw `^(\0\d+c\x7F)*={1,6}|`}\[\[|-\{(?!\{)`, openBraces = String.raw `|\{{2,}`, { parserFunction: [, , , subst] } = config, stack = [];
23
23
  wikitext = wikitext.replace(re, (m, p1) => {
24
24
  // @ts-expect-error abstract class
25
25
  new transclude_1.TranscludeToken(m.slice(2, -2), [], config, accum);
26
26
  return `\0${accum.length - 2}${marks.get(p1)}\x7F`;
27
27
  });
28
28
  const lastBraces = wikitext.lastIndexOf('}}') - wikitext.length;
29
- let regex = new RegExp(source, 'gmu'), mt = regex.exec(wikitext), moreBraces = lastBraces + wikitext.length !== -1, lastIndex;
29
+ let moreBraces = lastBraces + wikitext.length !== -1, regex = new RegExp(source + (moreBraces ? openBraces : ''), 'gmu'), mt = regex.exec(wikitext), lastIndex;
30
30
  while (mt
31
31
  || lastIndex !== undefined && lastIndex <= wikitext.length && stack[stack.length - 1]?.[0]?.startsWith('=')) {
32
32
  if (mt?.[1]) {
@@ -125,13 +125,17 @@ const parseBraces = (wikitext, config, accum) => {
125
125
  }
126
126
  stack.push(...'0' in top ? [top] : [], mt);
127
127
  }
128
- moreBraces &&= lastBraces + wikitext.length >= lastIndex;
129
128
  let curTop = stack[stack.length - 1];
130
- if (!moreBraces && curTop?.[0]?.startsWith('{')) {
131
- stack.pop();
132
- curTop = stack[stack.length - 1];
129
+ if (moreBraces && lastBraces + wikitext.length < lastIndex) {
130
+ moreBraces = false;
131
+ while (curTop?.[0]?.startsWith('{')) {
132
+ stack.pop();
133
+ curTop = stack[stack.length - 1];
134
+ }
133
135
  }
134
- regex = new RegExp(source + (curTop ? `|${closes[curTop[0][0]]}${curTop.findEqual ? '|=' : ''}` : ''), 'gmu');
136
+ regex = new RegExp(source
137
+ + (moreBraces ? openBraces : '')
138
+ + (curTop ? `|${closes[curTop[0][0]]}${curTop.findEqual ? '|=' : ''}` : ''), 'gmu');
135
139
  regex.lastIndex = lastIndex;
136
140
  mt = regex.exec(wikitext);
137
141
  }
@@ -235,6 +235,7 @@ class AttributeToken extends index_2.Token {
235
235
  this.#tag = this.parentNode.name;
236
236
  }
237
237
  this.setAttribute('name', this.firstChild.text().trim().toLowerCase());
238
+ super.afterBuild();
238
239
  }
239
240
  /** @private */
240
241
  toString() {
@@ -64,10 +64,11 @@ class AttributesToken extends index_2.Token {
64
64
  }
65
65
  /** @private */
66
66
  afterBuild() {
67
- if (this.type === 'table-attrs') {
68
- const { parentNode } = this;
69
- this.setAttribute('name', parentNode?.type === 'td' && parentNode.subtype === 'caption' ? 'caption' : parentNode?.type);
67
+ const { parentNode } = this;
68
+ if (parentNode?.type === 'td' && parentNode.subtype === 'caption') {
69
+ this.setAttribute('name', 'caption');
70
70
  }
71
+ super.afterBuild();
71
72
  }
72
73
  /**
73
74
  * 所有指定属性名的AttributeToken
@@ -22,6 +22,7 @@ class ConverterFlagsToken extends index_2.Token {
22
22
  /** @private */
23
23
  afterBuild() {
24
24
  this.#flags = this.childNodes.map(child => child.text().trim());
25
+ super.afterBuild();
25
26
  }
26
27
  /** @private */
27
28
  toString() {
@@ -81,6 +81,7 @@ class ImageParameterToken extends index_2.Token {
81
81
  if (this.parentNode?.type === 'gallery-image' && !exports.galleryParams.has(this.name)) {
82
82
  this.setAttribute('name', 'invalid');
83
83
  }
84
+ super.afterBuild();
84
85
  }
85
86
  /** @private */
86
87
  toString() {
package/dist/src/index.js CHANGED
@@ -43,6 +43,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
43
43
  exports.Token = void 0;
44
44
  const string_1 = require("../util/string");
45
45
  const constants_1 = require("../util/constants");
46
+ const debug_1 = require("../util/debug");
46
47
  const lint_1 = require("../util/lint");
47
48
  const index_1 = require("../index");
48
49
  const element_1 = require("../lib/element");
@@ -147,8 +148,8 @@ class Token extends element_1.AstElement {
147
148
  }
148
149
  return nodes;
149
150
  }
150
- /** 将占位符替换为子Token */
151
- #build() {
151
+ /** @private */
152
+ build() {
152
153
  this.#stage = constants_1.MAX_STAGE;
153
154
  const { length, firstChild } = this, str = String(firstChild);
154
155
  if (length === 1 && firstChild.type === 'text' && str.includes('\0')) {
@@ -156,7 +157,7 @@ class Token extends element_1.AstElement {
156
157
  this.normalize();
157
158
  if (this.type === 'root') {
158
159
  for (const token of this.#accum) {
159
- token.#build();
160
+ token.build();
160
161
  }
161
162
  }
162
163
  }
@@ -177,7 +178,7 @@ class Token extends element_1.AstElement {
177
178
  this.parseOnce(this.#stage, include);
178
179
  }
179
180
  if (n) {
180
- this.#build();
181
+ this.build();
181
182
  this.afterBuild();
182
183
  }
183
184
  return this;
@@ -293,6 +294,8 @@ class Token extends element_1.AstElement {
293
294
  return (this.#include ?? Boolean(this.getRootNode().#include));
294
295
  case 'accum':
295
296
  return this.#accum;
297
+ case 'built':
298
+ return this.#built;
296
299
  default:
297
300
  return super.getAttribute(key);
298
301
  }
@@ -393,11 +396,11 @@ class Token extends element_1.AstElement {
393
396
  }
394
397
  /** @private */
395
398
  toString(separator) {
396
- const root = this.getRootNode();
399
+ const { rev } = debug_1.Shadow, root = this.getRootNode();
397
400
  if (root.type === 'root'
398
401
  && root.#built) {
399
- this.#string ??= super.toString(separator);
400
- return this.#string;
402
+ this.#string ??= [rev, super.toString(separator)];
403
+ return this.#string[1];
401
404
  }
402
405
  return super.toString(separator);
403
406
  }
@@ -38,6 +38,7 @@ class LinkBaseToken extends index_2.Token {
38
38
  this.#delimiter = this.buildFromStr(this.#delimiter, constants_1.BuildMethod.String);
39
39
  }
40
40
  this.setAttribute('name', this.#title.title);
41
+ super.afterBuild();
41
42
  }
42
43
  /** @private */
43
44
  setAttribute(key, value) {
@@ -54,6 +54,7 @@ class GalleryImageToken extends file_1.FileToken {
54
54
  /** @private */
55
55
  afterBuild() {
56
56
  this.#setName(this.getTitle());
57
+ super.afterBuild();
57
58
  }
58
59
  }
59
60
  exports.GalleryImageToken = GalleryImageToken;
@@ -42,6 +42,7 @@ class ParameterToken extends index_2.Token {
42
42
  parentNode.getArgs(name, false, false).add(this);
43
43
  }
44
44
  }
45
+ super.afterBuild();
45
46
  }
46
47
  /** @private */
47
48
  toString() {
@@ -3,18 +3,20 @@ import { Token } from '../index';
3
3
  import { SyntaxToken } from '../syntax';
4
4
  import { AttributesToken } from '../attributes';
5
5
  import type { AttributesParentBase } from '../../mixin/attributesParent';
6
+ declare type TableTypes = 'table' | 'tr' | 'td';
6
7
  export interface TableBaseToken extends AttributesParentBase {
7
8
  }
8
9
  export declare abstract class TableBaseToken extends Token {
9
- type: 'table' | 'tr' | 'td';
10
+ type: TableTypes;
10
11
  readonly childNodes: readonly [SyntaxToken, AttributesToken, ...Token[]];
11
12
  abstract get firstChild(): SyntaxToken;
12
13
  abstract get lastChild(): Token;
13
14
  /**
14
15
  * @param pattern 表格语法正则
15
16
  * @param syntax 表格语法
17
+ * @param type 节点类型
16
18
  * @param attr 表格属性
17
19
  */
18
- constructor(pattern: RegExp, syntax?: string, attr?: string, config?: Parser.Config, accum?: Token[], acceptable?: Acceptable);
20
+ constructor(pattern: RegExp, syntax: string, type: TableTypes, attr?: string, config?: Parser.Config, accum?: Token[], acceptable?: Acceptable);
19
21
  }
20
22
  export {};
@@ -14,13 +14,14 @@ class TableBaseToken extends (0, attributesParent_1.attributesParent)(1)(index_2
14
14
  /**
15
15
  * @param pattern 表格语法正则
16
16
  * @param syntax 表格语法
17
+ * @param type 节点类型
17
18
  * @param attr 表格属性
18
19
  */
19
- constructor(pattern, syntax, attr, config = index_1.default.getConfig(), accum = [], acceptable = {}) {
20
+ constructor(pattern, syntax, type, attr, config = index_1.default.getConfig(), accum = [], acceptable = {}) {
20
21
  super(undefined, config, accum, acceptable);
21
22
  this.append(new syntax_1.SyntaxToken(syntax, pattern, 'table-syntax', config, accum, {}),
22
23
  // @ts-expect-error abstract class
23
- new attributes_1.AttributesToken(attr, 'table-attrs', this.type, config, accum));
24
+ new attributes_1.AttributesToken(attr, 'table-attrs', type, config, accum));
24
25
  }
25
26
  }
26
27
  exports.TableBaseToken = TableBaseToken;
@@ -33,7 +33,7 @@ class TableToken extends trBase_1.TrBaseToken {
33
33
  * @param attr 表格属性
34
34
  */
35
35
  constructor(syntax, attr, config, accum) {
36
- super(/^(?:\{\||\{\{\{\s*!\s*\}\}|\{\{\s*\(!\s*\}\})$/u, syntax, attr, config, accum, {});
36
+ super(/^(?:\{\||\{\{\{\s*!\s*\}\}|\{\{\s*\(!\s*\}\})$/u, syntax, 'table', attr, config, accum, {});
37
37
  }
38
38
  /** @private */
39
39
  lint(start = this.getAbsoluteIndex(), re) {
@@ -73,10 +73,10 @@ class TableToken extends trBase_1.TrBaseToken {
73
73
  /** @private */
74
74
  close(syntax = '\n|}', halfParsed) {
75
75
  const config = this.getAttribute('config'), accum = this.getAttribute('accum'), inner = halfParsed ? [syntax] : index_1.default.parse(syntax, this.getAttribute('include'), 2, config).childNodes;
76
- const token = debug_1.Shadow.run(() => super.insertAt(new syntax_1.SyntaxToken(undefined, closingPattern, 'table-syntax', config, accum, {})));
77
- if (!halfParsed) {
78
- token.afterBuild();
79
- }
76
+ debug_1.Shadow.run(() => {
77
+ const token = new syntax_1.SyntaxToken(undefined, closingPattern, 'table-syntax', config, accum, {});
78
+ super.insertAt(token);
79
+ });
80
80
  this.lastChild.replaceChildren(...inner);
81
81
  }
82
82
  /**
@@ -4,6 +4,7 @@ exports.TdToken = void 0;
4
4
  const lint_1 = require("../../util/lint");
5
5
  const rect_1 = require("../../lib/rect");
6
6
  const constants_1 = require("../../util/constants");
7
+ const debug_1 = require("../../util/debug");
7
8
  const index_1 = require("../../index");
8
9
  const index_2 = require("../index");
9
10
  const base_1 = require("./base");
@@ -37,7 +38,7 @@ class TdToken extends base_1.TableBaseToken {
37
38
  innerSyntax = null;
38
39
  attr = '';
39
40
  }
40
- super(/^(?:\n[^\S\n]*(?:[|!]|\|\+|\{\{\s*!\s*\}\}\+?)|(?:\||\{\{\s*!\s*\}\}){2}|!!|\{\{\s*!!\s*\}\})$/u, syntax, attr, config, accum);
41
+ super(/^(?:\n[^\S\n]*(?:[|!]|\|\+|\{\{\s*!\s*\}\}\+?)|(?:\||\{\{\s*!\s*\}\}){2}|!!|\{\{\s*!!\s*\}\})$/u, syntax, 'td', attr, config, accum);
41
42
  if (innerSyntax) {
42
43
  [this.#innerSyntax] = innerSyntax;
43
44
  }
@@ -48,8 +49,9 @@ class TdToken extends base_1.TableBaseToken {
48
49
  }
49
50
  /** 表格语法信息 */
50
51
  #getSyntax() {
51
- this.#syntax ??= this.#computeSyntax();
52
- return this.#syntax;
52
+ const { rev } = debug_1.Shadow;
53
+ this.#syntax ??= [rev, this.#computeSyntax()];
54
+ return this.#syntax[1];
53
55
  }
54
56
  /** 表格语法信息 */
55
57
  #computeSyntax() {
@@ -75,6 +77,7 @@ class TdToken extends base_1.TableBaseToken {
75
77
  if (this.#innerSyntax.includes('\0')) {
76
78
  this.#innerSyntax = this.buildFromStr(this.#innerSyntax, constants_1.BuildMethod.String);
77
79
  }
80
+ super.afterBuild();
78
81
  }
79
82
  /** @private */
80
83
  toString() {
@@ -13,7 +13,7 @@ class TrToken extends trBase_1.TrBaseToken {
13
13
  * @param attr 表格属性
14
14
  */
15
15
  constructor(syntax, attr, config, accum) {
16
- super(/^\n[^\S\n]*(?:\|-+|\{\{\s*!\s*\}\}-+|\{\{\s*!-\s*\}\}-*)$/u, syntax, attr, config, accum, {});
16
+ super(/^\n[^\S\n]*(?:\|-+|\{\{\s*!\s*\}\}-+|\{\{\s*!-\s*\}\}-*)$/u, syntax, 'tr', attr, config, accum, {});
17
17
  }
18
18
  }
19
19
  exports.TrToken = TrToken;
@@ -128,6 +128,7 @@ class TranscludeToken extends index_2.Token {
128
128
  if (this.modifier.includes('\0')) {
129
129
  this.setAttribute('modifier', this.buildFromStr(this.modifier, constants_1.BuildMethod.String));
130
130
  }
131
+ super.afterBuild();
131
132
  }
132
133
  /** @private */
133
134
  toString() {
@@ -8,7 +8,11 @@ exports.Shadow = {
8
8
  const { running } = this;
9
9
  this.running = true;
10
10
  try {
11
+ const { Token: AnyToken } = require('../src/index');
11
12
  const result = callback();
13
+ if (result instanceof AnyToken && !result.getAttribute('built')) {
14
+ result.afterBuild();
15
+ }
12
16
  this.running = running;
13
17
  return result;
14
18
  }
@@ -17,6 +21,7 @@ exports.Shadow = {
17
21
  throw e;
18
22
  }
19
23
  },
24
+ rev: 0,
20
25
  };
21
26
  /**
22
27
  * 是否是某一特定类型的节点
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikilint",
3
- "version": "2.9.1",
3
+ "version": "2.9.2",
4
4
  "description": "A Node.js linter for MediaWiki markup",
5
5
  "keywords": [
6
6
  "mediawiki",
@@ -24,7 +24,7 @@
24
24
  "!/dist/bin/declaration.js"
25
25
  ],
26
26
  "bin": {
27
- "wikilint": "./bin/cli.js"
27
+ "wikilint": "bin/cli.js"
28
28
  },
29
29
  "main": "./dist/index.js",
30
30
  "types": "./dist/index.d.ts",