Skip to content

Commit

Permalink
chore: upgrade eslint and typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfbecker committed Nov 21, 2020
1 parent 0966fed commit d9613c9
Show file tree
Hide file tree
Showing 11 changed files with 26,886 additions and 3,112 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,6 @@
{
"extends": ["@sourcegraph/eslint-config"],
"parserOptions": {
"project": "tsconfig.json"
}
}
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -16,7 +16,7 @@ jobs:
node-version: '14.15.1'
- run: npm ci
- run: npm run prettier
- run: npm run tslint
- run: npm run eslint
- run: npm run build
- run: npm test
env:
Expand Down
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
dist/test/
29,918 changes: 26,844 additions & 3,074 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Expand Up @@ -11,16 +11,16 @@
"LICENSE.txt"
],
"bin": {
"highlight": "./bin/highlight"
"highlight": "bin/highlight"
},
"engines": {
"node": ">=8.0.0",
"npm": ">=5.0.0"
},
"scripts": {
"test": "jest",
"lint": "npm run tslint && npm run prettier",
"tslint": "tslint -c tslint.json -p tsconfig.json",
"lint": "npm run eslint && npm run prettier",
"eslint": "eslint 'src/**/*.ts'",
"prettier": "prettier --write --list-different '**/{*.ts,*.json,.prettierrc}'",
"build": "tsc -p .",
"watch": "tsc -p . -w",
Expand Down Expand Up @@ -108,22 +108,22 @@
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@eclass/semantic-release-surge": "^1.0.7",
"@sourcegraph/eslint-config": "^0.20.16",
"@sourcegraph/prettierrc": "^3.0.3",
"@sourcegraph/tslint-config": "^11.0.1",
"@types/highlight.js": "^9.12.1",
"@types/jest": "^24.0.9",
"@types/mz": "0.0.32",
"@types/node": "^8.0.53",
"@types/node": "^14.14.9",
"@types/parse5": "^5.0.2",
"@types/parse5-htmlparser2-tree-adapter": "^5.0.1",
"@types/yargs": "^13.0.0",
"eslint": "^7.14.0",
"husky": "^3.0.0",
"jest": "^24.1.0",
"prettier": "^2.2.0",
"semantic-release": "^17.2.4",
"ts-jest": "^24.0.0",
"tslint": "^5.8.0",
"typedoc": "^0.19.0",
"typescript": "~3.6.4"
"typescript": "^4.1.2"
}
}
17 changes: 8 additions & 9 deletions src/cli.ts
@@ -1,8 +1,8 @@
import * as fs from 'mz/fs'
import * as path from 'path'
import * as tty from 'tty'
import yargs = require('yargs')
import { highlight, HighlightOptions, supportsLanguage } from './index'
import yargs from 'yargs'
import { highlight, HighlightOptions, supportsLanguage } from '.'
import { parse } from './theme'

yargs
Expand Down Expand Up @@ -61,7 +61,6 @@ if (!file && !(process.stdin as tty.ReadStream).isTTY) {
} else {
yargs.showHelp()
process.exit(1)
throw new Error()
}

Promise.all<string, string | undefined>([codePromise, argv.theme ? fs.readFile(argv.theme, 'utf8') : undefined])
Expand All @@ -71,20 +70,20 @@ Promise.all<string, string | undefined>([codePromise, argv.theme ? fs.readFile(a
theme: (theme && parse(theme)) || undefined,
}
if (file) {
const ext = path.extname(file).substr(1)
if (ext && supportsLanguage(ext)) {
options.language = ext
const extension = path.extname(file).slice(1)
if (extension && supportsLanguage(extension)) {
options.language = extension
}
}
options.language = argv.language
return new Promise<void>((resolve, reject) =>
process.stdout.write(highlight(code, options), (err: any) => (err ? reject(err) : resolve()))
process.stdout.write(highlight(code, options), (error: any) => (error ? reject(error) : resolve()))
)
})
.then(() => {
process.exit(0)
})
.catch((err: any) => {
console.error(err)
.catch((error: any) => {
console.error(error)
process.exit(1)
})
8 changes: 4 additions & 4 deletions src/index.ts
@@ -1,6 +1,7 @@
import * as hljs from 'highlight.js'
import * as parse5 from 'parse5'
import htmlparser2Adapter from 'parse5-htmlparser2-tree-adapter'
// eslint-disable-next-line no-duplicate-imports
import * as HtmlParser2 from 'parse5-htmlparser2-tree-adapter'
import { DEFAULT_THEME, plain, Theme } from './theme'

Expand All @@ -10,9 +11,8 @@ function colorizeNode(node: HtmlParser2.Node, theme: Theme = {}, context?: strin
const text = (node as HtmlParser2.TextNode).data
if (context === undefined) {
return (theme.default || DEFAULT_THEME.default || plain)(text)
} else {
return text
}
return text
}
case 'tag': {
const hljsClass = /hljs-(\w+)/.exec((node as HtmlParser2.Element).attribs.class)
Expand Down Expand Up @@ -49,8 +49,8 @@ export interface HighlightOptions {
language?: string

/**
* When present and evaluates to a true value, forces highlighting to finish even in case of
* detecting illegal syntax for the language instead of throwing an exception.
* When present and evaluates to a true value, forces highlighting to finish even in case of
* detecting illegal syntax for the language instead of throwing an exception.
*/
ignoreIllegals?: boolean

Expand Down
23 changes: 12 additions & 11 deletions src/test/test.ts
@@ -1,19 +1,20 @@
/* eslint-disable no-sync */
import * as fs from 'fs'
import { highlight, listLanguages, supportsLanguage } from '../index'
import { highlight, listLanguages, supportsLanguage } from '..'

describe('highlight()', () => {
function test(language: string, code: string): void {
it(`should color ${language} correctly`, () => {
const highlighted = highlight(code)
function test(language: string, code: string): void {
it(`should color ${language} correctly`, () => {
const highlighted = highlight(code)

if (process.env.OUTPUT_CODE_SAMPLES) {
console.log(language + ':\n\n' + highlighted)
}
if (process.env.OUTPUT_CODE_SAMPLES) {
console.log(language + ':\n\n' + highlighted)
}

expect(highlighted).toMatchSnapshot()
})
}
expect(highlighted).toMatchSnapshot()
})
}

describe('highlight()', () => {
const fixtures = fs.readdirSync(`${__dirname}/__fixtures__`)

for (const fixture of fixtures) {
Expand Down
4 changes: 2 additions & 2 deletions src/theme.ts
Expand Up @@ -291,7 +291,7 @@ export interface Theme extends Tokens<(codePart: string) => string> {
* Identity function for tokens that should not be styled (returns the input string as-is).
* See [[Theme]] for an example.
*/
export const plain = (codePart: string) => codePart
export const plain = (codePart: string): string => codePart

/**
* The default theme. It is possible to override just individual keys.
Expand Down Expand Up @@ -519,7 +519,7 @@ export function fromJson(json: JsonTheme): Theme {
const style: string | string[] = (json as any)[key]
if (Array.isArray(style)) {
;(theme as any)[key] = style.reduce(
(prev: typeof chalk, curr: string) => (curr === 'plain' ? plain : (prev as any)[curr]),
(previous: typeof chalk, current: string) => (current === 'plain' ? plain : (previous as any)[current]),
chalk
)
} else {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -22,5 +22,5 @@
"parse5": ["./node_modules/parse5/lib/index.d.ts"],
},
},
"exclude": ["node_modules", "dist", "src/test"],
"exclude": ["node_modules", "dist"],
}
3 changes: 0 additions & 3 deletions tslint.json

This file was deleted.

0 comments on commit d9613c9

Please sign in to comment.