Skip to content

Commit

Permalink
implement export default
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Jul 6, 2024
1 parent bf70943 commit c00d0e5
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
90 changes: 90 additions & 0 deletions spec/eslint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// this file comes from https://github.com/msgpack/msgpack-javascript/blob/main/.eslintrc.js

export default {
root: true,
extends: [
// https://eslint.org/docs/rules/
"eslint:recommended",
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/recommended.ts
"plugin:@typescript-eslint/recommended",

// https://github.com/benmosher/eslint-plugin-import
"plugin:import/recommended",
"plugin:import/typescript",

// https://prettier.io/docs/en/integrating-with-linters.html
// > Make sure to put it last, so it gets the chance to override other configs.
"prettier",
],
plugins: [
"@typescript-eslint/eslint-plugin",
"eslint-plugin-tsdoc"
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json",
},
settings: {},
rules: {
"no-constant-condition": ["warn", { checkLoops: false }],
"no-useless-escape": "warn",
"no-console": "warn",
"no-var": "warn",
"no-return-await": "warn",
"prefer-const": "warn",
"guard-for-in": "warn",
"curly": "warn",
"no-param-reassign": "warn",
"prefer-spread": "warn",

"import/no-unresolved": "off", // cannot handle `paths` in tsconfig
"import/no-cycle": "error",
"import/no-default-export": "warn",

"tsdoc/syntax": "warn",

"@typescript-eslint/await-thenable": "warn",
"@typescript-eslint/array-type": ["warn", { default: "generic" }],
"@typescript-eslint/naming-convention": [
"warn",
{ "selector": "default", "format": ["camelCase", "UPPER_CASE", "PascalCase"], "leadingUnderscore": "allow" },
{ "selector": "typeLike", "format": ["PascalCase"], "leadingUnderscore": "allow" },
],
"@typescript-eslint/restrict-plus-operands": "warn",
"@typescript-eslint/no-throw-literal": "warn",
"@typescript-eslint/unbound-method": "warn",
"@typescript-eslint/explicit-module-boundary-types": "warn",
"@typescript-eslint/no-extra-semi": "warn",
"@typescript-eslint/no-extra-non-null-assertion": "warn",
"@typescript-eslint/no-unused-vars": ["warn", { "argsIgnorePattern": "^_" }],
"@typescript-eslint/no-use-before-define": "warn",
"@typescript-eslint/no-for-in-array": "warn",
"@typescript-eslint/no-unsafe-argument": "warn",
"@typescript-eslint/no-unsafe-call": "warn",
// "@typescript-eslint/no-unsafe-member-access": "warn", // too strict, especially for testing
"@typescript-eslint/no-unnecessary-condition": ["warn", { "allowConstantLoopConditions": true }],
"@typescript-eslint/no-unnecessary-type-constraint": "warn",
"@typescript-eslint/no-implied-eval": "warn",
"@typescript-eslint/no-non-null-asserted-optional-chain": "warn",
"@typescript-eslint/no-invalid-void-type": "warn",
"@typescript-eslint/no-loss-of-precision": "warn",
"@typescript-eslint/no-confusing-void-expression": "warn",
"@typescript-eslint/no-redundant-type-constituents": "warn",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-includes": "warn",
"@typescript-eslint/prefer-string-starts-ends-with": "warn",
"@typescript-eslint/prefer-readonly": "warn",
"@typescript-eslint/prefer-regexp-exec": "warn",
"@typescript-eslint/prefer-nullish-coalescing": "warn",
"@typescript-eslint/prefer-optional-chain": "warn",
"@typescript-eslint/prefer-ts-expect-error": "warn",

"@typescript-eslint/indent": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/ban-ts-comment": "off",
},
};
112 changes: 112 additions & 0 deletions spec/eslint.ts.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"default": {
"root": true,
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
"prettier"
],
"plugins": [
"@typescript-eslint/eslint-plugin",
"eslint-plugin-tsdoc"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
},
"settings": {},
"rules": {
"no-constant-condition": [
"warn",
{
"checkLoops": false
}
],
"no-useless-escape": "warn",
"no-console": "warn",
"no-var": "warn",
"no-return-await": "warn",
"prefer-const": "warn",
"guard-for-in": "warn",
"curly": "warn",
"no-param-reassign": "warn",
"prefer-spread": "warn",
"import/no-unresolved": "off",
"import/no-cycle": "error",
"import/no-default-export": "warn",
"tsdoc/syntax": "warn",
"@typescript-eslint/await-thenable": "warn",
"@typescript-eslint/array-type": [
"warn",
{
"default": "generic"
}
],
"@typescript-eslint/naming-convention": [
"warn",
{
"selector": "default",
"format": [
"camelCase",
"UPPER_CASE",
"PascalCase"
],
"leadingUnderscore": "allow"
},
{
"selector": "typeLike",
"format": [
"PascalCase"
],
"leadingUnderscore": "allow"
}
],
"@typescript-eslint/restrict-plus-operands": "warn",
"@typescript-eslint/no-throw-literal": "warn",
"@typescript-eslint/unbound-method": "warn",
"@typescript-eslint/explicit-module-boundary-types": "warn",
"@typescript-eslint/no-extra-semi": "warn",
"@typescript-eslint/no-extra-non-null-assertion": "warn",
"@typescript-eslint/no-unused-vars": [
"warn",
{
"argsIgnorePattern": "^_"
}
],
"@typescript-eslint/no-use-before-define": "warn",
"@typescript-eslint/no-for-in-array": "warn",
"@typescript-eslint/no-unsafe-argument": "warn",
"@typescript-eslint/no-unsafe-call": "warn",
"@typescript-eslint/no-unnecessary-condition": [
"warn",
{
"allowConstantLoopConditions": true
}
],
"@typescript-eslint/no-unnecessary-type-constraint": "warn",
"@typescript-eslint/no-implied-eval": "warn",
"@typescript-eslint/no-non-null-asserted-optional-chain": "warn",
"@typescript-eslint/no-invalid-void-type": "warn",
"@typescript-eslint/no-loss-of-precision": "warn",
"@typescript-eslint/no-confusing-void-expression": "warn",
"@typescript-eslint/no-redundant-type-constituents": "warn",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-includes": "warn",
"@typescript-eslint/prefer-string-starts-ends-with": "warn",
"@typescript-eslint/prefer-readonly": "warn",
"@typescript-eslint/prefer-regexp-exec": "warn",
"@typescript-eslint/prefer-nullish-coalescing": "warn",
"@typescript-eslint/prefer-optional-chain": "warn",
"@typescript-eslint/prefer-ts-expect-error": "warn",
"@typescript-eslint/indent": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/ban-ts-comment": "off"
}
}
}
2 changes: 2 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub enum Statement<'src> {
},
Return(Expression<'src>),
Yield(Expression<'src>),
ExportDefault(Expression<'src>),
Export(Statements<'src>), // stmts.len() == 1
}

Expand All @@ -111,6 +112,7 @@ impl<'src> Statement<'src> {
FnDef { name, stmts, .. } => calc_offset(*name, stmts.span()),
Return(ex) => ex.span,
Yield(ex) => ex.span,
ExportDefault(ex) => ex.span,
Export(stmts) => stmts[0].span().unwrap(),
})
}
Expand Down
9 changes: 9 additions & 0 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,15 @@ impl Compiler {
self.add_inst(OpCode::Yield, (self.target_stack.len() - res.0 - 1) as u8);
self.target_stack.pop();
}
Statement::ExportDefault(ex) => {
// `export default expr` is a syntactic sugar for `export const default = expr`.
let res = self.compile_expr(ex)?;

let name_id = self.add_literal(Value::Str("default".to_string()));
self.add_load_literal_inst(name_id);
self.add_copy_inst(res);
self.add_inst(OpCode::Export, 0);
}
Statement::Export(stmts) => {
assert!(stmts.len() == 1);

Expand Down
8 changes: 8 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ fn fn_def_statement(i: Span) -> IResult<Span, Statement> {
))
}

fn export_default_statement(i: Span) -> IResult<Span, Statement> {
let (i, _) = space_delimited(tag("export"))(i)?;
let (i, _) = space_delimited(tag("default"))(i)?;
let (i, ex) = space_delimited(expr)(i)?;
Ok((i, Statement::ExportDefault(ex)))
}

fn export_statement(i: Span) -> IResult<Span, Statement> {
let (i, _) = space_delimited(tag("export"))(i)?;
let (i, stmt) = statement(i)?;
Expand Down Expand Up @@ -613,6 +620,7 @@ fn general_statement<'a>(last: bool) -> impl Fn(Span<'a>) -> IResult<Span<'a>, S
fn_def_statement,
return_statement,
yield_statement,
export_default_statement,
export_statement,
terminated(expr_statement, terminator),
))(input)
Expand Down
3 changes: 3 additions & 0 deletions src/type_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ pub fn type_check<'src>(
tc_expr(e, ctx)?;
// TODO: check type with the return type, but don't escape from this function.
}
Statement::ExportDefault(e) => {
res = tc_expr(e, ctx)?;
}
Statement::Export(stmts) => {
res = type_check(stmts, ctx)?;
}
Expand Down

0 comments on commit c00d0e5

Please sign in to comment.