mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Introduce type declaration file for project after updating typescript dependency to 3.7.2.
This commit is contained in:
parent
37307d0764
commit
0a9ac73524
9 changed files with 703 additions and 18 deletions
1
example/typescript/.gitignore
vendored
Normal file
1
example/typescript/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
type-check.js
|
11
example/typescript/tsconfig.json
Normal file
11
example/typescript/tsconfig.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
|
||||
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
|
||||
"strict": true, /* Enable all strict type-checking options. */
|
||||
"noUnusedLocals": true, /* Report errors on unused locals. */
|
||||
"noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
||||
}
|
||||
}
|
142
example/typescript/type-check.ts
Normal file
142
example/typescript/type-check.ts
Normal file
|
@ -0,0 +1,142 @@
|
|||
// Attempt to validate all the type declarations in markdownlint.d.ts
|
||||
|
||||
import markdownlint from "../..";
|
||||
|
||||
const assert = require("assert");
|
||||
const { URL } = require("url");
|
||||
const markdownlintJsonPath = "../../.markdownlint.json";
|
||||
|
||||
function assertConfiguration(config: markdownlint.Configuration) {
|
||||
assert(!!config);
|
||||
assert.equal(config["line-length"], false);
|
||||
assert.deepEqual(config["no-inline-html"], {
|
||||
"allowed_elements": [
|
||||
"a"
|
||||
]
|
||||
});
|
||||
// config assignment is covered by markdownlint.Options
|
||||
}
|
||||
|
||||
function assertConfigurationCallback(err: Error | null, config?: markdownlint.Configuration) {
|
||||
assert(!err);
|
||||
config && assertConfiguration(config);
|
||||
}
|
||||
|
||||
function assertLintResults(results: markdownlint.LintResults) {
|
||||
assert(!!results);
|
||||
assert.equal(results["string"].length, 1);
|
||||
assert.equal(results["string"][0].lineNumber, 1);
|
||||
assert.deepEqual(results["string"][0].ruleNames, [ "MD047", "single-trailing-newline" ]);
|
||||
assert.equal(results["string"][0].ruleDescription, "Files should end with a single newline character");
|
||||
assert.equal(results["string"][0].ruleInformation.replace(/v\d+\.\d+\.\d+/, "v0.0.0"), "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md047");
|
||||
assert.equal(results["string"][0].errorDetail, null);
|
||||
assert.equal(results["string"][0].errorContext, null);
|
||||
assert.deepEqual(results["string"][0].errorRange, [ 9, 1 ]);
|
||||
assert(!!results["string"][0].fixInfo);
|
||||
assert.equal(results["string"][0].fixInfo.editColumn, 10);
|
||||
assert(!results["string"][0].fixInfo.deleteCount);
|
||||
assert.equal(results["string"][0].fixInfo.insertText, "\n");
|
||||
assert.equal(results["../bad.md"].length, 2);
|
||||
results = {
|
||||
"key": [
|
||||
{
|
||||
"lineNumber": 1,
|
||||
"ruleNames": [ "rule", "names" ],
|
||||
"ruleDescription": "description",
|
||||
"ruleInformation": "https://example.com/ruleInformation",
|
||||
"errorDetail": "detail",
|
||||
"errorContext": "context",
|
||||
"errorRange": [ 1, 2 ],
|
||||
"fixInfo": {
|
||||
"editColumn": 1,
|
||||
"deleteCount": 1,
|
||||
"insertText": "text"
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
function assertLintResultsCallback(err: Error | null, results?: markdownlint.LintResults) {
|
||||
assert(!err);
|
||||
results && assertLintResults(results);
|
||||
}
|
||||
|
||||
assertConfiguration(markdownlint.readConfigSync(markdownlintJsonPath));
|
||||
assertConfiguration(markdownlint.readConfigSync(markdownlintJsonPath, [ JSON.parse ]));
|
||||
|
||||
// The following call is valid, but disallowed because TypeScript does not
|
||||
// currently propagate {ConfigurationParser[] | null} into the .d.ts file.
|
||||
// markdownlint.readConfig(markdownlintJsonPath, null, assertConfigCallback);
|
||||
markdownlint.readConfig(markdownlintJsonPath, [ JSON.parse ], assertConfigurationCallback);
|
||||
|
||||
let options: markdownlint.Options;
|
||||
options = {
|
||||
"files": [ "../bad.md" ],
|
||||
"strings": {
|
||||
"string": "# Heading"
|
||||
},
|
||||
"config": {
|
||||
"no-missing-space-atx": false,
|
||||
"no-hard-tabs": {
|
||||
"code_blocks": true
|
||||
}
|
||||
},
|
||||
"customRules": undefined,
|
||||
"frontMatter": /---/,
|
||||
"handleRuleFailures": false,
|
||||
"noInlineConfig": false,
|
||||
"resultVersion": 3,
|
||||
"markdownItPlugins": [ [ require("markdown-it-sub") ] ]
|
||||
};
|
||||
|
||||
assertLintResults(markdownlint.sync(options));
|
||||
markdownlint(options, assertLintResultsCallback);
|
||||
|
||||
options.files = "../bad.md";
|
||||
assertLintResults(markdownlint.sync(options));
|
||||
markdownlint(options, assertLintResultsCallback);
|
||||
|
||||
const testRule = {
|
||||
"names": [ "test-rule" ],
|
||||
"description": "Test rule",
|
||||
"information": new URL("https://example.com/test-rule"),
|
||||
"tags": [ "test-tag" ],
|
||||
"function": function rule(params: markdownlint.RuleParams, onError: markdownlint.RuleOnError) {
|
||||
assert(!!params);
|
||||
assert(!!onError);
|
||||
let ruleParams: markdownlint.RuleParams;
|
||||
ruleParams = {
|
||||
"name": "name",
|
||||
"tokens": <markdownlint.MarkdownItToken[]>[],
|
||||
"lines": [
|
||||
"one",
|
||||
"two"
|
||||
],
|
||||
"frontMatterLines": [
|
||||
"three"
|
||||
],
|
||||
"config": options.config
|
||||
};
|
||||
assert(ruleParams);
|
||||
let ruleOnErrorInfo: markdownlint.RuleOnErrorInfo;
|
||||
ruleOnErrorInfo = {
|
||||
"lineNumber": 1,
|
||||
"details": "details",
|
||||
"context": "context",
|
||||
"range": [ 1, 2 ],
|
||||
"fixInfo": {
|
||||
"lineNumber": 1,
|
||||
"editColumn": 1,
|
||||
"deleteCount": 1,
|
||||
"insertText": "text"
|
||||
}
|
||||
};
|
||||
assert(ruleOnErrorInfo);
|
||||
false && onError(ruleOnErrorInfo);
|
||||
}
|
||||
};
|
||||
|
||||
options.customRules = [ testRule ];
|
||||
assertLintResults(markdownlint.sync(options));
|
||||
markdownlint(options, assertLintResultsCallback);
|
Loading…
Add table
Add a link
Reference in a new issue