Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it strict by default and add ability to configure compilerOptions #13

Merged
merged 4 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@
"eslint-formatter-pretty": "^1.3.0",
"meow": "^5.0.0",
"path-exists": "^3.0.0",
"pkg-conf": "^3.0.0",
"read-pkg-up": "^4.0.0",
"typescript": "^3.0.1",
"update-notifier": "^2.5.0"
},
"devDependencies": {
"@types/node": "^10.7.1",
"@types/node": "^11.10.4",
"@types/update-notifier": "^2.2.0",
"ava": "*",
"cpy-cli": "^2.0.0",
Expand Down
35 changes: 27 additions & 8 deletions source/lib/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as path from 'path';
import * as pkgConf from 'pkg-conf';
import {
ScriptTarget,
ModuleResolutionKind,
flattenDiagnosticMessageText,
CompilerOptions,
createProgram
createProgram,
JsxEmit
} from 'typescript';
import {Diagnostic, Context} from './interfaces';

Expand All @@ -13,11 +15,24 @@ const ignoredDiagnostics = new Set<number>([
1308 // Support top-level `await`
]);

const loadConfig = (): CompilerOptions => {
const loadConfig = (cwd: string): CompilerOptions => {
const config = pkgConf.sync('tsd-check', {
cwd,
defaults: {
compilerOptions: {
strict: true,
jsx: JsxEmit.React,
target: ScriptTarget.ES2017
}
}
});

return {
moduleResolution: ModuleResolutionKind.NodeJs,
skipLibCheck: true,
target: ScriptTarget.ES2015
...config.compilerOptions,
...{
moduleResolution: ModuleResolutionKind.NodeJs,
skipLibCheck: true
}
};
};

Expand All @@ -28,22 +43,26 @@ const loadConfig = (): CompilerOptions => {
* @returns List of diagnostics
*/
export const getDiagnostics = (context: Context): Diagnostic[] => {
const compilerOptions = loadConfig();
const compilerOptions = loadConfig(context.cwd);

const fileName = path.join(context.cwd, context.testFile);

const result: Diagnostic[] = [];

const program = createProgram([fileName], compilerOptions);

const diagnostics = program.getSemanticDiagnostics().concat(program.getSyntacticDiagnostics());
const diagnostics = program
.getSemanticDiagnostics()
.concat(program.getSyntacticDiagnostics());

for (const diagnostic of diagnostics) {
if (!diagnostic.file || ignoredDiagnostics.has(diagnostic.code)) {
continue;
}

const position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start as number);
const position = diagnostic.file.getLineAndCharacterOfPosition(
diagnostic.start as number
);

result.push({
fileName: diagnostic.file.fileName,
Expand Down
1 change: 1 addition & 0 deletions source/test/fixtures/failure-strict-null-checks/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function (foo: number): number | null;
3 changes: 3 additions & 0 deletions source/test/fixtures/failure-strict-null-checks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = foo => {
return foo > 0 ? foo : null;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {expectType} from '../../..';
import aboveZero from '.';

expectType<number>(aboveZero(1));
3 changes: 3 additions & 0 deletions source/test/fixtures/failure-strict-null-checks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "foo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function (foo: number): number | null;
3 changes: 3 additions & 0 deletions source/test/fixtures/non-strict-check-with-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = foo => {
return foo > 0 ? foo : null;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {expectType} from '../../..';
import aboveZero from '.';

expectType<number>(aboveZero(1));
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "foo",
"tsd-check": {
"compilerOptions": {
"strict": false
}
}
}
25 changes: 25 additions & 0 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ test('fail if typings file is not part of `files` list', async t => {
]);
});

test('fail if tests don\'t pass in strict mode', async t => {
const diagnostics = await m({
cwd: path.join(__dirname, 'fixtures/failure-strict-null-checks')
});

const {fileName, message, severity, line, column} = diagnostics[0];
t.true(/failure-strict-null-checks\/index.test-d.ts$/.test(fileName));
t.is(
message,
`Argument of type 'number | null' is not assignable to parameter of type 'number'.
Type \'null\' is not assignable to type 'number'.`
);
t.is(severity, 'error');
t.is(line, 4);
t.is(column, 19);
});

test('pass in loose mode when strict mode is disabled in settings', async t => {
const diagnostics = await m({
cwd: path.join(__dirname, 'fixtures/non-strict-check-with-config')
});

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

test('return no diagnostics', async t => {
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/success')});

Expand Down