Skip to content

Commit

Permalink
fix: default export cases (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Feb 23, 2023
1 parent 82c3db6 commit 24f85fb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 71 deletions.
2 changes: 1 addition & 1 deletion lib/lexer.asm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/lexer.emcc.asm.js

Large diffs are not rendered by default.

Binary file modified lib/lexer.wasm
Binary file not shown.
28 changes: 10 additions & 18 deletions src/lexer.c
Expand Up @@ -461,26 +461,18 @@ void tryParseExportStatement () {
localName = true;
}
break;
default: {
const char16_t* localStartPos = pos;
ch = readToWsOrPunctuator(ch);
ch = commentWhitespace(true);
if (ch == ';') {
break;
}
localName = true;
}
}
const char16_t* localStartPos = pos;
ch = readToWsOrPunctuator(ch);
if (localName && pos > localStartPos) {
addExport(startPos, startPos + 7, localStartPos, pos);
pos--;
}
else {
addExport(startPos, startPos + 7, NULL, NULL);
pos = (char16_t*)(startPos + 6);
if (localName) {
const char16_t* localStartPos = pos;
readToWsOrPunctuator(ch);
if (pos > localStartPos) {
addExport(startPos, startPos + 7, localStartPos, pos);
pos--;
return;
}
}
addExport(startPos, startPos + 7, NULL, NULL);
pos = (char16_t*)(startPos + 6);
return;
}
// export async? function*? name () {
Expand Down
112 changes: 61 additions & 51 deletions test/_unit.cjs
Expand Up @@ -36,9 +36,20 @@ function assertExportIs(source, actual, expected) {
assert.strictEqual(actual.ln, expected.ln, `export.ln: ${actual.ln} != ${expected.ln}`);
}

suite('Invalid syntax', () => {
suite('Lexer', () => {
beforeEach(async () => await init);

test(`Export default cases`, () => {
const source = `
export default "export default a"
export default "export default 'a'"
export default "export function foo() {}"
export default "export function foo() {return bar}"
`;
const [, exports] = parse(source);
assert.deepStrictEqual(exports.map(expt => expt.n), ['default', 'default', 'default', 'default']);
});

test(`import.meta spread`, () => {
const source = `console.log(...import.meta.obj);`;
const [impts] = parse(source);
Expand Down Expand Up @@ -128,56 +139,6 @@ suite('Invalid syntax', () => {
assert.strictEqual(source.substring(imports[0].s, imports[0].e), 'import.meta.url');
});

test('Unterminated object', () => {
const source = `
const foo = };
const bar = {};
`;
try {
parse(source);
}
catch (err) {
assert.strictEqual(err.message, 'Parse error @:2:19');
}
});

test('Invalid string', () => {
const source = `import './export.js';
import d from './export.js';
import { s as p } from './reexport1.js';
import { z, q as r } from './reexport2.js';
'
import * as q from './reexport1.js';
export { d as a, p as b, z as c, r as d, q }`;
try {
parse(source);
}
catch (err) {
assert.strictEqual(err.message, 'Parse error @:9:5');
}
});

test('Invalid export', () => {
try {
const source = `export { a = };`;
parse(source);
assert(false, 'Should error');
}
catch (err) {
assert.strictEqual(err.idx, 11);
}
});
});

suite('Lexer', () => {
beforeEach(async () => await init);

test('Export', () => {
const source = `export var p=5`;
const [, exports] = parse(source);
Expand Down Expand Up @@ -1352,3 +1313,52 @@ function x() {
});
});

suite('Invalid syntax', () => {
beforeEach(async () => await init);

test('Unterminated object', () => {
const source = `
const foo = };
const bar = {};
`;
try {
parse(source);
}
catch (err) {
assert.strictEqual(err.message, 'Parse error @:2:19');
}
});

test('Invalid string', () => {
const source = `import './export.js';
import d from './export.js';
import { s as p } from './reexport1.js';
import { z, q as r } from './reexport2.js';
'
import * as q from './reexport1.js';
export { d as a, p as b, z as c, r as d, q }`;
try {
parse(source);
}
catch (err) {
assert.strictEqual(err.message, 'Parse error @:9:5');
}
});

test('Invalid export', () => {
try {
const source = `export { a = };`;
parse(source);
assert(false, 'Should error');
}
catch (err) {
assert.strictEqual(err.idx, 11);
}
});
});

0 comments on commit 24f85fb

Please sign in to comment.