Skip to content

fix: resolve property assignments in object literals #373

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

Merged
merged 11 commits into from
Sep 30, 2024
Merged
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
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nodejs 20.8.1
pnpm 8.9.2
yarn 1.22.17
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@
"@types/diff": "5.0.9",
"@types/google-protobuf": "3.15.12",
"@types/node": "20.10.5",
"ts-node": "^10.7.0",
"@types/progress": "2.0.7",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"diff": "^5.1.0",
"eslint": "^8.57.0",
"eslint-plugin-etc": "^2.0.3",
"eslint-plugin-rxjs": "^5.0.3",
"eslint-plugin-unicorn": "^55.0.0",
"eslint-plugin-unused-imports": "^4.1.3",
"pnpm": "8.12.1",
"prettier": "3.3.2",
"ts-node": "^10.7.0",
"tsm": "^2.3.0",
"typescript-eslint": "7.18.0",
"uvu": "^0.5.6",
"tsm": "^2.3.0"
"uvu": "^0.5.6"
}
}
4 changes: 4 additions & 0 deletions snapshots/input/pure-js/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ function var_function_scope() {
}
print_fib(k)
}

function array_of_objects() {
var a = [{ element: 0 }, { element: 1 }]
}
12 changes: 12 additions & 0 deletions snapshots/input/syntax/src/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Configuration } from './reusable-types'

function MyDecorator(value: Configuration) {
return function (target: Function) {
console.log(`MyDecorator is called with value: ${value}`)
}
}

@MyDecorator({ property: 42, property2: '42' })
class MyClass {
//...
}
60 changes: 0 additions & 60 deletions snapshots/input/syntax/src/infer-relationship.ts

This file was deleted.

12 changes: 1 addition & 11 deletions snapshots/input/syntax/src/inheritance.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Superinterface } from './reusable-types'
import { Overloader } from './overload'

export interface Superinterface {
property: string
interfaceMethod(): string
}
export interface IntermediateSuperinterface extends Superinterface {
intermediateInterfaceMethod(): string
}
Expand Down Expand Up @@ -43,10 +40,3 @@ export const objectLiteralImplementation: Superinterface = {
throw new Error('Function not implemented.')
},
}
export function consumesInterface(superInterface: Superinterface): void {}
export function infersInterface(): void {
consumesInterface({
interfaceMethod: (): string => 'inferred',
property: 'inferred',
})
}
35 changes: 35 additions & 0 deletions snapshots/input/syntax/src/object-literals-arrow-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Option } from './reusable-types'

interface Foobar {
foobar: number
}

export function hasArrowFunctionParameter(
something: number,
fn: (foobar: Foobar) => Foobar
): Foobar {
return fn({ foobar: 42 + something })
}

export function consumesArrowFunction(): number {
return (
hasArrowFunctionParameter(1, ({ foobar }) => ({ foobar: foobar + 1 }))
.foobar +
hasArrowFunctionParameter(2, foobar => ({ foobar: foobar.foobar + 2 }))
.foobar
)
}

export function genericArrow(): Foobar[] {
return [1].map<Foobar>(n => ({ foobar: n + 1 }))
}

export function genericArrowOption(): Option<Foobar>[] {
return [1].map<Option<Foobar>>(n => ({ value: { foobar: n + 1 } }))
}

export function genericArrow2(): Foobar[] {
// navigation to `foobar` below does not work with tsserver or scip-java
// because `map` is missing an explicit `map<Foobar>` annotation.
return [1].map(n => ({ foobar: n + 1 }))
}
73 changes: 73 additions & 0 deletions snapshots/input/syntax/src/object-literals-call-signatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {
Configuration,
GenericClass,
GenericInterface,
Option,
Superinterface,
} from './reusable-types'

export function consumesInterface(superInterface: Superinterface): void {}
export function consumesArray(superInterface: Superinterface[]): void {}
export function consumesGenericInterface<T>(
genercInterface: GenericInterface<T>
): void {}

export function infersInterface(): void {
consumesInterface({
interfaceMethod: (): string => 'inferred',
property: 'inferred',
})
consumesArray([
{
interfaceMethod: (): string => 'inferred',
property: 'inferred',
},
])
consumesGenericInterface<number>({
interfaceMethod: (): string => 'inferred',
property: 123,
})
consumesGenericInterface<Option<Configuration>[]>({
interfaceMethod: (): string => 'inferred',
property: [{ value: { property: 42, property2: '42' } }],
})
}
export function returnStatementInsideArgumentExpression(): Configuration[] {
if (1 == 1) {
return [1].map<Configuration>((number: number): Configuration => {
const incremented = number + 1
return {
property: incremented,
property2: incremented.toString(),
}
})
} else {
return [1].map<Configuration>(number => {
const incremented = number + 1
return {
property: incremented,
property2: incremented.toString(),
}
})
}
}

export function createGenericClass(): GenericClass<Configuration> {
return new GenericClass<Configuration>([{ property: 1, property2: '2' }])
}

export function handleGenericClass() {
return createGenericClass().map(({ property, property2 }) => ({
property: property + 1,
property2: property2 + '1',
}))
}

export function handleShorthand() {
const property = '42'
const interfaceMethod = (): string => 'inferred'
consumesInterface({
interfaceMethod,
property,
})
}
40 changes: 40 additions & 0 deletions snapshots/input/syntax/src/object-literals-nested.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Option } from './reusable-types'

interface Address {
street: string
people: Person[]
}
interface Person {
name: string
address?: Address
}

export function handleNestedObjectLiterals(): Person {
return {
name: 'John',
address: {
street: 'Oxford Street',
people: [
{
name: 'Susan',
},
],
},
}
}

export function handleNestedTypeVariables(): Option<Person> {
return {
value: {
name: 'John',
address: {
street: 'Oxford Street',
people: [
{
name: 'Susan',
},
],
},
},
}
}
91 changes: 91 additions & 0 deletions snapshots/input/syntax/src/object-literals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Configuration } from './reusable-types'

function random(): number {
return Math.random()
}

export function handleArrayLiteral(): Configuration[] {
return [
{
property: 41,
property2: '41',
},
]
}

export function returnStatement(): Configuration {
if (random() > 0) {
return {
property: 41,
property2: '41',
}
}
for (let i = 0; i < 9; i++) {
if (random() > i) {
return {
property: 41,
property2: '41',
}
}
}
for (const i of [1, 2, 3]) {
if (random() > i) {
return {
property: 41,
property2: '41',
}
}
}
for (const i in { '1': 2 }) {
if (random() > Number.parseInt(i)) {
return {
property: 41,
property2: '41',
}
}
}
while (random() < 0) {
return {
property: 41,
property2: '41',
}
}
do {
if (random() > 0) {
return {
property: 41,
property2: '41',
}
}
} while (random() < 0)

return {
property: 42,
property2: '41',
}
}

export function constDeclaration(): number[] {
var configuration1: Configuration = {
property: 1,
property2: '1',
}
configuration1 = {
property: 2,
property2: '2',
}
let configuration2: Configuration = {
property: 3,
property2: '3',
}
configuration2.property = configuration1.property
const configuration3: Configuration = {
property: 4,
property2: '4',
}
return [
configuration1.property,
configuration2.property,
configuration3.property,
]
}
5 changes: 5 additions & 0 deletions snapshots/input/syntax/src/property-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ export function shorthandPropertyAssignment() {
const a = 'a'
return { a }
}
type A = { a: string; b: number }
export function typedPropertyAssignment(): A {
// prettier-ignore
return { a: 'a', "b": 10 }
}
Loading
Loading