Skip to content

Commit

Permalink
Added index signature fixer error case. (#63)
Browse files Browse the repository at this point in the history
Fixes #62
  • Loading branch information
Victor Widell authored and jonaskello committed Jan 16, 2018
1 parent 8db42f4 commit 7e4b593
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/readonlyKeywordRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,10 @@ function checkPropertySignatureAndIndexSignature(
if (Ignore.shouldIgnorePrefix(node, ctx.options, ctx.sourceFile)) {
return [];
}
const length = node.getWidth(ctx.sourceFile);
// const fulltext = node.getText(ctx.sourceFile);
const fulltext = node.getText(ctx.sourceFile);
return [
createInvalidNode(
node,
new Lint.Replacement(
node.end - length,
length,
`readonly ${fulltext}`
)
new Lint.Replacement(node.getStart(ctx.sourceFile), 0, "readonly ")
)
];
}
Expand Down
17 changes: 17 additions & 0 deletions test/rules/readonly-keyword/default/index-signature.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

// Index signature as part of interface
interface Foo {
readonly [key: string]: string
}

// Index signature as return value from function

function foo(): { readonly [source: string]: string } {
return undefined;
}

// Index signature with nested object.
interface Foo {
readonly [key: string]: { readonly bar: string }
}

7 changes: 7 additions & 0 deletions test/rules/readonly-keyword/default/index-signature.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ function foo(): { [source: string]: string } {
return undefined;
}

// Index signature with nested object.
interface Foo {
[key: string]: { bar: string }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [failure]
~~~~~~~~~~~ [failure]
}

[failure]: A readonly modifier is required.

0 comments on commit 7e4b593

Please sign in to comment.