Skip to content

Commit

Permalink
refactor: update typescript to 3.7.1-rc and modify eslint config
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo930021 committed Nov 5, 2019
1 parent 6839f03 commit ff3a9dd
Show file tree
Hide file tree
Showing 25 changed files with 178 additions and 126 deletions.
21 changes: 8 additions & 13 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,15 @@ module.exports = {
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
rules: {
'@typescript-eslint/consistent-type-definitions': ['error', 'interface'],
'no-console': 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
'no-undef': 'off',
'no-unused-vars': 'off',
strict: 'off',
camelcase: 'off',
'@typescript-eslint/quotes': ['error', 'single'],
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/unbound-method': 'off',
'@typescript-eslint/member-delimiter-style': 'off',

'comma-dangle': ['error', 'never'],
curly: ['error', 'all'],
'no-mixed-operators': 'error',
'no-console': 'error',
'no-process-exit': 'error'
'@typescript-eslint/member-delimiter-style': ['error', { multiline: { delimiter: 'comma', requireLast: false }, singleline: { delimiter: 'comma', requireLast: false }, overrides: { interface: { multiline: { delimiter: 'none' } } } }]
},
parserOptions: {
sourceType: 'module',
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"LICENSE"
],
"devDependencies": {
"@semantic-release/git": "^7.0.17",
"@semantic-release/git": "^7.0.18",
"@types/inquirer": "^6.5.0",
"@types/jest": "^24.0.19",
"@types/node": "^12.11.1",
"@types/jest": "^24.0.21",
"@types/node": "^12.12.5",
"@types/prettier": "^1.18.3",
"@typescript-eslint/eslint-plugin": "^2.4.0",
"@typescript-eslint/parser": "^2.4.0",
"@typescript-eslint/eslint-plugin": "^2.6.1",
"@typescript-eslint/parser": "^2.6.1",
"codecov": "^3.6.1",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.18.2",
Expand All @@ -35,9 +35,9 @@
"monaco-editor": "^0.18.1",
"parcel-bundler": "^1.12.4",
"prettier": "^1.18.2",
"semantic-release": "^15.13.28",
"semantic-release": "^15.13.30",
"ts-jest": "^24.1.0",
"typescript": "^3.6.4",
"typescript": "3.7.1-rc",
"vue": "^2.6.10",
"vue-monaco": "^1.1.0"
},
Expand All @@ -50,7 +50,7 @@
},
"dependencies": {
"commander": "^3.0.2",
"eslint": "^6.5.1",
"eslint": "^6.6.0",
"inquirer": "^7.0.0",
"prettier-eslint": "^9.0.0",
"ts-node": "^8.4.1",
Expand Down
34 changes: 17 additions & 17 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { convertFile } from './index.js'
import inquirer from 'inquirer'
import { writeFileInfo } from './file'

function camelize (str: string) {
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '')
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getCmdOptions (cmd: any) {
const args: { [key: string]: boolean | string } = {}
cmd.options.forEach((o: { long: string }) => {
const key = camelize(o.long.replace(/^--/, ''))

if (typeof cmd[key] !== 'function' && typeof cmd[key] !== 'undefined') {
args[key] = cmd[key]
}
})
return args
}

program
.version(require('../../package.json').version)
.usage('<command> [options]')
Expand Down Expand Up @@ -39,20 +56,3 @@ program
})

program.parse(process.argv)

function camelize (str: string) {
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '')
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getCmdOptions (cmd: any) {
const args: { [key: string]: boolean | string } = {}
cmd.options.forEach((o: { long: string }) => {
const key = camelize(o.long.replace(/^--/, ''))

if (typeof cmd[key] !== 'function' && typeof cmd[key] !== 'undefined') {
args[key] = cmd[key]
}
})
return args
}
4 changes: 2 additions & 2 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type FileInfo = {
} | {
fsPath: string,
kind: FileKind,
fileContent: string
fileContent: string,
start: number,
end: number,
content: string,
content: string
}

export function readVueSFCOrTsFile (filePath: string, options: Vc2cOptions): FileInfo {
Expand Down
1 change: 1 addition & 0 deletions src/format.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { Vc2cOptions } from './options'
import path from 'path'
import { log } from './debug'
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function convert (content: string, inputOptions: InputVc2cOptions): strin

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function convertFile (filePath: string, root: string, config: string) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs')
root = (typeof root === 'string')
? (
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as vueTemplateCompiler from 'vue-template-compiler'

export interface Vc2cOptions {
root: string
debug: boolean,
debug: boolean
compatible: boolean
setupPropsKey: string
setupContextKey: string
Expand Down
3 changes: 2 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ export function getSingleFileProgram (content: string, options: Vc2cOptions) {
target: tsModule.ScriptTarget.Latest,
...defaultCompilerOptions
},
compilerHost,
compilerHost
)

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const ast = program.getSourceFile(fileName)!

return { ast, program }
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export function convertASTResultToImport (astResults: ASTResult<ts.Node>[], opti
for (const result of astResults) {
for (const importInfo of result.imports) {
const key: string = ('external' in importInfo) ? importInfo.external : importInfo.path
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const temp: Clause = (importMap.has(key)) ? importMap.get(key)! : { named: new Set() }
if ((!('default' in temp)) && 'default' in importInfo) {
temp.default = importInfo.default
Expand All @@ -212,6 +213,7 @@ export function convertASTResultToImport (astResults: ASTResult<ts.Node>[], opti
}

if (options.compatible && importMap.has('@vue/composition-api')) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const temp = importMap.get('@vue/composition-api')!
temp.named.add('createComponent')
importMap.set('@vue/composition-api', temp)
Expand Down
1 change: 1 addition & 0 deletions src/plugins/removeThisAndSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const removeThisAndSort: ASTTransform = (astResults, options) => {
['$emit', 'emit']
])

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return contextKey.has(key) ? contextKey.get(key)! : key
}

Expand Down
18 changes: 9 additions & 9 deletions src/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ export enum ReferenceKind {
}

export type ImportModule = {
default?: string
named?: string[]
default?: string,
named?: string[],
path: string
} | {
default?: string
named?: string[]
default?: string,
named?: string[],
external: string
}

Expand All @@ -34,13 +34,13 @@ export interface ASTResultBase {
}

export interface ASTResultToObject<N = ts.PropertyAssignment> extends ASTResultBase {
kind: ASTResultKind.OBJECT;
nodes: N[];
kind: ASTResultKind.OBJECT
nodes: N[]
}

export interface ASTResultToComposition<N = ts.Statement> extends ASTResultBase {
kind: ASTResultKind.COMPOSITION;
nodes: N[];
kind: ASTResultKind.COMPOSITION
nodes: N[]
}

export type ASTResult<N> = ASTResultToObject<N> | ASTResultToComposition<N>
Expand All @@ -51,7 +51,7 @@ export type ASTTransform = (astResults: Array<ASTResult<ts.Node>>, options: Vc2c

export interface ASTConvertPlugins {
[ts.SyntaxKind.Decorator]: {
[ts.SyntaxKind.PropertyAssignment]: Array<ASTConverter<ts.PropertyAssignment>>
[ts.SyntaxKind.PropertyAssignment]: Array<ASTConverter<ts.PropertyAssignment>>,
[ts.SyntaxKind.MethodDeclaration]: Array<ASTConverter<ts.MethodDeclaration>>
}
[ts.SyntaxKind.Identifier]: Array<ASTConverter<ts.Identifier>>
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/vue-class-component/ComponentName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const mergeName: ASTTransform = (astResults) => {
const nameTags = ['Class-Name', 'Obj-Name']

const nameASTResults = astResults.filter((el) => nameTags.includes(el.tag))
const nameObjASTResults = nameASTResults.find((el) => el.tag === 'Obj-Name')
const otherASTResults = astResults.filter((el) => !nameTags.includes(el.tag))

const resultNameASTResults = (nameASTResults.length === 1)
Expand All @@ -39,7 +40,7 @@ export const mergeName: ASTTransform = (astResults) => {
imports: [],
reference: ReferenceKind.NONE,
attrutibes: [],
nodes: nameASTResults.find((el) => el.tag === 'Obj-Name')!.nodes as ts.PropertyAssignment[]
...(nameObjASTResults) ? { nodes: nameObjASTResults.nodes as ts.PropertyAssignment[] } : { nodes: [] }
}

return [
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/vue-class-component/Computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const convertGetter: ASTConverter<ts.GetAccessorDeclaration> = (node, opt
[],
undefined,
tsModule.createToken(tsModule.SyntaxKind.EqualsGreaterThanToken),
node.body!
node.body ?? tsModule.createBlock([])
),
node
)
Expand Down Expand Up @@ -54,7 +54,7 @@ export const convertSetter: ASTConverter<ts.SetAccessorDeclaration> = (node, opt
node.parameters,
undefined,
tsModule.createToken(tsModule.SyntaxKind.EqualsGreaterThanToken),
node.body!
node.body ?? tsModule.createBlock([])
),
node
)
Expand Down Expand Up @@ -104,7 +104,7 @@ export const mergeComputed: ASTTransform = (astResults, options) => {
)
)
],
ts.NodeFlags.Const),
ts.NodeFlags.Const)
)

computedASTResults.push({
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/vue-class-component/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const convertData: ASTConverter<ts.PropertyDeclaration> = (node, options,
callExpr
)
],
tsModule.NodeFlags.Const),
tsModule.NodeFlags.Const)
),
node
)
Expand Down
8 changes: 6 additions & 2 deletions src/plugins/vue-class-component/IntervalHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ export const convertIntervalHook: ASTConverter<ts.MethodDeclaration> = (node, op
[],
undefined,
tsModule.createToken(tsModule.SyntaxKind.EqualsGreaterThanToken),
node.body!
node.body ?? tsModule.createBlock([])
)]
)) : node.body!.statements
)) : node.body?.statements

if (!outputNode) {
return false
}

const nodes: ts.Statement[] = (needNamedImports.length > 0)
? [copySyntheticComments(tsModule, outputNode as ts.Statement, node)]
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/vue-class-component/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const convertMethod: ASTConverter<ts.MethodDeclaration> = (node, options)
node.parameters,
node.type,
tsModule.createToken(tsModule.SyntaxKind.EqualsGreaterThanToken),
node.body!
node.body ?? tsModule.createBlock([])
)

return {
Expand All @@ -33,7 +33,7 @@ export const convertMethod: ASTConverter<ts.MethodDeclaration> = (node, options)
outputMethod
)
],
tsModule.NodeFlags.Const),
tsModule.NodeFlags.Const)
),
node
)
Expand Down
10 changes: 6 additions & 4 deletions src/plugins/vue-class-component/object/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import * as ts from 'typescript'
export const convertObjData: ASTConverter<ts.MethodDeclaration> = (node, options) => {
if (node.name.getText() === 'data') {
const tsModule = options.typesciprt
const returnStatement = node.body!.statements.find((el) => tsModule.isReturnStatement(el)) as ts.ReturnStatement
const attrutibes = (returnStatement.expression! as ts.ObjectLiteralExpression).properties.map((el) => el.name!.getText())
const returnStatement = node.body?.statements.find((el) => tsModule.isReturnStatement(el)) as ts.ReturnStatement | undefined
if (!returnStatement || !returnStatement.expression) return false
const attrutibes = (returnStatement.expression as ts.ObjectLiteralExpression).properties.map((el) => el.name?.getText() ?? '')
const arrowFn = tsModule.createArrowFunction(
node.modifiers,
[],
[],
undefined,
tsModule.createToken(tsModule.SyntaxKind.EqualsGreaterThanToken),
tsModule.createBlock(
node.body!.statements.map((el) => {
node.body?.statements.map((el) => {
if (tsModule.isReturnStatement(el)) {
return tsModule.createReturn(
tsModule.createCall(
Expand All @@ -22,13 +23,14 @@ export const convertObjData: ASTConverter<ts.MethodDeclaration> = (node, options
[tsModule.createCall(
tsModule.createIdentifier('reactive'),
undefined,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
[returnStatement.expression!]
)]
)
)
}
return el
}),
}) ?? [],
true
)
)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/vue-class-component/object/Prop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const convertObjProps: ASTConverter<ts.PropertyAssignment> = (node, optio
.filter(expr => expr.kind === tsModule.SyntaxKind.StringLiteral)
.map((el) => (el as ts.StringLiteral).text)
: (node.initializer as ts.ObjectLiteralExpression).properties
.map((el) => el.name!.getText())
.map((el) => el.name?.getText() ?? '')

const nodes = (tsModule.isArrayLiteralExpression(node.initializer))
? node.initializer.elements
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/vue-property-decorator/DomRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export const convertDomRef: ASTConverter<ts.PropertyDeclaration> = (node, option
undefined,
tsModule.createCall(
tsModule.createIdentifier('ref'),
[node.type!],
node.type ? [node.type] : [],
[tsModule.createNull()]
)
)
],
tsModule.NodeFlags.Const),
tsModule.NodeFlags.Const)
),
node
)
Expand Down
Loading

0 comments on commit ff3a9dd

Please sign in to comment.