diff --git a/types/xo/index.d.ts b/types/xo/index.d.ts new file mode 100644 index 0000000000..4c6cef09dd --- /dev/null +++ b/types/xo/index.d.ts @@ -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) +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import eslint = require('eslint'); + +/** + * From T pick a set of properties K + */ +export type Pick = { + [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; + +export type CLIEngineOptions = Pick< + eslint.CLIEngine.Options, + 'baseConfig' | 'cwd' | 'envs' | 'extensions' | 'fix' | 'globals' | 'ignore' | 'parser' | 'plugins' | 'rules' +>; +export type ESLintOptions = Pick; +export type ESLintConfig = Pick; + +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[]; +} diff --git a/types/xo/tsconfig.json b/types/xo/tsconfig.json new file mode 100644 index 0000000000..d5305f922e --- /dev/null +++ b/types/xo/tsconfig.json @@ -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" + ] +} diff --git a/types/xo/tslint.json b/types/xo/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/xo/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/xo/xo-tests.ts b/types/xo/xo-tests.ts new file mode 100644 index 0000000000..508c4d9864 --- /dev/null +++ b/types/xo/xo-tests.ts @@ -0,0 +1,65 @@ +/// + +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)); +})();