Skip to content

Commit ca2aa27

Browse files
authored
chore: port lowercase-name to TypeScript (#258)
1 parent 3df0058 commit ca2aa27

File tree

11 files changed

+382
-123
lines changed

11 files changed

+382
-123
lines changed

.eslintrc.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
const { globals } = require('./').environments.globals;
44

55
module.exports = {
6+
parser: '@typescript-eslint/parser',
67
extends: [
7-
'eslint:recommended',
88
'plugin:eslint-plugin/recommended',
99
'plugin:node/recommended',
10+
'plugin:@typescript-eslint/eslintRecommended',
1011
'prettier',
1112
],
12-
plugins: ['eslint-plugin', 'node', 'prettier'],
13+
plugins: ['eslint-plugin', 'node', 'prettier', '@typescript-eslint'],
1314
parserOptions: {
1415
ecmaVersion: 2017,
1516
},
@@ -37,8 +38,14 @@ module.exports = {
3738
},
3839
overrides: [
3940
{
40-
files: ['*.test.js'],
41+
files: ['*.test.js', '*.test.ts'],
4142
globals,
4243
},
44+
{
45+
files: ['*.ts'],
46+
parserOptions: {
47+
sourceType: 'module',
48+
},
49+
},
4350
],
4451
};

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ script:
1414
- yarn commitlint --from="$TRAVIS_BRANCH" --to="$TRAVIS_COMMIT"
1515
- yarn commitlint --from=$TRAVIS_COMMIT
1616
- yarn prettylint
17+
- yarn typecheck
1718
- yarn test --coverage --maxWorkers 2
1819
after_script: greenkeeper-lockfile-upload
1920
jobs:

babel.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

33
module.exports = {
4-
presets: [['@babel/preset-env', { targets: { node: 6 } }]],
4+
presets: [
5+
'@babel/preset-typescript',
6+
['@babel/preset-env', { targets: { node: 6 } }],
7+
],
58
};

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,25 @@
2828
},
2929
"scripts": {
3030
"postinstall": "yarn build",
31-
"lint": "eslint . --ignore-pattern '!.eslintrc.js'",
31+
"lint": "eslint . --ignore-pattern '!.eslintrc.js' --ext js,ts",
3232
"prettylint": "prettylint docs/**/*.md README.md package.json",
3333
"prepublishOnly": "yarn build",
3434
"test": "jest",
35-
"build": "babel src --out-dir lib"
35+
"build": "babel --extensions .js,.ts src --out-dir lib",
36+
"typecheck": "tsc -p ."
3637
},
3738
"devDependencies": {
3839
"@babel/cli": "^7.4.4",
3940
"@babel/core": "^7.4.4",
4041
"@babel/preset-env": "^7.4.4",
42+
"@babel/preset-typescript": "^7.3.3",
4143
"@commitlint/cli": "^7.0.0",
4244
"@commitlint/config-conventional": "^7.0.1",
45+
"@types/eslint": "^4.16.6",
46+
"@types/jest": "^24.0.12",
47+
"@types/node": "^12.0.0",
48+
"@typescript-eslint/eslint-plugin": "^1.8.0",
49+
"@typescript-eslint/experimental-utils": "^1.8.0",
4350
"babel-jest": "^24.8.0",
4451
"eslint": "^5.1.0",
4552
"eslint-config-prettier": "^4.1.0",
@@ -51,15 +58,16 @@
5158
"jest-runner-eslint": "^0.7.1",
5259
"lint-staged": "^8.0.4",
5360
"prettier": "^1.10.2",
54-
"prettylint": "^1.0.0"
61+
"prettylint": "^1.0.0",
62+
"typescript": "^3.4.5"
5563
},
5664
"prettier": {
5765
"proseWrap": "always",
5866
"singleQuote": true,
5967
"trailingComma": "all"
6068
},
6169
"lint-staged": {
62-
"*.js": [
70+
"*.{js,ts}": [
6371
"eslint --fix",
6472
"git add"
6573
],

src/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@
33
const fs = require('fs');
44
const path = require('path');
55

6+
// copied from https://github.com/babel/babel/blob/56044c7851d583d498f919e9546caddf8f80a72f/packages/babel-helpers/src/helpers.js#L558-L562
7+
function interopRequireDefault(obj) {
8+
return obj && obj.__esModule ? obj : { default: obj };
9+
}
10+
611
const rules = fs
712
.readdirSync(path.join(__dirname, 'rules'))
8-
.filter(rule => rule !== '__tests__' && rule !== 'util.js')
9-
.map(rule => path.basename(rule, '.js'))
13+
.filter(
14+
rule => rule !== '__tests__' && rule !== 'util.js' && rule !== 'tsUtils.ts',
15+
)
16+
.map(rule =>
17+
rule.endsWith('js')
18+
? path.basename(rule, '.js')
19+
: path.basename(rule, '.ts'),
20+
)
1021
.reduce(
11-
(acc, curr) => Object.assign(acc, { [curr]: require(`./rules/${curr}`) }),
22+
(acc, curr) => ({
23+
...acc,
24+
[curr]: interopRequireDefault(require(`./rules/${curr}`)).default,
25+
}),
1226
{},
1327
);
1428

src/rules/__tests__/lowercase-name.test.js renamed to src/rules/__tests__/lowercase-name.test.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
'use strict';
1+
import { RuleTester as ESLintRuleTester } from 'eslint';
2+
import { TSESLint } from '@typescript-eslint/experimental-utils';
3+
import rule from '../lowercase-name';
24

3-
const { RuleTester } = require('eslint');
4-
const rule = require('../lowercase-name');
5+
const RuleTester: TSESLint.RuleTester = ESLintRuleTester as any;
56

67
const ruleTester = new RuleTester({
8+
parser: '@typescript-eslint/parser',
79
parserOptions: {
810
ecmaVersion: 6,
911
},
1012
});
1113

1214
ruleTester.run('lowercase-name', rule, {
1315
valid: [
16+
'randomFunction()',
17+
'foo.bar()',
1418
'it()',
1519
"it(' ', function () {})",
1620
'it(" ", function () {})',
@@ -22,6 +26,8 @@ ruleTester.run('lowercase-name', rule, {
2226
'it("123 foo", function () {})',
2327
'it(42, function () {})',
2428
'it(``)',
29+
'it("")',
30+
'it(42)',
2531
'test()',
2632
"test('foo', function () {})",
2733
'test("foo", function () {})',
@@ -30,6 +36,8 @@ ruleTester.run('lowercase-name', rule, {
3036
'test("123 foo", function () {})',
3137
'test("42", function () {})',
3238
'test(``)',
39+
'test("")',
40+
'test(42)',
3341
'describe()',
3442
"describe('foo', function () {})",
3543
'describe("foo", function () {})',
@@ -39,6 +47,8 @@ ruleTester.run('lowercase-name', rule, {
3947
'describe("42", function () {})',
4048
'describe(function () {})',
4149
'describe(``)',
50+
'describe("")',
51+
'describe(42)',
4252
],
4353

4454
invalid: [
@@ -47,7 +57,8 @@ ruleTester.run('lowercase-name', rule, {
4757
output: "it('foo', function () {})",
4858
errors: [
4959
{
50-
message: '`it`s should begin with lowercase',
60+
messageId: 'unexpectedLowercase',
61+
data: { method: 'it' },
5162
column: 1,
5263
line: 1,
5364
},
@@ -58,7 +69,8 @@ ruleTester.run('lowercase-name', rule, {
5869
output: 'it("foo", function () {})',
5970
errors: [
6071
{
61-
message: '`it`s should begin with lowercase',
72+
messageId: 'unexpectedLowercase',
73+
data: { method: 'it' },
6274
column: 1,
6375
line: 1,
6476
},
@@ -69,7 +81,8 @@ ruleTester.run('lowercase-name', rule, {
6981
output: 'it(`foo`, function () {})',
7082
errors: [
7183
{
72-
message: '`it`s should begin with lowercase',
84+
messageId: 'unexpectedLowercase',
85+
data: { method: 'it' },
7386
column: 1,
7487
line: 1,
7588
},
@@ -80,7 +93,8 @@ ruleTester.run('lowercase-name', rule, {
8093
output: "test('foo', function () {})",
8194
errors: [
8295
{
83-
message: '`test`s should begin with lowercase',
96+
messageId: 'unexpectedLowercase',
97+
data: { method: 'test' },
8498
column: 1,
8599
line: 1,
86100
},
@@ -91,7 +105,8 @@ ruleTester.run('lowercase-name', rule, {
91105
output: 'test("foo", function () {})',
92106
errors: [
93107
{
94-
message: '`test`s should begin with lowercase',
108+
messageId: 'unexpectedLowercase',
109+
data: { method: 'test' },
95110
column: 1,
96111
line: 1,
97112
},
@@ -102,7 +117,8 @@ ruleTester.run('lowercase-name', rule, {
102117
output: 'test(`foo`, function () {})',
103118
errors: [
104119
{
105-
message: '`test`s should begin with lowercase',
120+
messageId: 'unexpectedLowercase',
121+
data: { method: 'test' },
106122
column: 1,
107123
line: 1,
108124
},
@@ -113,7 +129,8 @@ ruleTester.run('lowercase-name', rule, {
113129
output: "describe('foo', function () {})",
114130
errors: [
115131
{
116-
message: '`describe`s should begin with lowercase',
132+
messageId: 'unexpectedLowercase',
133+
data: { method: 'describe' },
117134
column: 1,
118135
line: 1,
119136
},
@@ -124,7 +141,8 @@ ruleTester.run('lowercase-name', rule, {
124141
output: 'describe("foo", function () {})',
125142
errors: [
126143
{
127-
message: '`describe`s should begin with lowercase',
144+
messageId: 'unexpectedLowercase',
145+
data: { method: 'describe' },
128146
column: 1,
129147
line: 1,
130148
},
@@ -135,7 +153,8 @@ ruleTester.run('lowercase-name', rule, {
135153
output: 'describe(`foo`, function () {})',
136154
errors: [
137155
{
138-
message: '`describe`s should begin with lowercase',
156+
messageId: 'unexpectedLowercase',
157+
data: { method: 'describe' },
139158
column: 1,
140159
line: 1,
141160
},
@@ -146,7 +165,8 @@ ruleTester.run('lowercase-name', rule, {
146165
output: 'describe(`some longer description`, function () {})',
147166
errors: [
148167
{
149-
message: '`describe`s should begin with lowercase',
168+
messageId: 'unexpectedLowercase',
169+
data: { method: 'describe' },
150170
column: 1,
151171
line: 1,
152172
},

src/rules/lowercase-name.js

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

0 commit comments

Comments
 (0)