Skip to content

Commit

Permalink
Merge pull request #22893 from mshima/skip_ci-eslint
Browse files Browse the repository at this point in the history
integrate eslint
  • Loading branch information
DanielFran authored Jul 18, 2023
2 parents 320e543 + 10c24d0 commit 637993a
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 107 deletions.
10 changes: 8 additions & 2 deletions generators/bootstrap/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ import {
createForceWriteConfigFilesTransform,
autoCrlfTransform,
isPrettierConfigFile,
createSortConfigFilesTransform,
createESLintTransform,
} from './support/index.mjs';
import { PRETTIER_EXTENSIONS } from '../generator-constants.mjs';
import { GENERATOR_UPGRADE } from '../generator-list.mjs';
import { PRIORITY_NAMES, QUEUES } from '../base-application/priorities.mjs';
import type { BaseGeneratorDefinition, GenericTaskGroup } from '../base/tasks.mjs';
import command from './command.mjs';
import { createSortConfigFilesTransform } from './support/index.mjs';

const { MULTISTEP_TRANSFORM, PRE_CONFLICTS } = PRIORITY_NAMES;
const { MULTISTEP_TRANSFORM_QUEUE } = QUEUES;
Expand Down Expand Up @@ -154,7 +155,12 @@ export default class BootstrapGenerator extends BaseGenerator {
forceYoFiles(),
createSortConfigFilesTransform(),
createForceWriteConfigFilesTransform(),
...(this.skipPrettier ? [] : [createPrettierTransform(prettierOptions, this, prettierTransformOptions)]),
...(this.skipPrettier
? []
: [
createESLintTransform.call(this, { ignoreErrors: ignoreErrors || this.upgradeCommand, extensions: 'ts,js' }),
createPrettierTransform(prettierOptions, this, prettierTransformOptions),
]),
...(this.jhipsterConfig.autoCrlf ? [autoCrlfTransform(this.createGit())] : []),
createConflicterTransform(this.env.adapter, { ...(this.env as any).conflicterOptions, memFs: this.env.sharedFs }),
];
Expand Down
77 changes: 77 additions & 0 deletions generators/bootstrap/support/eslint-transform.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright 2013-2023 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { passthrough } from 'p-transform';
import { isFileStateDeleted } from 'mem-fs-editor/state';
import ESLint from 'eslint';
import { Minimatch } from 'minimatch';

import BaseGenerator from '../../base-core/index.mjs';
import { getPackageRoot } from '../../../lib/index.mjs';

// eslint-disable-next-line import/prefer-default-export
export const createESLintTransform = function (
this: BaseGenerator | void,
transformOptions: { ignoreErrors?: boolean; extensions?: string } = {},
) {
const { extensions = 'js,ts', ignoreErrors } = transformOptions;
const minimatch = new Minimatch(`**/*.{${extensions}}`, { dot: true });
const eslint = new ESLint.ESLint({
fix: true,
// Disable destination configs. We should apply plugins and rules which jhipster depends on.
useEslintrc: false,
resolvePluginsRelativeTo: getPackageRoot(),
overrideConfig: {
plugins: ['unused-imports'],
extends: ['plugin:@typescript-eslint/base'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 'latest',
},
rules: {
'unused-imports/no-unused-imports': 'error',
'unused-imports/no-unused-vars': ['warn', { vars: 'all', varsIgnorePattern: '^_', args: 'after-used', argsIgnorePattern: '^_' }],
},
},
});

return passthrough(async file => {
if (!minimatch.match(file.path) || isFileStateDeleted(file)) {
return;
}
if (!file.contents) {
throw new Error(`File content doesn't exist for ${file.relative}`);
}
try {
if (await eslint.isPathIgnored(file.path)) {
return;
}
const [result] = await eslint.lintText(file.contents.toString(), { filePath: file.path });
if (result.output) {
file.contents = Buffer.from(result.output);
}
} catch (error) {
if (ignoreErrors) {
this?.log?.warn?.(error);
return;
}

throw error;
}
});
};
28 changes: 28 additions & 0 deletions generators/bootstrap/support/eslint-transform.spec.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect } from 'esmocha';
import { Readable } from 'stream';
import { pipeline } from 'stream/promises';
import { transform } from 'p-transform';

import { createESLintTransform } from './eslint-transform.mjs';

describe('generator - bootstrap - eslint', () => {
describe('::createESLintTransform', () => {
it('should remove unused imports', async () => {
const file = {
path: 'foo.ts',
contents: Buffer.from(`
import { Foo } from 'bar';
export const foo = 'bar';
`),
};
await pipeline(
Readable.from([file]),
createESLintTransform(),
transform(() => undefined),
);
expect(file.contents.toString()).toBe(`
export const foo = 'bar';
`);
});
});
});
1 change: 1 addition & 0 deletions generators/bootstrap/support/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
export { default as autoCrlfTransform } from './auto-crlf-transform.mjs';
export { default as createForceWriteConfigFilesTransform } from './force-write-config-files-transform.mjs';
export * from './eslint-transform.mjs';
export * from './multi-step-transform/index.mjs';
export * from './prettier-support.mjs';
export { default as createSortConfigFilesTransform } from './sort-config-files-transform.mjs';
2 changes: 1 addition & 1 deletion generators/generate-blueprint/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export default class extends BaseGenerator {
},
devDependencies: {
'ejs-lint': `${packagejs.devDependencies['ejs-lint']}`,
eslint: `${packagejs.devDependencies.eslint}`,
eslint: `${packagejs.dependencies.eslint}`,
'eslint-config-airbnb-base': `${packagejs.devDependencies['eslint-config-airbnb-base']}`,
'eslint-config-prettier': `${packagejs.devDependencies['eslint-config-prettier']}`,
'eslint-plugin-import': `${packagejs.devDependencies['eslint-plugin-import']}`,
Expand Down
2 changes: 1 addition & 1 deletion lib/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const getPackageRoot = relativePath => {
export const getPackageRoot = (relativePath?: string) => {
const sourceRoot = join(__dirname, '..');
const sourceBasename = basename(sourceRoot);
const packageDirectory = sourceBasename === 'generator-jhipster' ? sourceRoot : join(sourceRoot, '..');
Expand Down
Loading

0 comments on commit 637993a

Please sign in to comment.