Skip to content

Commit

Permalink
chore(misc): update @nuxt/eslint-config version to resolve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Sep 9, 2024
1 parent f38db75 commit 6fe2831
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const getGlobalFlatEslintConfiguration = (
content = addBlockToFlatConfigExport(
content,
generateFlatOverride({
files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'],
rules: {},
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('@nx/eslint:lint-project', () => {
},
},
{
files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'],
// Override or add rules here
rules: {},
},
Expand Down
6 changes: 1 addition & 5 deletions packages/eslint/src/generators/lint-project/lint-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ interface LintProjectOptions {
*/
addExplicitTargets?: boolean;
addPackageJsonDependencyChecks?: boolean;
skipExtendedBaseConfig?: boolean;
}

export function lintProjectGenerator(tree: Tree, options: LintProjectOptions) {
Expand Down Expand Up @@ -194,10 +193,7 @@ function createEsLintConfiguration(
rootProject: boolean
) {
// we are only extending root for non-standalone projects or their complementary e2e apps
const extendedRootConfig =
rootProject || options.skipExtendedBaseConfig
? undefined
: findEslintFile(tree);
const extendedRootConfig = rootProject ? undefined : findEslintFile(tree);
const pathToRootConfig = extendedRootConfig
? `${offsetFromRoot(projectConfig.root)}${extendedRootConfig}`
: undefined;
Expand Down
5 changes: 3 additions & 2 deletions packages/eslint/src/generators/utils/eslint-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ export function addExtendsToLintConfig(
plugin:
| string
| { name: string; needCompatFixup: boolean }
| Array<string | { name: string; needCompatFixup: boolean }>
| Array<string | { name: string; needCompatFixup: boolean }>,
insertAtTheEnd = false
): GeneratorCallback {
if (useFlatConfig(tree)) {
const pluginExtends: ts.SpreadElement[] = [];
Expand Down Expand Up @@ -381,7 +382,7 @@ export function addExtendsToLintConfig(
// start of the `extends` array
for (const pluginExtend of pluginExtends.reverse()) {
content = addBlockToFlatConfigExport(content, pluginExtend, {
insertAtTheEnd: false,
insertAtTheEnd,
});
}
tree.write(fileName, content);
Expand Down
26 changes: 13 additions & 13 deletions packages/eslint/src/generators/utils/flat-config/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ export function addImportToFlatConfig(
ts.isObjectBindingPattern(node.declarationList.declarations[0].name) &&
ts.isCallExpression(node.declarationList.declarations[0].initializer) &&
node.declarationList.declarations[0].initializer.expression.getText() ===
'require' &&
'require' &&
ts.isStringLiteral(
node.declarationList.declarations[0].initializer.arguments[0]
) &&
node.declarationList.declarations[0].initializer.arguments[0].text ===
imp
imp
) {
return node.declarationList.declarations[0].name.elements;
}
Expand Down Expand Up @@ -274,12 +274,12 @@ export function addImportToFlatConfig(
node.declarationList.declarations[0].name.getText() === variable &&
ts.isCallExpression(node.declarationList.declarations[0].initializer) &&
node.declarationList.declarations[0].initializer.expression.getText() ===
'require' &&
'require' &&
ts.isStringLiteral(
node.declarationList.declarations[0].initializer.arguments[0]
) &&
node.declarationList.declarations[0].initializer.arguments[0].text ===
imp
imp
) {
return true;
}
Expand All @@ -295,10 +295,10 @@ export function addImportToFlatConfig(
typeof variable === 'string'
? variable
: ts.factory.createObjectBindingPattern(
variable.map((v) =>
ts.factory.createBindingElement(undefined, undefined, v)
)
),
variable.map((v) =>
ts.factory.createBindingElement(undefined, undefined, v)
)
),
imp
);
const insert = printer.printNode(
Expand Down Expand Up @@ -393,7 +393,7 @@ export function removePlugin(
node.declarationList.declarations[0].initializer.arguments[0]
) &&
node.declarationList.declarations[0].initializer.arguments[0].text ===
pluginImport
pluginImport
) {
changes.push({
type: ChangeType.Delete,
Expand Down Expand Up @@ -470,7 +470,7 @@ export function removePlugin(
) as ts.PropertyAssignment;
const removeComma =
pluginsObj.properties.indexOf(plugin) <
pluginsObj.properties.length - 1 ||
pluginsObj.properties.length - 1 ||
pluginsObj.properties.hasTrailingComma;
changes.push({
type: ChangeType.Delete,
Expand Down Expand Up @@ -534,7 +534,7 @@ export function removeCompatExtends(
const callExp = node.expression.expression.expression;
if (
((callExp.expression.getText() === 'compat.config' &&
callExp.arguments[0].getText().includes('extends')) ||
callExp.arguments[0].getText().includes('extends')) ||
callExp.expression.getText() === 'compat.extends') &&
compatExtends.some((ext) =>
callExp.arguments[0].getText().includes(ext)
Expand Down Expand Up @@ -877,8 +877,8 @@ export function generateFlatOverride(
[
ts.factory.createStringLiteral(
override['languageOptions']?.['parserOptions']?.parser ??
override['languageOptions']?.parser ??
override.parser
override['languageOptions']?.parser ??
override.parser
),
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,41 @@ exports[`app generated files content - as-provided - my-app general application

exports[`app generated files content - as-provided - my-app general application should configure eslint correctly (eslintrc) 1`] = `
"{
"extends": ["@nuxt/eslint-config", "../.eslintrc.json"],
"ignorePatterns": ["!**/*", ".nuxt/**", ".output/**", "node_modules"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx", "*.vue"],
"rules": {
"vue/multi-word-component-names": "off"
}
"rules": {}
}
],
"extends": ["@nuxt/eslint-config"]
]
}
"
`;

exports[`app generated files content - as-provided - my-app general application should configure eslint correctly (flat config) 1`] = `
"const { FlatCompat } = require('@eslint/eslintrc');
const js = require('@eslint/js');
const baseConfig = require('../eslint.config.js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
...baseConfig,
{
files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx', '**/*.vue'],
// Override or add rules here
rules: {},
},
...compat.extends('@nuxt/eslint-config'),
{
rules: { 'vue/multi-word-component-names': 'off' },
files: ['**/*.vue'],
languageOptions: {
parserOptions: { parser: require('@typescript-eslint/parser') },
},
},
{ ignores: ['.nuxt/**', '.output/**', 'node_modules'] },
];
Expand Down Expand Up @@ -379,33 +387,41 @@ exports[`app generated files content - as-provided - myApp general application s

exports[`app generated files content - as-provided - myApp general application should configure eslint correctly (eslintrc) 1`] = `
"{
"extends": ["@nuxt/eslint-config", "../.eslintrc.json"],
"ignorePatterns": ["!**/*", ".nuxt/**", ".output/**", "node_modules"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx", "*.vue"],
"rules": {
"vue/multi-word-component-names": "off"
}
"rules": {}
}
],
"extends": ["@nuxt/eslint-config"]
]
}
"
`;

exports[`app generated files content - as-provided - myApp general application should configure eslint correctly (flat config) 1`] = `
"const { FlatCompat } = require('@eslint/eslintrc');
const js = require('@eslint/js');
const baseConfig = require('../eslint.config.js');
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
});
module.exports = [
...baseConfig,
{
files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx', '**/*.vue'],
// Override or add rules here
rules: {},
},
...compat.extends('@nuxt/eslint-config'),
{
rules: { 'vue/multi-word-component-names': 'off' },
files: ['**/*.vue'],
languageOptions: {
parserOptions: { parser: require('@typescript-eslint/parser') },
},
},
{ ignores: ['.nuxt/**', '.output/**', 'node_modules'] },
];
Expand Down
95 changes: 46 additions & 49 deletions packages/nuxt/src/utils/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { nuxtEslintConfigVersion } from './versions';
import { useFlatConfig } from '@nx/eslint/src/utils/flat-config';

// TODO(colum): Look into the recommended set up using `withNuxt` inside eslint.config.mjs. https://eslint.nuxt.com/packages/config
export async function addLinting(
host: Tree,
options: {
Expand All @@ -39,18 +40,33 @@ export async function addLinting(
skipFormat: true,
rootProject: options.rootProject,
addPlugin: true,
// Merging base flat config with `@nuxt/eslint-config` results in merge error due to `@typescript-eslint` being registered twice.
skipExtendedBaseConfig: true,
});
tasks.push(lintTask);

editEslintConfigFiles(host, options.projectRoot);

if (isEslintConfigSupported(host, options.projectRoot)) {
const addExtendsTask = addExtendsToLintConfig(host, options.projectRoot, [
'@nuxt/eslint-config',
]);
editEslintConfigFiles(host, options.projectRoot);

const addExtendsTask = addExtendsToLintConfig(
host,
options.projectRoot,
['@nuxt/eslint-config'],
true
);
tasks.push(addExtendsTask);

if (useFlatConfig(host)) {
addOverrideToLintConfig(
host,
options.projectRoot,
{
files: ['**/*.vue'],
languageOptions: {
parserOptions: { parser: '@typescript-eslint/parser' },
},
} as unknown // languageOptions is not in eslintrc format but for flat config
);
}

addIgnoresToLintConfig(host, options.projectRoot, [
'.nuxt/**',
'.output/**',
Expand Down Expand Up @@ -89,49 +105,30 @@ function editEslintConfigFiles(tree: Tree, projectRoot: string) {
o.files = [o.files, '*.vue'];
}
};

if (isEslintConfigSupported(tree, projectRoot)) {
if (useFlatConfig(tree)) {
addOverrideToLintConfig(
tree,
projectRoot,
{
rules: { 'vue/multi-word-component-names': 'off' },
},
{ insertAtTheEnd: false }
);
} else {
if (
lintConfigHasOverride(
tree,
projectRoot,
(o) => o.parserOptions && !hasVueFiles(o),
true
)
) {
updateOverrideInLintConfig(
tree,
projectRoot,
(o) => !!o.parserOptions,
(o) => {
addVueFiles(o);
return o;
}
);
} else {
replaceOverridesInLintConfig(tree, projectRoot, [
{
files: ['*.ts', '*.tsx', '*.js', '*.jsx', '*.vue'],
rules: { 'vue/multi-word-component-names': 'off' },
},
]);
if (
lintConfigHasOverride(
tree,
projectRoot,
(o) => o.parserOptions && !hasVueFiles(o),
true
)
) {
updateOverrideInLintConfig(
tree,
projectRoot,
(o) => !!o.parserOptions,
(o) => {
addVueFiles(o);
return o;
}
}
}

// Edit root config too
if (!isEslintConfigSupported(tree)) {
return;
);
} else {
replaceOverridesInLintConfig(tree, projectRoot, [
{
files: ['*.ts', '*.tsx', '*.js', '*.jsx', '*.vue'],
rules: {},
},
]);
}

if (
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export const nuxtDevtoolsVersion = '1.0.0';
export const nuxtUiTemplatesVersion = '^1.3.1';

// linting deps
export const nuxtEslintConfigVersion = '~0.3.6';
export const nuxtEslintConfigVersion = '~0.5.6';
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ module.exports = [
},
},
{
files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx', '**/*.vue'],
rules: { 'vue/multi-word-component-names': 'off' },
},
];
Expand Down
1 change: 1 addition & 0 deletions packages/vue/src/utils/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function editEslintConfigFiles(tree: Tree, projectRoot: string) {
);
// Add an empty rules object to users know how to add/override rules
addOverrideToLintConfig(tree, projectRoot, {
files: ['*.ts', '*.tsx', '*.js', '*.jsx', '*.vue'],
rules: { 'vue/multi-word-component-names': 'off' },
});
} else {
Expand Down

0 comments on commit 6fe2831

Please sign in to comment.