-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin): implement missing function argument members
fix #6
- Loading branch information
Showing
7 changed files
with
420 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
packages/plugin/src/code-fixes/missing-argument-members-fix.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { MissingArgumentMembersFix } from './missing-argument-members-fix' | ||
import { createDummyLogger, createTestProgram, FsMocker, getNodeRange } from '../test-helpers' | ||
|
||
describe('Fill Missing Argument Member', () => { | ||
afterEach(() => FsMocker.reset()) | ||
|
||
it('fills in undeclared argument members when none are declared', () => { | ||
const argumentValue = '{}' | ||
const [filePath, fileContent] = FsMocker.addFile(` | ||
function targetFunction(someArgument: { min: number; max: number } ) { | ||
return 123 | ||
} | ||
export const functionOutput = targetFunction(${argumentValue}) | ||
`) | ||
|
||
const initializerLocation = getNodeRange(fileContent, argumentValue) | ||
const fix = new MissingArgumentMembersFix({ | ||
filePath, | ||
program: createTestProgram([filePath], MissingArgumentMembersFix.supportedErrorCodes), | ||
logger: createDummyLogger(), | ||
...initializerLocation, | ||
}) | ||
|
||
expect(fix.changes).toStrictEqual<ts.FileTextChanges[]>([ | ||
{ | ||
fileName: filePath, | ||
textChanges: [ | ||
{ | ||
span: { start: initializerLocation.start, length: argumentValue.length }, | ||
newText: `{ min: 0, max: 0 }`, | ||
}, | ||
], | ||
isNewFile: false, | ||
}, | ||
]) | ||
}) | ||
|
||
it('fills in undeclared argument members when some are declared', () => { | ||
const argumentValue = '{ max: 123 }' | ||
const [filePath, fileContent] = FsMocker.addFile(` | ||
function targetFunction(someArgument: { min: number; max: number } ) { | ||
return 123 | ||
} | ||
export const functionOutput = targetFunction(${argumentValue}) | ||
`) | ||
|
||
const initializerLocation = getNodeRange(fileContent, argumentValue) | ||
const fix = new MissingArgumentMembersFix({ | ||
filePath, | ||
program: createTestProgram([filePath], MissingArgumentMembersFix.supportedErrorCodes), | ||
logger: createDummyLogger(), | ||
...initializerLocation, | ||
}) | ||
|
||
expect(fix.changes).toStrictEqual<ts.FileTextChanges[]>([ | ||
{ | ||
fileName: filePath, | ||
textChanges: [ | ||
{ | ||
span: { start: initializerLocation.start, length: argumentValue.length }, | ||
newText: `{ max: 123, min: 0 }`, | ||
}, | ||
], | ||
isNewFile: false, | ||
}, | ||
]) | ||
}) | ||
|
||
it('fills in undeclared argument members that were defined in an interface', () => { | ||
const argumentValue = '{}' | ||
const [filePath, fileContent] = FsMocker.addFile(` | ||
interface TargetArgs { | ||
name: string | ||
date: Date | ||
} | ||
function targetFunction(someArgument: TargetArgs) { | ||
return 123 | ||
} | ||
export const functionOutput = targetFunction(${argumentValue}) | ||
`) | ||
|
||
const initializerLocation = getNodeRange(fileContent, argumentValue) | ||
const fix = new MissingArgumentMembersFix({ | ||
filePath, | ||
program: createTestProgram([filePath], MissingArgumentMembersFix.supportedErrorCodes), | ||
logger: createDummyLogger(), | ||
...initializerLocation, | ||
}) | ||
|
||
expect(fix.changes).toStrictEqual<ts.FileTextChanges[]>([ | ||
{ | ||
fileName: filePath, | ||
textChanges: [ | ||
{ | ||
span: { start: initializerLocation.start, length: argumentValue.length }, | ||
newText: `{ name: 'todo', date: new Date() }`, | ||
}, | ||
], | ||
isNewFile: false, | ||
}, | ||
]) | ||
}) | ||
}) |
Oops, something went wrong.