Skip to content

Commit

Permalink
Support types property and correctly test files for types in proj…
Browse files Browse the repository at this point in the history
…ect sub-dir (#12)
  • Loading branch information
BendingBender authored and SamVerschueren committed Mar 15, 2019
1 parent 1b20f1f commit a6233f8
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 4 deletions.
2 changes: 1 addition & 1 deletion source/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Options {
}

const findTypingsFile = async (pkg: any, options: Options) => {
const typings = pkg.typings || 'index.d.ts';
const typings = pkg.types || pkg.typings || 'index.d.ts';

const typingsExist = await pathExists(path.join(options.cwd, typings));

Expand Down
12 changes: 9 additions & 3 deletions source/lib/rules/files-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ import {getJSONPropertyPosition} from '../utils';
*/
export default (context: Context): Diagnostic[] => {
const {pkg, typingsFile} = context;
const typingsFileName = path.basename(typingsFile);

if (!Array.isArray(pkg.files) || pkg.files.indexOf(typingsFileName) !== -1) {
if (!Array.isArray(pkg.files)) {
return [];
}

const normalizedTypingsFile = path.normalize(typingsFile);
const normalizedFiles = (pkg.files as string[]).map(path.normalize);

if (normalizedFiles.includes(normalizedTypingsFile)) {
return [];
}

Expand All @@ -22,7 +28,7 @@ export default (context: Context): Diagnostic[] => {
return [
{
fileName: 'package.json',
message: `TypeScript type definition \`${typingsFileName}\` is not part of the \`files\` list.`,
message: `TypeScript type definition \`${normalizedTypingsFile}\` is not part of the \`files\` list.`,
severity: 'error',
...getJSONPropertyPosition(content, 'files')
}
Expand Down
9 changes: 9 additions & 0 deletions source/test/fixtures/test-in-subdir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "foo",
"main": "src/index.js",
"typings": "src/index.d.ts",
"files": [
"src/index.js",
"src/index.d.ts"
]
}
6 changes: 6 additions & 0 deletions source/test/fixtures/test-in-subdir/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare const one: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
};

export default one;
3 changes: 3 additions & 0 deletions source/test/fixtures/test-in-subdir/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = (foo, bar) => {
return foo + bar;
};
5 changes: 5 additions & 0 deletions source/test/fixtures/test-in-subdir/src/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {expectType} from '../../../..';
import one from '.';

expectType<string>(one('foo', 'bar'));
expectType<number>(one(1, 2));
6 changes: 6 additions & 0 deletions source/test/fixtures/test-non-barrel-main-via-types/foo.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare const one: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
};

export default one;
3 changes: 3 additions & 0 deletions source/test/fixtures/test-non-barrel-main-via-types/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = (foo, bar) => {
return foo + bar;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {expectType} from '../../..';
import one from './foo';

expectType<string>(one('foo', 'bar'));
expectType<number>(one(1, 2));
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "foo",
"main": "foo.js",
"types": "foo.d.ts",
"files": [
"foo.js",
"foo.d.ts"
]
}
6 changes: 6 additions & 0 deletions source/test/fixtures/test-non-barrel-main/foo.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare const one: {
(foo: string, bar: string): string;
(foo: number, bar: number): number;
};

export default one;
3 changes: 3 additions & 0 deletions source/test/fixtures/test-non-barrel-main/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = (foo, bar) => {
return foo + bar;
};
5 changes: 5 additions & 0 deletions source/test/fixtures/test-non-barrel-main/foo.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {expectType} from '../../..';
import one from './foo';

expectType<string>(one('foo', 'bar'));
expectType<number>(one(1, 2));
9 changes: 9 additions & 0 deletions source/test/fixtures/test-non-barrel-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "foo",
"main": "foo.js",
"typings": "foo.d.ts",
"files": [
"foo.js",
"foo.d.ts"
]
}
18 changes: 18 additions & 0 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ test('return no diagnostics', async t => {
t.true(diagnostics.length === 0);
});

test('support non-barrel main', async t => {
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/test-non-barrel-main')});

t.true(diagnostics.length === 0);
});

test('support non-barrel main using `types` property', async t => {
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/test-non-barrel-main-via-types')});

t.true(diagnostics.length === 0);
});

test('support testing in sub-directories', async t => {
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/test-in-subdir')});

t.true(diagnostics.length === 0);
});

test('support top-level await', async t => {
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/top-level-await')});

Expand Down

0 comments on commit a6233f8

Please sign in to comment.