Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: add JSDoc types for RuleTester test cases #12325

Merged
merged 9 commits into from Jan 3, 2020
51 changes: 50 additions & 1 deletion lib/rule-tester/rule-tester.js
Expand Up @@ -50,6 +50,52 @@ const

const ajv = require("../shared/ajv")({ strictDefaults: true });


//------------------------------------------------------------------------------
// Typedefs
//------------------------------------------------------------------------------

/**
* A test case that is expected to pass lint.
* @typedef {Object} ValidTestCase
* @property {string} code Code for the test case.
* @property {any[]} [options] Options for the test case.
* @property {{ [name: string]: any }} [settings] Settings for the test case.
* @property {string} [filename] The fake filename for the test case. Useful for rules that make assertion about filenames.
* @property {string} [parser] The absolute path for the parser.
* @property {{ [name: string]: any }} [parserOptions] Options for the parser.
* @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables.
* @property {{ [name: string]: boolean }} [env] Environments for the test case.
*/

/**
* A test case that is expected to fail lint.
* @typedef {Object} InvalidTestCase
* @property {string} code Code for the test case.
* @property {number | Array<TestCaseError | string>} errors Expected errors.
golopot marked this conversation as resolved.
Show resolved Hide resolved
* @property {string | null} [output] The expected code after autofixes are applied. If set to `null`, the test runner will assert that no autofix is suggested.
* @property {any[]} [options] Options for the test case.
* @property {{ [name: string]: any }} [settings] Settings for the test case.
* @property {string} [filename] The fake filename for the test case. Useful for rules that make assertion about filenames.
* @property {string} [parser] The absolute path for the parser.
* @property {{ [name: string]: any }} [parserOptions] Options for the parser.
* @property {{ [name: string]: "readonly" | "writable" | "off" }} [globals] The additional global variables.
* @property {{ [name: string]: boolean }} [env] Environments for the test case.
*/
golopot marked this conversation as resolved.
Show resolved Hide resolved

/**
* A description of a reported error used in a rule tester test.
* @typedef {Object} TestCaseError
* @property {string | RegExp} [message] Message.
* @property {string} [messageId] Message ID.
* @property {string} [type] The type of the reported AST node.
* @property {{ [name: string]: string }} [data] The data used to fill the message template.
* @property {number} [line] The 1-based line number of the reported start location.
* @property {number} [column] The 1-based column number of the reported start location.
* @property {number} [endLine] The 1-based line number of the reported end location.
* @property {number} [endColumn] The 1-based column number of the reported end location.
*/

//------------------------------------------------------------------------------
// Private Members
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -276,7 +322,10 @@ class RuleTester {
* Adds a new rule test to execute.
* @param {string} ruleName The name of the rule to run.
* @param {Function} rule The rule to test.
* @param {Object} test The collection of tests to run.
* @param {{
* valid: (ValidTestCase | string)[],
* invalid: InvalidTestCase[]
* }} test The collection of tests to run.
* @returns {void}
*/
run(ruleName, rule, test) {
Expand Down