Skip to content

Commit

Permalink
feat(vue): fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Sep 13, 2023
1 parent 7912d38 commit e4cd6e0
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 31 deletions.
1 change: 0 additions & 1 deletion packages/create-nx-workspace/bin/create-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ interface VueArguments extends BaseArguments {
appName: string;
// framework: 'none' | 'nuxt';
style: string;
// nextAppDir: boolean;
e2eTestRunner: 'none' | 'cypress' | 'playwright';
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`application generator should set up project correctly with given options 1`] = `
"{
"root": true,
"ignorePatterns": ["**/*"],
"plugins": ["@nx"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx", "*.vue"],
"rules": {
"@nx/enforce-module-boundaries": [
"error",
{
"enforceBuildableLibDependency": true,
"allow": [],
"depConstraints": [
{
"sourceTag": "*",
"onlyDependOnLibsWithTags": ["*"]
}
]
}
]
}
},
{
"files": ["*.ts", "*.tsx"],
"extends": ["plugin:@nx/typescript"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"extends": ["plugin:@nx/javascript"],
"rules": {}
}
]
}
"
`;

exports[`application generator should set up project correctly with given options 2`] = `
"import vue from '@vitejs/plugin-vue';
import { defineConfig } from 'vite';
Expand Down Expand Up @@ -36,7 +75,7 @@ export default defineConfig({
"
`;

exports[`application generator should set up project correctly with given options 2`] = `
exports[`application generator should set up project correctly with given options 3`] = `
"{
"name": "test",
"$schema": "../node_modules/nx/schemas/project-schema.json",
Expand Down Expand Up @@ -118,7 +157,7 @@ exports[`application generator should set up project correctly with given option
"
`;

exports[`application generator should set up project correctly with given options 3`] = `
exports[`application generator should set up project correctly with given options 4`] = `
"{
"extends": [
"plugin:vue/vue3-essential",
Expand All @@ -130,23 +169,15 @@ exports[`application generator should set up project correctly with given option
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"files": ["*.ts", "*.tsx", "*.js", "*.jsx", "*.vue"],
"rules": {}
}
]
}
"
`;

exports[`application generator should set up project correctly with given options 4`] = `
exports[`application generator should set up project correctly with given options 5`] = `
"import { describe, it, expect } from 'vitest';
import { mount } from '@vue/test-utils';
Expand All @@ -161,7 +192,7 @@ describe('App', () => {
"
`;

exports[`application generator should set up project correctly with given options 5`] = `
exports[`application generator should set up project correctly with given options 6`] = `
[
".eslintignore",
".eslintrc.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('application generator', () => {

it('should set up project correctly with given options', async () => {
await applicationGenerator(tree, { ...options, unitTestRunner: 'vitest' });
expect(tree.read('.eslintrc.json', 'utf-8')).toMatchSnapshot();
expect(tree.read('test/vite.config.ts', 'utf-8')).toMatchSnapshot();
expect(tree.read('test/project.json', 'utf-8')).toMatchSnapshot();
expect(tree.read('test/.eslintrc.json', 'utf-8')).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,78 @@ exports[`lib should add correct jest.config.ts and dependencies to package.json
`;

exports[`lib should add correct jest.config.ts and dependencies to package.json 2`] = `
{
"ignorePatterns": [
"**/*",
],
"overrides": [
{
"files": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx",
"*.vue",
],
"rules": {
"@nx/enforce-module-boundaries": [
"error",
{
"allow": [],
"depConstraints": [
{
"onlyDependOnLibsWithTags": [
"*",
],
"sourceTag": "*",
},
],
"enforceBuildableLibDependency": true,
},
],
},
},
{
"extends": [
"plugin:@nx/typescript",
],
"files": [
"*.ts",
"*.tsx",
],
"rules": {},
},
{
"extends": [
"plugin:@nx/javascript",
],
"files": [
"*.js",
"*.jsx",
],
"rules": {},
},
{
"env": {
"jest": true,
},
"files": [
"*.spec.ts",
"*.spec.tsx",
"*.spec.js",
"*.spec.jsx",
],
"rules": {},
},
],
"plugins": [
"@nx",
],
"root": true,
}
`;

exports[`lib should add correct jest.config.ts and dependencies to package.json 3`] = `
"/* eslint-disable */
export default {
displayName: 'my-lib',
Expand All @@ -91,7 +163,7 @@ export default {
"
`;

exports[`lib should add correct jest.config.ts and dependencies to package.json 3`] = `
exports[`lib should add correct jest.config.ts and dependencies to package.json 4`] = `
"{
"presets": ["@nx/js/babel"]
}
Expand Down Expand Up @@ -205,20 +277,7 @@ exports[`lib should generate files 1`] = `
"*.tsx",
"*.js",
"*.jsx",
],
"rules": {},
},
{
"files": [
"*.ts",
"*.tsx",
],
"rules": {},
},
{
"files": [
"*.js",
"*.jsx",
"*.vue",
],
"rules": {},
},
Expand Down
3 changes: 2 additions & 1 deletion packages/vue/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('lib', () => {
it('should add correct jest.config.ts and dependencies to package.json', async () => {
await libraryGenerator(tree, { ...defaultSchema, unitTestRunner: 'jest' });
expect(readJson(tree, '/package.json')).toMatchSnapshot();
expect(readJson(tree, '.eslintrc.json')).toMatchSnapshot();
expect(tree.read('my-lib/jest.config.ts', 'utf-8')).toMatchSnapshot();
expect(tree.read('my-lib/.babelrc', 'utf-8')).toMatchSnapshot();
});
Expand Down Expand Up @@ -399,10 +400,10 @@ describe('lib', () => {
});

const eslintConfig = readJson(tree, 'my-lib/.eslintrc.json');

expect(eslintConfig.overrides[0].parserOptions.project).toEqual([
'my-lib/tsconfig.*?.json',
]);
expect(eslintConfig.overrides[0].files).toContain('*.vue');
});
});
});
91 changes: 90 additions & 1 deletion packages/vue/src/utils/add-linting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Tree } from 'nx/src/generators/tree';
import { Linter, lintProjectGenerator } from '@nx/linter';
import { joinPathFragments } from 'nx/src/utils/path';
import { addDependenciesToPackageJson, runTasksInSerial } from '@nx/devkit';
import {
addDependenciesToPackageJson,
runTasksInSerial,
updateJson,
} from '@nx/devkit';
import { extraEslintDependencies } from './lint';
import {
addExtendsToLintConfig,
Expand Down Expand Up @@ -44,6 +48,8 @@ export async function addLinting(
]);
}

editEslintConfigFiles(host, options.projectRoot, options.rootProject);

let installTask = () => {};
if (!options.skipPackageJson) {
installTask = addDependenciesToPackageJson(
Expand All @@ -58,3 +64,86 @@ export async function addLinting(
return () => {};
}
}

function editEslintConfigFiles(
tree: Tree,
projectRoot: string,
rootProject?: boolean
) {
if (tree.exists(joinPathFragments(projectRoot, 'eslint.config.js'))) {
const fileName = joinPathFragments(projectRoot, 'eslint.config.js');
updateJson(tree, fileName, (json) => {
let updated = false;
for (let override of json.overrides) {
if (override.parserOptions) {
if (!override.files.includes('*.vue')) {
override.files.push('*.vue');
}
updated = true;
}
}
if (!updated) {
json.overrides = [
{
files: ['*.ts', '*.tsx', '*.js', '*.jsx', '*.vue'],
rules: {},
},
];
}
return json;
});
} else {
const fileName = joinPathFragments(projectRoot, '.eslintrc.json');
updateJson(tree, fileName, (json) => {
let updated = false;
for (let override of json.overrides) {
if (override.parserOptions) {
if (!override.files.includes('*.vue')) {
override.files.push('*.vue');
}
updated = true;
}
}
if (!updated) {
json.overrides = [
{
files: ['*.ts', '*.tsx', '*.js', '*.jsx', '*.vue'],
rules: {},
},
];
}
return json;
});
}

// Edit root config too
if (tree.exists('.eslintrc.base.json')) {
updateJson(tree, '.eslintrc.base.json', (json) => {
for (let override of json.overrides) {
if (
override.rules &&
'@nx/enforce-module-boundaries' in override.rules
) {
if (!override.files.includes('*.vue')) {
override.files.push('*.vue');
}
}
}
return json;
});
} else if (tree.exists('.eslintrc.json') && !rootProject) {
updateJson(tree, '.eslintrc.json', (json) => {
for (let override of json.overrides) {
if (
override.rules &&
'@nx/enforce-module-boundaries' in override.rules
) {
if (!override.files.includes('*.vue')) {
override.files.push('*.vue');
}
}
}
return json;
});
}
}

0 comments on commit e4cd6e0

Please sign in to comment.