Skip to content

Commit 6802ecc

Browse files
authored
Merge pull request #45 from bmish/linting
Add/fix internal linting
2 parents b8b98d5 + 940903e commit 6802ecc

File tree

13 files changed

+356
-100
lines changed

13 files changed

+356
-100
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Compiled
2+
lib/

.eslintrc.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = {
2+
root: true,
3+
parserOptions: {
4+
ecmaVersion: 2021,
5+
sourceType: 'module',
6+
},
7+
extends: [
8+
'eslint:recommended',
9+
'plugin:eslint-plugin/recommended',
10+
'plugin:node/recommended',
11+
'plugin:prettier/recommended',
12+
],
13+
env: {
14+
node: true,
15+
},
16+
settings: {
17+
node: {
18+
tryExtensions: ['.js', '.json', '.node', '.ts', '.d.ts'],
19+
},
20+
},
21+
rules: {},
22+
overrides: [
23+
{
24+
files: ['test/**/*.js'],
25+
env: {
26+
mocha: true,
27+
},
28+
},
29+
{
30+
parser: '@typescript-eslint/parser',
31+
files: ['*.ts'],
32+
extends: ['plugin:@typescript-eslint/recommended'],
33+
rules: {
34+
'node/no-unsupported-features/es-syntax': ['error', { ignores: ['modules'] }],
35+
},
36+
},
37+
],
38+
};

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ test
22
src
33
docs
44
node_modules
5-
test-results
5+
test-results
6+
gulpfile.js

gulpfile.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ const SRC = 'src/**/?(*.js|*.ts)';
66
const DEST = 'lib';
77
const tsProject = ts.createProject('tsconfig.json');
88

9-
gulp.task('clean', function(done) {
9+
gulp.task('clean', function (done) {
1010
rimraf(DEST, done);
1111
});
1212

13-
gulp.task('src', ['clean'], function() {
14-
return gulp
15-
.src(SRC)
16-
.pipe(tsProject())
17-
.pipe(gulp.dest(DEST));
13+
gulp.task('src', ['clean'], function () {
14+
return gulp.src(SRC).pipe(tsProject()).pipe(gulp.dest(DEST));
1815
});
1916

2017
gulp.task('prepublish', ['src']);

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
"main": "lib/index.js",
66
"scripts": {
77
"build": "gulp src",
8+
"lint": "npm run lint:js",
9+
"lint:js": "eslint --cache .",
810
"prepublish": "npm run build",
911
"test": "npm run build && npm run test-quick",
1012
"test-quick": "NODE_PATH=./lib nyc -s mocha -R dot --recursive test -t test-results"
1113
},
1214
"keywords": [
1315
"eslint",
16+
"eslint-plugin",
17+
"eslintplugin",
1418
"import",
1519
"eslint-plugin-import",
1620
"configurable"
@@ -25,12 +29,18 @@
2529
},
2630
"devDependencies": {
2731
"@types/node": "^14.17.33",
32+
"@typescript-eslint/eslint-plugin": "^5.9.0",
2833
"@typescript-eslint/parser": "^5.3.1",
29-
"eslint": "^8.2.0",
34+
"eslint": "^8.6.0",
35+
"eslint-config-prettier": "^8.3.0",
36+
"eslint-plugin-eslint-plugin": "^4.1.0",
37+
"eslint-plugin-node": "^11.1.0",
38+
"eslint-plugin-prettier": "^4.0.0",
3039
"gulp": "^3.9.0",
3140
"gulp-typescript": "^5.0.1",
3241
"mocha": "^6.1.2",
3342
"nyc": "^13.3.0",
43+
"prettier": "^2.5.1",
3444
"rimraf": "^3.0.2",
3545
"typescript": "^4.4.4"
3646
},

src/index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import orderImports from './rules/order-imports';
2+
3+
export = {
4+
rules: {
5+
'order-imports': orderImports,
6+
},
7+
};

src/rules/order-imports.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type Imported = { name: string; rank: number; node: NodeOrToken };
3535

3636
function reverse(array: Imported[]) {
3737
return array
38-
.map(function(v) {
38+
.map(function (v) {
3939
return {
4040
name: v.name,
4141
rank: -v.rank,
@@ -102,7 +102,7 @@ function findOutOfOrder(imported) {
102102
return [];
103103
}
104104
let maxSeenRankNode = imported[0];
105-
return imported.filter(function(importedModule) {
105+
return imported.filter(function (importedModule) {
106106
const res = importedModule.rank < maxSeenRankNode.rank;
107107
if (maxSeenRankNode.rank < importedModule.rank) {
108108
maxSeenRankNode = importedModule;
@@ -121,7 +121,7 @@ function findRootNode(node) {
121121

122122
function findEndOfLineWithComments(sourceCode, node) {
123123
const tokensToEndOfLine = takeTokensAfterWhile(sourceCode, node, commentOnSameLineAs(node));
124-
let endOfTokens =
124+
const endOfTokens =
125125
tokensToEndOfLine.length > 0 ? tokensToEndOfLine[tokensToEndOfLine.length - 1].range[1] : node.range[1];
126126
let result = endOfTokens;
127127
for (let i = endOfTokens; i < sourceCode.text.length; i++) {
@@ -146,7 +146,7 @@ function commentOnSameLineAs(node): (token: NodeOrToken) => boolean {
146146

147147
function findStartOfLineWithComments(sourceCode, node) {
148148
const tokensToEndOfLine = takeTokensBeforeWhile(sourceCode, node, commentOnSameLineAs(node));
149-
let startOfTokens = tokensToEndOfLine.length > 0 ? tokensToEndOfLine[0].range[0] : node.range[0];
149+
const startOfTokens = tokensToEndOfLine.length > 0 ? tokensToEndOfLine[0].range[0] : node.range[0];
150150
let result = startOfTokens;
151151
for (let i = startOfTokens - 1; i > 0; i--) {
152152
if (sourceCode.text[i] !== ' ' && sourceCode.text[i] !== '\t') {
@@ -192,7 +192,7 @@ function canReorderItems(firstNode: NodeOrToken, secondNode: NodeOrToken): boole
192192
const firstIndex = parent.body.indexOf(firstNode);
193193
const secondIndex = parent.body.indexOf(secondNode);
194194
const nodesBetween = parent.body.slice(firstIndex, secondIndex + 1);
195-
for (var nodeBetween of nodesBetween) {
195+
for (const nodeBetween of nodesBetween) {
196196
if (!canCrossNodeWhileReorder(nodeBetween)) {
197197
return false;
198198
}
@@ -247,7 +247,7 @@ function fixOutOfOrder(context, firstNode: NodeOrToken, secondNode: NodeOrToken,
247247
}
248248

249249
function reportOutOfOrder(context, imported: Imported[], outOfOrder, order: 'before' | 'after'): void {
250-
outOfOrder.forEach(function(imp) {
250+
outOfOrder.forEach(function (imp) {
251251
const found = imported.find(function hasHigherRank(importedItem) {
252252
return importedItem.rank > imp.rank;
253253
});
@@ -271,7 +271,7 @@ function makeOutOfOrderReport(context, imported: Imported[]) {
271271
}
272272

273273
function mutateRanksToAlphabetize(imported, order, ignoreCase) {
274-
const groupedByRanks = imported.reduce(function(acc, importedItem) {
274+
const groupedByRanks = imported.reduce(function (acc, importedItem) {
275275
acc[importedItem.rank] = acc[importedItem.rank] || [];
276276
acc[importedItem.rank].push(importedItem.name);
277277
return acc;
@@ -280,8 +280,8 @@ function mutateRanksToAlphabetize(imported, order, ignoreCase) {
280280
const groupRanks = Object.keys(groupedByRanks);
281281

282282
// sort imports locally within their group
283-
groupRanks.forEach(function(groupRank) {
284-
groupedByRanks[groupRank].sort(function(importA, importB) {
283+
groupRanks.forEach(function (groupRank) {
284+
groupedByRanks[groupRank].sort(function (importA, importB) {
285285
return ignoreCase ? importA.localeCompare(importB) : importA < importB ? -1 : importA === importB ? 0 : 1;
286286
});
287287

@@ -291,15 +291,15 @@ function mutateRanksToAlphabetize(imported, order, ignoreCase) {
291291
});
292292

293293
// add decimal ranking to sort within the group
294-
const alphabetizedRanks = groupRanks.sort().reduce(function(acc, groupRank) {
295-
groupedByRanks[groupRank].forEach(function(importedItemName, index) {
294+
const alphabetizedRanks = groupRanks.sort().reduce(function (acc, groupRank) {
295+
groupedByRanks[groupRank].forEach(function (importedItemName, index) {
296296
acc[importedItemName] = +groupRank + index / MAX_GROUP_SIZE;
297297
});
298298
return acc;
299299
}, {});
300300

301301
// mutate the original group-rank with alphabetized-rank
302-
imported.forEach(function(importedItem) {
302+
imported.forEach(function (importedItem) {
303303
importedItem.rank = alphabetizedRanks[importedItem.name];
304304
});
305305
}
@@ -333,9 +333,9 @@ const knownTypes: KnownImportType[] = ['absolute', 'module', 'parent', 'sibling'
333333
// Example: { index: 0, sibling: 1, parent: 1, module: 2 }
334334
// Will throw an error if it: contains a type that does not exist in the list, does not start and end with '/', or has a duplicate
335335
function convertGroupsToRanks(groups: Groups): Ranks {
336-
const rankObject = groups.reduce(function(res, group, index) {
336+
const rankObject = groups.reduce(function (res, group, index) {
337337
if (typeof group === 'string') group = [group]; // wrap them all in arrays
338-
group.forEach(function(groupItem: ValidImportType) {
338+
group.forEach(function (groupItem: ValidImportType) {
339339
if (!isRegularExpressionGroup(groupItem) && knownTypes.indexOf(groupItem as KnownImportType) === -1) {
340340
throw new Error(
341341
`Incorrect configuration of the rule: Unknown type ${JSON.stringify(
@@ -351,11 +351,11 @@ function convertGroupsToRanks(groups: Groups): Ranks {
351351
return res;
352352
}, {});
353353

354-
const omittedTypes = knownTypes.filter(function(type) {
354+
const omittedTypes = knownTypes.filter(function (type) {
355355
return rankObject[type] === undefined;
356356
});
357357

358-
return omittedTypes.reduce(function(res, type) {
358+
return omittedTypes.reduce(function (res, type) {
359359
res[type] = groups.length;
360360
return res;
361361
}, rankObject);
@@ -400,7 +400,7 @@ function makeNewlinesBetweenReport(
400400
};
401401
let previousImport = imported[0];
402402

403-
imported.slice(1).forEach(function(currentImport) {
403+
imported.slice(1).forEach(function (currentImport) {
404404
const emptyLinesBetween: number = getNumberOfEmptyLinesBetween(currentImport, previousImport);
405405

406406
const currentGroupRank = Math.floor(currentImport.rank); // each group rank is a whole number, within a group, decimals indicate subranking. yeah, not great.
@@ -479,7 +479,7 @@ function getAlphabetizeConfig(options: RuleOptions): AlphabetizeConfig {
479479
return { order, ignoreCase };
480480
}
481481

482-
module.exports = {
482+
export default {
483483
meta: {
484484
type: 'suggestion',
485485
docs: {
@@ -531,8 +531,8 @@ module.exports = {
531531
} catch (error) {
532532
// Malformed configuration
533533
return {
534-
Program: function(node) {
535-
context.report(node, error.message);
534+
Program: function (node) {
535+
context.report({ node, message: error.message });
536536
},
537537
};
538538
}

src/util/import-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export function isAbsolute(name: string): boolean {
33
}
44

55
// a module is anything that doesn't start with a . or a / or a \
6-
const moduleRegExp = /^[^\/\\.]/;
6+
const moduleRegExp = /^[^/\\.]/;
77
export function isModule(name: string): boolean {
88
return moduleRegExp.test(name);
99
}

test/.eslintrc

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)