Skip to content
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

fix(loader): support type assertion #45

Merged
merged 1 commit into from
Aug 26, 2022
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
12 changes: 8 additions & 4 deletions src/loader/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ const babelPluginUntyped: PluginItem = function (api: ConfigAPI) {
.map(c => c.value)
)

if (p.node.value.type === 'ObjectExpression') {
const schemaProp = p.node.value.properties.find(prop =>
const valueNode = (p.node.value.type === 'TSTypeAssertion' || p.node.value.type === 'TSAsExpression')
? p.node.value.expression
: p.node.value

if (valueNode.type === 'ObjectExpression') {
const schemaProp = valueNode.properties.find(prop =>
('key' in prop) && prop.key.type === 'Identifier' && prop.key.name === '$schema'
)
if (schemaProp && 'value' in schemaProp) {
Expand All @@ -45,12 +49,12 @@ const babelPluginUntyped: PluginItem = function (api: ConfigAPI) {
}
} else {
// Object has not $schema
p.node.value.properties.unshift(...astify({ $schema: schema }).properties)
valueNode.properties.unshift(...astify({ $schema: schema }).properties)
}
} else {
// Literal value
p.node.value = t.objectExpression([
t.objectProperty(t.identifier('$default'), p.node.value),
t.objectProperty(t.identifier('$default'), valueNode),
t.objectProperty(t.identifier('$schema'), astify(schema))
])
}
Expand Down
72 changes: 71 additions & 1 deletion test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,80 @@ describe('transform (jsdoc)', () => {
}
})
})

it('correctly parses type assertion', () => {
const result = transform(`
import type { InputObject } from 'untyped'
export default {
/**
* @typedef {'src' | 'root'} HumanReadable
* @type {Array<HumanReadable>}
*/
srcDir: <InputObject>{
$resolve(val, get) {
return val ?? 'src'
}
}
}
`)
expect(result).toMatchInlineSnapshot(`
"export default {
srcDir: {
$schema: {
title: \\"\\",
description: \\"\\",
tags: [],
tsType: \\"Array<'src' | 'root'>\\",
markdownType: \\"Array<HumanReadable>\\"
},

$resolve(val, get) {
return val ?? 'src';
}

}
};"
`)
})

it('correctly parses type as assertion', () => {
const result = transform(`
import type { InputObject } from 'untyped'
export default {
/**
* @typedef {'src' | 'root'} HumanReadable
* @type {Array<HumanReadable>}
*/
srcDir: {
$resolve(val, get) {
return val ?? 'src'
}
} as InputObject
}
`)
expect(result).toMatchInlineSnapshot(`
"export default {
srcDir: {
$schema: {
title: \\"\\",
description: \\"\\",
tags: [],
tsType: \\"Array<'src' | 'root'>\\",
markdownType: \\"Array<HumanReadable>\\"
},

$resolve(val, get) {
return val ?? 'src';
}

}
};"
`)
})
})

function expectCodeToMatch (code: string, pattern: RegExp, expected: any) {
const [, result] = code.match(pattern)
const [, result] = code.match(pattern) || []
expect(result).toBeDefined()
// eslint-disable-next-line
const obj = Function('"use strict";return (' + result.replace(/;$/, '') + ')')()
Expand Down