Skip to content

Commit

Permalink
add support to typescriptEslint
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jul 16, 2024
1 parent f4d76df commit 7424f42
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 13 deletions.
1 change: 1 addition & 0 deletions generators/base-application/support/task-type-inference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function asWriteFilesSection<Data = GeneratorDefinition['writingTaskParam
) {
return section;
}

export function asWriteFilesBlock<Data = GeneratorDefinition['writingTaskParam']['application']>(
section: WriteFileBlock<CoreGenerator, Data>,
) {
Expand Down
7 changes: 6 additions & 1 deletion generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,12 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;

const normalizedFile = resolveCallback(sourceFile || file);
sourceFile = join(blockPath, normalizedFile);
destinationFile = this.destinationPath(blockTo, join(resolveCallback(destinationFile || renameTo, normalizedFile)));
destinationFile = join(resolveCallback(destinationFile || renameTo, normalizedFile));
if (blockRenameTo) {
destinationFile = this.destinationPath(blockRenameTo.call(this, context, destinationFile, this));
} else {
destinationFile = this.destinationPath(blockTo, destinationFile);
}

const override = resolveCallback(fileSpec.override);
if (override !== undefined && !override && (this as any).fs.exists(destinationFile)) {
Expand Down
5 changes: 4 additions & 1 deletion generators/client/support/files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { WriteFileBlock } from '../../base/api.js';
import type CoreGenerator from '../../base-core/index.js';
import { CLIENT_MAIN_SRC_DIR, CLIENT_TEST_SRC_DIR } from '../../generator-constants.js';
import type { CommonClientServerApplication } from '../../base-application/types.js';

export const replaceEntityFilePath = (data: any, filepath: string) =>
filepath
Expand All @@ -10,7 +11,9 @@ export const replaceEntityFilePath = (data: any, filepath: string) =>

const CLIENT_TEMPLATES_SRC_DIR = CLIENT_MAIN_SRC_DIR;

type RelativeWriteFileBlock = WriteFileBlock & { relativePath?: string };
type RelativeWriteFileBlock = WriteFileBlock<CoreGenerator, CommonClientServerApplication & Record<string, any>> & {
relativePath?: string;
};

export function clientRootTemplatesBlock(blockOrRelativePath?: string): Pick<WriteFileBlock, 'path' | 'renameTo'>;
export function clientRootTemplatesBlock(blockOrRelativePath: RelativeWriteFileBlock): WriteFileBlock;
Expand Down
8 changes: 3 additions & 5 deletions generators/generate-blueprint/generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { fileURLToPath } from 'url';
import { before, it, describe, expect } from 'esmocha';
import { snakeCase } from 'lodash-es';

import { defaultHelpers as helpers } from '../../testing/index.js';
import { defaultHelpers as helpers, runResult } from '../../testing/index.js';
import { shouldSupportFeatures, testBlueprintSupport } from '../../test/support/tests.js';
import Generator from './index.js';

Expand All @@ -43,9 +43,8 @@ describe(`generator - ${generator}`, () => {

describe('with', () => {
describe('default config', () => {
let runResult;
before(async () => {
runResult = await helpers.run(generatorPath).withJHipsterConfig().withMockedGenerators(mockedGenerators);
await helpers.run(generatorPath).withJHipsterConfig().withMockedGenerators(mockedGenerators);
});
it('should compose with init generator', () => {
expect(runResult.mockedGenerators['jhipster:init'].calledOnce).toBe(true);
Expand All @@ -55,9 +54,8 @@ describe(`generator - ${generator}`, () => {
});
});
describe('all option', () => {
let runResult;
before(async () => {
runResult = await helpers.run(generatorPath).withOptions({ allGenerators: true }).withMockedGenerators(mockedGenerators);
await helpers.run(generatorPath).withOptions({ allGenerators: true }).withMockedGenerators(mockedGenerators);
});
it('should compose with init generator', () => {
expect(runResult.mockedGenerators['jhipster:init'].calledOnce).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
<&_ if (fragment.developmentSection) { -&>
<&_ if (fragment.importsSection) { -&>
import jhipster from 'generator-jhipster/eslint/recommended';
<&_ } -&>
<&_ if (fragment.configSection) { -&>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ exports[`generator - javascript:bootstrap packageJsonNodeEngine(true) should mat
"engines": {
"node": "^18.19.0 || >= 20.6.1"
},
"scripts": {}
"scripts": {},
"type": "commonjs"
}
",
"stateCleared": "modified",
Expand Down
1 change: 1 addition & 0 deletions generators/javascript/generators/bootstrap/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const command: JHipsterCommandDefinition = {
hide: true,
},
choices: ['commonjs', 'module'],
default: 'commonjs',
scope: 'storage',
},
},
Expand Down
8 changes: 7 additions & 1 deletion generators/javascript/generators/eslint/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
import type { JHipsterCommandDefinition } from '../../../base/api.js';

const command: JHipsterCommandDefinition = {
configs: {},
configs: {
typescriptEslint: {
cli: {
type: Boolean,
},
},
},
import: [],
};

Expand Down
6 changes: 5 additions & 1 deletion generators/javascript/generators/eslint/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ export default class EslintGenerator extends BaseApplicationGenerator {
return this.asWritingTaskGroup({
async writing({ application }) {
await this.writeFiles({
blocks: [{ templates: [{ sourceFile: 'eslint.config.js.jhi', destinationFile: ctx => `${ctx.eslintConfigFile}.jhi` }] }],
blocks: [
{
templates: [{ sourceFile: 'eslint.config.js.jhi', destinationFile: ctx => `${ctx.eslintConfigFile}.jhi` }],
},
],
context: application,
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@
<&_
// Register sections and max allowed fragments, 0 for unlimited.
fragments.registerSections({
importSection: 0,
importsSection: 0,
configSection: 0,
});
_&>
<%_ if (typescriptEslint) { _%>
// @ts-check
<%_ } _%>
import globals from 'globals';
import prettier from 'eslint-plugin-prettier/recommended';
<&- fragments.importSection() &>
<%_ if (typescriptEslint) { _%>
import tseslint from 'typescript-eslint';
<%_ } _%>
<&- fragments.importsSection() &>
// jhipster-needle-eslint-add-import - JHipster will add additional import here

<%_ if (typescriptEslint) { _%>
export default tseslint.config(
<%_ } else { _%>
export default [
<%_ } _%>
{
languageOptions: {
globals: {
Expand All @@ -39,4 +50,8 @@ export default [
<&- fragments.configSection() &>
// jhipster-needle-eslint-add-config - JHipster will add additional config here
prettier,
<%_ if (typescriptEslint) { _%>
);
<%_ } else { _%>
];
<%_ } _%>

0 comments on commit 7424f42

Please sign in to comment.