From 6c89bb367a1c00d78af05fbfcb4f891c3119e78f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=86=E8=91=B5?= Date: Thu, 12 May 2022 17:36:18 +0800 Subject: [PATCH 1/3] feat(compiler): handle error --- packages/pwc-compiler/package.json | 3 +- packages/pwc-compiler/src/compileScript.ts | 9 +- packages/pwc-compiler/src/errors.ts | 46 ++++++ packages/pwc-compiler/src/index.ts | 1 + packages/pwc-compiler/src/parse.ts | 136 ++++++++++++------ packages/pwc-compiler/src/validate.ts | 31 +++- packages/rollup-plugin-pwc/src/utils/error.ts | 33 ++++- 7 files changed, 201 insertions(+), 58 deletions(-) create mode 100644 packages/pwc-compiler/src/errors.ts diff --git a/packages/pwc-compiler/package.json b/packages/pwc-compiler/package.json index 9b50a1b..6dd2468 100644 --- a/packages/pwc-compiler/package.json +++ b/packages/pwc-compiler/package.json @@ -42,14 +42,13 @@ "npm": ">=3.0.0" }, "dependencies": { - "@babel/core": "^7.17.9", "@babel/generator": "^7.17.7", "@babel/parser": "^7.17.9", "@babel/plugin-proposal-decorators": "^7.17.2", "@babel/traverse": "^7.17.3", "@babel/types": "^7.17.0", "@swc/helpers": "^0.3.8", - "parse5": "^6.0.1", + "parse5": "^7.0.0", "postcss": "^8.4.12", "rfdc": "^1.3.0", "source-map": "^0.7.3" diff --git a/packages/pwc-compiler/src/compileScript.ts b/packages/pwc-compiler/src/compileScript.ts index 801ea82..2770995 100644 --- a/packages/pwc-compiler/src/compileScript.ts +++ b/packages/pwc-compiler/src/compileScript.ts @@ -1,4 +1,5 @@ import generate from '@babel/generator'; +import { join } from 'path'; import rfdc from 'rfdc'; import type { SFCDescriptor, SFCScriptBlock } from './parse'; import { compileTemplate } from './compileTemplate'; @@ -8,9 +9,11 @@ const deepClone = rfdc(); export interface SFCScriptCompileResult extends SFCScriptBlock { filename: string; + sourceRoot: string; } export function compileScript(descriptor: SFCDescriptor): SFCScriptCompileResult { + const rootContext = process.cwd(); const { script, filename } = descriptor; const ast = deepClone(descriptor.script.ast); @@ -26,8 +29,11 @@ export function compileScript(descriptor: SFCDescriptor): SFCScriptCompileResult transformScript(ast, { templateString: null }); } + const sourceRoot = join(rootContext, 'src'); const { code, map } = generate(ast, { - sourceMaps: false, + sourceMaps: true, + sourceRoot, + sourceFileName: filename, decoratorsBeforeExport: true, }); @@ -35,6 +41,7 @@ export function compileScript(descriptor: SFCDescriptor): SFCScriptCompileResult return { ...script, filename, + sourceRoot, content: code, map, }; diff --git a/packages/pwc-compiler/src/errors.ts b/packages/pwc-compiler/src/errors.ts new file mode 100644 index 0000000..7366edd --- /dev/null +++ b/packages/pwc-compiler/src/errors.ts @@ -0,0 +1,46 @@ +import type { SourceLocation } from './parse.js'; + +export interface CompilerError extends SyntaxError { + code: number | string; + loc?: SourceLocation; +} + +export function createCompilerError( + code: number | string, + loc?: SourceLocation, +): CompilerError { + const msg = typeof code === 'string' ? code : errorMessages[code]; + const error = new SyntaxError(String(msg)) as CompilerError; + error.code = code; + error.loc = loc; + return error; +} + +export const enum ErrorCodes { + // parse errors + MISSING_SCRIPT_TAG, + DUPLICATE_SCRIPT_TAG, + DUPLICATE_TEMPLATE_TAG, + DUPLICATE_STYLE_TAG, + // script errors + MISSING_EXPORT_DEFAULT, + MISSING_EXPORT_CLASS, + MISSING_EXPORT_CLASS_EXTENDED_FROM_HTMLELEMENT, + // transform errors + X_V_IF_NO_EXPRESSION, +} + +export const errorMessages: Record = { + // parse errors + [ErrorCodes.MISSING_SCRIPT_TAG]: 'PWC must contain one