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

Chore: ts-parser support in demo #1972

Merged
merged 3 commits into from Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.js
Expand Up @@ -131,7 +131,8 @@ module.exports = {
'unicorn/no-useless-undefined': 'off',
'unicorn/prefer-optional-catch-binding': 'off', // not supported by current ESLint parser version
'unicorn/prefer-module': 'off',
'unicorn/prevent-abbreviations': 'off'
'unicorn/prevent-abbreviations': 'off',
'unicorn/no-nested-ternary': 'off' // conflicts with Prettier
Copy link
Member

Choose a reason for hiding this comment

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

But it makes the code more readable than Prettier 🙈

},
overrides: [
{
Expand Down
62 changes: 51 additions & 11 deletions docs/.vuepress/components/eslint-code-block.vue
Expand Up @@ -3,7 +3,7 @@
<eslint-editor
:linter="linter"
:config="config"
:code="code"
v-model="code"
:style="{ height }"
class="eslint-code-block"
:filename="filename"
Expand Down Expand Up @@ -43,18 +43,24 @@ export default {
language: {
type: String,
default: 'html'
},
typescript: {
type: Boolean,
default: false
}
},

data() {
return {
code: this.computeCodeFromSlot(),
linter: null,
preprocess: processors['.vue'].preprocess,
postprocess: processors['.vue'].postprocess,
format: {
insertSpaces: true,
tabSize: 2
}
},
tsEslintParser: null
}
},

Expand Down Expand Up @@ -90,6 +96,14 @@ export default {
rules: this.rules,
parser: 'vue-eslint-parser',
parserOptions: {
parser: this.typescript
? this.tsEslintParser
: this.langTs
? {
ts: this.tsEslintParser,
typescript: this.tsEslintParser
}
: null,
Copy link
Member

Choose a reason for hiding this comment

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

It's really hard to read this. Can we extract part of this to a separate variable to make it more readable? Or use if? Or configure Prettier to allow parentheses here?

Also, is the second check really needed? Or can we always set the TS parser for the ts and typescript blocks?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for review!
I would like to rewrite this and write comments.

this.typescript is intended to be used when using ts code blocks.
The this.langTs condition is used during <script lang="ts"> or <script lang="typescript">. <script lang="typescript"> is not currently used, but I think someone may PR a new rule to enforce it in the future.

ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
Expand All @@ -99,8 +113,10 @@ export default {
}
},

code() {
return `${this.computeCodeFromSlot(this.$slots.default).trim()}\n`
langTs() {
return /lang\s*=\s*(?:"ts"|ts|'ts'|"typescript"|typescript|'typescript')/u.test(
this.code
)
},

height() {
Expand All @@ -109,14 +125,26 @@ export default {
}
},

methods: {
computeCodeFromSlot(nodes) {
if (!Array.isArray(nodes)) {
return ''
watch: {
typescript(value) {
if (value) {
this.loadTypescriptESLint()
}
},
langTs(value) {
if (value) {
this.loadTypescriptESLint()
}
return nodes
.map((node) => node.text || this.computeCodeFromSlot(node.children))
.join('')
}
},

methods: {
computeCodeFromSlot() {
return `${computeCodeFromSlot(this.$slots.default).trim()}\n`
},

async loadTypescriptESLint() {
this.tsEslintParser = await import('@typescript-eslint/parser')
}
},

Expand All @@ -126,6 +154,9 @@ export default {
import('eslint/lib/linter'),
import('espree').then(() => import('vue-eslint-parser'))
])
if (this.langTs || this.typescript) {
await this.loadTypescriptESLint()
}

const linter = (this.linter = new Linter())

Expand All @@ -136,6 +167,15 @@ export default {
linter.defineParser('vue-eslint-parser', { parseForESLint })
}
}

function computeCodeFromSlot(nodes) {
if (!Array.isArray(nodes)) {
return ''
}
return nodes
.map((node) => node.text || computeCodeFromSlot(node.children))
.join('')
}
</script>

<style>
Expand Down
3 changes: 2 additions & 1 deletion docs/.vuepress/config.js
Expand Up @@ -132,7 +132,8 @@ module.exports = {
'@eslint/eslintrc/universal': path.resolve(
__dirname,
'../../node_modules/@eslint/eslintrc/dist/eslintrc-universal.cjs'
)
),
globby: require.resolve('./shim/globby')
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/.vuepress/enhanceApp.js
Expand Up @@ -12,7 +12,7 @@ export default (
window.process = new Proxy(
{
env: {},
cwd: () => undefined
cwd: () => ''
},
{
get(target, name) {
Expand Down
1 change: 1 addition & 0 deletions docs/.vuepress/shim/globby.js
@@ -0,0 +1 @@
module.exports = {}
10 changes: 9 additions & 1 deletion docs/.vuepress/shim/module.js
@@ -1,3 +1,11 @@
module.exports = {
createRequire: () => () => null
createRequire: () => (module) => {
if (module === 'espree') {
return require('espree')
}
if (module === 'eslint-scope') {
return require('eslint-scope')
}
throw new Error(`Not implemented: ${module}`)
}
}
8 changes: 8 additions & 0 deletions docs/rules/valid-define-emits.md
Expand Up @@ -45,13 +45,17 @@ This rule reports `defineEmits` compiler macros in the following cases:

</eslint-code-block>

<eslint-code-block :rules="{'vue/valid-define-emits': ['error']}">

```vue
<script setup lang="ts">
/* ✓ GOOD */
defineEmits<(e: 'notify')=>void>()
</script>
```

</eslint-code-block>

<eslint-code-block :rules="{'vue/valid-define-emits': ['error']}">

```vue
Expand All @@ -78,13 +82,17 @@ This rule reports `defineEmits` compiler macros in the following cases:

</eslint-code-block>

<eslint-code-block :rules="{'vue/valid-define-emits': ['error']}">

```vue
<script setup lang="ts">
/* ✗ BAD */
defineEmits<(e: 'notify')=>void>({ submit: null })
</script>
```

</eslint-code-block>

<eslint-code-block :rules="{'vue/valid-define-emits': ['error']}">

```vue
Expand Down
8 changes: 8 additions & 0 deletions docs/rules/valid-define-props.md
Expand Up @@ -45,13 +45,17 @@ This rule reports `defineProps` compiler macros in the following cases:

</eslint-code-block>

<eslint-code-block :rules="{'vue/valid-define-props': ['error']}">

```vue
<script setup lang="ts">
/* ✓ GOOD */
defineProps<{ msg?:string }>()
</script>
```

</eslint-code-block>

<eslint-code-block :rules="{'vue/valid-define-props': ['error']}">

```vue
Expand All @@ -78,13 +82,17 @@ This rule reports `defineProps` compiler macros in the following cases:

</eslint-code-block>

<eslint-code-block :rules="{'vue/valid-define-props': ['error']}">

```vue
<script setup lang="ts">
/* ✗ BAD */
defineProps<{ msg?:string }>({ msg: String })
</script>
```

</eslint-code-block>

<eslint-code-block :rules="{'vue/valid-define-props': ['error']}">

```vue
Expand Down