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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
<eslint-editor | ||
:linter="linter" | ||
:config="config" | ||
:code="code" | ||
v-model="code" | ||
:style="{ height }" | ||
class="eslint-code-block" | ||
:filename="filename" | ||
|
@@ -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 | ||
} | ||
}, | ||
|
||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Also, is the second check really needed? Or can we always set the TS parser for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for review!
|
||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
|
@@ -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() { | ||
|
@@ -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') | ||
} | ||
}, | ||
|
||
|
@@ -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()) | ||
|
||
|
@@ -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> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`) | ||
} | ||
} |
There was a problem hiding this comment.
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 🙈