Skip to content

Commit 564fe00

Browse files
committedMar 16, 2019
Add rule to check for types property in package.json - fixes #11
1 parent 718cdd8 commit 564fe00

File tree

22 files changed

+96
-11
lines changed

22 files changed

+96
-11
lines changed
 

‎source/lib/rules/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import filesProperty from './files-property';
2+
import typesProperty from './types-property';
23
import {Diagnostic, Context} from '../interfaces';
34

45
type RuleFunction = (context: Context) => Diagnostic[];
56

67
// List of custom rules
78
const rules = new Set<RuleFunction>([
8-
filesProperty
9+
filesProperty,
10+
typesProperty
911
]);
1012

1113
/**

‎source/lib/rules/types-property.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as path from 'path';
2+
import * as fs from 'fs';
3+
import {Context, Diagnostic} from '../interfaces';
4+
import {getJSONPropertyPosition} from '../utils';
5+
6+
/**
7+
* Rule which enforces the use of a `types` property over a `typings` property.
8+
*
9+
* @param context - The context object.
10+
* @returns A list of custom diagnostics.
11+
*/
12+
export default (context: Context): Diagnostic[] => {
13+
const {pkg} = context;
14+
15+
if (!pkg.types && !pkg.typings) {
16+
return [
17+
{
18+
fileName: 'package.json',
19+
message: 'Can\'t find `types` property.',
20+
severity: 'error'
21+
}
22+
];
23+
}
24+
25+
if (!pkg.types && pkg.typings) {
26+
const content = fs.readFileSync(path.join(context.cwd, 'package.json'), 'utf8');
27+
28+
return [
29+
{
30+
fileName: 'package.json',
31+
message: 'Use property `types` instead of `typings`.',
32+
severity: 'error',
33+
...getJSONPropertyPosition(content, 'typings')
34+
}
35+
];
36+
}
37+
38+
return [];
39+
};
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"name": "foo"
2+
"name": "foo",
3+
"types": "index.d.ts"
34
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"name": "foo"
2+
"name": "foo",
3+
"types": "index.d.ts"
34
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"name": "foo"
2+
"name": "foo",
3+
"types": "index.d.ts"
34
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"name": "foo"
2+
"name": "foo",
3+
"types": "index.d.ts"
34
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"name": "foo"
2+
"name": "foo",
3+
"types": "index.d.ts"
34
}
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"name": "foo"
2+
"name": "foo",
3+
"types": "index.d.ts"
34
}

‎source/test/fixtures/no-files/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "foo",
3+
"types": "index.d.ts",
34
"files": [
45
"index.js"
56
]
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"name": "foo"
2+
"name": "foo",
3+
"types": "index.d.ts"
34
}

‎source/test/fixtures/non-strict-check-with-config/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "foo",
3+
"types": "index.d.ts",
34
"tsd-check": {
45
"compilerOptions": {
56
"strict": false

‎source/test/fixtures/success/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "foo",
3+
"types": "index.d.ts",
34
"files": [
45
"index.js",
56
"index.d.ts"

‎source/test/fixtures/test-in-subdir/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "foo",
33
"main": "src/index.js",
4-
"typings": "src/index.d.ts",
4+
"types": "src/index.d.ts",
55
"files": [
66
"src/index.js",
77
"src/index.d.ts"

‎source/test/fixtures/test-non-barrel-main/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "foo",
33
"main": "foo.js",
4-
"typings": "foo.d.ts",
4+
"types": "foo.d.ts",
55
"files": [
66
"foo.js",
77
"foo.d.ts"

‎source/test/fixtures/top-level-await/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "foo",
3+
"types": "index.d.ts",
34
"files": [
45
"index.js",
56
"index.d.ts"

‎source/test/fixtures/types-property/no-property/index.d.ts

Whitespace-only changes.

‎source/test/fixtures/types-property/no-property/index.test-d.ts

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "foo"
3+
}

‎source/test/fixtures/types-property/typings/index.d.ts

Whitespace-only changes.

‎source/test/fixtures/types-property/typings/index.test-d.ts

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "foo",
3+
"typings": "index.d.ts"
4+
}

‎source/test/test.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,38 @@ test('fail if typings file is not part of `files` list', async t => {
3737
fileName: 'package.json',
3838
message: 'TypeScript type definition `index.d.ts` is not part of the `files` list.',
3939
severity: 'error',
40-
line: 3,
40+
line: 4,
4141
column: 1
4242
}
4343
]);
4444
});
4545

46+
test('fail if `types` property is not set', async t => {
47+
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/types-property/no-property')});
48+
49+
t.deepEqual(diagnostics, [
50+
{
51+
fileName: 'package.json',
52+
message: 'Can\'t find `types` property.',
53+
severity: 'error'
54+
}
55+
]);
56+
});
57+
58+
test('fail if `typings` property is is used instead of `types`', async t => {
59+
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/types-property/typings')});
60+
61+
t.deepEqual(diagnostics, [
62+
{
63+
fileName: 'package.json',
64+
message: 'Use property `types` instead of `typings`.',
65+
severity: 'error',
66+
column: 1,
67+
line: 3
68+
}
69+
]);
70+
});
71+
4672
test('fail if tests don\'t pass in strict mode', async t => {
4773
const diagnostics = await m({
4874
cwd: path.join(__dirname, 'fixtures/failure-strict-null-checks')

0 commit comments

Comments
 (0)
Please sign in to comment.