Merge pull request #1738 from rinsuki/features/ts-noimplicitany-true

[WIP] noImplicitAny: true
This commit is contained in:
syuilo 2018-06-18 08:42:17 +09:00 committed by GitHub
commit a766faeae9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 260 additions and 101 deletions

View file

@ -8,12 +8,12 @@ import * as gutil from 'gulp-util';
import * as ts from 'gulp-typescript'; import * as ts from 'gulp-typescript';
const sourcemaps = require('gulp-sourcemaps'); const sourcemaps = require('gulp-sourcemaps');
import tslint from 'gulp-tslint'; import tslint from 'gulp-tslint';
import cssnano = require('gulp-cssnano'); const cssnano = require('gulp-cssnano');
import * as uglifyComposer from 'gulp-uglify/composer'; import * as uglifyComposer from 'gulp-uglify/composer';
import pug = require('gulp-pug'); import pug = require('gulp-pug');
import * as rimraf from 'rimraf'; import * as rimraf from 'rimraf';
import chalk from 'chalk'; import chalk from 'chalk';
import imagemin = require('gulp-imagemin'); const imagemin = require('gulp-imagemin');
import * as rename from 'gulp-rename'; import * as rename from 'gulp-rename';
import * as mocha from 'gulp-mocha'; import * as mocha from 'gulp-mocha';
import * as replace from 'gulp-replace'; import * as replace from 'gulp-replace';

View file

@ -5,12 +5,16 @@
import * as fs from 'fs'; import * as fs from 'fs';
import * as yaml from 'js-yaml'; import * as yaml from 'js-yaml';
const loadLang = lang => yaml.safeLoad( export type LangKey = 'de' | 'en' | 'fr' | 'ja' | 'pl';
fs.readFileSync(`./locales/${lang}.yml`, 'utf-8')); export type LocaleObjectChildren = LocaleObject | string | undefined;
export type LocaleObject = {[key: string]: LocaleObjectChildren };
const loadLang = (lang: LangKey) => yaml.safeLoad(
fs.readFileSync(`./locales/${lang}.yml`, 'utf-8')) as LocaleObject;
const native = loadLang('ja'); const native = loadLang('ja');
const langs = { const langs: {[key in LangKey]: LocaleObject} = {
'de': loadLang('de'), 'de': loadLang('de'),
'en': loadLang('en'), 'en': loadLang('en'),
'fr': loadLang('fr'), 'fr': loadLang('fr'),
@ -23,4 +27,8 @@ Object.entries(langs).map(([, locale]) => {
locale = Object.assign({}, native, locale); locale = Object.assign({}, native, locale);
}); });
export function isAvailableLanguage(lang: string): lang is LangKey {
return lang in langs;
}
export default langs; export default langs;

View file

@ -23,10 +23,10 @@
"format": "gulp format" "format": "gulp format"
}, },
"dependencies": { "dependencies": {
"@fortawesome/fontawesome": "1.0.1", "@fortawesome/fontawesome": "1.1.8",
"@fortawesome/fontawesome-free-brands": "5.0.2", "@fortawesome/fontawesome-free-brands": "5.0.13",
"@fortawesome/fontawesome-free-regular": "5.0.2", "@fortawesome/fontawesome-free-regular": "5.0.13",
"@fortawesome/fontawesome-free-solid": "5.0.2", "@fortawesome/fontawesome-free-solid": "5.0.13",
"@koa/cors": "2.2.1", "@koa/cors": "2.2.1",
"@prezzemolo/rap": "0.1.2", "@prezzemolo/rap": "0.1.2",
"@prezzemolo/zip": "0.0.3", "@prezzemolo/zip": "0.0.3",
@ -216,5 +216,9 @@
"websocket": "1.0.26", "websocket": "1.0.26",
"ws": "5.2.0", "ws": "5.2.0",
"xev": "2.0.1" "xev": "2.0.1"
},
"devDependencies": {
"@types/file-type": "5.2.1",
"@types/jsdom": "11.0.5"
} }
} }

View file

@ -3,18 +3,18 @@
*/ */
import * as fontawesome from '@fortawesome/fontawesome'; import * as fontawesome from '@fortawesome/fontawesome';
import * as regular from '@fortawesome/fontawesome-free-regular'; import regular from '@fortawesome/fontawesome-free-regular';
import * as solid from '@fortawesome/fontawesome-free-solid'; import solid from '@fortawesome/fontawesome-free-solid';
import * as brands from '@fortawesome/fontawesome-free-brands'; import brands from '@fortawesome/fontawesome-free-brands';
fontawesome.library.add(regular, solid, brands); fontawesome.library.add(regular, solid, brands);
export const pattern = /%fa:(.+?)%/g; export const pattern = /%fa:(.+?)%/g;
export const replacement = (match, key) => { export const replacement = (match: string, key: string) => {
const args = key.split(' '); const args = key.split(' ');
let prefix = 'fas'; let prefix = 'fas';
const classes = []; const classes: string[] = [];
let transform = ''; let transform = '';
let name; let name;
@ -34,12 +34,12 @@ export const replacement = (match, key) => {
} }
}); });
const icon = fontawesome.icon({ prefix, iconName: name }, { const icon = fontawesome.icon({ prefix, iconName: name } as fontawesome.IconLookup, {
classes: classes classes: classes,
transform: fontawesome.parse.transform(transform)
}); });
if (icon) { if (icon) {
icon.transform = fontawesome.parse.transform(transform);
return `<i data-fa class="${name}">${icon.html[0]}</i>`; return `<i data-fa class="${name}">${icon.html[0]}</i>`;
} else { } else {
console.warn(`'${name}' not found in fa`); console.warn(`'${name}' not found in fa`);

View file

@ -2,7 +2,7 @@
* Replace i18n texts * Replace i18n texts
*/ */
import locale from '../../locales'; import locale, { isAvailableLanguage, LocaleObject, LocaleObjectChildren } from '../../locales';
export default class Replacer { export default class Replacer {
private lang: string; private lang: string;
@ -16,19 +16,19 @@ export default class Replacer {
this.replacement = this.replacement.bind(this); this.replacement = this.replacement.bind(this);
} }
private get(path: string, key: string) { private get(path: string, key: string): string {
const texts = locale[this.lang]; if (!isAvailableLanguage(this.lang)) {
if (texts == null) {
console.warn(`lang '${this.lang}' is not supported`); console.warn(`lang '${this.lang}' is not supported`);
return key; // Fallback return key; // Fallback
} }
let text = texts; const texts = locale[this.lang];
let text: LocaleObjectChildren = texts;
if (path) { if (path) {
if (text.hasOwnProperty(path)) { if (text.hasOwnProperty(path)) {
text = text[path]; text = text[path] as LocaleObject;
} else { } else {
console.warn(`path '${path}' not found in '${this.lang}'`); console.warn(`path '${path}' not found in '${this.lang}'`);
return key; // Fallback return key; // Fallback
@ -38,7 +38,7 @@ export default class Replacer {
// Check the key existance // Check the key existance
const error = key.split('.').some(k => { const error = key.split('.').some(k => {
if (text.hasOwnProperty(k)) { if (text.hasOwnProperty(k)) {
text = text[k]; text = (text as LocaleObject)[k];
return false; return false;
} else { } else {
return true; return true;
@ -48,12 +48,15 @@ export default class Replacer {
if (error) { if (error) {
console.warn(`key '${key}' not found in '${path}' of '${this.lang}'`); console.warn(`key '${key}' not found in '${path}' of '${this.lang}'`);
return key; // Fallback return key; // Fallback
} else if (typeof text !== "string") {
console.warn(`key '${key}' is not string in '${path}' of '${this.lang}'`);
return key; // Fallback
} else { } else {
return text; return text;
} }
} }
public replacement(match, key) { public replacement(match: string, key: string) {
let path = null; let path = null;
if (key.indexOf('|') != -1) { if (key.indexOf('|') != -1) {

View file

@ -19,9 +19,10 @@ import generateVars from '../vars';
const langs = Object.keys(locales); const langs = Object.keys(locales);
const kebab = string => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(); const kebab = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
const parseParam = param => { // WIP type
const parseParam = (param: any) => {
const id = param.type.match(/^id\((.+?)\)|^id/); const id = param.type.match(/^id\((.+?)\)|^id/);
const entity = param.type.match(/^entity\((.+?)\)/); const entity = param.type.match(/^entity\((.+?)\)/);
const isObject = /^object/.test(param.type); const isObject = /^object/.test(param.type);
@ -57,7 +58,7 @@ const parseParam = param => {
return param; return param;
}; };
const sortParams = params => { const sortParams = (params: Array<{name: string}>) => {
params.sort((a, b) => { params.sort((a, b) => {
if (a.name < b.name) if (a.name < b.name)
return -1; return -1;
@ -68,14 +69,15 @@ const sortParams = params => {
return params; return params;
}; };
const extractDefs = params => { // WIP type
let defs = []; const extractDefs = (params: any[]) => {
let defs: any[] = [];
params.forEach(param => { params.forEach(param => {
if (param.def) { if (param.def) {
defs.push({ defs.push({
name: param.defName, name: param.defName,
params: sortParams(param.def.map(p => parseParam(p))) params: sortParams(param.def.map((p: any) => parseParam(p)))
}); });
const childDefs = extractDefs(param.def); const childDefs = extractDefs(param.def);
@ -109,8 +111,10 @@ gulp.task('doc:api:endpoints', async () => {
path: ep.endpoint path: ep.endpoint
}, },
desc: ep.desc, desc: ep.desc,
// @ts-ignore
params: sortParams(ep.params.map(p => parseParam(p))), params: sortParams(ep.params.map(p => parseParam(p))),
paramDefs: extractDefs(ep.params), paramDefs: extractDefs(ep.params),
// @ts-ignore
res: ep.res ? sortParams(ep.res.map(p => parseParam(p))) : null, res: ep.res ? sortParams(ep.res.map(p => parseParam(p))) : null,
resDefs: ep.res ? extractDefs(ep.res) : null, resDefs: ep.res ? extractDefs(ep.res) : null,
}; };
@ -155,7 +159,8 @@ gulp.task('doc:api:entities', async () => {
const vars = { const vars = {
name: entity.name, name: entity.name,
desc: entity.desc, desc: entity.desc,
props: sortParams(entity.props.map(p => parseParam(p))), // WIP type
props: sortParams(entity.props.map((p: any) => parseParam(p))),
propDefs: extractDefs(entity.props), propDefs: extractDefs(entity.props),
}; };
langs.forEach(lang => { langs.forEach(lang => {

View file

@ -8,8 +8,8 @@ import * as glob from 'glob';
import * as gulp from 'gulp'; import * as gulp from 'gulp';
import * as pug from 'pug'; import * as pug from 'pug';
import * as mkdirp from 'mkdirp'; import * as mkdirp from 'mkdirp';
import stylus = require('gulp-stylus'); const stylus = require('gulp-stylus');
import cssnano = require('gulp-cssnano'); const cssnano = require('gulp-cssnano');
import I18nReplacer from '../../build/i18n'; import I18nReplacer from '../../build/i18n';
import fa from '../../build/fa'; import fa from '../../build/fa';

View file

@ -38,7 +38,7 @@ export default async function(): Promise<{ [key: string]: any }> {
vars['docs'][name]['title'][lang] = fs.readFileSync(x, 'utf-8').match(/^h1 (.+?)\r?\n/)[1]; vars['docs'][name]['title'][lang] = fs.readFileSync(x, 'utf-8').match(/^h1 (.+?)\r?\n/)[1];
}); });
vars['kebab'] = string => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase(); vars['kebab'] = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
vars['config'] = config; vars['config'] = config;

View file

@ -7,7 +7,7 @@ import * as crypto from 'crypto';
import * as _gm from 'gm'; import * as _gm from 'gm';
import * as debug from 'debug'; import * as debug from 'debug';
import fileType = require('file-type'); import fileType = require('file-type');
import prominence = require('prominence'); const prominence = require('prominence');
import DriveFile, { IMetadata, getDriveFileBucket, IDriveFile } from '../../models/drive-file'; import DriveFile, { IMetadata, getDriveFileBucket, IDriveFile } from '../../models/drive-file';
import DriveFolder from '../../models/drive-folder'; import DriveFolder from '../../models/drive-folder';
@ -33,7 +33,7 @@ const writeChunks = (name: string, readable: stream.Readable, type: string, meta
readable.pipe(writeStream); readable.pipe(writeStream);
})); }));
const writeThumbnailChunks = (name: string, readable: stream.Readable, originalId) => const writeThumbnailChunks = (name: string, readable: stream.Readable, originalId: mongodb.ObjectID) =>
getDriveFileThumbnailBucket() getDriveFileThumbnailBucket()
.then(bucket => new Promise((resolve, reject) => { .then(bucket => new Promise((resolve, reject) => {
const writeStream = bucket.openUploadStream(name, { const writeStream = bucket.openUploadStream(name, {
@ -89,7 +89,7 @@ export default async function(
const calcHash = new Promise<string>((res, rej) => { const calcHash = new Promise<string>((res, rej) => {
const readable = fs.createReadStream(path); const readable = fs.createReadStream(path);
const hash = crypto.createHash('md5'); const hash = crypto.createHash('md5');
const chunks = []; const chunks: Buffer[] = [];
readable readable
.on('error', rej) .on('error', rej)
.pipe(hash) .pipe(hash)
@ -201,7 +201,7 @@ export default async function(
return driveFolder; return driveFolder;
}; };
const properties = {}; const properties: {[key: string]: any} = {};
let propPromises: Array<Promise<void>> = []; let propPromises: Array<Promise<void>> = [];

View file

@ -8,10 +8,12 @@ import * as request from 'request';
import { IDriveFile, validateFileName } from '../../models/drive-file'; import { IDriveFile, validateFileName } from '../../models/drive-file';
import create from './add-file'; import create from './add-file';
import config from '../../config'; import config from '../../config';
import { IUser } from '../../models/user';
import * as mongodb from "mongodb";
const log = debug('misskey:drive:upload-from-url'); const log = debug('misskey:drive:upload-from-url');
export default async (url: string, user, folderId = null, uri: string = null): Promise<IDriveFile> => { export default async (url: string, user: IUser, folderId: mongodb.ObjectID = null, uri: string = null): Promise<IDriveFile> => {
log(`REQUESTED: ${url}`); log(`REQUESTED: ${url}`);
let name = URL.parse(url).pathname.split('/').pop(); let name = URL.parse(url).pathname.split('/').pop();

View file

@ -33,7 +33,7 @@ class NotificationManager {
reason: Reason; reason: Reason;
}> = []; }> = [];
constructor(user, note) { constructor(user: IUser, note: any) {
this.user = user; this.user = user;
this.note = note; this.note = note;
} }
@ -451,7 +451,7 @@ export default async (user: IUser, data: {
// $ne: note._id // $ne: note._id
// } // }
//}); //});
const existRenote = null; const existRenote: INote | null = null;
//#endregion //#endregion
if (!existRenote) { if (!existRenote) {

View file

@ -36,7 +36,7 @@ export default async (user: IUser, note: INote, reaction: string) => new Promise
res(); res();
const inc = {}; const inc: {[key: string]: number} = {};
inc[`reactionCounts.${reaction}`] = 1; inc[`reactionCounts.${reaction}`] = 1;
// Increment reactions count // Increment reactions count

View file

@ -1,7 +1,8 @@
import { lib as emojilib } from 'emojilib'; const { lib: emojilib } = require('emojilib');
import { JSDOM } from 'jsdom'; import { JSDOM } from 'jsdom';
import config from '../config'; import config from '../config';
import { INote } from '../models/note'; import { INote } from '../models/note';
import { TextElement } from './parse';
const handlers: {[key: string]: (window: any, token: any, mentionedRemoteUsers: INote["mentionedRemoteUsers"]) => void} = { const handlers: {[key: string]: (window: any, token: any, mentionedRemoteUsers: INote["mentionedRemoteUsers"]) => void} = {
bold({ document }, { bold }) { bold({ document }, { bold }) {
@ -90,7 +91,7 @@ const handlers: {[key: string]: (window: any, token: any, mentionedRemoteUsers:
} }
}; };
export default (tokens, mentionedRemoteUsers: INote['mentionedRemoteUsers'] = []) => { export default (tokens: TextElement[], mentionedRemoteUsers: INote['mentionedRemoteUsers'] = []) => {
const { window } = new JSDOM(''); const { window } = new JSDOM('');
for (const token of tokens) { for (const token of tokens) {

View file

@ -1,4 +1,4 @@
function escape(text) { function escape(text: string) {
return text return text
.replace(/>/g, '&gt;') .replace(/>/g, '&gt;')
.replace(/</g, '&lt;'); .replace(/</g, '&lt;');
@ -110,7 +110,14 @@ const symbols = [
'?' '?'
]; ];
const elements = [ type Token = {
html: string
next: number
};
type Element = (code: string, i: number, source: string) => (Token | null);
const elements: Element[] = [
// comment // comment
code => { code => {
if (code.substr(0, 2) != '//') return null; if (code.substr(0, 2) != '//') return null;
@ -305,7 +312,7 @@ export default (source: string, lang?: string) => {
let i = 0; let i = 0;
function push(token) { function push(token: Token) {
html += token.html; html += token.html;
code = code.substr(token.next); code = code.substr(token.next);
i += token.next; i += token.next;

View file

@ -2,7 +2,13 @@
* Bold * Bold
*/ */
module.exports = text => { export type TextElementBold = {
type: "bold"
content: string
bold: string
};
export default function(text: string) {
const match = text.match(/^\*\*(.+?)\*\*/); const match = text.match(/^\*\*(.+?)\*\*/);
if (!match) return null; if (!match) return null;
const bold = match[0]; const bold = match[0];
@ -10,5 +16,5 @@ module.exports = text => {
type: 'bold', type: 'bold',
content: bold, content: bold,
bold: bold.substr(2, bold.length - 4) bold: bold.substr(2, bold.length - 4)
}; } as TextElementBold;
}; }

View file

@ -4,7 +4,14 @@
import genHtml from '../core/syntax-highlighter'; import genHtml from '../core/syntax-highlighter';
module.exports = text => { export type TextElementCode = {
type: "code"
content: string
code: string
html: string
};
export default function(text: string) {
const match = text.match(/^```([\s\S]+?)```/); const match = text.match(/^```([\s\S]+?)```/);
if (!match) return null; if (!match) return null;
const code = match[0]; const code = match[0];
@ -13,5 +20,5 @@ module.exports = text => {
content: code, content: code,
code: code.substr(3, code.length - 6).trim(), code: code.substr(3, code.length - 6).trim(),
html: genHtml(code.substr(3, code.length - 6).trim()) html: genHtml(code.substr(3, code.length - 6).trim())
}; } as TextElementCode;
}; }

View file

@ -2,7 +2,13 @@
* Emoji * Emoji
*/ */
module.exports = text => { export type TextElementEmoji = {
type: "emoji"
content: string
emoji: string
};
export default function(text: string) {
const match = text.match(/^:[a-zA-Z0-9+-_]+:/); const match = text.match(/^:[a-zA-Z0-9+-_]+:/);
if (!match) return null; if (!match) return null;
const emoji = match[0]; const emoji = match[0];
@ -10,5 +16,5 @@ module.exports = text => {
type: 'emoji', type: 'emoji',
content: emoji, content: emoji,
emoji: emoji.substr(1, emoji.length - 2) emoji: emoji.substr(1, emoji.length - 2)
}; } as TextElementEmoji;
}; }

View file

@ -2,7 +2,13 @@
* Hashtag * Hashtag
*/ */
module.exports = (text, i) => { export type TextElementHashtag = {
type: "hashtag"
content: string
hashtag: string
};
export default function(text: string, i: number) {
if (!(/^\s#[^\s]+/.test(text) || (i == 0 && /^#[^\s]+/.test(text)))) return null; if (!(/^\s#[^\s]+/.test(text) || (i == 0 && /^#[^\s]+/.test(text)))) return null;
const isHead = text[0] == '#'; const isHead = text[0] == '#';
const hashtag = text.match(/^\s?#[^\s]+/)[0]; const hashtag = text.match(/^\s?#[^\s]+/)[0];
@ -15,5 +21,5 @@ module.exports = (text, i) => {
content: isHead ? hashtag : hashtag.substr(1), content: isHead ? hashtag : hashtag.substr(1),
hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2) hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2)
}); });
return res; return res as TextElementHashtag[];
}; }

View file

@ -4,7 +4,14 @@
import genHtml from '../core/syntax-highlighter'; import genHtml from '../core/syntax-highlighter';
module.exports = text => { export type TextElementInlineCode = {
type: "inline-code"
content: string
code: string
html: string
};
export default function(text: string) {
const match = text.match(/^`(.+?)`/); const match = text.match(/^`(.+?)`/);
if (!match) return null; if (!match) return null;
const code = match[0]; const code = match[0];
@ -13,5 +20,5 @@ module.exports = text => {
content: code, content: code,
code: code.substr(1, code.length - 2).trim(), code: code.substr(1, code.length - 2).trim(),
html: genHtml(code.substr(1, code.length - 2).trim()) html: genHtml(code.substr(1, code.length - 2).trim())
}; } as TextElementInlineCode;
}; }

View file

@ -2,7 +2,15 @@
* Link * Link
*/ */
module.exports = text => { export type TextElementLink = {
type: "link"
content: string
title: string
url: string
silent: boolean
};
export default function(text: string) {
const match = text.match(/^\??\[([^\[\]]+?)\]\((https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+?)\)/); const match = text.match(/^\??\[([^\[\]]+?)\]\((https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+?)\)/);
if (!match) return null; if (!match) return null;
const silent = text[0] == '?'; const silent = text[0] == '?';
@ -15,5 +23,5 @@ module.exports = text => {
title: title, title: title,
url: url, url: url,
silent: silent silent: silent
}; } as TextElementLink;
}; }

View file

@ -3,7 +3,14 @@
*/ */
import parseAcct from '../../../acct/parse'; import parseAcct from '../../../acct/parse';
module.exports = text => { export type TextElementMention = {
type: "mention"
content: string
username: string
host: string
};
export default function(text: string) {
const match = text.match(/^@[a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9])?/i); const match = text.match(/^@[a-z0-9_]+(?:@[a-z0-9\.\-]+[a-z0-9])?/i);
if (!match) return null; if (!match) return null;
const mention = match[0]; const mention = match[0];
@ -13,5 +20,5 @@ module.exports = text => {
content: mention, content: mention,
username, username,
host host
}; } as TextElementMention;
}; }

View file

@ -2,7 +2,13 @@
* Quoted text * Quoted text
*/ */
module.exports = text => { export type TextElementQuote = {
type: "quote"
content: string
quote: string
};
export default function(text: string) {
const match = text.match(/^"([\s\S]+?)\n"/); const match = text.match(/^"([\s\S]+?)\n"/);
if (!match) return null; if (!match) return null;
const quote = match[0]; const quote = match[0];
@ -10,5 +16,5 @@ module.exports = text => {
type: 'quote', type: 'quote',
content: quote, content: quote,
quote: quote.substr(1, quote.length - 2).trim(), quote: quote.substr(1, quote.length - 2).trim(),
}; } as TextElementQuote;
}; }

View file

@ -2,7 +2,13 @@
* Search * Search
*/ */
module.exports = text => { export type TextElementSearch = {
type: "search"
content: string
query: string
};
export default function(text: string) {
const match = text.match(/^(.+?) 検索(\n|$)/); const match = text.match(/^(.+?) 検索(\n|$)/);
if (!match) return null; if (!match) return null;
return { return {
@ -10,4 +16,4 @@ module.exports = text => {
content: match[0], content: match[0],
query: match[1] query: match[1]
}; };
}; }

View file

@ -2,7 +2,13 @@
* Title * Title
*/ */
module.exports = text => { export type TextElementTitle = {
type: "title"
content: string
title: string
};
export default function(text: string) {
const match = text.match(/^【(.+?)】\n/); const match = text.match(/^【(.+?)】\n/);
if (!match) return null; if (!match) return null;
const title = match[0]; const title = match[0];
@ -10,5 +16,5 @@ module.exports = text => {
type: 'title', type: 'title',
content: title, content: title,
title: title.substr(1, title.length - 3) title: title.substr(1, title.length - 3)
}; } as TextElementTitle;
}; }

View file

@ -2,7 +2,13 @@
* URL * URL
*/ */
module.exports = text => { export type TextElementUrl = {
type: "url"
content: string
url: string
};
export default function(text: string) {
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/); const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/);
if (!match) return null; if (!match) return null;
const url = match[0]; const url = match[0];
@ -10,5 +16,5 @@ module.exports = text => {
type: 'url', type: 'url',
content: url, content: url,
url: url url: url
}; } as TextElementUrl;
}; }

View file

@ -2,6 +2,18 @@
* Misskey Text Analyzer * Misskey Text Analyzer
*/ */
import { TextElementBold } from "./elements/bold";
import { TextElementCode } from "./elements/code";
import { TextElementEmoji } from "./elements/emoji";
import { TextElementHashtag } from "./elements/hashtag";
import { TextElementInlineCode } from "./elements/inline-code";
import { TextElementLink } from "./elements/link";
import { TextElementMention } from "./elements/mention";
import { TextElementQuote } from "./elements/quote";
import { TextElementSearch } from "./elements/search";
import { TextElementTitle } from "./elements/title";
import { TextElementUrl } from "./elements/url";
const elements = [ const elements = [
require('./elements/bold'), require('./elements/bold'),
require('./elements/title'), require('./elements/title'),
@ -14,17 +26,31 @@ const elements = [
require('./elements/quote'), require('./elements/quote'),
require('./elements/emoji'), require('./elements/emoji'),
require('./elements/search') require('./elements/search')
]; ].map(element => element.default as TextElementProcessor);
export default (source: string): any[] => { export type TextElement = {type: "text", content: string}
| TextElementBold
| TextElementCode
| TextElementEmoji
| TextElementHashtag
| TextElementInlineCode
| TextElementLink
| TextElementMention
| TextElementQuote
| TextElementSearch
| TextElementTitle
| TextElementUrl;
export type TextElementProcessor = (text: string, i: number) => TextElement | TextElement[];
export default (source: string): TextElement[] => {
if (source == '') { if (source == '') {
return null; return null;
} }
const tokens = []; const tokens: TextElement[] = [];
function push(token) { function push(token: TextElement) {
if (token != null) { if (token != null) {
tokens.push(token); tokens.push(token);
source = source.substr(token.content.length); source = source.substr(token.content.length);
@ -59,9 +85,8 @@ export default (source: string): any[] => {
} }
// テキストを纏める // テキストを纏める
tokens[0] = [tokens[0]];
return tokens.reduce((a, b) => { return tokens.reduce((a, b) => {
if (a[a.length - 1].type == 'text' && b.type == 'text') { if (a.length && a[a.length - 1].type == 'text' && b.type == 'text') {
const tail = a.pop(); const tail = a.pop();
return a.concat({ return a.concat({
type: 'text', type: 'text',
@ -70,5 +95,5 @@ export default (source: string): any[] => {
} else { } else {
return a.concat(b); return a.concat(b);
} }
}); }, [] as TextElement[]);
}; };

View file

@ -2,7 +2,7 @@
"compilerOptions": { "compilerOptions": {
"allowJs": true, "allowJs": true,
"noEmitOnError": false, "noEmitOnError": false,
"noImplicitAny": false, "noImplicitAny": true,
"noImplicitReturns": true, "noImplicitReturns": true,
"noUnusedParameters": false, "noUnusedParameters": false,
"noUnusedLocals": true, "noUnusedLocals": true,

View file

@ -2,21 +2,33 @@
# yarn lockfile v1 # yarn lockfile v1
"@fortawesome/fontawesome-free-brands@5.0.2": "@fortawesome/fontawesome-common-types@^0.1.7":
version "5.0.2" version "0.1.7"
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-brands/-/fontawesome-free-brands-5.0.2.tgz#a1cc602eec40a379a3dd8a44c78b31110dd3d3d3" resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-common-types/-/fontawesome-common-types-0.1.7.tgz#4336c4b06d0b5608ff1215464b66fcf9f4795284"
"@fortawesome/fontawesome-free-regular@5.0.2": "@fortawesome/fontawesome-free-brands@5.0.13":
version "5.0.2" version "5.0.13"
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-regular/-/fontawesome-free-regular-5.0.2.tgz#429af86bed14689f87648e6322983c65c782c017" resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-brands/-/fontawesome-free-brands-5.0.13.tgz#4d15ff4e1e862d5e4a4df3654f8e8acbd47e9c09"
dependencies:
"@fortawesome/fontawesome-common-types" "^0.1.7"
"@fortawesome/fontawesome-free-solid@5.0.2": "@fortawesome/fontawesome-free-regular@5.0.13":
version "5.0.2" version "5.0.13"
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-solid/-/fontawesome-free-solid-5.0.2.tgz#090ce2c59dd5ec76983f3da8a43e1ab0321b42d5" resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-regular/-/fontawesome-free-regular-5.0.13.tgz#eb78c30184e3f456a423a1dcfa0f682f7b50de4a"
dependencies:
"@fortawesome/fontawesome-common-types" "^0.1.7"
"@fortawesome/fontawesome@1.0.1": "@fortawesome/fontawesome-free-solid@5.0.13":
version "1.0.1" version "5.0.13"
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome/-/fontawesome-1.0.1.tgz#8ac60e1e7b437889baf9c9d6e3a61ef3b637170d" resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome-free-solid/-/fontawesome-free-solid-5.0.13.tgz#24b61aaf471a9d34a5364b052d64a516285ba894"
dependencies:
"@fortawesome/fontawesome-common-types" "^0.1.7"
"@fortawesome/fontawesome@1.1.8":
version "1.1.8"
resolved "https://registry.yarnpkg.com/@fortawesome/fontawesome/-/fontawesome-1.1.8.tgz#75fe66a60f95508160bb16bd781ad7d89b280f5b"
dependencies:
"@fortawesome/fontawesome-common-types" "^0.1.7"
"@gulp-sourcemaps/identity-map@1.X": "@gulp-sourcemaps/identity-map@1.X":
version "1.0.1" version "1.0.1"
@ -164,6 +176,12 @@
version "1.3.0" version "1.3.0"
resolved "https://registry.yarnpkg.com/@types/fancy-log/-/fancy-log-1.3.0.tgz#a61ab476e5e628cd07a846330df53b85e05c8ce0" resolved "https://registry.yarnpkg.com/@types/fancy-log/-/fancy-log-1.3.0.tgz#a61ab476e5e628cd07a846330df53b85e05c8ce0"
"@types/file-type@5.2.1":
version "5.2.1"
resolved "https://registry.yarnpkg.com/@types/file-type/-/file-type-5.2.1.tgz#e7af49e08187b6b7598509c5e416669d25fa3461"
dependencies:
"@types/node" "*"
"@types/form-data@*": "@types/form-data@*":
version "2.2.1" version "2.2.1"
resolved "https://registry.yarnpkg.com/@types/form-data/-/form-data-2.2.1.tgz#ee2b3b8eaa11c0938289953606b745b738c54b1e" resolved "https://registry.yarnpkg.com/@types/form-data/-/form-data-2.2.1.tgz#ee2b3b8eaa11c0938289953606b745b738c54b1e"
@ -265,6 +283,15 @@
version "3.11.1" version "3.11.1"
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.11.1.tgz#ac5bab26be5f9c6f74b6b23420f2cfa5a7a6ba40" resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.11.1.tgz#ac5bab26be5f9c6f74b6b23420f2cfa5a7a6ba40"
"@types/jsdom@11.0.5":
version "11.0.5"
resolved "https://registry.yarnpkg.com/@types/jsdom/-/jsdom-11.0.5.tgz#b12fffc73eb3731b218e9665a50f023b6b84b5cb"
dependencies:
"@types/events" "*"
"@types/node" "*"
"@types/tough-cookie" "*"
parse5 "^3.0.2"
"@types/keygrip@*": "@types/keygrip@*":
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/keygrip/-/keygrip-1.0.1.tgz#ff540462d2fb4d0a88441ceaf27d287b01c3d878" resolved "https://registry.yarnpkg.com/@types/keygrip/-/keygrip-1.0.1.tgz#ff540462d2fb4d0a88441ceaf27d287b01c3d878"
@ -8209,6 +8236,12 @@ parse5@4.0.0:
version "4.0.0" version "4.0.0"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608" resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608"
parse5@^3.0.2:
version "3.0.3"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-3.0.3.tgz#042f792ffdd36851551cf4e9e066b3874ab45b5c"
dependencies:
"@types/node" "*"
parseurl@^1.3.0, parseurl@~1.3.2: parseurl@^1.3.0, parseurl@~1.3.2:
version "1.3.2" version "1.3.2"
resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.2.tgz#fc289d4ed8993119460c156253262cdc8de65bf3" resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.2.tgz#fc289d4ed8993119460c156253262cdc8de65bf3"