feat(xo): type definition for v0.26 (#42542)

* feat(xo): type definition for v0.26

- definition file
- tests

https://github.com/xojs/xo/#config

Thanks!

* Correct export details as per PR comment

/cc @sheetalkamat

* Add missing `getFormatter` function as per PR comment

- implementation
- tests

/cc @sheetalkamat
This commit is contained in:
Piotr Błażejewicz (Peter Blazejewicz) 2020-02-27 00:36:40 +01:00 committed by GitHub
parent f740de01a3
commit 7752c903f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 155 additions and 0 deletions

66
types/xo/index.d.ts vendored Normal file
View File

@ -0,0 +1,66 @@
// Type definitions for xo 0.26
// Project: https://github.com/xojs/xo#readme
// Definitions by: Piotr Błażejewicz (Peter Blazejewicz) <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import eslint = require('eslint');
/**
* From T pick a set of properties K
*/
export type Pick<T, K extends keyof T> = {
[P in K]: T[P];
};
// eslint.CLIEngine.getErrorResults without modification
// eslint.CLIEngine.getFormatter without modification
// eslint.CLIEngine.outputFixes redeclared to match input shape
/**
* Can be used to filter out all the non error messages from the report object.
*/
export const getErrorResults: typeof eslint.CLIEngine.getErrorResults;
/**
* Returns the formatter representing the given format
* or null if no formatter with the given name can be found.
* see {@link https://github.com/eslint/eslint/blob/master/docs/developer-guide/nodejs-api.md#clienginegetformatter}
*/
export const getFormatter: typeof eslint.CLIEngine.prototype.getFormatter;
/**
* Used to output fixes from report to disk.
* It does by looking for files that have an output property in their results
*/
export function outputFixes(report: ResultReport): void;
export function lintText(text: string, options?: Options): ResultReport;
export function lintFiles(patterns: string | string[], options?: Options): ResultReport | Promise<ResultReport>;
export type CLIEngineOptions = Pick<
eslint.CLIEngine.Options,
'baseConfig' | 'cwd' | 'envs' | 'extensions' | 'fix' | 'globals' | 'ignore' | 'parser' | 'plugins' | 'rules'
>;
export type ESLintOptions = Pick<eslint.Linter.LintOptions, 'filename'>;
export type ESLintConfig = Pick<eslint.Linter.Config, 'extends' | 'settings'>;
export type Options = {
/** Enforce ES2015+ rules. Disabling this will make it not enforce ES2015+ syntax and conventions */
esnext?: boolean;
/** Some paths are ignored by default, including paths in .gitignore and .eslintignore. Additional ignores can be added here */
ignores?: string[];
/** Enable rules specific to the Node.js versions within the configured range */
nodeVersion?: string | boolean;
/** Format code with Prettier */
prettier?: boolean;
/** Set it to false to enforce no-semicolon style. */
semicolon?: boolean;
/** Set it to true to get 2-space indentation or specify the number of spaces. */
space?: number | string;
} & CLIEngineOptions &
ESLintConfig &
ESLintOptions;
export interface ResultReport {
readonly errorCount: number;
readonly warningCount: number;
readonly results: eslint.CLIEngine.LintResult[];
}

23
types/xo/tsconfig.json Normal file
View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"xo-tests.ts"
]
}

1
types/xo/tslint.json Normal file
View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

65
types/xo/xo-tests.ts Normal file
View File

@ -0,0 +1,65 @@
/// <reference types="node" />
import xo = require('xo');
import path = require('path');
const glob = path.join(__dirname, '..', '*.md');
const options: xo.Options = {};
if (options.semicolon === false && !options.prettier) {
if (options.rules) {
options.rules.semi = ['error', 'never'];
options.rules['semi-spacing'] = [
'error',
{
before: false,
after: true,
},
];
options.rules['node/no-unsupported-features/es-builtins'] = ['error', { version: options.nodeVersion }];
options.rules['node/no-unsupported-features/es-syntax'] = [
'error',
{ version: options.nodeVersion, ignores: ['modules'] },
];
options.rules['node/no-unsupported-features/node-builtins'] = ['error', { version: options.nodeVersion }];
}
if (options.plugins) {
options.plugins = options.plugins.concat('react');
}
if (options.prettier && options.plugins) {
options.plugins = options.plugins.concat('prettier');
if (options.baseConfig) {
options.baseConfig.extends = options.baseConfig.extends.concat('prettier');
options.baseConfig.extends = options.baseConfig.extends.concat('prettier/unicorn');
}
}
}
let result = xo.lintText("'use strict'\nconsole.log('unicorn');\n");
result = xo.lintText("'use strict'\nconsole.log('unicorn');\n", {
filename: 'ignored/index.js',
ignores: ['ignored/**/*.js'],
});
xo.lintText("'use strict'\nconsole.log('unicorn');\n", {
filename: 'node_modules/ignored/index.js',
});
result.errorCount; // $ExpectType number
result.warningCount; // $ExpectType number
result.results; // LintResult[]
(async () => {
const moreExtensionsResults = await xo.lintFiles(glob, { extensions: ['md'] });
const { errorCount, results, warningCount } = await xo.lintFiles('**/*', { cwd: 'fixtures/cwd' });
const report = await xo.lintFiles('**/*', { cwd: 'fixtures/cwd' });
// only get the error messages
const errorReport = xo.getErrorResults(result.results);
// output fixes to disk
xo.outputFixes(report);
// tests for the formatter
let formatter = xo.getFormatter();
formatter = xo.getFormatter('compact');
formatter = xo.getFormatter('./my/formatter.js');
xo.getFormatter('compact')(report.results);
// output to console
console.log(formatter(report.results));
})();