Skip to content

Emit references instead of definitions for object literal properties #256

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

Closed
wants to merge 5 commits into from
Closed
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
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,
]
}
29 changes: 29 additions & 0 deletions snapshots/input/syntax/src/reusable-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Reusable types for other snapshot tests

export interface Option<A> {
value?: A
}

export interface Numbers {
property: number
}
export interface Strings {
property2: string
}
export type Configuration = Numbers & Strings

export class GenericClass<A> {
constructor(public readonly values: A[]) {}
public map(fn: (a: A) => A): A[] {
return this.values.map(a => fn(a))
}
}

export interface Superinterface {
property: string
interfaceMethod(): string
}
export interface GenericInterface<T> {
property: T
interfaceMethod(): string
}
3 changes: 2 additions & 1 deletion snapshots/input/syntax/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"outDir": "dist",
"target": "ES2015"
"target": "ES2015",
"experimentalDecorators": true
}
}
Loading
Loading