-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace documentation example with
vue-eslint-editor
- Loading branch information
Showing
28 changed files
with
2,596 additions
and
1,690 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
<template> | ||
<div class="eslint-code-container"> | ||
<eslint-editor | ||
ref="editor" | ||
:linter="linter" | ||
:config="config" | ||
v-model="code" | ||
:style="{ height }" | ||
class="eslint-code-block" | ||
:filename="resplvedFilename" | ||
:language="language" | ||
dark | ||
:format="format" | ||
:fix="fix" | ||
/> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import './setup' | ||
import EslintEditor from 'vue-eslint-editor' | ||
import { rules } from '../../../' | ||
import { setTimeouts } from '../../../dist/utils/default-timeouts' | ||
import { setFileContents } from '../shim/fs/fake-fs' | ||
setTimeouts({ CACHE_LOADER: -1, MTIME_MS_CHECK: -1 }) | ||
export default { | ||
name: 'ESLintCodeBlock', | ||
components: { EslintEditor }, | ||
inject: { | ||
$resourceGroup: { | ||
from: '$resourceGroup', | ||
default: null | ||
} | ||
}, | ||
props: { | ||
fix: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
rules: { | ||
type: Object, | ||
default() { | ||
return {} | ||
} | ||
}, | ||
filename: { | ||
type: String | ||
}, | ||
language: { | ||
type: String, | ||
default: 'html' | ||
}, | ||
localeKey: { | ||
type: String, | ||
default: 'file' | ||
}, | ||
messageSyntaxVersion: { | ||
type: String, | ||
default: '^9' | ||
} | ||
}, | ||
data() { | ||
return { | ||
linter: null, | ||
format: { | ||
insertSpaces: true, | ||
tabSize: 2 | ||
}, | ||
code: '' | ||
} | ||
}, | ||
watch: { | ||
code(newCode) { | ||
if (this.$resourceGroup) { | ||
this.$resourceGroup.set(this.resplvedFilename, newCode) | ||
} | ||
} | ||
}, | ||
computed: { | ||
isResource() { | ||
return this.language === 'json' || this.language === 'yaml' | ||
}, | ||
resplvedFilename() { | ||
return ( | ||
this.filename || | ||
(this.language === 'json' | ||
? 'example.json' | ||
: this.language === 'yaml' | ||
? 'example.yaml' | ||
: this.language === 'javascript' | ||
? 'example.js' | ||
: 'example.vue') | ||
) | ||
}, | ||
config() { | ||
return { | ||
globals: { | ||
console: false, | ||
// ES2015 globals | ||
ArrayBuffer: false, | ||
DataView: false, | ||
Float32Array: false, | ||
Float64Array: false, | ||
Int16Array: false, | ||
Int32Array: false, | ||
Int8Array: false, | ||
Map: false, | ||
Promise: false, | ||
Proxy: false, | ||
Reflect: false, | ||
Set: false, | ||
Symbol: false, | ||
Uint16Array: false, | ||
Uint32Array: false, | ||
Uint8Array: false, | ||
Uint8ClampedArray: false, | ||
WeakMap: false, | ||
WeakSet: false, | ||
// ES2017 globals | ||
Atomics: false, | ||
SharedArrayBuffer: false | ||
}, | ||
rules: this.rules, | ||
parser: | ||
this.language === 'json' | ||
? 'jsonc-eslint-parser' | ||
: this.language === 'yaml' | ||
? 'yaml-eslint-parser' | ||
: 'vue-eslint-parser', | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true | ||
} | ||
}, | ||
settings: { | ||
'vue-i18n': { | ||
localeDir: (this.$resourceGroup | ||
? this.$resourceGroup | ||
.getFiles() | ||
.filter(file => /\.(?:json5?|ya?ml)$/i.test(file)) | ||
: this.isResource | ||
? [this.resplvedFilename] | ||
: [] | ||
).map(pattern => ({ pattern, localeKey: this.localeKey })), | ||
messageSyntaxVersion: this.messageSyntaxVersion | ||
} | ||
} | ||
} | ||
}, | ||
height() { | ||
const lines = this.code.split('\n').length | ||
return `${Math.max(120, 19 * lines)}px` | ||
} | ||
}, | ||
methods: { | ||
computeCodeFromSlot(nodes) { | ||
if (!Array.isArray(nodes)) { | ||
return '' | ||
} | ||
return nodes | ||
.map(node => node.text || this.computeCodeFromSlot(node.children)) | ||
.join('') | ||
}, | ||
verifyHook() { | ||
setFileContents( | ||
this.$resourceGroup ? this.$resourceGroup.getFileContents() : {} | ||
) | ||
} | ||
}, | ||
async mounted() { | ||
this.code = `${this.computeCodeFromSlot(this.$slots.default).trim()}\n` | ||
this.$refs.editor.$watch('monaco', monaco => { | ||
monaco.languages.register({ id: 'yaml' }) | ||
monaco.languages.setMonarchTokensProvider( | ||
'yaml', | ||
require('monaco-editor/esm/vs/basic-languages/yaml/yaml').language | ||
) | ||
}) | ||
// Load linter. | ||
const [ | ||
{ default: Linter }, | ||
{ default: coreRules }, | ||
vueESLintParser, | ||
jsoncESLintParser, | ||
yamlESLintParser | ||
] = await Promise.all([ | ||
import('eslint4b/dist/linter'), | ||
import('eslint4b/dist/core-rules'), | ||
import('espree').then(() => import('vue-eslint-parser')), | ||
import('espree').then(() => import('jsonc-eslint-parser')), | ||
import('yaml-eslint-parser') | ||
]) | ||
const linter = (this.linter = new Linter({ cwd: '/path' })) | ||
linter.defineRules(coreRules) | ||
for (const ruleId of Object.keys(rules)) { | ||
linter.defineRule(`@intlify/vue-i18n/${ruleId}`, rules[ruleId]) | ||
} | ||
linter.defineParser('vue-eslint-parser', vueESLintParser) | ||
linter.defineParser('jsonc-eslint-parser', jsoncESLintParser) | ||
linter.defineParser('yaml-eslint-parser', yamlESLintParser) | ||
const verifyHook = this.verifyHook | ||
const verify = linter.verify | ||
linter.verify = function (...args) { | ||
verifyHook() | ||
return verify.apply(this, args) | ||
} | ||
const verifyAndFix = linter.verifyAndFix | ||
linter.verifyAndFix = function (...args) { | ||
verifyHook() | ||
return verifyAndFix.apply(this, args) | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style> | ||
.eslint-code-container { | ||
border-radius: 6px; | ||
padding: 1.25rem 0; | ||
margin: 1em 0; | ||
background-color: #1e1e1e; | ||
} | ||
.eslint-code-block { | ||
width: 100%; | ||
} | ||
.eslint-editor-actions { | ||
bottom: -0.9rem; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<template> | ||
<div> | ||
<slot /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import Vue from 'vue' | ||
export default { | ||
provide() { | ||
const data = Vue.observable({ fileContents: {} }) | ||
return { | ||
$resourceGroup: { | ||
set(fileName, code) { | ||
Vue.set(data.fileContents, fileName, code) | ||
}, | ||
getFileContents() { | ||
return data.fileContents | ||
}, | ||
getFiles() { | ||
return Object.keys(data.fileContents) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
if (typeof window !== 'undefined') { | ||
window.process = { | ||
cwd() { | ||
return '' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
docs/.vuepress/shim/eslint-plugin-vue-i18n/utils/glob-utils.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { getFiles } from '../../fs/fake-fs' | ||
export function listFilesToProcess() { | ||
return getFiles() | ||
.filter(filename => /\.(?:vue|js)$/i.test(filename)) | ||
.map(filename => ({ filename, ignored: false })) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { SourceCode } from '../../../../node_modules/eslint/lib/source-code' | ||
export { SourceCode } | ||
export class CLIEngine { | ||
addPlugin() {} | ||
getConfigForFile() { | ||
return { | ||
parser: 'vue-eslint-parser', | ||
parserOptions: { | ||
sourceType: 'module' | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export function statSync(filename) { | ||
return { | ||
mtimeMs: Date.now() | ||
} | ||
} | ||
|
||
let files = {} | ||
|
||
export function existsSync(filename) { | ||
return Boolean(files[filename]) || filename === '.' | ||
} | ||
export function readFileSync(filename) { | ||
return files[filename] || '' | ||
} | ||
|
||
// utility | ||
export function setFileContents(filesMap) { | ||
files = filesMap | ||
} | ||
export function getFiles() { | ||
return Object.keys(files) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const fakeFS = require('./fake-fs') | ||
fakeFS.default = fakeFS | ||
|
||
module.exports = fakeFS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default { | ||
sync(p) { | ||
return [p] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
createRequire: () => () => null | ||
} |
Oops, something went wrong.