Skip to content

Typescript conversion #32

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

Closed
wants to merge 13 commits into from
Closed
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
25 changes: 20 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,35 @@ module.exports = {
browser: true,
jest: true,
},
extends: ['airbnb-base', 'plugin:flowtype/recommended', 'prettier', 'plugin:prettier/recommended'],
extends: [
'airbnb-typescript/base',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'prettier',
'plugin:prettier/recommended',
'prettier/@typescript-eslint',
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
project: './tsconfig.json'
},
plugins: ['flowtype', 'prettier'],
plugins: ['prettier', '@typescript-eslint'],
rules: {
'prettier/prettier': 'error',
'no-underscore-dangle': 'off',
'camelcase': 'off',
'no-param-reassign': ["error", { "props": false }],
'no-param-reassign': ['error', { 'props': false }],
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/prefer-regexp-exec': 'off',
'@typescript-eslint/array-type': ['error', {
'default' : 'array',
'readonly': 'array'
}],
'max-classes-per-file': 'off',
},
};
16 changes: 0 additions & 16 deletions .flowconfig

This file was deleted.

4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* text=auto

*.example text eol=lf
*.js text eol=lf
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "6"
- "8"
script:
- yarn ci
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
Expand Down
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"typescript.validate.enable": false,
"javascript.validate.enable": false,
"typescript.validate.enable": true,
"javascript.validate.enable": true,
"eslint.autoFixOnSave": true
}
9 changes: 2 additions & 7 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{ useBuiltIns: 'usage', corejs: '3' }
],
'@babel/flow',
],
presets: [['@babel/preset-env', {useBuiltIns: 'usage', corejs: '3'}], '@babel/typescript'],
plugins: ['@babel/plugin-proposal-optional-chaining', '@babel/proposal-class-properties', '@babel/transform-runtime'],
};
4 changes: 2 additions & 2 deletions fixtures/dangerjs/github.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do some files start with an empty line?

Copy link
Contributor Author

@rossknudsen rossknudsen Jan 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a good question. It took me ages to figure out why some unit tests were failing. Turns out the line numbers are hard-coded in them and I was trying to change the minimal amount of code, so that this PR was readable.

So I didn't update the tests with new line numbers.

/* eslint-disable */

import { GitHub } from "../GitHub"
Expand All @@ -9,7 +9,7 @@ const mockGitHubWithGetForPath = (expectedPath): GitHub => {
const mockSource = new Fake({})
const github = new GitHub("Token", mockSource)

// $FlowFixMe: Ignore an error from setting an unexistant func

github.get = (path: string, headers: any = {}, body: any = {}, method: string = "GET"): Promise<any> => {
return new Promise((resolve: any, reject: any) => {
expect(path).toBe(expectedPath)
Expand Down
2 changes: 1 addition & 1 deletion fixtures/global_its.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @flow

it('works with old functions', function() {

});
Expand Down
142 changes: 51 additions & 91 deletions fixtures/parser_tests.js → fixtures/parser_tests.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
/* eslint-disable prefer-destructuring */
/* eslint-disable no-template-curly-in-string */
/* eslint-disable import/prefer-default-export */
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {ParseResult, NamedBlock, ItBlock, DescribeBlock} from '../src/parsers/parser_nodes';

const fixtures = __dirname;

function parserTests(parse: (file: string) => ParserResult) {
const assertBlock2 = (
block,
sl: number,
sc: number,
el: number,
ec: number,
name: ?string = null,
) => assertBlock(block, {column: sc, line: sl}, {column: ec, line: el}, name);
const assertBlock = (block, start, end, name: ?string = null) => {
export function parserTests(parse: (file: string, data?: string) => ParseResult) {
const assertBlock = (block, start, end, name?: string) => {
expect(block.start).toEqual(start);
expect(block.end).toEqual(end);
if (name) {
expect(block.name).toEqual(name);
}
};
const assertBlock2 = (block, sl: number, sc: number, el: number, ec: number, name?: string) =>
assertBlock(block, {column: sc, line: sl}, {column: ec, line: el}, name);
describe('File parsing without throwing', () => {
it('Should not throw', () => {
expect(() => {
Expand Down Expand Up @@ -174,35 +173,30 @@ function parserTests(parse: (file: string) => ParserResult) {
expect(data.describeBlocks.length).toEqual(4);

const firstDescribe = data.describeBlocks[0];
assertBlock(
firstDescribe,
{column: 1, line: 10},
{column: 2, line: 20},
'.isCI',
);
assertBlock(firstDescribe, {column: 1, line: 10}, {column: 2, line: 20}, '.isCI');
});
it('finds test blocks within describe blocks', () => {
const data = parse(`${fixtures}/dangerjs/travis-ci.example`);
const descBlock = data.describeBlocks[1];
expect(descBlock.children.length).toBe(4);

// check test blocks, including the template literal
const found = descBlock.children.filter(
b =>
b.name === 'needs to have a PR number' ||
b.name === 'does not validate without josh' ||
b.name === 'does not validate when ${key} is missing',
);
const found = descBlock.children
.map(b => b as NamedBlock)
.filter(
b =>
b.name === 'needs to have a PR number' ||
b.name === 'does not validate without josh' ||
b.name === 'does not validate when ${key} is missing'
);
expect(found.length).toBe(3);
});
});
describe('Nested Elements', () => {
let nested;
beforeEach(() => {
const data = parse(`${fixtures}/nested_elements.example`);
nested = data.root.children.filter(
e => e.type === 'describe' && e.name === 'describe 1.0',
)[0];
nested = data.root.children.filter(e => e.type === 'describe' && (e as DescribeBlock).name === 'describe 1.0')[0];
});
it('can find nested describe or test blocks', () => {
expect(nested.children.length).toBe(2);
Expand All @@ -227,108 +221,78 @@ function parserTests(parse: (file: string) => ParserResult) {
describe('template literals', () => {
const parseResult = parse(`${fixtures}/template-literal.example`);

test(`all blocks are parsed`, () => {
test('all blocks are parsed', () => {
expect(parseResult.describeBlocks.length).toEqual(6);
expect(parseResult.itBlocks.length).toEqual(7);
expect(parseResult.expects.length).toEqual(4);
});
test(`no expression template`, () => {
const dBlock = parseResult.describeBlocks.filter(
b => b.name === 'no expression template',
)[0];
test('no expression template', () => {
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'no expression template')[0];
const t1 = dBlock.children[0];
const e1 = t1.children[0];
const e1 = t1?.children[0];

expect(dBlock.children.length).toBe(1);
expect(t1.children.length).toBe(1);
expect(t1?.children.length).toBe(1);

assertBlock(
t1,
{column: 3, line: 2},
{column: 5, line: 4},
'test has no expression either',
);
assertBlock(t1, {column: 3, line: 2}, {column: 5, line: 4}, 'test has no expression either');
assertBlock(e1, {column: 5, line: 3}, {column: 25, line: 3});
});

test(`simple template literal`, () => {
const dBlock = parseResult.describeBlocks.filter(
b => b.name === 'simple template literal',
)[0];
test('simple template literal', () => {
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'simple template literal')[0];
expect(dBlock.children.length).toBe(3);

const t1 = dBlock.children[0];
const t2 = dBlock.children[1];
const t3 = dBlock.children[2];

assertBlock(
t1,
{column: 3, line: 8},
{column: 46, line: 8},
'${expression} up front',
);
assertBlock(
t2,
{column: 3, line: 9},
{column: 4, line: 10},
'at the end ${expression}',
);
assertBlock(
t3,
{column: 3, line: 11},
{column: 5, line: 12},
'mixed ${expression1} and ${expression2}',
);
assertBlock(t1, {column: 3, line: 8}, {column: 46, line: 8}, '${expression} up front');
assertBlock(t2, {column: 3, line: 9}, {column: 4, line: 10}, 'at the end ${expression}');
assertBlock(t3, {column: 3, line: 11}, {column: 5, line: 12}, 'mixed ${expression1} and ${expression2}');
});

test(`template literal with functions`, () => {
const dBlock = parseResult.describeBlocks.filter(
b => b.name === 'template literal with functions',
)[0];
test('template literal with functions', () => {
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'template literal with functions')[0];
const t1 = dBlock.children[0];
const e1 = t1.children[0];
const e1 = t1?.children[0];

expect(dBlock.children.length).toBe(1);
expect(t1.children.length).toBe(1);
expect(t1?.children.length).toBe(1);

assertBlock(
t1,
{column: 3, line: 16},
{column: 5, line: 18},
'this ${test} calls ${JSON.stringfy(expression)} should still work',
'this ${test} calls ${JSON.stringfy(expression)} should still work'
);
assertBlock(e1, {column: 5, line: 17}, {column: 31, line: 17});
});

test(`multiline template literal`, () => {
const dBlock = parseResult.describeBlocks.filter(
b => b.name === 'multiline template literal',
)[0];
test('multiline template literal', () => {
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'multiline template literal')[0];
const t1 = dBlock.children[0];
const e1 = t1.children[0];
const e1 = t1?.children[0];

expect(dBlock.children.length).toBe(1);
expect(t1.children.length).toBe(1);
expect(t1?.children.length).toBe(1);

assertBlock(
t1,
{column: 3, line: 22},
{column: 5, line: 25},
`this \${test} will span in
multiple lines`,
multiple lines`
);
assertBlock(e1, {column: 5, line: 24}, {column: 32, line: 24});
});

test(`edge case: should not fail`, () => {
const dBlock = parseResult.describeBlocks.filter(
b => b.name === 'edge case: should not fail',
)[0];
test('edge case: should not fail', () => {
const dBlock = parseResult.describeBlocks.filter(b => b.name === 'edge case: should not fail')[0];
const t1 = dBlock.children[0];
const e1 = t1.children[0];
const e1 = t1?.children[0];

expect(dBlock.children.length).toBe(2);
expect(t1.children.length).toBe(1);
expect(t1?.children.length).toBe(1);

assertBlock(t1, {column: 3, line: 29}, {column: 5, line: 31}, '');
assertBlock(e1, {column: 5, line: 30}, {column: 30, line: 30});
Expand All @@ -341,13 +305,13 @@ function parserTests(parse: (file: string) => ParserResult) {
startLine: number,
startCol: number,
endLine: number,
endCol: number,
endCol: number
) => {
expect(nBlock.name).toEqual(name);
expect(nBlock.nameRange.start.line).toEqual(startLine);
expect(nBlock.nameRange.start.column).toEqual(startCol);
expect(nBlock.nameRange.end.line).toEqual(endLine);
expect(nBlock.nameRange.end.column).toEqual(endCol);
expect(nBlock.nameRange?.start.line).toEqual(startLine);
expect(nBlock.nameRange?.start.column).toEqual(startCol);
expect(nBlock.nameRange?.end.line).toEqual(endLine);
expect(nBlock.nameRange?.end.column).toEqual(endCol);
};

describe('name range', () => {
Expand All @@ -372,7 +336,7 @@ function parserTests(parse: (file: string) => ParserResult) {
22,
7,
23,
18,
18
);
});
});
Expand Down Expand Up @@ -409,11 +373,7 @@ function parserTests(parse: (file: string) => ParserResult) {

block = parseResult.describeBlocks[parseResult.describeBlocks.length - 1];
expect(block.name).toEqual('empty describe should not crash');
expect(block.children).toBeFalsy();
expect(block.children.length).toBe(0);
});
});
}

module.exports = {
parserTests,
};
Loading