markuplint 2.0.0-rc.3 → 2.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.
Files changed (71) hide show
  1. package/README.md +87 -30
  2. package/lib/api/index.d.mts +2 -0
  3. package/lib/api/index.mjs +2 -0
  4. package/lib/api/lint.d.mts +4 -0
  5. package/lib/api/lint.mjs +15 -0
  6. package/lib/api/ml-engine.d.mts +27 -0
  7. package/lib/api/ml-engine.js +7 -3
  8. package/lib/api/ml-engine.mjs +232 -0
  9. package/lib/api/types.d.mts +43 -0
  10. package/lib/api/types.d.ts +1 -1
  11. package/lib/api/types.mjs +1 -0
  12. package/lib/api/v1.d.mts +51 -0
  13. package/lib/api/v1.d.ts +0 -9
  14. package/lib/api/v1.js +0 -1
  15. package/lib/api/v1.mjs +39 -0
  16. package/lib/cli/bootstrap.d.mts +52 -0
  17. package/lib/cli/bootstrap.d.ts +24 -9
  18. package/lib/cli/bootstrap.js +39 -20
  19. package/lib/cli/bootstrap.mjs +81 -0
  20. package/lib/cli/command.d.mts +3 -0
  21. package/lib/cli/command.js +15 -2
  22. package/lib/cli/command.mjs +50 -0
  23. package/lib/cli/create-rule/index.d.mts +1 -0
  24. package/lib/cli/create-rule/index.js +4 -1
  25. package/lib/cli/create-rule/index.mjs +51 -0
  26. package/lib/cli/index.d.mts +1 -0
  27. package/lib/cli/index.mjs +64 -0
  28. package/lib/cli/index.spec.d.mts +1 -0
  29. package/lib/cli/index.spec.mjs +48 -0
  30. package/lib/cli/init/index.d.mts +1 -0
  31. package/lib/cli/init/index.js +92 -55
  32. package/lib/cli/init/index.mjs +266 -0
  33. package/lib/cli/init/install-module.d.mts +5 -0
  34. package/lib/cli/init/install-module.mjs +61 -0
  35. package/lib/cli/output.d.mts +3 -0
  36. package/lib/cli/output.mjs +31 -0
  37. package/lib/cli/prompt.d.mts +18 -0
  38. package/lib/cli/prompt.mjs +68 -0
  39. package/lib/debug.d.mts +3 -0
  40. package/lib/debug.mjs +10 -0
  41. package/lib/global-settings.d.mts +5 -0
  42. package/lib/global-settings.mjs +10 -0
  43. package/lib/i18n.d.mts +6 -0
  44. package/lib/i18n.mjs +19 -0
  45. package/lib/index.d.mts +8 -0
  46. package/lib/index.mjs +8 -0
  47. package/lib/reporter/index.d.mts +2 -0
  48. package/lib/reporter/index.mjs +2 -0
  49. package/lib/reporter/simple-reporter.d.mts +3 -0
  50. package/lib/reporter/simple-reporter.js +7 -7
  51. package/lib/reporter/simple-reporter.mjs +30 -0
  52. package/lib/reporter/standard-reporter.d.mts +3 -0
  53. package/lib/reporter/standard-reporter.js +10 -10
  54. package/lib/reporter/standard-reporter.mjs +43 -0
  55. package/lib/testing-tool/index.d.mts +35 -0
  56. package/lib/testing-tool/index.js +2 -0
  57. package/lib/testing-tool/index.mjs +83 -0
  58. package/lib/types.d.mts +27 -0
  59. package/lib/types.mjs +1 -0
  60. package/lib/util.d.mts +19 -0
  61. package/lib/util.d.ts +16 -0
  62. package/lib/util.js +75 -1
  63. package/lib/util.mjs +73 -0
  64. package/lib/v1.d.mts +4 -0
  65. package/lib/v1.mjs +4 -0
  66. package/media/screenshot01.png +0 -0
  67. package/package.json +14 -14
  68. package/tsconfig.tsbuildinfo +1 -1
  69. package/RESOLVE_CONFIG.md +0 -77
  70. package/lib/reporter/utils.d.ts +0 -6
  71. package/lib/reporter/utils.js +0 -40
@@ -0,0 +1,68 @@
1
+ import c from 'cli-color';
2
+ import enquirer from 'enquirer';
3
+ const { prompt } = enquirer;
4
+ export async function select(question) {
5
+ const res = await prompt({
6
+ ...question,
7
+ name: '__Q__',
8
+ type: 'select',
9
+ result(resName) {
10
+ // @ts-ignore
11
+ return this.options.choices.find(c => c.name === resName)?.value;
12
+ },
13
+ });
14
+ // @ts-ignore
15
+ return res['__Q__'];
16
+ }
17
+ export async function multiSelect(question) {
18
+ const res = await prompt({
19
+ ...question,
20
+ name: '__Q__',
21
+ type: 'multiselect',
22
+ result(names) {
23
+ // @ts-ignore
24
+ const map = this.map(names);
25
+ // @ts-ignore
26
+ const values = names.map(name => map[name]);
27
+ return values;
28
+ },
29
+ });
30
+ // @ts-ignore
31
+ return res['__Q__'];
32
+ }
33
+ export async function input(question, validation) {
34
+ // eslint-disable-next-line no-constant-condition
35
+ while (true) {
36
+ const _res = await prompt({
37
+ message: question,
38
+ name: '__Q__',
39
+ type: 'input',
40
+ });
41
+ // @ts-ignore
42
+ const res = _res['__Q__'];
43
+ if (validation && !validation.test(res)) {
44
+ process.stdout.write(c.yellow('Oops! The name that you type is an invalid format.\n'));
45
+ continue;
46
+ }
47
+ return res;
48
+ }
49
+ }
50
+ export async function confirm(question, options) {
51
+ const res = await prompt({
52
+ message: question,
53
+ name: '__Q__',
54
+ type: 'confirm',
55
+ initial: !!options?.initial,
56
+ });
57
+ // @ts-ignore
58
+ return !!res['__Q__'];
59
+ }
60
+ export async function confirmSequence(questions) {
61
+ const res = await prompt(questions.map(question => {
62
+ return {
63
+ ...question,
64
+ type: 'confirm',
65
+ };
66
+ }));
67
+ return res;
68
+ }
@@ -0,0 +1,3 @@
1
+ import debug from 'debug';
2
+ export declare const log: debug.Debugger;
3
+ export declare function verbosely(): void;
package/lib/debug.mjs ADDED
@@ -0,0 +1,10 @@
1
+ import { enableDebug } from '@markuplint/ml-core';
2
+ import debug from 'debug';
3
+ export const log = debug('markuplint-cli');
4
+ export function verbosely() {
5
+ if (!log.enabled) {
6
+ debug.enable(`${log.namespace}*`);
7
+ log(`Debug enable: ${log.namespace}`);
8
+ }
9
+ enableDebug();
10
+ }
@@ -0,0 +1,5 @@
1
+ export declare type GlobalSettings = {
2
+ locale: string;
3
+ };
4
+ export declare function setGlobal(settings: Partial<GlobalSettings>): void;
5
+ export declare function getGlobal(): Readonly<Partial<GlobalSettings>>;
@@ -0,0 +1,10 @@
1
+ let globalSettings = {};
2
+ export function setGlobal(settings) {
3
+ globalSettings = {
4
+ ...globalSettings,
5
+ ...settings,
6
+ };
7
+ }
8
+ export function getGlobal() {
9
+ return globalSettings;
10
+ }
package/lib/i18n.d.mts ADDED
@@ -0,0 +1,6 @@
1
+ export declare function i18n(locale?: string): Promise<{
2
+ locale: string;
3
+ listFormat?: import("packages/@markuplint/i18n/src/types.js").ListFormat | undefined;
4
+ keywords?: import("packages/@markuplint/i18n/src/types.js").LocalesKeywords | undefined;
5
+ sentences?: import("packages/@markuplint/i18n/src/types.js").LocalesKeywords | undefined;
6
+ }>;
package/lib/i18n.mjs ADDED
@@ -0,0 +1,19 @@
1
+ import { osLocale } from 'os-locale';
2
+ let cachedLocale = null;
3
+ async function getLocale() {
4
+ if (!cachedLocale) {
5
+ cachedLocale = await osLocale({ spawn: true });
6
+ }
7
+ return cachedLocale;
8
+ }
9
+ export async function i18n(locale) {
10
+ locale = locale || (await getLocale()) || 'en';
11
+ const langCode = locale.split('-')[0];
12
+ const localeSet = langCode
13
+ ? await import(`@markuplint/i18n/locales/${langCode}`).catch(() => null)
14
+ : null;
15
+ return {
16
+ locale: langCode,
17
+ ...localeSet,
18
+ };
19
+ }
@@ -0,0 +1,8 @@
1
+ export { MLEngine } from './api/index.mjs';
2
+ export * from './i18n.mjs';
3
+ export * from './testing-tool/index.mjs';
4
+ export * from './types.mjs';
5
+ /**
6
+ * @deprecated
7
+ */
8
+ export * from './v1.mjs';
package/lib/index.mjs ADDED
@@ -0,0 +1,8 @@
1
+ export { MLEngine } from './api/index.mjs';
2
+ export * from './i18n.mjs';
3
+ export * from './testing-tool/index.mjs';
4
+ export * from './types.mjs';
5
+ /**
6
+ * @deprecated
7
+ */
8
+ export * from './v1.mjs';
@@ -0,0 +1,2 @@
1
+ export * from './standard-reporter.mjs';
2
+ export * from './simple-reporter.mjs';
@@ -0,0 +1,2 @@
1
+ export * from './standard-reporter.mjs';
2
+ export * from './simple-reporter.mjs';
@@ -0,0 +1,3 @@
1
+ import type { CLIOptions } from '../cli/bootstrap.mjs';
2
+ import type { MLResultInfo } from '../types.mjs';
3
+ export declare function simpleReporter(results: MLResultInfo, options: CLIOptions): string[];
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.simpleReporter = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const cli_color_1 = (0, tslib_1.__importDefault)(require("cli-color"));
6
- const utils_1 = require("./utils");
6
+ const util_1 = require("../util");
7
7
  const loggerError = cli_color_1.default.red;
8
8
  const loggerWarning = cli_color_1.default.xterm(208);
9
9
  function simpleReporter(results, options) {
@@ -15,20 +15,20 @@ function simpleReporter(results, options) {
15
15
  for (const violation of results.violations) {
16
16
  sizes.line = Math.max(sizes.line, violation.line.toString(10).length);
17
17
  sizes.col = Math.max(sizes.col, violation.col.toString(10).length);
18
- const meg = (0, utils_1.messageToString)(violation.message, violation.reason);
19
- sizes.meg = Math.max(sizes.meg, (0, utils_1.w)(meg));
18
+ const meg = (0, util_1.messageToString)(violation.message, violation.reason);
19
+ sizes.meg = Math.max(sizes.meg, (0, util_1.w)(meg));
20
20
  }
21
21
  const out = [];
22
22
  if (results.violations.length) {
23
- out.push(`<${utils_1.markuplint}> ${cli_color_1.default.underline(results.filePath)}: ${loggerError('✗')}`);
23
+ out.push(`<${util_1.markuplint}> ${cli_color_1.default.underline(results.filePath)}: ${loggerError('✗')}`);
24
24
  for (const violation of results.violations) {
25
25
  const s = violation.severity === 'error' ? loggerError('✖') : loggerWarning('⚠️');
26
- const meg = (0, utils_1.messageToString)(violation.message, violation.reason);
27
- out.push(` ${cli_color_1.default.cyan(`${(0, utils_1.p)(violation.line, sizes.line, true)}:${(0, utils_1.p)(violation.col, sizes.col)}`)} ${s} ${(0, utils_1.p)(meg, sizes.meg)} ${cli_color_1.default.xterm(8)(violation.ruleId)} `);
26
+ const meg = (0, util_1.messageToString)(violation.message, violation.reason);
27
+ out.push(` ${cli_color_1.default.cyan(`${(0, util_1.p)(violation.line, sizes.line, true)}:${(0, util_1.p)(violation.col, sizes.col)}`)} ${s} ${(0, util_1.p)(meg, sizes.meg)} ${cli_color_1.default.xterm(8)(violation.ruleId)} `);
28
28
  }
29
29
  }
30
30
  else if (!options.problemOnly) {
31
- out.push(`<${utils_1.markuplint}> ${cli_color_1.default.underline(results.filePath)}: ${cli_color_1.default.green('✓')}`);
31
+ out.push(`<${util_1.markuplint}> ${cli_color_1.default.underline(results.filePath)}: ${cli_color_1.default.green('✓')}`);
32
32
  }
33
33
  return out;
34
34
  }
@@ -0,0 +1,30 @@
1
+ import c from 'cli-color';
2
+ import { markuplint, messageToString, p, w } from '../util.mjs';
3
+ const loggerError = c.red;
4
+ const loggerWarning = c.xterm(208);
5
+ export function simpleReporter(results, options) {
6
+ const sizes = {
7
+ line: 0,
8
+ col: 0,
9
+ meg: 0,
10
+ };
11
+ for (const violation of results.violations) {
12
+ sizes.line = Math.max(sizes.line, violation.line.toString(10).length);
13
+ sizes.col = Math.max(sizes.col, violation.col.toString(10).length);
14
+ const meg = messageToString(violation.message, violation.reason);
15
+ sizes.meg = Math.max(sizes.meg, w(meg));
16
+ }
17
+ const out = [];
18
+ if (results.violations.length) {
19
+ out.push(`<${markuplint}> ${c.underline(results.filePath)}: ${loggerError('✗')}`);
20
+ for (const violation of results.violations) {
21
+ const s = violation.severity === 'error' ? loggerError('✖') : loggerWarning('⚠️');
22
+ const meg = messageToString(violation.message, violation.reason);
23
+ out.push(` ${c.cyan(`${p(violation.line, sizes.line, true)}:${p(violation.col, sizes.col)}`)} ${s} ${p(meg, sizes.meg)} ${c.xterm(8)(violation.ruleId)} `);
24
+ }
25
+ }
26
+ else if (!options.problemOnly) {
27
+ out.push(`<${markuplint}> ${c.underline(results.filePath)}: ${c.green('✓')}`);
28
+ }
29
+ return out;
30
+ }
@@ -0,0 +1,3 @@
1
+ import type { CLIOptions } from '../cli/bootstrap.mjs';
2
+ import type { MLResultInfo } from '../types.mjs';
3
+ export declare function standardReporter(results: MLResultInfo, options: CLIOptions): string[];
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.standardReporter = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const cli_color_1 = (0, tslib_1.__importDefault)(require("cli-color"));
6
- const utils_1 = require("./utils");
6
+ const util_1 = require("../util");
7
7
  const loggerError = cli_color_1.default.red;
8
8
  const loggerWarning = cli_color_1.default.xterm(208);
9
9
  function standardReporter(results, options) {
@@ -15,8 +15,8 @@ function standardReporter(results, options) {
15
15
  for (const violation of results.violations) {
16
16
  sizes.line = Math.max(sizes.line, violation.line.toString(10).length);
17
17
  sizes.col = Math.max(sizes.col, violation.col.toString(10).length);
18
- const meg = (0, utils_1.messageToString)(violation.message, violation.reason);
19
- sizes.meg = Math.max(sizes.meg, (0, utils_1.w)(meg));
18
+ const meg = (0, util_1.messageToString)(violation.message, violation.reason);
19
+ sizes.meg = Math.max(sizes.meg, (0, util_1.w)(meg));
20
20
  }
21
21
  const out = [];
22
22
  if (results.violations.length) {
@@ -28,20 +28,20 @@ function standardReporter(results, options) {
28
28
  const before = line.substring(0, violation.col - 1);
29
29
  const after = line.substring(violation.col - 1 + violation.raw.length);
30
30
  const logger = violation.severity === 'error' ? loggerError : loggerWarning;
31
- const meg = (0, utils_1.messageToString)(violation.message, violation.reason);
32
- out.push(`<${utils_1.markuplint}> ${logger(`${violation.severity}: ${meg} (${violation.ruleId}) ${cli_color_1.default.underline(`${results.filePath}:${violation.line}:${violation.col}`)}`)}`);
31
+ const meg = (0, util_1.messageToString)(violation.message, violation.reason);
32
+ out.push(`<${util_1.markuplint}> ${logger(`${violation.severity}: ${meg} (${violation.ruleId}) ${cli_color_1.default.underline(`${results.filePath}:${violation.line}:${violation.col}`)}`)}`);
33
33
  if (violation.line - 1 > 0) {
34
- out.push(` ${cli_color_1.default.cyan((0, utils_1.p)(violation.line - 1, sizes.col, true))}: ${(0, utils_1.space)(prev)}`);
34
+ out.push(` ${cli_color_1.default.cyan((0, util_1.p)(violation.line - 1, sizes.col, true))}: ${(0, util_1.space)(prev)}`);
35
35
  }
36
- out.push(` ${cli_color_1.default.cyan((0, utils_1.p)(violation.line, sizes.col, true))}: ${(0, utils_1.space)(before)}${cli_color_1.default.bgRed(violation.raw)}${(0, utils_1.space)(after)}`);
36
+ out.push(` ${cli_color_1.default.cyan((0, util_1.p)(violation.line, sizes.col, true))}: ${(0, util_1.space)(before)}${cli_color_1.default.bgRed(violation.raw)}${(0, util_1.space)(after)}`);
37
37
  if (!options.color) {
38
- out.push(` ${(0, utils_1.invisibleSpace)(before)}${'^'.repeat(violation.raw.length)}${(0, utils_1.invisibleSpace)(after)}`);
38
+ out.push(` ${(0, util_1.invisibleSpace)(before)}${'^'.repeat(violation.raw.length)}${(0, util_1.invisibleSpace)(after)}`);
39
39
  }
40
- out.push(` ${cli_color_1.default.cyan((0, utils_1.p)(violation.line + 1, sizes.col, true))}: ${(0, utils_1.space)(next)}`);
40
+ out.push(` ${cli_color_1.default.cyan((0, util_1.p)(violation.line + 1, sizes.col, true))}: ${(0, util_1.space)(next)}`);
41
41
  }
42
42
  }
43
43
  else if (!options.problemOnly) {
44
- out.push(`<${utils_1.markuplint}> ${cli_color_1.default.green('passed')} ${cli_color_1.default.underline(results.filePath)}`);
44
+ out.push(`<${util_1.markuplint}> ${cli_color_1.default.green('passed')} ${cli_color_1.default.underline(results.filePath)}`);
45
45
  }
46
46
  return out;
47
47
  }
@@ -0,0 +1,43 @@
1
+ import c from 'cli-color';
2
+ import { invisibleSpace, markuplint, messageToString, p, space, w } from '../util.mjs';
3
+ const loggerError = c.red;
4
+ const loggerWarning = c.xterm(208);
5
+ export function standardReporter(results, options) {
6
+ const sizes = {
7
+ line: 0,
8
+ col: 0,
9
+ meg: 0,
10
+ };
11
+ for (const violation of results.violations) {
12
+ sizes.line = Math.max(sizes.line, violation.line.toString(10).length);
13
+ sizes.col = Math.max(sizes.col, violation.col.toString(10).length);
14
+ const meg = messageToString(violation.message, violation.reason);
15
+ sizes.meg = Math.max(sizes.meg, w(meg));
16
+ }
17
+ const out = [];
18
+ if (results.violations.length) {
19
+ const lines = results.sourceCode.split(/\r?\n/g);
20
+ for (const violation of results.violations) {
21
+ const prev = lines[violation.line - 2] || '';
22
+ const line = lines[violation.line - 1] || '';
23
+ const next = lines[violation.line - 0] || '';
24
+ const before = line.substring(0, violation.col - 1);
25
+ const after = line.substring(violation.col - 1 + violation.raw.length);
26
+ const logger = violation.severity === 'error' ? loggerError : loggerWarning;
27
+ const meg = messageToString(violation.message, violation.reason);
28
+ out.push(`<${markuplint}> ${logger(`${violation.severity}: ${meg} (${violation.ruleId}) ${c.underline(`${results.filePath}:${violation.line}:${violation.col}`)}`)}`);
29
+ if (violation.line - 1 > 0) {
30
+ out.push(` ${c.cyan(p(violation.line - 1, sizes.col, true))}: ${space(prev)}`);
31
+ }
32
+ out.push(` ${c.cyan(p(violation.line, sizes.col, true))}: ${space(before)}${c.bgRed(violation.raw)}${space(after)}`);
33
+ if (!options.color) {
34
+ out.push(` ${invisibleSpace(before)}${'^'.repeat(violation.raw.length)}${invisibleSpace(after)}`);
35
+ }
36
+ out.push(` ${c.cyan(p(violation.line + 1, sizes.col, true))}: ${space(next)}`);
37
+ }
38
+ }
39
+ else if (!options.problemOnly) {
40
+ out.push(`<${markuplint}> ${c.green('passed')} ${c.underline(results.filePath)}`);
41
+ }
42
+ return out;
43
+ }
@@ -0,0 +1,35 @@
1
+ import type { Target } from '@markuplint/file-resolver';
2
+ import type { Config, RuleConfigValue, Rule, RegexSelector } from '@markuplint/ml-config';
3
+ import type { AnyMLRule, RuleSeed } from '@markuplint/ml-core';
4
+ export declare function mlTest(sourceCode: string, config: Config, rules?: AnyMLRule[], locale?: string, fix?: boolean): Promise<{
5
+ violations: import("@markuplint/ml-config").Violation[];
6
+ fixedCode: string;
7
+ }>;
8
+ export declare function mlRuleTest<T extends RuleConfigValue, O = null>(rule: RuleSeed<T, O>, sourceCode: string, config?: Omit<Config, 'rules' | 'nodeRules' | 'childNodeRules'> & {
9
+ rule?: Rule<T, O>;
10
+ nodeRule?: NodeRule<T, O>[];
11
+ childNodeRule?: ChildNodeRule<T, O>[];
12
+ }, fix?: boolean, locale?: string): Promise<{
13
+ violations: import("@markuplint/ml-config").Violation[];
14
+ fixedCode: string;
15
+ }>;
16
+ export declare function mlTestFile(target: Target, config?: Config, rules?: AnyMLRule[], locale?: string, fix?: boolean): Promise<{
17
+ violations: import("@markuplint/ml-config").Violation[];
18
+ fixedCode: string;
19
+ }>;
20
+ export interface NodeRule<T extends RuleConfigValue, O = void> {
21
+ tagName?: string;
22
+ selector?: string;
23
+ regexSelector?: RegexSelector;
24
+ categories?: string[];
25
+ roles?: string[];
26
+ obsolete?: boolean;
27
+ rule?: Rule<T, O>;
28
+ }
29
+ export interface ChildNodeRule<T extends RuleConfigValue, O = void> {
30
+ tagName?: string;
31
+ selector?: string;
32
+ regexSelector?: RegexSelector;
33
+ inheritance?: boolean;
34
+ rule?: Rule<T, O>;
35
+ }
@@ -12,6 +12,7 @@ async function mlTest(sourceCode, config, rules, locale = 'en', fix = false) {
12
12
  rules,
13
13
  locale: locale !== null && locale !== void 0 ? locale : global.locale,
14
14
  fix,
15
+ ignoreExt: true,
15
16
  autoLoad: true,
16
17
  importPresetRules: !rules,
17
18
  });
@@ -76,6 +77,7 @@ async function mlTestFile(target, config, rules, locale, fix = false) {
76
77
  rules,
77
78
  locale: locale !== null && locale !== void 0 ? locale : global.locale,
78
79
  fix,
80
+ ignoreExt: true,
79
81
  noSearchConfig: !!config,
80
82
  autoLoad: true,
81
83
  importPresetRules: !rules,
@@ -0,0 +1,83 @@
1
+ import { MLRule } from '@markuplint/ml-core';
2
+ import { lint } from '../api/index.mjs';
3
+ import { getGlobal } from '../global-settings.mjs';
4
+ export async function mlTest(sourceCode, config, rules, locale = 'en', fix = false) {
5
+ const global = getGlobal();
6
+ const results = await lint([{ sourceCode }], {
7
+ config,
8
+ rules,
9
+ locale: locale ?? global.locale,
10
+ fix,
11
+ ignoreExt: true,
12
+ autoLoad: true,
13
+ importPresetRules: !rules,
14
+ });
15
+ const result = results[0];
16
+ return {
17
+ violations: result?.violations ?? [],
18
+ fixedCode: result?.fixedCode ?? sourceCode,
19
+ };
20
+ }
21
+ export async function mlRuleTest(rule, sourceCode, config = { rule: true }, fix = false, locale = 'en') {
22
+ const _config = {
23
+ ...config,
24
+ rules: config.rule !== undefined
25
+ ? {
26
+ '<current-rule>': config.rule,
27
+ }
28
+ : config.rule === undefined && config.nodeRule === undefined && config.childNodeRule === undefined
29
+ ? {
30
+ '<current-rule>': true,
31
+ }
32
+ : undefined,
33
+ nodeRules: config.nodeRule !== undefined
34
+ ? config.nodeRule.map(nodeConfig => ({
35
+ ...nodeConfig,
36
+ rules: nodeConfig.rule !== undefined
37
+ ? {
38
+ '<current-rule>': nodeConfig.rule,
39
+ }
40
+ : undefined,
41
+ }))
42
+ : undefined,
43
+ childNodeRules: config.childNodeRule !== undefined
44
+ ? config.childNodeRule.map(childNodeConfig => ({
45
+ ...childNodeConfig,
46
+ rules: childNodeConfig.rule !== undefined
47
+ ? {
48
+ '<current-rule>': childNodeConfig.rule,
49
+ }
50
+ : undefined,
51
+ }))
52
+ : undefined,
53
+ };
54
+ const res = await mlTest(sourceCode, _config, [
55
+ new MLRule({
56
+ name: '<current-rule>',
57
+ ...rule,
58
+ }),
59
+ ], locale, fix);
60
+ res.violations.map(v => {
61
+ // @ts-ignore
62
+ delete v.ruleId;
63
+ });
64
+ return res;
65
+ }
66
+ export async function mlTestFile(target, config, rules, locale, fix = false) {
67
+ const global = getGlobal();
68
+ const results = await lint([target], {
69
+ config,
70
+ rules,
71
+ locale: locale ?? global.locale,
72
+ fix,
73
+ ignoreExt: true,
74
+ noSearchConfig: !!config,
75
+ autoLoad: true,
76
+ importPresetRules: !rules,
77
+ });
78
+ const result = results[0];
79
+ return {
80
+ violations: result?.violations ?? [],
81
+ fixedCode: result?.fixedCode ?? result?.sourceCode,
82
+ };
83
+ }
@@ -0,0 +1,27 @@
1
+ import type { Config, Violation } from '@markuplint/ml-config';
2
+ import type { Document, Ruleset } from '@markuplint/ml-core';
3
+ export interface MLResultInfo {
4
+ violations: Violation[];
5
+ filePath: string;
6
+ sourceCode: string;
7
+ fixedCode: string;
8
+ }
9
+ /**
10
+ * @deprecated
11
+ */
12
+ export interface MLResultInfo_v1 {
13
+ results: Violation[];
14
+ filePath: string;
15
+ sourceCode: string;
16
+ fixedCode: string;
17
+ document: Document<any, unknown> | null;
18
+ parser: string;
19
+ locale?: string;
20
+ ruleset: Ruleset;
21
+ configSet: {
22
+ config: Config;
23
+ files: string[];
24
+ error: string[];
25
+ };
26
+ }
27
+ export declare type Nullable<T> = T | null | undefined;
package/lib/types.mjs ADDED
@@ -0,0 +1 @@
1
+ export {};
package/lib/util.d.mts ADDED
@@ -0,0 +1,19 @@
1
+ import type { Nullable } from './types.mjs';
2
+ export declare function nonNullableFilter<T>(item: Nullable<T>): item is T;
3
+ export declare function uuid(): string;
4
+ export declare const markuplint: string;
5
+ export declare function write(message: string): void;
6
+ export declare namespace write {
7
+ var _a: () => boolean;
8
+ export { _a as break };
9
+ }
10
+ export declare function error(message: string): void;
11
+ export declare namespace error {
12
+ var exit: () => never;
13
+ }
14
+ export declare const head: (method: string, noColor?: boolean | undefined) => string;
15
+ export declare function p(s: number | string, pad: number, start?: boolean): string;
16
+ export declare function w(s: string): number;
17
+ export declare function space(str: string): string;
18
+ export declare function invisibleSpace(str: string): string;
19
+ export declare function messageToString(message: string, reason?: string): string;
package/lib/util.d.ts CHANGED
@@ -1,3 +1,19 @@
1
1
  import type { Nullable } from './types';
2
2
  export declare function nonNullableFilter<T>(item: Nullable<T>): item is T;
3
3
  export declare function uuid(): string;
4
+ export declare const markuplint: string;
5
+ export declare function write(message: string): void;
6
+ export declare namespace write {
7
+ var _a: () => boolean;
8
+ export { _a as break };
9
+ }
10
+ export declare function error(message: string): void;
11
+ export declare namespace error {
12
+ var exit: () => never;
13
+ }
14
+ export declare const head: (method: string, noColor?: boolean | undefined) => string;
15
+ export declare function p(s: number | string, pad: number, start?: boolean): string;
16
+ export declare function w(s: string): number;
17
+ export declare function space(str: string): string;
18
+ export declare function invisibleSpace(str: string): string;
19
+ export declare function messageToString(message: string, reason?: string): string;
package/lib/util.js CHANGED
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uuid = exports.nonNullableFilter = void 0;
3
+ exports.messageToString = exports.invisibleSpace = exports.space = exports.w = exports.p = exports.head = exports.error = exports.write = exports.markuplint = exports.uuid = exports.nonNullableFilter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const cli_color_1 = (0, tslib_1.__importDefault)(require("cli-color"));
6
+ // @ts-ignore
7
+ const eastasianwidth_1 = (0, tslib_1.__importDefault)(require("eastasianwidth"));
8
+ const strip_ansi_1 = (0, tslib_1.__importDefault)(require("strip-ansi"));
4
9
  const uuid_1 = require("uuid");
5
10
  function nonNullableFilter(item) {
6
11
  return !!item;
@@ -10,3 +15,72 @@ function uuid() {
10
15
  return (0, uuid_1.v4)();
11
16
  }
12
17
  exports.uuid = uuid;
18
+ const logo = `<${cli_color_1.default.xterm(39)('✔')}>`;
19
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
20
+ const version = require('../package.json').version;
21
+ const eaw = eastasianwidth_1.default;
22
+ const box = (lines, { width = 40, padding = 1, center = false, noColor = false }) => {
23
+ const bt = `┌${'─'.repeat(width - 2)}┐`;
24
+ const pd = `│${' '.repeat(width - 2)}│`;
25
+ const bb = `└${'─'.repeat(width - 2)}┘`;
26
+ const texts = lines.map(line => {
27
+ const nc = (0, strip_ansi_1.default)(line);
28
+ const length = nc.length;
29
+ const pad = width - 2 - 1 - length;
30
+ const pad2 = Math.floor(pad / 2);
31
+ const padD = pad % 2;
32
+ const padl = center ? pad2 : 0;
33
+ const padr = center ? pad2 + padD : pad;
34
+ const text = noColor ? nc : line;
35
+ return `│ ${' '.repeat(padl)}${text}${' '.repeat(padr)}│`;
36
+ });
37
+ const result = [bt, pd, ...texts, pd, bb];
38
+ return result.join('\n');
39
+ };
40
+ exports.markuplint = `markup${cli_color_1.default.xterm(39)('lint')}`;
41
+ function write(message) {
42
+ process.stdout.write(message + '\n');
43
+ }
44
+ exports.write = write;
45
+ write.break = () => process.stdout.write('\n');
46
+ function error(message) {
47
+ process.stderr.write(message + '\n');
48
+ }
49
+ exports.error = error;
50
+ error.exit = () => process.exit(1);
51
+ const head = (method, noColor) => box([`${logo} ${exports.markuplint}`, cli_color_1.default.blackBright(`v${version}`), '', cli_color_1.default.bold(method)], {
52
+ center: true,
53
+ noColor,
54
+ });
55
+ exports.head = head;
56
+ function p(s, pad, start = false) {
57
+ const l = w(`${s}`.trim());
58
+ const d = pad - l;
59
+ const _ = ' '.repeat(d < 0 ? 0 : d);
60
+ return start ? `${_}${s}` : `${s}${_}`;
61
+ }
62
+ exports.p = p;
63
+ function w(s) {
64
+ return s.replace(/./g, _ => '0'.repeat(eaw.characterLength(_))).length;
65
+ }
66
+ exports.w = w;
67
+ function space(str) {
68
+ return str
69
+ .replace(/\s+/g, $0 => {
70
+ return cli_color_1.default.xterm(8)($0);
71
+ })
72
+ .replace(/ /g, $0 => '•')
73
+ .replace(/\t/g, $0 => '→ ');
74
+ }
75
+ exports.space = space;
76
+ function invisibleSpace(str) {
77
+ return str.replace(/\t/g, $0 => ' ').replace(/./g, $0 => ' ');
78
+ }
79
+ exports.invisibleSpace = invisibleSpace;
80
+ function messageToString(message, reason) {
81
+ if (!reason) {
82
+ return message;
83
+ }
84
+ return `${message} / ${reason}`;
85
+ }
86
+ exports.messageToString = messageToString;