Skip to content

Commit

Permalink
refactor(backend): convert all to typescript modules
Browse files Browse the repository at this point in the history
  • Loading branch information
fmauNeko committed Dec 16, 2024
1 parent 0da7484 commit 148e3e1
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 49 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

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

5 changes: 5 additions & 0 deletions packages/backend/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export default tseslint.config(
tseslint.configs.recommended,
nodePlugin.configs['flat/recommended'],
eslintPlugin.configs['flat/recommended'],
{
rules: {
'n/file-extension-in-import': ['error', 'always'],
},
},
{
languageOptions: {
globals: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
const defaultConfig = {
plugins: ['prettier'],
extends: ['eslint:recommended'],
rules: {
Expand Down Expand Up @@ -82,3 +82,5 @@ module.exports = {
},
],
};

export default defaultConfig;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
const nodeConfig = {
rules: {},
env: {
node: true,
Expand All @@ -12,3 +12,5 @@ module.exports = {
ecmaVersion: 2018,
},
};

export default nodeConfig;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
const typescriptConfig = {
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
Expand All @@ -20,3 +20,5 @@ module.exports = {
'@typescript-eslint/no-shadow': 'error',
},
};

export default typescriptConfig;
11 changes: 0 additions & 11 deletions packages/backend/lib/index.js

This file was deleted.

16 changes: 16 additions & 0 deletions packages/backend/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import defaultConfig from './configs/default.js';
import nodeConfig from './configs/node.js';
import typescriptConfig from './configs/typescript.js';
import arrayForeach from './rules/array-foreach.js';
import noThen from './rules/no-then.js';

export const rules = {
'array-foreach': arrayForeach,
'no-then': noThen,
};

export const configs = {
default: defaultConfig,
node: nodeConfig,
typescript: typescriptConfig,
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module.exports = {
import { Rule } from 'eslint';
import docUrl from '../utils/docUrl.js';

const arrayForeach: Rule.RuleModule = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce `for..of` loops over `Array.forEach`',
url: require('../utils/url')(module),
url: docUrl(import.meta),
},
schema: [],
messages: {
Expand All @@ -13,7 +16,11 @@ module.exports = {
create(context) {
return {
CallExpression(node) {
if (node.callee.property && node.callee.property.name === 'forEach') {
if (
node.callee.type === 'MemberExpression' &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'forEach'
) {
context.report({
node,
messageId: 'preferMessage',
Expand All @@ -23,3 +30,5 @@ module.exports = {
};
},
};

export default arrayForeach;
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module.exports = {
import { Rule } from 'eslint';
import docUrl from '../utils/docUrl.js';

const noThen: Rule.RuleModule = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce using `async/await` syntax over Promises',
url: require('../utils/url')(module),
url: docUrl(import.meta),
},
schema: [],
messages: {
Expand All @@ -15,12 +18,12 @@ module.exports = {
create(context) {
return {
MemberExpression(node) {
if (node.property && node.property.name === 'then') {
if (node.property.type === 'Identifier' && node.property.name === 'then') {
context.report({
node: node.property,
messageId: 'thenMessage',
});
} else if (node.property && node.property.name === 'catch') {
} else if (node.property.type === 'Identifier' && node.property.name === 'catch') {
context.report({
node: node.property,
messageId: 'catchMessage',
Expand All @@ -30,3 +33,5 @@ module.exports = {
};
},
};

export default noThen;
13 changes: 13 additions & 0 deletions packages/backend/lib/utils/docUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as path from 'node:path';

import packageJson from '../../package.json' with { type: 'json' };

const docUrl = ({ filename }: ImportMeta) => {
const url = new URL(packageJson.homepage);
const rule = path.basename(filename, '.js');
url.hash = '';
url.pathname += `/blob/v${packageJson.version}/docs/rules/${rule}.md`;
return url.toString();
};

export default docUrl;
11 changes: 0 additions & 11 deletions packages/backend/lib/utils/url.js

This file was deleted.

3 changes: 2 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"main": "lib/index.js",
"scripts": {
"lint": "eslint",
"test": "mocha tests --recursive",
"test": "npm run build && mocha tests --recursive",
"clean": "rimraf dist",
"build": "npm run clean && tsc --build"
},
Expand All @@ -41,6 +41,7 @@
"typescript-eslint": "^8.17.0"
},
"devDependencies": {
"@types/node": "^22.10.2",
"eslint-plugin-eslint-plugin": "^6.3.2",
"eslint-plugin-n": "^17.14.0",
"mocha": "^11.0.1",
Expand Down
26 changes: 13 additions & 13 deletions packages/backend/tests/array-foreach.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
const rule = require('../lib/rules/array-foreach')
const RuleTester = require('eslint').RuleTester
import { RuleTester } from 'eslint';
import rule from '../dist/lib/rules/array-foreach.js';

const ruleTester = new RuleTester()
const ruleTester = new RuleTester();

ruleTester.run('array-foreach', rule, {
valid: [
{
code: 'for (const el of els) { el }',
parserOptions: {ecmaVersion: 6}
languageOptions: { ecmaVersion: 2018 },
},
{
code: 'els.map(el => el)',
parserOptions: {ecmaVersion: 6}
languageOptions: { ecmaVersion: 2018 },
},
{code: 'forEach()'}
{ code: 'forEach()' },
],
invalid: [
{
code: 'els.forEach(el => el)',
parserOptions: {ecmaVersion: 6},
languageOptions: { ecmaVersion: 2018 },
errors: [
{
message: 'Prefer for...of instead of Array.forEach',
type: 'CallExpression'
}
]
}
]
})
type: 'CallExpression',
},
],
},
],
});
39 changes: 39 additions & 0 deletions packages/backend/tests/no-then.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { RuleTester } from 'eslint';
import rule from '../dist/lib/rules/no-then.js';

const ruleTester = new RuleTester();

ruleTester.run('no-then', rule, {
valid: [
{
code: 'async () => { await promise }',
languageOptions: { ecmaVersion: 2018 },
},
{
code: 'async () => { try { await promise } catch (e) { throw e } }',
languageOptions: { ecmaVersion: 2018 },
},
],
invalid: [
{
code: 'promise.then(() => {})',
languageOptions: { ecmaVersion: 2018 },
errors: [
{
message: 'Prefer async/await to Promise.then()',
type: 'Identifier',
},
],
},
{
code: 'promise.catch(() => {})',
languageOptions: { ecmaVersion: 2018 },
errors: [
{
message: 'Prefer async/await to Promise.catch()',
type: 'Identifier',
},
],
},
],
});
7 changes: 4 additions & 3 deletions packages/backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"lib": ["es2023"],
"module": "node16",
"module": "nodenext",
"target": "es2022",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "node16",
"moduleResolution": "nodenext",
"allowUnusedLabels": false,
"allowUnreachableCode": false,
"exactOptionalPropertyTypes": true,
Expand All @@ -22,7 +22,8 @@
"checkJs": true,
"outDir": "./dist",
"sourceMap": true,
"baseUrl": "./lib"
"baseUrl": "./lib",
"resolveJsonModule": true
},
"include": ["./lib"]
}

0 comments on commit 148e3e1

Please sign in to comment.