chore: improve lint

This commit is contained in:
tamaina
2023-12-30 16:28:45 +00:00
parent 718465e498
commit 71fe234d3e
16 changed files with 93 additions and 167 deletions

View File

@ -3,120 +3,14 @@ module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
project: ['./tsconfig.json', './test/tsconfig.json'],
},
plugins: [
'@typescript-eslint',
'import'
],
ignorePatterns: ['**/.eslintrc.cjs'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:import/recommended',
'plugin:import/typescript'
'plugin:@misskey-dev/recommended',
],
rules: {
'indent': ['warn', 'tab', {
'SwitchCase': 1,
'MemberExpression': 1,
'flatTernaryExpressions': true,
'ArrayExpression': 'first',
'ObjectExpression': 'first',
}],
'eol-last': ['error', 'always'],
'semi': ['error', 'always'],
'semi-spacing': ['error', { 'before': false, 'after': true }],
'quotes': ['warn', 'single'],
'comma-dangle': ['warn', 'always-multiline'],
'comma-spacing': ['error', { 'before': false, 'after': true }],
'array-bracket-spacing': ['error', 'never'],
'keyword-spacing': ['error', {
'before': true,
'after': true,
}],
'key-spacing': ['error', {
'beforeColon': false,
'afterColon': true,
}],
'arrow-spacing': ['error', {
'before': true,
'after': true,
}],
'brace-style': ['error', '1tbs', {
'allowSingleLine': true,
}],
'padded-blocks': ['error', 'never'],
/* TODO: path aliasを使わないとwarnする
'no-restricted-imports': ['warn', {
'patterns': [
]
}],
*/
'eqeqeq': ['error', 'always', { 'null': 'ignore' }],
'no-multi-spaces': ['error'],
'no-var': ['error'],
'prefer-arrow-callback': ['error'],
'no-throw-literal': ['error'],
'no-param-reassign': ['warn'],
'no-constant-condition': ['warn'],
'no-empty-pattern': ['warn'],
'no-async-promise-executor': ['off'],
'no-useless-escape': ['off'],
'no-multiple-empty-lines': ['error', { 'max': 1 }],
'no-control-regex': ['warn'],
'no-empty': ['warn'],
'no-inner-declarations': ['off'],
'no-sparse-arrays': ['off'],
'nonblock-statement-body-position': ['error', 'beside'],
'object-curly-spacing': ['error', 'always'],
'space-infix-ops': ['error'],
'space-before-blocks': ['error', 'always'],
'padding-line-between-statements': [
'error',
{ 'blankLine': 'always', 'prev': 'function', 'next': '*' },
{ 'blankLine': 'always', 'prev': '*', 'next': 'function' },
],
"lines-between-class-members": "off",
/* typescript-eslint では enforce に対応してないっぽい
'@typescript-eslint/lines-between-class-members': ['error', {
enforce: [{
blankLine: 'always',
prev: 'method',
next: '*',
}]
}],
*/
'@typescript-eslint/func-call-spacing': ['error', 'never'],
'@typescript-eslint/no-explicit-any': ['warn'],
'@typescript-eslint/no-unused-vars': ['warn'],
'@typescript-eslint/no-unnecessary-condition': ['warn'],
'@typescript-eslint/no-var-requires': ['warn'],
'@typescript-eslint/no-inferrable-types': ['warn'],
'@typescript-eslint/no-empty-function': ['off'],
'@typescript-eslint/no-non-null-assertion': ['warn'],
'@typescript-eslint/explicit-function-return-type': ['off'],
'@typescript-eslint/no-misused-promises': ['error', {
'checksVoidReturn': false,
}],
'@typescript-eslint/consistent-type-imports': 'off',
'@typescript-eslint/prefer-nullish-coalescing': [
'warn',
],
'@typescript-eslint/naming-convention': [
'error',
{
"selector": "typeLike",
"format": ["PascalCase"]
},
{
"selector": "typeParameter",
"format": []
}
],
'import/no-unresolved': ['off'],
'import/no-default-export': ['warn'],
'import/order': ['warn', {
'groups': ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object', 'type'],
}]
'@typescript-eslint/prefer-nullish-coalescing': 'off',
'import/no-default-export': 'off',
},
};

View File

@ -1,6 +1,6 @@
{
"name": "@misskey-dev/summaly",
"version": "5.0.3",
"version": "5.0.4",
"description": "Get web page's summary",
"author": "syuilo <syuilotan@yahoo.co.jp>",
"license": "MIT",
@ -16,12 +16,13 @@
],
"scripts": {
"build": "tsc",
"eslint": "eslint --quiet \"src/**/*.ts\"",
"eslint": "eslint src --ext .js,.jsx,.ts,.tsx",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --silent=false --verbose false",
"serve": "fastify start ./built/index.js"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@misskey-dev/eslint-plugin": "^1.0.0",
"@swc/core": "^1.3.101",
"@swc/jest": "^0.2.29",
"@types/cheerio": "0.22.18",

17
pnpm-lock.yaml generated
View File

@ -34,6 +34,9 @@ devDependencies:
'@jest/globals':
specifier: ^29.7.0
version: 29.7.0
'@misskey-dev/eslint-plugin':
specifier: ^1.0.0
version: 1.0.0(@typescript-eslint/eslint-plugin@6.16.0)(@typescript-eslint/parser@6.16.0)(eslint-plugin-import@2.29.1)(eslint@8.56.0)
'@swc/core':
specifier: ^1.3.101
version: 1.3.101
@ -781,6 +784,20 @@ packages:
'@jridgewell/sourcemap-codec': 1.4.15
dev: true
/@misskey-dev/eslint-plugin@1.0.0(@typescript-eslint/eslint-plugin@6.16.0)(@typescript-eslint/parser@6.16.0)(eslint-plugin-import@2.29.1)(eslint@8.56.0):
resolution: {integrity: sha512-dh6UbcrNDVg5DD8k8Qh4ab30OPpuEYIlJCqaBV/lkIV8wNN/AfCJ2V7iTP8V8KjryM4t+sf5IqzQLQnT0mWI4A==}
peerDependencies:
'@typescript-eslint/eslint-plugin': '>= 6'
'@typescript-eslint/parser': '>= 6'
eslint: '>= 3'
eslint-plugin-import: '>= 2'
dependencies:
'@typescript-eslint/eslint-plugin': 6.16.0(@typescript-eslint/parser@6.16.0)(eslint@8.56.0)(typescript@5.3.3)
'@typescript-eslint/parser': 6.16.0(eslint@8.56.0)(typescript@5.3.3)
eslint: 8.56.0
eslint-plugin-import: 2.29.1(@typescript-eslint/parser@6.16.0)(eslint@8.56.0)
dev: true
/@nodelib/fs.scandir@2.1.5:
resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==}
engines: {node: '>= 8'}

View File

@ -36,7 +36,7 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
const body = (() => {
try {
return JSON.parse(oEmbed);
} catch {}
} catch { /* empty */ }
})();
if (!body || body.version !== '1.0' || !['rich', 'video'].includes(body.type)) {
@ -131,6 +131,7 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
}
export default async (_url: URL | string, lang: string | null = null): Promise<Summary | null> => {
// eslint-disable-next-line no-param-reassign
if (lang && !lang.match(/^[\w-]+(\s*,\s*[\w-]+)*$/)) lang = null;
const url = typeof _url === 'string' ? new URL(_url) : _url;
@ -153,6 +154,7 @@ export default async (_url: URL | string, lang: string | null = null): Promise<S
$('meta[property="twitter:title"]').attr('content') ||
$('title').text();
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (title === undefined || title === null) {
return null;
}

View File

@ -5,14 +5,14 @@
import { URL } from 'node:url';
import tracer from 'trace-redirect';
import * as Got from 'got';
import { SummalyResult } from './summary.js';
import { SummalyPlugin } from './iplugin.js';
export * from './iplugin.js';
import general from './general.js';
import * as Got from 'got';
import { setAgent } from './utils/got.js';
import type { FastifyInstance } from 'fastify';
import { plugins as builtinPlugins } from './plugins/index.js';
import type { FastifyInstance } from 'fastify';
export type SummalyOptions = {
/**
@ -68,6 +68,7 @@ export const summaly = async (url: string, options?: SummalyOptions): Promise<Su
const match = plugins.filter(plugin => plugin.test(_url))[0];
// Get summary
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const summary = await (match ? match.summarize : general)(_url, opts.lang || undefined);
if (summary == null) {
@ -75,7 +76,7 @@ export const summaly = async (url: string, options?: SummalyOptions): Promise<Su
}
return Object.assign(summary, {
url: actualUrl
url: actualUrl,
});
};
@ -87,9 +88,10 @@ export default function (fastify: FastifyInstance, options: SummalyOptions, done
};
}>('/', async (req, reply) => {
const url = req.query.url as string;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (url == null) {
return reply.status(400).send({
error: 'url is required'
error: 'url is required',
});
}
@ -103,7 +105,7 @@ export default function (fastify: FastifyInstance, options: SummalyOptions, done
return summary;
} catch (e) {
return reply.status(500).send({
error: e
error: e,
});
}
});

View File

@ -1,5 +1,5 @@
import type { URL } from 'node:url';
import Summary from './summary.js';
import type { URL } from 'node:url';
export interface SummalyPlugin {
test: (url: URL) => boolean;

View File

@ -1,5 +1,4 @@
import { URL } from 'node:url';
import { scpaping } from '../utils/got.js';
import general from '../general.js';
import Summary from '../summary.js';

View File

@ -1,10 +1,10 @@
import { SummalyPlugin } from '@/iplugin.js';
import * as amazon from './amazon.js';
import * as wikipedia from './wikipedia.js';
import * as branchIoDeeplinks from './branchio-deeplinks.js';
import { SummalyPlugin } from '@/iplugin.js';
export const plugins: SummalyPlugin[] = [
amazon,
wikipedia,
branchIoDeeplinks,
amazon,
wikipedia,
branchIoDeeplinks,
];

View File

@ -1,6 +1,6 @@
import { URL } from 'node:url';
import { get } from '../utils/got.js';
import debug from 'debug';
import { get } from '../utils/got.js';
import summary from '../summary.js';
import clip from './../utils/clip.js';
@ -20,6 +20,7 @@ export async function summarize(url: URL): Promise<summary> {
log(`title is ${title}`);
log(`endpoint is ${endpoint}`);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let body = await get(endpoint) as any;
body = JSON.parse(body);
log(body);

View File

@ -1,3 +1,4 @@
/* eslint-disable no-param-reassign */
import escapeRegExp from 'escape-regexp';
export default function(title: string, siteName?: string | null): string {
@ -9,12 +10,12 @@ export default function(title: string, siteName?: string | null): string {
const x = escapeRegExp(siteName);
const patterns = [
`^(.+?)\\s?[\\-\\|:・]\\s?${x}$`
`^(.+?)\\s?[\\-\\|:・]\\s?${x}$`,
];
for (let i = 0; i < patterns.length; i++) {
const pattern = new RegExp(patterns[i]);
const [, match] = pattern.exec(title) || [null, null];
const [, match] = pattern.exec(title) ?? [null, null];
if (match) {
return match;
}

View File

@ -5,6 +5,7 @@ export default function(s: string, max: number): string {
return s;
}
// eslint-disable-next-line no-param-reassign
s = s.trim();
if (s.length > max) {

View File

@ -11,6 +11,7 @@ const regCharset = new RegExp(/charset\s*=\s*["']?([\w-]+)/, 'i');
export function detectEncoding(body: Buffer): string {
// By detection
const detected = jschardet.detect(body, { minimumThreshold: 0.99 });
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (detected) {
const candicate = detected.encoding;
const encoding = toEncoding(candicate);

View File

@ -1,17 +1,18 @@
import got, * as Got from 'got';
import { StatusError } from './status-error.js';
import { detectEncoding, toUtf8 } from './encoding.js';
import * as cheerio from 'cheerio';
import PrivateIp from 'private-ip';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import got, * as Got from 'got';
import * as cheerio from 'cheerio';
import PrivateIp from 'private-ip';
import { StatusError } from './status-error.js';
import { detectEncoding, toUtf8 } from './encoding.js';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
export let agent: Got.Agents = {};
export function setAgent(_agent: Got.Agents) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
agent = _agent || {};
}
@ -37,7 +38,7 @@ export async function scpaping(url: string, opts?: { lang?: string; }) {
headers: {
'accept': 'text/html,application/xhtml+xml',
'user-agent': BOT_UA,
'accept-language': opts?.lang
'accept-language': opts?.lang,
},
typeFilter: /^(text\/html|application\/xhtml\+xml)/,
});

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
export default function(val: string): boolean {
if (val === undefined) {
return true;

View File

@ -8,13 +8,13 @@
import fs, { readdirSync } from 'node:fs';
import process from 'node:process';
import fastify from 'fastify';
import { summaly } from '../src/index.js';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { expect, jest, test, describe, beforeEach, afterEach } from '@jest/globals';
import { Agent as httpAgent } from 'node:http';
import { Agent as httpsAgent } from 'node:https';
import { expect, test, describe, beforeEach, afterEach } from '@jest/globals';
import fastify from 'fastify';
import { summaly } from '../src/index.js';
import { StatusError } from '../src/utils/status-error.js';
const _filename = fileURLToPath(import.meta.url);
@ -35,7 +35,6 @@ const host = `http://localhost:${port}`;
process.on('unhandledRejection', console.dir);
let app: ReturnType<typeof fastify> | null = null;
let n = 0;
afterEach(async () => {
if (app) {
@ -61,10 +60,10 @@ test('basic', async () => {
url: null,
width: null,
height: null,
"allow": [
"autoplay",
"encrypted-media",
"fullscreen",
'allow': [
'autoplay',
'encrypted-media',
'fullscreen',
],
},
sitename: 'localhost:3060',
@ -80,28 +79,28 @@ test('Stage Bye Stage', async () => {
const summary = await summaly('https://www.youtube.com/watch?v=NMIEAhH_fTU');
expect(summary).toEqual(
{
"title": "【アイドルマスター】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)",
"icon": "https://www.youtube.com/s/desktop/28b0985e/img/favicon.ico",
"description": "Website▶https://columbia.jp/idolmaster/Playlist▶https://www.youtube.com/playlist?list=PL83A2998CF3BBC86D2018年7月18日発売予定THE IDOLM@STER CINDERELLA GIRLS CG STAR...",
"thumbnail": "https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg",
"player": {
"url": "https://www.youtube.com/embed/NMIEAhH_fTU?feature=oembed",
"width": 200,
"height": 113,
"allow": [
"autoplay",
"clipboard-write",
"encrypted-media",
"picture-in-picture",
"web-share",
"fullscreen",
]
'title': '【アイドルマスター】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)',
'icon': 'https://www.youtube.com/s/desktop/28b0985e/img/favicon.ico',
'description': 'Website▶https://columbia.jp/idolmaster/Playlist▶https://www.youtube.com/playlist?list=PL83A2998CF3BBC86D2018年7月18日発売予定THE IDOLM@STER CINDERELLA GIRLS CG STAR...',
'thumbnail': 'https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg',
'player': {
'url': 'https://www.youtube.com/embed/NMIEAhH_fTU?feature=oembed',
'width': 200,
'height': 113,
'allow': [
'autoplay',
'clipboard-write',
'encrypted-media',
'picture-in-picture',
'web-share',
'fullscreen',
],
},
"sitename": "YouTube",
"sensitive": false,
"activityPub": null,
"url": "https://www.youtube.com/watch?v=NMIEAhH_fTU"
}
'sitename': 'YouTube',
'sensitive': false,
'activityPub': null,
'url': 'https://www.youtube.com/watch?v=NMIEAhH_fTU',
},
);
});
@ -164,7 +163,7 @@ describe('Private IP blocking', () => {
agent: {
http: new httpAgent({ keepAlive: true }),
https: new httpsAgent({ keepAlive: true }),
}
},
});
expect(summary.title).toBe('Strawberry Pasta');
});
@ -299,7 +298,7 @@ describe('TwitterCard', () => {
});
});
describe("oEmbed", () => {
describe('oEmbed', () => {
const setUpFastify = async (oEmbedPath: string, htmlPath = 'htmls/oembed.html') => {
app = fastify();
app.get('/', (request, reply) => {
@ -307,11 +306,11 @@ describe("oEmbed", () => {
});
app.get('/oembed.json', (request, reply) => {
return reply.send(fs.createReadStream(
new URL(oEmbedPath, new URL('oembed/', import.meta.url))
new URL(oEmbedPath, new URL('oembed/', import.meta.url)),
));
});
await app.listen({ port });
}
};
for (const filename of readdirSync(new URL('oembed/invalid', import.meta.url))) {
test(`Invalidity test: ${filename}`, async () => {

6
test/tsconfig.json Normal file
View File

@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"include": [
"./**/*.ts"
],
}