Skip to content

Commit 77919ff

Browse files
authored
refactor: add return types everywhere (#248)
Also enable TSLint checking that every function has a return type.
1 parent 4de012e commit 77919ff

File tree

11 files changed

+35
-25
lines changed

11 files changed

+35
-25
lines changed

script/check-file.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ for (let i = 2; i < process.argv.length; i++) {
1111
processPath(process.argv[i]);
1212
}
1313

14-
function processPath(path: string) {
14+
function processPath(path: string): void {
1515
const stat = statSync(path);
1616

1717
if (stat.isDirectory()) {
@@ -21,7 +21,7 @@ function processPath(path: string) {
2121
}
2222
}
2323

24-
function processFile(path: string) {
24+
function processFile(path: string): void {
2525
const content = readFileSync(path, { encoding: 'utf8' });
2626
try {
2727
parse(content);
@@ -33,7 +33,7 @@ function processFile(path: string) {
3333
}
3434
}
3535

36-
function processDirectory(path: string) {
36+
function processDirectory(path: string): void {
3737
readdirSync(path).forEach(child => {
3838
if (child[0] === '.' || child === 'node_modules') {
3939
return;
@@ -43,6 +43,6 @@ function processDirectory(path: string) {
4343
});
4444
}
4545

46-
function isCoffeeScriptFile(path: string) {
46+
function isCoffeeScriptFile(path: string): boolean {
4747
return extname(path) === '.coffee' && basename(path, '.coffee').length > 0;
4848
}

src/ext/coffee-script.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Base, Op } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
22

3-
export function patchCoffeeScript() {
3+
export function patchCoffeeScript(): void {
44
Op.prototype.invert = invert;
55
Base.prototype.invert = invert;
66
}
77

8-
function invert() {
8+
// tslint:disable-next-line no-any
9+
function invert(): any {
910
this.inverted = !this.inverted;
1011
return this;
1112
}

src/mappers/mapOp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default function mapOp(context: ParseContext, node: CoffeeOp): Node {
2626
}
2727
}
2828

29-
function mapChainedComparisonOp(context: ParseContext, node: CoffeeOp) {
29+
function mapChainedComparisonOp(context: ParseContext, node: CoffeeOp): ChainedComparisonOp {
3030
let { line, column, start, end, raw } = getLocation(context, node);
3131
let coffeeOperands = unwindChainedComparison(node);
3232
let operands = coffeeOperands.map(operand => mapAny(context, operand));

src/util/ParseContext.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@ export class ParseState {
2323
this.currentClassCtor = null;
2424
}
2525

26-
isInClassBody() {
26+
isInClassBody(): boolean {
2727
return this.currentClassBoundMethods !== null;
2828
}
2929

30-
recordBoundMethod(method: ClassProtoAssignOp) {
30+
recordBoundMethod(method: ClassProtoAssignOp): void {
3131
if (!this.currentClassBoundMethods) {
3232
throw new Error('Cannot assign a bound method name when there is no current class.');
3333
}
3434
this.currentClassBoundMethods.push(method);
3535
}
3636

37-
recordConstructor(ctor: Constructor) {
37+
recordConstructor(ctor: Constructor): void {
3838
this.currentClassCtor = ctor;
3939
}
4040

41-
pushCurrentClass() {
41+
pushCurrentClass(): ParseState {
4242
return new ParseState([]);
4343
}
4444

45-
dropCurrentClass() {
45+
dropCurrentClass(): ParseState {
4646
return new ParseState(null);
4747
}
4848

@@ -76,7 +76,7 @@ export default class ParseContext {
7676
}
7777
}
7878

79-
static fromSource(source: string, sourceLex: (source: string) => SourceTokenList, parse: (source: string) => Block) {
79+
static fromSource(source: string, sourceLex: (source: string) => SourceTokenList, parse: (source: string) => Block): ParseContext {
8080
try {
8181
const sourceTokens = sourceLex(source);
8282
return new ParseContext(

src/util/fixLocations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import mergeLocations from './mergeLocations';
55
import ParseContext from './ParseContext';
66
import rangeOfBracketTokensForIndexNode from './rangeOfBracketTokensForIndexNode';
77

8-
export default function fixLocations(context: ParseContext, node: Base) {
8+
export default function fixLocations(context: ParseContext, node: Base): void {
99
let { linesAndColumns, source } = context;
1010
node.eachChild(child => {
1111
if (child && child.locationData) {

src/util/getLocation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SourceType } from 'coffee-lex';
2+
import SourceToken from 'coffee-lex/dist/SourceToken';
23
import SourceTokenListIndex from 'coffee-lex/dist/SourceTokenListIndex';
34
import { Base } from 'decaffeinate-coffeescript/lib/coffee-script/nodes';
45
import { inspect } from 'util';
@@ -43,7 +44,7 @@ export default function getLocation(context: ParseContext, node: Base): NodeLoca
4344
return {line, column, start, end, raw};
4445
}
4546

46-
function firstSemanticTokenAfter(context: ParseContext, index: number, node: Base) {
47+
function firstSemanticTokenAfter(context: ParseContext, index: number, node: Base): SourceToken {
4748
let tokenIndex = context.sourceTokens.indexOfTokenMatchingPredicate(token => {
4849
return (
4950
token.start >= index &&
@@ -54,7 +55,7 @@ function firstSemanticTokenAfter(context: ParseContext, index: number, node: Bas
5455
return tokenFromIndex(context, tokenIndex, node);
5556
}
5657

57-
function firstSemanticTokenBefore(context: ParseContext, index: number, node: Base) {
58+
function firstSemanticTokenBefore(context: ParseContext, index: number, node: Base): SourceToken {
5859
let tokenIndex = context.sourceTokens.lastIndexOfTokenMatchingPredicate(token => {
5960
return (
6061
token.end <= index &&
@@ -65,7 +66,7 @@ function firstSemanticTokenBefore(context: ParseContext, index: number, node: Ba
6566
return tokenFromIndex(context, tokenIndex, node);
6667
}
6768

68-
function tokenFromIndex(context: ParseContext, tokenIndex: SourceTokenListIndex | null, node: Base) {
69+
function tokenFromIndex(context: ParseContext, tokenIndex: SourceTokenListIndex | null, node: Base): SourceToken {
6970
if (tokenIndex === null) {
7071
throw new Error(`unable to find token index for node: ${inspect(node)}`);
7172
}

src/util/getTemplateLiteralComponents.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@ import isImplicitPlusOp from './isImplicitPlusOp';
88
import ParseContext from './ParseContext';
99
import parseString from './parseString';
1010

11+
export type TemplateLiteralComponents = {
12+
quasis: Array<Quasi>,
13+
unmappedExpressions: Array<Base | null>;
14+
start: number,
15+
end: number,
16+
};
17+
1118
/**
1219
* Reconstruct template literal information given the coffee-lex tokens and the
1320
* CoffeeScript AST. Since the CoffeeScript AST doesn't attempt to represent a
1421
* template literal (it's a bunch of + operations instead), the source locations
1522
* are generally unreliable and we need to rely on the token locations instead.
1623
*/
17-
export default function getTemplateLiteralComponents(context: ParseContext, node: Base) {
24+
export default function getTemplateLiteralComponents(context: ParseContext, node: Base): TemplateLiteralComponents {
1825
let tokens = context.sourceTokens;
1926

2027
let quasis: Array<Quasi> = [];
@@ -80,7 +87,7 @@ function getElements(node: Base, context: ParseContext): Array<Base> {
8087
* if the start of the template literal is an interpolation, it's two before
8188
* that one, so check to see which case we are and return what we find.
8289
*/
83-
function getStartToken(start: number, tokens: SourceTokenList) {
90+
function getStartToken(start: number, tokens: SourceTokenList): { startToken: SourceToken, startTokenIndex: SourceTokenListIndex } {
8491
let tokenIndex = tokens.indexOfTokenNearSourceIndex(start);
8592
for (let i = 0; i < 5; i++) {
8693
let token = tokens.tokenAtIndex(tokenIndex);

src/util/isHeregexTemplateNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ParseContext from './ParseContext';
66
* Determine if the given CoffeeScript AST node is an interpolated heregex node
77
* that's pretending to be a function call to the RegExp function.
88
*/
9-
export default function isHeregexTemplateNode(node: Base, context: ParseContext) {
9+
export default function isHeregexTemplateNode(node: Base, context: ParseContext): boolean {
1010
if (!(node instanceof Call) ||
1111
!node.variable ||
1212
!(node.variable instanceof Value) ||

src/util/makeHeregex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Heregex, RegexFlags } from '../nodes';
44
import getTemplateLiteralComponents from './getTemplateLiteralComponents';
55
import ParseContext from './ParseContext';
66

7-
export default function makeHeregex(context: ParseContext, node: Base, flags: string) {
7+
export default function makeHeregex(context: ParseContext, node: Base, flags: string): Heregex {
88
let { quasis, unmappedExpressions, start, end } = getTemplateLiteralComponents(context, node);
99
let startLoc = context.linesAndColumns.locationForIndex(start);
1010
if (!startLoc) {

test/test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ readdirSync(examplesPath).forEach(entry => {
2525
});
2626
});
2727

28-
function stripContext(programNode: Program) {
28+
function stripContext(programNode: Program): Program {
2929
delete programNode.context;
3030
return programNode;
3131
}
3232

33-
function stripExtraInfo(node: Node) {
33+
function stripExtraInfo(node: Node): Node {
3434
if (node && typeof node === 'object') {
3535
for (let key in node) {
3636
if (node.range && (key === 'start' || key === 'end')) {
@@ -48,7 +48,8 @@ function stripExtraInfo(node: Node) {
4848
return node;
4949
}
5050

51-
function requireOptional(path: string) {
51+
// tslint:disable-next-line no-any
52+
function requireOptional(path: string): any {
5253
try {
5354
return require(path);
5455
} catch (err) {

0 commit comments

Comments
 (0)