Skip to content

Commit

Permalink
fix: should be able to parse decorators in script lang="ts" & jsx (#2088
Browse files Browse the repository at this point in the history
)

* fix: should be able to parse decorators in script lang="ts"

* fix: should also support parsing jsx

Added to `compileScript` instead of `babelParserDefaultPlugins` because
it's not needed for template expression parsing
  • Loading branch information
haoqunjiang authored Sep 15, 2020
1 parent 0cddde6 commit 273d19a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,22 @@ describe('SFC compile <script setup>', () => {
})

describe('SFC analyze <script> bindings', () => {
it('can parse decorators syntax in typescript block', () => {
const { scriptAst } = compile(`
<script lang="ts">
import { Options, Vue } from 'vue-class-component';
@Options({
components: {
HelloWorld,
},
props: ['foo', 'bar']
})
export default class Home extends Vue {}
</script>
`)

expect(scriptAst).toBeDefined()
})
it('recognizes props array declaration', () => {
const { bindings } = compile(`
<script>
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export function compileScript(
const scriptLang = script && script.lang
const scriptSetupLang = scriptSetup && scriptSetup.lang
const isTS = scriptLang === 'ts' || scriptSetupLang === 'ts'
const plugins: ParserPlugin[] = [...babelParserDefaultPlugins]
const plugins: ParserPlugin[] = [...babelParserDefaultPlugins, 'jsx']
if (options.babelParserPlugins) plugins.push(...options.babelParserPlugins)
if (isTS) plugins.push('typescript')
if (isTS) plugins.push('typescript', 'decorators-legacy')

if (!scriptSetup) {
if (!script) {
Expand Down

1 comment on commit 273d19a

@jods4
Copy link
Contributor

@jods4 jods4 commented on 273d19a Sep 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make JSX parsing an opt-in.
It is known that JSX syntax is sometimes ambiguous with TS generics or casts <>.
So when using JSX, some valid TS code doesn't parse, which is very annoying (if you're not using JSX).

I think using JSX inside a script setup, which is used along a <template>, is unlikely so this shouldn't be the default behavior.

If you want an example of real-world failures induced by JSX:
vuejs/eslint-plugin-vue#1223

EDIT: another argument to consider here is that we can add jsx support through babelParserPlugins, but we can't remove it.

Please sign in to comment.