Skip to content

Commit

Permalink
feat: use local variables to fill in missing members, instead of defa…
Browse files Browse the repository at this point in the history
…ult values

re #124
  • Loading branch information
tamj0rd2 committed Mar 20, 2021
1 parent 06543d2 commit 33e0fc7
Show file tree
Hide file tree
Showing 23 changed files with 403 additions and 44 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

## Features

### Declare missing object members
### Declare missing members

![Declare missing object members](gifs/implement-missing-members.gif)
This fix declares members that are missing from a certain object. It works in the following scenarios including in
situations where a nested object has missing members:

### Declare missing (object) argument members
#### Missing variable members

![Declare missing (object) argument members](gifs/declare-missing-argument-members.gif)
![Missing variable members](gifs/missing-variable-members.gif)

#### Missing argument members

![Missing argument members](gifs/missing-argument-members.gif)

#### Missing constructor argument members

![Missing constructor members](gifs/missing-constructor-argument-members.gif)

## Requirements

Expand All @@ -31,8 +40,7 @@ This extension contributes the following settings:

## Known Issues

I've only covered a few use cases/scenarios. If your use case is not covered,
please open a github issue.
If you expect a fix to be available but it isn't, please [open a github issue](https://github.com/tamj0rd2/ts-quickfixes/issues/new) with a small example explaining the issue

### Formatting

Expand All @@ -46,4 +54,4 @@ See [CHANGELOG.md](https://github.com/tamj0rd2/ts-quickfixes/blob/master/package

## Contributing

See [CONTRIBUTING.md](https://github.com/tamj0rd2/ts-quickfixes/blob/master/packages/plugin/CONTRIBUTING.md)
See [CONTRIBUTING.md](https://github.com/tamj0rd2/ts-quickfixes/blob/master/CONTRIBUTING.md)
Binary file removed gifs/declare-missing-argument-members.gif
Binary file not shown.
Binary file removed gifs/implement-missing-members.gif
Binary file not shown.
Binary file added gifs/missing-argument-members.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/missing-constructor-argument-members.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gifs/missing-variable-members.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion packages/e2e/src/fixtures/new-http-client.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const client = new HttpClient({
timeout: 456,
baseUrl: 'todo'
baseUrl: baseUrl,
operation: 'todo'
})
14 changes: 8 additions & 6 deletions packages/e2e/src/tests/extension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ describe('Acceptance tests', () => {
})

describe('Declare missing members', () => {
const TS_FOLDER = TEST_ENV_DIR + '/declare-missing-members'

it('declares missing members for interfaces that have been extended', async () => {
const { getCodeActions } = createTestDeps()
const testFileUri = vscode.Uri.file(TEST_ENV_DIR + '/testing.ts')
const testFileUri = vscode.Uri.file(TS_FOLDER + '/variable-members.ts')
const testingDocument = await vscode.workspace.openTextDocument(testFileUri)
await vscode.window.showTextDocument(testingDocument)

Expand All @@ -31,7 +33,7 @@ describe('Acceptance tests', () => {

it('declares missing members for function arguments', async () => {
const { getCodeActions } = createTestDeps()
const fileUri = vscode.Uri.file(TEST_ENV_DIR + `/testing.ts`)
const fileUri = vscode.Uri.file(TS_FOLDER + `/argument-members.ts`)
const document = await vscode.workspace.openTextDocument(fileUri)
await vscode.window.showTextDocument(document)

Expand All @@ -46,7 +48,7 @@ describe('Acceptance tests', () => {

it('declares missing members for const arrow function arguments', async () => {
const { getCodeActions } = createTestDeps()
const fileUri = vscode.Uri.file(TEST_ENV_DIR + '/testing.ts')
const fileUri = vscode.Uri.file(TS_FOLDER + '/argument-members.ts')
const document = await vscode.workspace.openTextDocument(fileUri)
await vscode.window.showTextDocument(document)

Expand All @@ -61,7 +63,7 @@ describe('Acceptance tests', () => {

it('declares missing members for nested objects', async () => {
const { getCodeActions } = createTestDeps()
const testFileUri = vscode.Uri.file(TEST_ENV_DIR + '/testing.ts')
const testFileUri = vscode.Uri.file(TS_FOLDER + '/nesting.ts')
const testingDocument = await vscode.workspace.openTextDocument(testFileUri)
await vscode.window.showTextDocument(testingDocument)

Expand All @@ -73,9 +75,9 @@ describe('Acceptance tests', () => {
expect(documentText).toContain(await readFixture('compensation'))
})

it('declares missing members for constructor arguments', async () => {
it('declares missing members for constructor arguments using locals if available', async () => {
const { getCodeActions } = createTestDeps()
const testFileUri = vscode.Uri.file(TEST_ENV_DIR + '/testing.ts')
const testFileUri = vscode.Uri.file(TS_FOLDER + '/constructor-argument-members.ts')
const testingDocument = await vscode.workspace.openTextDocument(testFileUri)
await vscode.window.showTextDocument(testingDocument)

Expand Down
22 changes: 15 additions & 7 deletions packages/extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

## Features

### Declare missing object members
### Declare missing members

![Declare missing object members](gifs/implement-missing-members.gif)
This fix declares members that are missing from a certain object. It works in the following scenarios including in
situations where a nested object has missing members:

### Declare missing (object) argument members
#### Missing variable members

![Declare missing (object) argument members](gifs/declare-missing-argument-members.gif)
![Missing variable members](gifs/missing-variable-members.gif)

#### Missing argument members

![Missing argument members](gifs/missing-argument-members.gif)

#### Missing constructor argument members

![Missing constructor members](gifs/missing-constructor-argument-members.gif)

## Requirements

Expand All @@ -31,8 +40,7 @@ This extension contributes the following settings:

## Known Issues

I've only covered a few use cases/scenarios. If your use case is not covered,
please open a github issue.
If you expect a fix to be available but it isn't, please [open a github issue](https://github.com/tamj0rd2/ts-quickfixes/issues/new) with a small example explaining the issue

### Formatting

Expand All @@ -46,4 +54,4 @@ See [CHANGELOG.md](https://github.com/tamj0rd2/ts-quickfixes/blob/master/package

## Contributing

See [CONTRIBUTING.md](https://github.com/tamj0rd2/ts-quickfixes/blob/master/packages/plugin/CONTRIBUTING.md)
See [CONTRIBUTING.md](https://github.com/tamj0rd2/ts-quickfixes/blob/master/CONTRIBUTING.md)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 15 additions & 7 deletions packages/plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

## Features

### Declare missing object members
### Declare missing members

![Declare missing object members](gifs/implement-missing-members.gif)
This fix declares members that are missing from a certain object. It works in the following scenarios including in
situations where a nested object has missing members:

### Declare missing (object) argument members
#### Missing variable members

![Declare missing (object) argument members](gifs/declare-missing-argument-members.gif)
![Missing variable members](gifs/missing-variable-members.gif)

#### Missing argument members

![Missing argument members](gifs/missing-argument-members.gif)

#### Missing constructor argument members

![Missing constructor members](gifs/missing-constructor-argument-members.gif)

## Requirements

Expand All @@ -31,8 +40,7 @@ This extension contributes the following settings:

## Known Issues

I've only covered a few use cases/scenarios. If your use case is not covered,
please open a github issue.
If you expect a fix to be available but it isn't, please [open a github issue](https://github.com/tamj0rd2/ts-quickfixes/issues/new) with a small example explaining the issue

### Formatting

Expand All @@ -46,4 +54,4 @@ See [CHANGELOG.md](./CHANGELOG.md)

## Contributing

See [CONTRIBUTING.md](https://github.com/tamj0rd2/ts-quickfixes/blob/master/packages/plugin/CONTRIBUTING.md)
See [CONTRIBUTING.md](https://github.com/tamj0rd2/ts-quickfixes/blob/master/CONTRIBUTING.md)
Binary file added packages/plugin/gifs/missing-argument-members.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/plugin/gifs/missing-variable-members.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,67 @@ describe('declareMissingObjectMembers', () => {
}`),
)
})

describe('scope', () => {
it('can use locals that are in scope', () => {
const initializer = '{}'
const [filePath, fileContent] = FsMocker.addFile(`
interface TargetType {
greeting: string
name: string
}
interface ParentType {
target: TargetType
}
const target = { greeting: 'hello', name: 'John Doe' }
const something: ParentType = ${initializer}
`)

const newText = getNewText({
filePath,
initializerPos: getNodeRange(fileContent, initializer),
errorPos: getNodeRange(fileContent, 'something'),
})

expect(newText).toBe(`{\n target: target\n}`)
})

it('does not use a local in scope if the type is not sufficient', () => {
const initializer = '{}'
const [filePath, fileContent] = FsMocker.addFile(`
interface TargetType {
greeting: string
name: string
}
interface ParentType {
target: TargetType
}
const target = { woah: 'This is so wrong' }
const something: ParentType = ${initializer}
`)

const newText = getNewText({
filePath,
initializerPos: getNodeRange(fileContent, initializer),
errorPos: getNodeRange(fileContent, 'something'),
})

expect(newText).toBe(
stripLeadingWhitespace(`{
target: {
greeting: 'todo',
name: 'todo'
}
}`),
)
})
})
})

interface GetNewText {
Expand Down
Loading

0 comments on commit 33e0fc7

Please sign in to comment.