Skip to content

Commit

Permalink
fix: ignore js file when the same ts file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 29, 2024
1 parent a7c9e4e commit 0dac399
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 2 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
"clean": "rimraf dist",
"lint": "eslint src test --ext ts",
"pretest": "npm run clean && npm run lint -- --fix && npm run prepublishOnly",
"test": "npm run test-local",
"test": "egg-bin test",
"posttest": "npm run clean",
"test-local": "egg-bin test",
"preci": "npm run clean && npm run lint && npm run prepublishOnly",
"ci": "egg-bin cov",
"postci": "npm run clean",
"prepublishOnly": "tshy && tshy-after && attw --pack"
},
"repository": {
Expand Down
9 changes: 8 additions & 1 deletion src/loader/file_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import globby from 'globby';
import { isClass, isGeneratorFunction, isAsyncFunction, isPrimitive } from 'is-type-of';
import utils, { Fun } from '../utils/index.js';

const debug = debuglog('@eggjs/core:file_loader');
const debug = debuglog('@eggjs/core/file_loader');

export const FULLPATH = Symbol('EGG_LOADER_ITEM_FULLPATH');
export const EXPORTS = Symbol('EGG_LOADER_ITEM_EXPORTS');
Expand Down Expand Up @@ -185,6 +185,13 @@ export class FileLoader {
for (const filepath of filepaths) {
const fullpath = path.join(directory, filepath);
if (!fs.statSync(fullpath).isFile()) continue;
if (filepath.endsWith('.js')) {
const filepathTs = filepath.replace(/\.js$/, '.ts');
if (filepaths.includes(filepathTs)) {
debug('[parse] ignore %s, because %s exists', fullpath, filepathTs);
continue;
}
}
// get properties
// app/service/foo/bar.js => [ 'foo', 'bar' ]
const properties = getProperties(filepath, this.options.caseStyle);
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/helloworld-ts/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
1 change: 1 addition & 0 deletions test/fixtures/helloworld-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
13 changes: 13 additions & 0 deletions test/fixtures/helloworld-ts/app/controller/foo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions test/fixtures/helloworld-ts/app/controller/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default class FooController {
async render() {
}
}
7 changes: 7 additions & 0 deletions test/fixtures/helloworld-ts/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (app) => {
app.router.get('/', async (ctx) => {
ctx.body = 'Hello World';
});
};
5 changes: 5 additions & 0 deletions test/fixtures/helloworld-ts/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default (app: any) => {
app.router.get('/', async (ctx: any) => {
ctx.body = 'Hello World';
});
};
5 changes: 5 additions & 0 deletions test/fixtures/helloworld-ts/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {
keys: 'my secret keys',
};
3 changes: 3 additions & 0 deletions test/fixtures/helloworld-ts/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
keys: 'my secret keys',
};
29 changes: 29 additions & 0 deletions test/fixtures/helloworld-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "helloworld-ts",
"dependencies": {
"egg": "beta"
},
"devDependencies": {
"@eggjs/bin": "7",
"@eggjs/mock": "6",
"@eggjs/tsconfig": "1",
"@types/mocha": "10",
"@types/node": "22",
"eslint": "8",
"eslint-config-egg": "14",
"typescript": "5"
},
"scripts": {
"lint": "eslint . --ext .ts",
"dev": "egg-bin dev",
"pretest": "npm run lint -- --fix",
"test": "egg-bin test",
"preci": "npm run lint",
"ci": "egg-bin cov",
"postci": "npm run prepublishOnly && npm run clean",
"clean": "tsc -b --clean",
"prepublishOnly": "npm run clean && tsc"
},
"private": true,
"repository": "[email protected]:eggjs/examples.git"
}
19 changes: 19 additions & 0 deletions test/fixtures/helloworld-ts/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { app } from '@eggjs/mock/bootstrap';

describe('example helloworld test', () => {
it('should GET / 200', () => {
return app.httpRequest()
.get('/')
.expect(200)
.expect('Hello World');
});

it('should GET /foo', async () => {
await app.httpRequest()
.get('/foo')
.expect(400)
.expect({
foo: 'bar',
});
});
});
14 changes: 14 additions & 0 deletions test/fixtures/helloworld-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": false
},
"exclude": [
"test"
]
}
22 changes: 22 additions & 0 deletions test/support-typescript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { strict as assert } from 'node:assert';
import request from 'supertest';
import { getFilepath } from './helper.js';
import { Application } from './fixtures/egg-esm/index.js';

describe('test/support-typescript.test.ts', () => {
let app: Application;
before(async () => {
app = new Application({
baseDir: getFilepath('helloworld-ts'),
type: 'application',
});
await app.loader.loadAll();
});

it('should ignore *.js when *.ts same file exists', async () => {
const res = await request(app.callback())
.get('/');
assert.equal(res.status, 200);
assert.equal(res.text, 'Hello World');
});
});

0 comments on commit 0dac399

Please sign in to comment.