I have the following in my .eslintrc:
'key-spacing': [ 'error', {
'singleLine': {
'beforeColon' : false,
'afterColon' : true
},
'multiLine': {
'beforeColon' : false,
'afterColon' : true,
'align' : 'colon'
}
}]
The goal being to make sure in an object assignment that the following is true:
in a single line assignment, there is no space before each colon,
but there is one after.
in a multi-line assignment, the colons line up horizontally, and
there is a space both before and after each colon.
the strange thing is that the following three code snippets:
1 [ from my app.vue file ].
export default {
name : 'app',
components : {
todos
}
}
2 [ from my main.js file ].
new Vue({
el : '#app',
render : h => h( App )
})
3 [ from my Hello.spec.js file ].
const vm = new Vue({
el : document.createElement( 'div' ),
render : h => h( Hello )
})
are each throwing errors on the key-spacing eslint rule:
/Users/autoboxer/development/learning-vue/src/app.vue
12:3 error Missing space after key 'name' key-spacing
/Users/autoboxer/development/learning-vue/src/main.js
6:2 error Missing space after key 'el' key-spacing
/Users/autoboxer/development/learning-vue/test/unit/specs/Hello.spec.js
7:4 error Missing space after key 'el' key-spacing
I can't figure out from my settings why they would be causing the errors listed as there are the requisite spaces after each specified word, but any help would be appreciated.
Configuration for this rule is really complex. I think what you are looking for is something like this:
'key-spacing': [ 'error', {
'singleLine': {
'beforeColon' : false,
'afterColon' : true
},
"align": {
"beforeColon" : true,
"afterColon" : true,
"on" : "colon"
}
}]
However, even with this configuration, ESLint doesn't allow for arbitrary location for colons. They have to be as close as possible to the longest key name in the object. So with the above configuration your code has to change to:
export default {
name : 'app',
components : {
todos
}
}
That will lint correctly with configuration I provided.
Related
I'm using vue-template-compiler to transform a text in the vue template to an expression
I have this vueJs file
<template>
<p>hi</p>
</template>
I'm trying to change the text with expression like this
<p>{{fun('hi')}}</p>
my code
const compiled = compiler.compile(
data,
{
modules: [
{
transformNode(data) {
const children = data.children.map((child) => {
if (child.type === 3 && child.text.trim()) {
return ({
type: 2,
expression: "_s(fun('hi'))",
tokens: [
{
'#binding': "fun('hi')"
}
],
text: "{{fun('hi)}}"
})
}
return child;
});
return { ...data, children };
},
},
],
},
{
whitespace: "condense",
}
);
it maps over the texts and replace it with the expression.
and it returns the final ast.
but I need to convert it back to string
like that:
<p>{{fun('hi')}}</p>
I need to convert the final ast back to the template
In my Angular project, I want to extend #commitlint/config-conventional with some pre-defined scopes.
The Angular project has a library for UI components (generated via ng generate library) and a default app which consumes the UI library.
In commitlint.config.js I've added the following lines:
module.exports = {
extends: ['#commitlint/config-conventional'],
rules: {
'scope-enum': [
2,
'always',
[
'ui-components',
'ui-components/badge',
'ui-components/button',
'ui-components/tooltip',
'core',
'account',
'plugins',
'settings',
'projects',
'shared',
'styles'
]
]
}
};
However, when I try to commit something with the scope: 'ui-components/tooltip':
fix(ui-components/tooltip): fix border
I get a commitlint error, saying that:
⧗ input: fix(ui-components/tooltip): fix border
✖ scope must be one of [ui-components, ui-components/badge, ui/button, ui-components/tooltip, core, account, plugins, settings, projects, shared, styles] [scope-enum]
✖ found 1 problems, 0 warnings
Unfortunately slashes aren't allowed in scopes.
To get around this I replace / with two dashes (--).
I wrote a script to grab subfolders and return an array:
https://gist.github.com/trevor-coleman/51f1730044e14081faaff098618aba36
[
'ui-components',
'ui-components--badge',
'ui-components--button',
'ui-components--tooltip',
...
]
According to source code, Commitlint use / for multiple scopes.
It means, you can commit like fix(core/account): fix border but you can't commit fix(ui-components/tooltip): fix border because you need to add tooltip in to your scopes first.
Here is source code: https://github.com/conventional-changelog/commitlint/blob/master/%40commitlint/rules/src/scope-enum.ts
Also, it is mentioned in here: https://github.com/conventional-changelog/commitlint/blob/master/docs/concepts-commit-conventions.md#multiple-scopes
You can write your own custom plugin to check scopes, I had the same issue, so I wrote one to solve this problem, see example commitlint.config.js below:
module.exports = {
extends: ["#commitlint/config-conventional"],
rules: {
"enhanced-scope-enum": [
2,
"always",
[
"ui-components",
"ui-components/badge",
"ui-components/button",
"ui-components/tooltip",
"core",
"account",
"plugins",
"settings",
"projects",
"shared",
"styles",
],
],
},
plugins: [
{
rules: {
"enhanced-scope-enum": (parsed, when = "always", value = []) => {
if (!parsed.scope) {
return [true, ""];
}
// only use comma sign as seperator
const scopeSegments = parsed.scope.split(",");
const check = (value, enums) => {
if (value === undefined) {
return false;
}
if (!Array.isArray(enums)) {
return false;
}
return enums.indexOf(value) > -1;
};
const negated = when === "never";
const result =
value.length === 0 ||
scopeSegments.every((scope) => check(scope, value));
return [
negated ? !result : result,
`scope must ${negated ? `not` : null} be one of [${value.join(
", "
)}]`,
];
},
},
},
],
}
I'm building an app with vue-cli, using airbnb rules to lint.
Despite me adding a rule to my .eslintrc.js config file, and the rule appying on other files, this particular variable in my Welcome.vue file keeps throwing a warning when linting.
The warning:
warning: Identifier 'tables_count' is not in camel case (camelcase) at src\components\Welcome.vue:49:33:
47 | enableAll: function enableAll() {
48 | const tables_count = this.tables.length;
49 | for (let i = 0; i < tables_count; i += 1) {
| ^
50 | this.tables[i].enabled = true;
51 | }
52 | },
The full .eslintrc.js file:
module.exports = {
root: true,
env: {
node: true,
},
extends: [
'plugin:vue/essential',
'#vue/airbnb',
],
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
indent: ['error', 4],
camelcase: ['warn', { properties: 'never' }],
},
parserOptions: {
parser: 'babel-eslint',
},
};
The strucure of my app is as follows:
App.vue
Welcome.vue
Game.vue
Both App.vue and Game.vue have variables with an under score and the linting is not throwing warnings for them.
App.vue: this.show_welcome = true;
Game.vue: this.current_answer = '';
What have I done wrong to make one particular Vue file offend the linter so much?!
This is either when I run npm run serve or npm run lint
NOTE: I thought I'd worked it out, but still no...
Currently I only have unit tests for welcome.vue, which has it's own lint file, but I've added the rule in there and I'm still getting the warnings:
tests/unit/eslintrc.js
module.exports = {
env: {
jest: true,
},
rules: {
camelcase: ['warn', { properties: 'never' }],
},
};
For a typescript project:
.eslintrc.js
{
rules: {
'#typescript-eslint/camelcase': 'off'
}
}
If you don't want eslint to check for variable casing then just turn it off with camelcase: 'off' in your .eslintrc.js.
I would like to know how to validate empty object using vuelidate. I tried to give a demonstration on jsfiddle as links follows
Vue.use(window.vuelidate.default)
const { required, minLength } = window.validators
new Vue(
{
el: "#app",
data: {
companies: [
{
id: 1,
name: 'facebook'
},
{
id: 2,
name: 'apple'
}
],
text: {
id: null,
name: null
}
},
validations: {
text: {
required
}
}
}
)
jsfiddle
$v.text is valid because it is a non-empty object. That means it doesn't have 'falsy' value so it meets the requirement. One way to make it work:
validations: {
text: {
id: {required},
name: {required},
},
},
JSFiddle
If you don't want to repeat items object structure, you can write a custom validator.
There is missing information about how to use withParams in the documentation of vuelidate page.
So i have searched on its github page and found this link .
According to link i came up with that solution
import { withParams } from 'vuelidate'
export const checkIfNull = withParams(
{ type: 'required' },
value => (value.id === null ? false : true)
)
There is nothing special about validating an object, you just need to define the structure and add any validation rules you require.
Please see the example I created and take another look at the Collections docs.
How can I make i18next load all languages from just one file?
I managed to do it by putting each language in a seperate file (translation-en.json, translation-no.json, etc), and also managed to input languages with the resStore option, but putting it all in a seperate .json file is really not documented anywhere (I've searched for 4 hours+ now)
My js code:
i18n.init({
debug: true,
lng: 'en',
resGetPath: 'translation.json'
},
function(t) {
console.log(t('test'));
});
My translation.json file:
{
en: {
translation: {
test: "some string"
}
},
no: {
translation: {
test: "litt tekst"
}
}
}
Ok, so I managed to "hack" it byt putting an object into a seperate .js file, include it in a script tag and loading it using resStore, but that just can't be the best way to use this lib.
Assume that your translation.json has loaded and assigned to a variable named resStore:
var resStore = {
en: {
translation: {
test: "some string"
}
},
no: {
translation: {
test: "litt tekst"
}
}
};
Next, you can override default ajax loading functionality with your customLoad function. An example might look like this:
var options = {
lng: 'en',
load: 'current',
lowerCaseLng: true,
fallbackLng: false,
resGetPath: 'i18n/__lng__/__ns__.json',
customLoad: function(lng, ns, options, loadComplete) {
var data = resStore[lng][ns];
loadComplete(null, data); // or loadComplete('some error'); if failed
},
ns: {
namespaces: ['translation'],
defaultNs: 'translation'
}
};
i18n.init(options, function(t) {
t('test'); // will get "some string"
});
new update on Mar 20, 2015
You can simply pass your resource store with the resStore option:
i18n.init({ resStore: resources });