Skip to content

Commit c445879

Browse files
Add support for JSX - fixes #15 (#36)
1 parent 03c170b commit c445879

File tree

17 files changed

+108
-22
lines changed

17 files changed

+108
-22
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
},
5050
"devDependencies": {
5151
"@types/node": "^11.10.4",
52+
"@types/react": "^16.9.2",
5253
"@types/update-notifier": "^2.2.0",
5354
"ava": "^1.4.1",
5455
"cpy-cli": "^2.0.0",
5556
"del-cli": "^1.1.0",
57+
"react": "^16.9.0",
5658
"tslint": "^5.11.0",
5759
"tslint-xo": "^0.9.0"
5860
}

source/lib/index.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ const findTypingsFile = async (pkg: any, options: Options) => {
2525

2626
const findTestFiles = async (typingsFile: string, options: Options & {config: Config}) => {
2727
const testFile = typingsFile.replace(/\.d\.ts$/, '.test-d.ts');
28+
const tsxTestFile = typingsFile.replace(/\.d\.ts$/, '.test-d.tsx');
2829
const testDir = options.config.directory;
2930

30-
const testFileExists = await pathExists(path.join(options.cwd, testFile));
31+
let testFiles = await globby([testFile, tsxTestFile], {cwd: options.cwd});
32+
3133
const testDirExists = await pathExists(path.join(options.cwd, testDir));
3234

33-
if (!testFileExists && !testDirExists) {
34-
throw new Error(`The test file \`${testFile}\` does not exist. Create one and try again.`);
35+
if (testFiles.length === 0 && !testDirExists) {
36+
throw new Error(`The test file \`${testFile}\` or \`${tsxTestFile}\` does not exist. Create one and try again.`);
3537
}
3638

37-
let testFiles = [testFile];
38-
39-
if (!testFileExists) {
40-
testFiles = await globby(`${testDir}/**/*.ts`, {cwd: options.cwd});
39+
if (testFiles.length === 0) {
40+
testFiles = await globby([`${testDir}/**/*.ts`, `${testDir}/**/*.tsx`], {cwd: options.cwd});
4141
}
4242

4343
return testFiles;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
declare const document: Document;
1+
declare const window: Window;
22

3-
export default document;
3+
export default window;
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports.default = window.document;
1+
module.exports.default = window;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import {expectType} from '../../../..';
2-
import document from '.';
2+
import window from '.';
33

4-
expectType<Document>(document);
4+
expectType<Window>(window);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference lib="dom"/>
22

3-
declare const document: Document;
3+
declare const window: Window;
44

5-
export default document;
5+
export default window;
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports.default = window.document;
1+
module.exports.default = window;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import {expectType} from '../../../..';
2-
import document from '.';
2+
import window from '.';
33

4-
expectType<Document>(document);
4+
expectType<Window>(window);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {Component} from 'react';
2+
3+
interface UnicornProps {
4+
rainbow: string;
5+
}
6+
7+
export class Unicorn extends Component<UnicornProps> {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
const React = require('react');
3+
4+
export class Unicorn extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
}
8+
9+
render() {
10+
return <h1>{this.props.rainbow}</h1>;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "foo",
3+
"files": [
4+
"index.js",
5+
"index.d.ts"
6+
],
7+
"dependencies": {
8+
"react": "*"
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from 'react';
2+
import {expectType, expectError} from '../../../../..';
3+
import {Unicorn} from '..';
4+
5+
expectType<JSX.Element>(<Unicorn rainbow='🌈' />);
6+
7+
expectError(<Unicorn foo='bar' />);

source/test/fixtures/tsx/index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {Component} from 'react';
2+
3+
interface UnicornProps {
4+
rainbow: string;
5+
}
6+
7+
export class Unicorn extends Component<UnicornProps> {}

source/test/fixtures/tsx/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
const React = require('react');
3+
4+
export class Unicorn extends React.Component {
5+
constructor(props) {
6+
super(props);
7+
}
8+
9+
render() {
10+
return <h1>{this.props.rainbow}</h1>;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from 'react';
2+
import {expectType, expectError} from '../../..';
3+
import {Unicorn} from '.';
4+
5+
expectType<JSX.Element>(<Unicorn rainbow='🌈' />);
6+
7+
expectError(<Unicorn foo='bar' />);

source/test/fixtures/tsx/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "foo",
3+
"files": [
4+
"index.js",
5+
"index.d.ts"
6+
],
7+
"dependencies": {
8+
"react": "*"
9+
}
10+
}

source/test/test.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test('throw if no type definition was found', async t => {
77
});
88

99
test('throw if no test is found', async t => {
10-
await t.throwsAsync(m({cwd: path.join(__dirname, 'fixtures/no-test')}), 'The test file `index.test-d.ts` does not exist. Create one and try again.');
10+
await t.throwsAsync(m({cwd: path.join(__dirname, 'fixtures/no-test')}), 'The test file `index.test-d.ts` or `index.test-d.tsx` does not exist. Create one and try again.');
1111
});
1212

1313
test('return diagnostics', async t => {
@@ -91,19 +91,19 @@ test('overridden config defaults to `strict` if `strict` is not explicitly overr
9191
t.is(column, 19);
9292
});
9393

94-
test('fail if types are used from a lib that wasn\'t explicitly specified', async t => {
94+
test('fail if types are used from a lib that was not explicitly specified', async t => {
9595
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/lib-config/failure-missing-lib')});
9696

9797
t.is(diagnostics.length, 2);
9898

9999
t.true(/failure-missing-lib\/index.d.ts$/.test(diagnostics[0].fileName));
100-
t.is(diagnostics[0].message, 'Cannot find name \'Document\'.');
100+
t.is(diagnostics[0].message, 'Cannot find name \'Window\'.');
101101
t.is(diagnostics[0].severity, 'error');
102102
t.is(diagnostics[0].line, 1);
103-
t.is(diagnostics[0].column, 24);
103+
t.is(diagnostics[0].column, 22);
104104

105105
t.true(/failure-missing-lib\/index.test-d.ts$/.test(diagnostics[1].fileName));
106-
t.is(diagnostics[1].message, 'Cannot find name \'Document\'.');
106+
t.is(diagnostics[1].message, 'Cannot find name \'Window\'.');
107107
t.is(diagnostics[1].severity, 'error');
108108
t.is(diagnostics[1].line, 4);
109109
t.is(diagnostics[1].column, 11);
@@ -177,6 +177,12 @@ test('support default test directory', async t => {
177177
t.true(diagnostics.length === 0);
178178
});
179179

180+
test('support tsx in subdirectory', async t => {
181+
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/test-directory/tsx')});
182+
183+
t.true(diagnostics.length === 0);
184+
});
185+
180186
test('support setting a custom test directory', async t => {
181187
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/test-directory/custom')});
182188

@@ -244,3 +250,9 @@ test('missing import', async t => {
244250
t.true(diagnostics[0].message === 'Cannot find name \'Primitive\'.');
245251
t.true(diagnostics[0].severity === 'error');
246252
});
253+
254+
test('tsx', async t => {
255+
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/tsx')});
256+
257+
t.true(diagnostics.length === 0);
258+
});

0 commit comments

Comments
 (0)