markuplint 3.0.0-alpha.66 → 3.0.0-dev.176

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 (46) hide show
  1. package/README.md +29 -24
  2. package/lib/api/lint.d.ts +2 -2
  3. package/lib/api/ml-engine.d.ts +26 -23
  4. package/lib/api/ml-engine.js +114 -83
  5. package/lib/api/types.d.ts +35 -38
  6. package/lib/api/v1.d.ts +44 -44
  7. package/lib/api/v1.js +4 -7
  8. package/lib/cli/bootstrap.d.ts +3 -2
  9. package/lib/cli/bootstrap.js +2 -2
  10. package/lib/cli/command.d.ts +6 -2
  11. package/lib/cli/command.js +17 -3
  12. package/lib/cli/create-rule/index.js +48 -19
  13. package/lib/cli/index.js +5 -2
  14. package/lib/cli/init/create-config.d.ts +8 -0
  15. package/lib/cli/init/create-config.js +95 -0
  16. package/lib/cli/init/get-default-rules.d.ts +3 -0
  17. package/lib/cli/init/get-default-rules.js +57 -0
  18. package/lib/cli/init/index.js +23 -199
  19. package/lib/cli/init/install-module.d.ts +6 -4
  20. package/lib/cli/init/install-module.js +16 -2
  21. package/lib/cli/init/types.d.ts +20 -0
  22. package/lib/cli/init/types.js +2 -0
  23. package/lib/cli/output.d.ts +1 -1
  24. package/lib/cli/output.js +6 -3
  25. package/lib/cli/prompt.d.ts +19 -14
  26. package/lib/cli/search/index.d.ts +1 -1
  27. package/lib/cli/search/index.js +2 -1
  28. package/lib/global-settings.d.ts +2 -2
  29. package/lib/i18n.d.ts +2 -6
  30. package/lib/i18n.js +8 -6
  31. package/lib/reporter/github-reporter.d.ts +2 -0
  32. package/lib/reporter/github-reporter.js +23 -0
  33. package/lib/reporter/index.d.ts +1 -0
  34. package/lib/reporter/index.js +1 -0
  35. package/lib/reporter/simple-reporter.js +1 -1
  36. package/lib/reporter/standard-reporter.js +5 -4
  37. package/lib/testing-tool/index.d.ts +44 -26
  38. package/lib/types.d.ts +18 -19
  39. package/lib/util.d.ts +4 -6
  40. package/lib/util.js +12 -10
  41. package/package.json +27 -23
  42. package/.markuplintrc +0 -19
  43. package/media/screenshot01.png +0 -0
  44. package/test/plugin001.js +0 -24
  45. package/tsconfig.test.json +0 -3
  46. package/tsconfig.tsbuildinfo +0 -1
package/lib/api/v1.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Config, RuleConfigValue } from '@markuplint/ml-config';
1
+ import type { Config, PlainData, RuleConfigValue } from '@markuplint/ml-config';
2
2
  import type { MLRule } from '@markuplint/ml-core';
3
3
  /**
4
4
  * @deprecated
@@ -6,46 +6,46 @@ import type { MLRule } from '@markuplint/ml-core';
6
6
  * @returns
7
7
  */
8
8
  export declare function lint_v1(options: {
9
- /**
10
- * Glob pattern
11
- */
12
- files?: string | string[];
13
- /**
14
- * Target source code of evaluation
15
- */
16
- sourceCodes?: string | string[];
17
- /**
18
- * File names when `sourceCodes`
19
- */
20
- names?: string | string[];
21
- /**
22
- * Workspace path when `sourceCodes`
23
- */
24
- workspace?: string;
25
- /**
26
- * Configure file or object
27
- */
28
- config?: string | Config;
29
- /**
30
- * The config applied when not resolved from files or set it explicitly.
31
- */
32
- defaultConfig?: Config;
33
- /**
34
- * Rules (default: `@markuplint/rules`)
35
- */
36
- rules?: MLRule<RuleConfigValue, unknown>[];
37
- /**
38
- * Auto resolve rules
39
- *
40
- * Auto importing form *node_modules* when set `@markuplint/rule-{RULE_NAME}` or `markuplint-rule-{RULE_NAME}` in config rules
41
- */
42
- rulesAutoResolve?: boolean;
43
- /**
44
- * Auto fix
45
- */
46
- fix?: boolean;
47
- /**
48
- * Locale
49
- */
50
- locale?: string;
51
- }): Promise<import("..").MLResultInfo[]>;
9
+ /**
10
+ * Glob pattern
11
+ */
12
+ readonly files?: string | readonly string[];
13
+ /**
14
+ * Target source code of evaluation
15
+ */
16
+ readonly sourceCodes?: string | readonly string[];
17
+ /**
18
+ * File names when `sourceCodes`
19
+ */
20
+ readonly names?: string | readonly string[];
21
+ /**
22
+ * Workspace path when `sourceCodes`
23
+ */
24
+ readonly workspace?: string;
25
+ /**
26
+ * Configure file or object
27
+ */
28
+ readonly config?: string | Config;
29
+ /**
30
+ * The config applied when not resolved from files or set it explicitly.
31
+ */
32
+ readonly defaultConfig?: Config;
33
+ /**
34
+ * Rules (default: `@markuplint/rules`)
35
+ */
36
+ readonly rules?: readonly Readonly<MLRule<RuleConfigValue, PlainData>>[];
37
+ /**
38
+ * Auto resolve rules
39
+ *
40
+ * Auto importing form *node_modules* when set `@markuplint/rule-{RULE_NAME}` or `markuplint-rule-{RULE_NAME}` in config rules
41
+ */
42
+ readonly rulesAutoResolve?: boolean;
43
+ /**
44
+ * Auto fix
45
+ */
46
+ readonly fix?: boolean;
47
+ /**
48
+ * Locale
49
+ */
50
+ readonly locale?: string;
51
+ }): Promise<import('..').MLResultInfo[]>;
package/lib/api/v1.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.lint_v1 = void 0;
4
+ const shared_1 = require("@markuplint/shared");
4
5
  const lint_1 = require("./lint");
5
6
  /**
6
7
  * @deprecated
@@ -9,12 +10,8 @@ const lint_1 = require("./lint");
9
10
  */
10
11
  async function lint_v1(options) {
11
12
  var _a;
12
- const filePathList = options.files ? (Array.isArray(options.files) ? options.files : [options.files]) : [];
13
- const codes = options.sourceCodes
14
- ? Array.isArray(options.sourceCodes)
15
- ? options.sourceCodes
16
- : [options.sourceCodes]
17
- : [];
13
+ const filePathList = (0, shared_1.toNoEmptyStringArrayFromStringOrArray)(options.files);
14
+ const codes = (0, shared_1.toNoEmptyStringArrayFromStringOrArray)(options.sourceCodes);
18
15
  const files = [
19
16
  ...filePathList,
20
17
  ...codes.map((code, i) => {
@@ -37,7 +34,7 @@ async function lint_v1(options) {
37
34
  const result = await (0, lint_1.lint)(files, {
38
35
  config,
39
36
  configFile,
40
- noSearchConfig: !options.files,
37
+ noSearchConfig: filePathList.length === 0,
41
38
  rules: options.rules,
42
39
  autoLoad: (_a = options.rulesAutoResolve) !== null && _a !== void 0 ? _a : true,
43
40
  locale: options.locale,
@@ -1,5 +1,6 @@
1
+ import type { ReadonlyDeep } from 'type-fest';
1
2
  import meow from 'meow';
2
- export declare const help = "\nUsage\n\t$ markuplint <HTML file pathes (glob format)>\n\t$ <stdout> | markuplint\n\nOptions\n\t--config, -c FILE_PATH A configuration file path.\n\t--fix, Fix HTML.\n\t--format, -f FORMAT Output format. Support \"JSON\", \"Simple\" and \"Standard\". Default: \"Standard\".\n\t--no-search-config No search a configure file automatically.\n\t--ignore-ext Evaluate files that are received even though the type of extension.\n\t--no-import-preset-rules No import preset rules.\n\t--locale Locale of the message of violation. Default is an OS setting.\n\t--no-color, Output no color.\n\t--problem-only, -p Output only problems, without passeds.\n\t--verbose Output with detailed information.\n\n\t--init Initialize settings interactively.\n\t--create-rule Add the scaffold of a custom rule.\n\t--search Search lines of codes that include the target element by selectors.\n\n\t--help, -h Show help.\n\t--version, -v Show version.\n\nExamples\n\t$ markuplint verifyee.html --config path/to/.markuplintrc\n\t$ cat verifyee.html | markuplint\n";
3
+ export declare const help = "\nUsage\n\t$ markuplint <HTML file paths (glob format)>\n\t$ <stdout> | markuplint\n\nOptions\n\t--config, -c FILE_PATH A configuration file path.\n\t--fix, Fix HTML.\n\t--format, -f FORMAT Output format. Support \"JSON\", \"Simple\", \"GitHub\" and \"Standard\". Default: \"Standard\".\n\t--no-search-config No search a configure file automatically.\n\t--ignore-ext Evaluate files that are received even though the type of extension.\n\t--no-import-preset-rules No import preset rules.\n\t--locale Locale of the message of violation. Default is an OS setting.\n\t--no-color, Output no color.\n\t--problem-only, -p Output only problems, without passeds.\n\t--verbose Output with detailed information.\n\n\t--init Initialize settings interactively.\n\t--create-rule Add the scaffold of a custom rule.\n\t--search Search lines of codes that include the target element by selectors.\n\n\t--help, -h Show help.\n\t--version, -v Show version.\n\nExamples\n\t$ markuplint verifyee.html --config path/to/.markuplintrc\n\t$ cat verifyee.html | markuplint\n";
3
4
  export declare const cli: meow.Result<{
4
5
  config: {
5
6
  type: "string";
@@ -53,4 +54,4 @@ export declare const cli: meow.Result<{
53
54
  type: "string";
54
55
  };
55
56
  }>;
56
- export declare type CLIOptions = typeof cli.flags;
57
+ export type CLIOptions = ReadonlyDeep<typeof cli.flags>;
@@ -5,13 +5,13 @@ const tslib_1 = require("tslib");
5
5
  const meow_1 = tslib_1.__importDefault(require("meow"));
6
6
  exports.help = `
7
7
  Usage
8
- $ markuplint <HTML file pathes (glob format)>
8
+ $ markuplint <HTML file paths (glob format)>
9
9
  $ <stdout> | markuplint
10
10
 
11
11
  Options
12
12
  --config, -c FILE_PATH A configuration file path.
13
13
  --fix, Fix HTML.
14
- --format, -f FORMAT Output format. Support "JSON", "Simple" and "Standard". Default: "Standard".
14
+ --format, -f FORMAT Output format. Support "JSON", "Simple", "GitHub" and "Standard". Default: "Standard".
15
15
  --no-search-config No search a configure file automatically.
16
16
  --ignore-ext Evaluate files that are received even though the type of extension.
17
17
  --no-import-preset-rules No import preset rules.
@@ -1,4 +1,8 @@
1
- import type { APIOptions } from '../api/types';
2
1
  import type { CLIOptions } from './bootstrap';
2
+ import type { APIOptions } from '../api/types';
3
3
  import type { Target } from '@markuplint/file-resolver';
4
- export declare function command(files: Target[], options: CLIOptions, apiOptions?: APIOptions): Promise<boolean>;
4
+ export declare function command(
5
+ files: readonly Readonly<Target>[],
6
+ options: CLIOptions,
7
+ apiOptions?: APIOptions,
8
+ ): Promise<boolean>;
@@ -9,6 +9,7 @@ const api_1 = require("../api");
9
9
  const debug_1 = require("../debug");
10
10
  const output_1 = require("./output");
11
11
  async function command(files, options, apiOptions) {
12
+ var _a;
12
13
  const fix = options.fix;
13
14
  const configFile = options.config && path_1.default.join(process.cwd(), options.config);
14
15
  const locale = options.locale;
@@ -19,10 +20,12 @@ async function command(files, options, apiOptions) {
19
20
  const fileList = await (0, file_resolver_1.resolveFiles)(files);
20
21
  if (debug_1.log.enabled) {
21
22
  (0, debug_1.log)('File list: %O', fileList.map(f => f.path));
22
- (0, debug_1.log)('Config: %s', configFile || 'N/A');
23
+ (0, debug_1.log)('Config: %s', configFile !== null && configFile !== void 0 ? configFile : 'N/A');
23
24
  (0, debug_1.log)('Fix option: %s', fix);
24
25
  }
26
+ const format = (_a = options.format) === null || _a === void 0 ? void 0 : _a.toLowerCase().trim();
25
27
  let hasError = false;
28
+ const jsonOutput = [];
26
29
  for (const file of fileList) {
27
30
  const engine = new api_1.MLEngine(file, {
28
31
  configFile,
@@ -38,7 +41,7 @@ async function command(files, options, apiOptions) {
38
41
  if (!result) {
39
42
  continue;
40
43
  }
41
- if (!hasError && result.violations.length) {
44
+ if (!hasError && result.violations.length > 0) {
42
45
  hasError = true;
43
46
  }
44
47
  if (fix) {
@@ -47,10 +50,21 @@ async function command(files, options, apiOptions) {
47
50
  process.stdout.write(`markuplint: Fix "${result.filePath}"\n`);
48
51
  }
49
52
  else {
53
+ if (format === 'json') {
54
+ jsonOutput.push(...result.violations.map(v => ({
55
+ ...v,
56
+ filePath: result.filePath,
57
+ })));
58
+ continue;
59
+ }
50
60
  (0, debug_1.log)('Output reports');
51
- await (0, output_1.output)(result, options);
61
+ (0, output_1.output)(result, options);
52
62
  }
53
63
  }
64
+ if (format === 'json') {
65
+ process.stdout.write(JSON.stringify(jsonOutput, null, 2) + '\n');
66
+ return false;
67
+ }
54
68
  return hasError;
55
69
  }
56
70
  exports.command = command;
@@ -2,23 +2,59 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createRule = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const node_path_1 = require("node:path");
5
6
  const create_rule_helper_1 = require("@markuplint/create-rule-helper");
6
7
  const cli_color_1 = tslib_1.__importDefault(require("cli-color"));
7
8
  const util_1 = require("../../util");
8
9
  const install_module_1 = require("../init/install-module");
9
10
  const prompt_1 = require("../prompt");
11
+ const icons = {
12
+ README: '📝',
13
+ index: '📜',
14
+ schema: '⚙️ ',
15
+ package: '🎁',
16
+ tsconfig: '💎',
17
+ };
10
18
  async function createRule() {
19
+ var _a;
11
20
  (0, util_1.write)((0, util_1.head)('Create a rule'));
12
21
  util_1.write.break();
22
+ const firstChoices = [
23
+ { name: 'Add the rule to this project', value: 'ADD_TO_PROJECT' },
24
+ { name: 'Create the rule and publish it as a package', value: 'PUBLISH_AS_PACKAGE' },
25
+ ];
26
+ if (await (0, create_rule_helper_1.isMarkuplintRepo)()) {
27
+ firstChoices.push({ name: 'Contribute the new rule to markuplint core rules', value: 'CONTRIBUTE_TO_CORE' });
28
+ }
13
29
  const purpose = await (0, prompt_1.select)({
14
30
  message: 'What purpose do you create the rule for?',
15
- choices: [
16
- { name: 'Add the rule to this project', value: 'ADD_TO_PROJECT' },
17
- { name: 'Create the rule and publish it as a package', value: 'PUBLISH_AS_PACKAGE' },
18
- { name: 'Contribute the new rule to markuplint core rules', value: 'CONTRIBUTE_TO_CORE' },
19
- ],
31
+ choices: firstChoices,
20
32
  });
21
- const name = await (0, prompt_1.input)('What is the name?', /^[a-z][a-z0-9]*(?:-[a-z][a-z0-9]*)*$/i);
33
+ const dirQuestion = purpose === 'ADD_TO_PROJECT' ? 'What is the directory name?' : 'What is the plugin name?';
34
+ const pluginName = purpose === 'CONTRIBUTE_TO_CORE' ? '' : await (0, prompt_1.input)(dirQuestion, /^[a-z][a-z0-9]*(?:-[a-z][a-z0-9]*)*$/i);
35
+ const ruleName = await (0, prompt_1.input)('What is the rule name?', /^[a-z][a-z0-9]*(?:-[a-z][a-z0-9]*)*$/i);
36
+ const core = purpose === 'CONTRIBUTE_TO_CORE'
37
+ ? {
38
+ description: await (0, prompt_1.input)('Description:'),
39
+ category: await (0, prompt_1.select)({
40
+ message: 'Category:',
41
+ choices: [
42
+ { name: 'Conformance checking', value: 'validation' },
43
+ { name: 'Accessibility', value: 'a11y' },
44
+ { name: 'Naming Convention', value: 'naming-convention' },
45
+ { name: 'Maintainability', value: 'maintainability' },
46
+ { name: 'Style', value: 'style' },
47
+ ],
48
+ }),
49
+ severity: await (0, prompt_1.select)({
50
+ message: 'Severity:',
51
+ choices: [
52
+ { name: 'error', value: 'error' },
53
+ { name: 'warning', value: 'warning' },
54
+ ],
55
+ }),
56
+ }
57
+ : undefined;
22
58
  const lang = purpose === 'CONTRIBUTE_TO_CORE'
23
59
  ? 'TYPESCRIPT'
24
60
  : await (0, prompt_1.select)({
@@ -29,21 +65,14 @@ async function createRule() {
29
65
  ],
30
66
  });
31
67
  const needTest = purpose === 'CONTRIBUTE_TO_CORE' ? true : await (0, prompt_1.confirm)('Do you need the test?', { initial: true });
32
- const result = await (0, create_rule_helper_1.createRuleHelper)({ purpose, name, lang, needTest });
33
- output(name, '📝', 'README.md', result.readme);
34
- output(name, '📜', 'index', result.main);
35
- if (result.test) {
36
- output(name, '🖍 ', 'index.spec', result.test);
37
- }
38
- if (result.schemaJson) {
39
- output(name, '⚙️ ', 'schema.json', result.schemaJson);
68
+ const result = await (0, create_rule_helper_1.createRuleHelper)({ purpose, pluginName, ruleName, lang, needTest, core });
69
+ for (const file of result.files) {
70
+ output(pluginName || 'core', file.test ? '🖍 ' : (_a = icons[file.name]) !== null && _a !== void 0 ? _a : '🛡 ', file.fileName, (0, node_path_1.resolve)(file.destDir, file.fileName + file.ext));
40
71
  }
41
- if (result.packageJson) {
42
- output(name, '🎁 ', 'package.json', result.packageJson);
43
- if (result.tsConfig) {
44
- output(name, '💎 ', 'tsconfig.json', result.tsConfig);
45
- }
72
+ if (result.dependencies.length > 0) {
46
73
  await (0, install_module_1.installModule)(result.dependencies);
74
+ }
75
+ if (result.devDependencies.length > 0) {
47
76
  await (0, install_module_1.installModule)(result.devDependencies, true);
48
77
  }
49
78
  }
package/lib/cli/index.js CHANGED
@@ -34,7 +34,7 @@ const search_1 = tslib_1.__importDefault(require("./search"));
34
34
  return;
35
35
  }
36
36
  const files = bootstrap_1.cli.input;
37
- if (files.length) {
37
+ if (files.length > 0) {
38
38
  if (bootstrap_1.cli.flags.search) {
39
39
  await (0, search_1.default)(files, bootstrap_1.cli.flags, bootstrap_1.cli.flags.search).catch(err => {
40
40
  process.stderr.write(err + '\n');
@@ -52,7 +52,10 @@ const search_1 = tslib_1.__importDefault(require("./search"));
52
52
  (0, get_stdin_1.default)()
53
53
  .then(async (stdin) => {
54
54
  if (stdin) {
55
- const hasError = await (0, command_1.command)([{ sourceCode: stdin }], bootstrap_1.cli.flags).catch(err => {
55
+ const hasError = await (0, command_1.command)([{ sourceCode: stdin }], {
56
+ ...bootstrap_1.cli.flags,
57
+ ignoreExt: true,
58
+ }).catch(err => {
56
59
  process.stderr.write(err + '\n');
57
60
  process.exit(1);
58
61
  });
@@ -0,0 +1,8 @@
1
+ import type { DefaultRules, Langs, RuleSettingMode } from './types';
2
+ import type { Config } from '@markuplint/ml-config';
3
+ export declare const langs: Record<Langs, string>;
4
+ export declare function createConfig(
5
+ langs: readonly Langs[],
6
+ mode: RuleSettingMode,
7
+ defaultRules: DefaultRules,
8
+ ): Config;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createConfig = exports.langs = void 0;
4
+ const extRExp = {
5
+ jsx: '\\.[jt]sx?$',
6
+ vue: '\\.vue$',
7
+ svelte: '\\.svelte$',
8
+ astro: '\\.astro$',
9
+ pug: '\\.pug$',
10
+ php: '\\.php$',
11
+ smarty: '\\.tpl$',
12
+ erb: '\\.erb$',
13
+ ejs: '\\.ejs$',
14
+ mustache: '\\.(mustache|hbs)$',
15
+ nunjucks: '\\.nunjucks$',
16
+ liquid: '\\.liquid$',
17
+ };
18
+ exports.langs = {
19
+ jsx: 'React (JSX)',
20
+ vue: 'Vue',
21
+ svelte: 'Svelte',
22
+ astro: 'Astro',
23
+ pug: 'Pug',
24
+ php: 'PHP',
25
+ smarty: 'Smarty',
26
+ erb: 'eRuby',
27
+ ejs: 'EJS',
28
+ mustache: 'Mustache/Handlebars',
29
+ nunjucks: 'Nunjucks',
30
+ liquid: 'liquid (Shopify)',
31
+ };
32
+ function createConfig(langs, mode, defaultRules) {
33
+ var _a;
34
+ let config = {};
35
+ const parser = { ...config.parser };
36
+ for (const lang of langs) {
37
+ const ext = extRExp[lang];
38
+ if (!ext) {
39
+ continue;
40
+ }
41
+ parser[ext] = `@markuplint/${lang}-parser`;
42
+ if (lang === 'vue') {
43
+ config = {
44
+ ...config,
45
+ specs: {
46
+ ...config.specs,
47
+ '\\.vue$': '@markuplint/vue-spec',
48
+ },
49
+ };
50
+ }
51
+ if (lang === 'jsx') {
52
+ config = {
53
+ ...config,
54
+ specs: {
55
+ ...config.specs,
56
+ '\\.[jt]sx?$': '@markuplint/react-spec',
57
+ },
58
+ };
59
+ }
60
+ }
61
+ if (Object.keys(parser).length > 0) {
62
+ config.parser = parser;
63
+ }
64
+ const rules = { ...config.rules };
65
+ if (Array.isArray(mode)) {
66
+ const ruleNames = Object.keys(defaultRules);
67
+ for (const ruleName of ruleNames) {
68
+ const rule = defaultRules[ruleName];
69
+ if (!rule) {
70
+ continue;
71
+ }
72
+ if (mode.includes(rule.category)) {
73
+ rules[ruleName] = rule.defaultValue;
74
+ }
75
+ }
76
+ }
77
+ else if (mode === 'recommended') {
78
+ config.extends = [...((_a = config.extends) !== null && _a !== void 0 ? _a : []), 'markuplint:recommended'];
79
+ }
80
+ else {
81
+ const ruleNames = Object.keys(defaultRules);
82
+ for (const ruleName of ruleNames) {
83
+ const rule = defaultRules[ruleName];
84
+ if (!rule) {
85
+ continue;
86
+ }
87
+ rules[ruleName] = rule.defaultValue;
88
+ }
89
+ }
90
+ if (Object.keys(rules).length > 0) {
91
+ config.rules = rules;
92
+ }
93
+ return config;
94
+ }
95
+ exports.createConfig = createConfig;
@@ -0,0 +1,3 @@
1
+ export declare function getDefaultRules(version: string): Promise<{
2
+ [x: string]: import('./types').Rule;
3
+ }>;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDefaultRules = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const gray_matter_1 = tslib_1.__importDefault(require("gray-matter"));
6
+ const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
7
+ const RULES_SCHEMA_URL = 'https://raw.githubusercontent.com/markuplint/markuplint/[VERSION]/packages/@markuplint/rules/schema.json';
8
+ const RULES_README_URL = 'https://raw.githubusercontent.com/markuplint/markuplint/[VERSION]/packages/@markuplint/rules/src/[NAME]/README.md';
9
+ async function getDefaultRules(version) {
10
+ const json = await safeFetch(RULES_SCHEMA_URL, version);
11
+ const rules = {};
12
+ await Promise.all(Object.entries(json.definitions.rules.properties).map(async ([name, rule]) => {
13
+ var _a, _b, _c, _d;
14
+ const json = await safeFetch(rule.$ref.replace('/main/', '/[VERSION]/'), version);
15
+ let severity = (_c = (_b = (_a = (Array.isArray(json.oneOf) ? json.oneOf : []).find((val) => val.properties)) === null || _a === void 0 ? void 0 : _a.properties) === null || _b === void 0 ? void 0 : _b.severity) === null || _c === void 0 ? void 0 : _c.default;
16
+ let category = json._category;
17
+ if (severity == null || category == null) {
18
+ const data = await getCatAndSeverityFromLegacy(version, name);
19
+ severity = data.severity;
20
+ category = data.category;
21
+ }
22
+ const defaultValue = severity === 'warning' ? false : (_d = json.definitions.value.default) !== null && _d !== void 0 ? _d : true;
23
+ rules[name] = {
24
+ defaultValue,
25
+ category,
26
+ };
27
+ }));
28
+ return rules;
29
+ }
30
+ exports.getDefaultRules = getDefaultRules;
31
+ async function safeFetch(baseUrl, version, type = 'json') {
32
+ const url = baseUrl.replace('[VERSION]', `v${version}`);
33
+ const res = await (0, node_fetch_1.default)(url);
34
+ if (!res.ok) {
35
+ return safeFetch(baseUrl, '3.0.0', type);
36
+ }
37
+ if (type === 'json') {
38
+ return (await res.json());
39
+ }
40
+ const md = await res.text();
41
+ const { data } = (0, gray_matter_1.default)(md);
42
+ return data;
43
+ }
44
+ /**
45
+ * Fallback fetching until 3.0.x
46
+ *
47
+ * @param version
48
+ * @param name
49
+ * @returns
50
+ */
51
+ async function getCatAndSeverityFromLegacy(version, name) {
52
+ const data = await safeFetch(RULES_README_URL.replace('[NAME]', name), version, 'md');
53
+ return {
54
+ category: data.category,
55
+ severity: data.severity,
56
+ };
57
+ }