Skip to content

Commit

Permalink
added TS test cases
Browse files Browse the repository at this point in the history
moved some generic AST functions into a common util file
  • Loading branch information
adashrod committed Feb 25, 2023
1 parent 556b2cd commit c3082c6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ dist

# TernJS port file
.tern-port

# IDE
.vscode
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Configure with EsLint, e.g. in `.eslintrc.json`
"ignoreSingleWords": false
}
],
"sequence/logical-expression-complexity": [
"sequence/logical-expression-complexity": [
"error", {
"maxHeight": 3,
"maxTerms": 6,
Expand Down
36 changes: 1 addition & 35 deletions src/lib/rules/ordered-import-members.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { findPunctuatorAfter, findPunctuatorBetween } = require("./util/ast.js");
const { nullishCoalesce } = require("./util/misc-js.js");

/**
Expand Down Expand Up @@ -56,41 +57,6 @@ module.exports = {
return nameA > nameB ? 1 : -1;
}

/**
* Finds the first matching punctuator at document position greater than or equal to startPos.
*
* @param {Ast.Token[]} tokens array of program tokens
* @param {number} startPos minimum document position
* @param {string} punctuator a punctuator, such as ","
* @returns found token or undefined if not found
*/
function findPunctuatorAfter(tokens, startPos, punctuator) {
return tokens.find(token =>
token.type === "Punctuator" &&
token.value === punctuator &&
token.range[0] >= startPos
);
}

/**
* Finds the first matching punctuator at document position greater than or equal to startPos and less than
* endPos.
*
* @param {Ast.Token[]} tokens array of program tokens
* @param {number} startPos minimum document position (inclusive)
* @param {number} endPos maximum document position (exclusive)
* @param {string} punctuator a punctuator, such as ","
* @returns found token or undefined if not found
*/
function findPunctuatorBetween(tokens, startPos, endPos, punctuator) {
return tokens.find(token =>
token.type === "Punctuator" &&
token.value === punctuator &&
token.range[0] >= startPos &&
token.range[1] < endPos
)
}

/**
* Given a list of specifiers that need to be sorted, and don't have surrounding comments, sort them by
* specifier name.
Expand Down
36 changes: 36 additions & 0 deletions src/lib/rules/util/ast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = {
/**
* Finds the first matching punctuator at document position greater than or equal to startPos.
*
* @param {Ast.Token[]} tokens array of program tokens
* @param {number} startPos minimum document position
* @param {string} punctuator a punctuator, such as ","
* @returns found token or undefined if not found
*/
findPunctuatorAfter(tokens, startPos, punctuator) {
return tokens.find(token =>
token.type === "Punctuator" &&
token.value === punctuator &&
token.range[0] >= startPos
);
},

/**
* Finds the first matching punctuator at document position greater than or equal to startPos and less than
* endPos.
*
* @param {Ast.Token[]} tokens array of program tokens
* @param {number} startPos minimum document position (inclusive)
* @param {number} endPos maximum document position (exclusive)
* @param {string} punctuator a punctuator, such as ","
* @returns found token or undefined if not found
*/
findPunctuatorBetween(tokens, startPos, endPos, punctuator) {
return tokens.find(token =>
token.type === "Punctuator" &&
token.value === punctuator &&
token.range[0] >= startPos &&
token.range[1] < endPos
);
}
};
21 changes: 19 additions & 2 deletions src/tests/lib/rules/ordered-import-members.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const rule = require("../../../lib/rules/ordered-import-members");
const { RuleTester } = require("eslint");

const ruleTester = new RuleTester({
const esRuleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
}
});

ruleTester.run("ordered-import-members", rule, {
esRuleTester.run("ordered-import-members", rule, {
valid: [
`import { Alpha, Bravo, Charlie } from "alphabet";`,
{
Expand Down Expand Up @@ -113,3 +113,20 @@ ruleTester.run("ordered-import-members", rule, {
`} from "alphabet";`
}]
});

const tsRuleTester = new RuleTester({
parser: require.resolve('@typescript-eslint/parser')
});

tsRuleTester.run("ordered-import-members", rule, {
valid: [`import { annoying, requirement, requires, valid } from "ugh";`],
invalid: [{
code: `import type { OnInit } from "@angular/core";\n` +
`import { ElementRef, HostListener, Directive, Input } from "@angular/core";`,
errors: [{
messageId: "sortMembersAlphabetically"
}],
output: `import type { OnInit } from "@angular/core";\n` +
`import { Directive, ElementRef, HostListener, Input } from "@angular/core";`
}]
});

0 comments on commit c3082c6

Please sign in to comment.