Skip to content

Commit

Permalink
feat(tyreal): class method, and normal property
Browse files Browse the repository at this point in the history
TyrealHu committed Dec 29, 2022

Partially verified

This commit is signed with the committer’s verified signature.
gsmet’s contribution has been verified via GPG key.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
1 parent 59814c5 commit fbf22ec
Showing 4 changed files with 974 additions and 12 deletions.
928 changes: 928 additions & 0 deletions __test__/__snapshot__/class/type.ts

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions __test__/class/type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { generateSource, parseSource } from '../utils'
import ClassTypeSnapshot from '../__snapshot__/class/type'

describe('class', () => {
it('normal property', () => {
const node = parseSource(generateSource([
`class Student {`,
` name: string`,
` age: number`,
` school: string`,
` constructor(name: string, age: number, school: string) {`,
` this.name = name`,
` this.age = age`,
` this.school = school`,
` }`,
` study() {`,
` console.log('Im studying')`,
` }`,
`}`
]))

console.log(JSON.stringify(node, null, 2))
expect(node).toEqual(ClassTypeSnapshot.NormalProperty);
})
})
17 changes: 7 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -4702,11 +4702,10 @@ export default function tsPlugin(options?: {
node: Node,
isBinding: boolean = false,
refDestructuringErrors = new DestructuringErrors()
): void {
): any {
switch (node.type) {
case 'ParenthesizedExpression':
this.toAssignableParenthesizedExpression(node, isBinding, refDestructuringErrors)
break
return this.toAssignableParenthesizedExpression(node, isBinding, refDestructuringErrors)
case 'TSAsExpression':
case 'TSNonNullExpression':
case 'TSTypeAssertion':
@@ -4720,18 +4719,18 @@ export default function tsPlugin(options?: {
this.raise(node.start, TypeScriptError.UnexpectedTypeCastInParameter)
}
// @ts-ignore
this.toAssignable(node.expression, isBinding, refDestructuringErrors)
break
return this.toAssignable(node.expression, isBinding, refDestructuringErrors)
case 'AssignmentExpression':
// @ts-ignore
if (!isBinding && node.left.type === 'TSTypeCastExpression') {
// @ts-ignore
node.left = this.typeCastToParameter(node.left)
}
return node
/* fall through */
default:
// @ts-ignore
super.toAssignable(node, isBinding, refDestructuringErrors)
return super.toAssignable(node, isBinding, refDestructuringErrors)
}
}

@@ -4747,11 +4746,10 @@ export default function tsPlugin(options?: {
case 'TSTypeAssertion':
case 'ParenthesizedExpression':
// @ts-ignore
this.toAssignable(node.expression, isBinding, refDestructuringErrors)
break
return this.toAssignable(node.expression, isBinding, refDestructuringErrors)
default:
// @ts-ignore
super.toAssignable(node, isBinding, refDestructuringErrors)
return super.toAssignable(node, isBinding, refDestructuringErrors)
}
}

@@ -5491,7 +5489,6 @@ export default function tsPlugin(options?: {
// @ts-ignore
this.checkYieldAwaitInDefaultParams()
// @ts-ignore
this.parseFunctionBody(node, false, true, false)
const finishNode = this.parseFunctionBodyAndFinish(node, 'FunctionExpression', true)
this['yieldPos'] = oldYieldPos
this['awaitPos'] = oldAwaitPos
16 changes: 14 additions & 2 deletions start.ts
Original file line number Diff line number Diff line change
@@ -2,8 +2,20 @@ import * as acorn from 'acorn'
import tsPlugin from './src/index'

const node = acorn.Parser.extend(tsPlugin()).parse(`
function test(name?: string, age: number): void {
console.log(name)
class Student {
name: string
age: number
school: string
constructor(name: string, age: number, school: string) {
this.name = name
this.age = age
this.school = school
}
study() {
console.log('Im studying')
}
}
`, {
sourceType: 'module',

0 comments on commit fbf22ec

Please sign in to comment.