Skip to content

es2022 class features #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 14 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
},
"license": "MIT",
"dependencies": {
"@types/estree": "^1.0.0",
"@jridgewell/sourcemap-codec": "^1.4.14",
"@types/estree": "^0.0.50",
"acorn": "^8.0.5",
"acorn": "^8.8.2",
"estree-walker": "^3.0.0",
"periscopic": "^3.0.4"
},
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const EMPTY = { type: 'Empty' };
const acorn_opts = (comments, raw) => {
const { onComment } = get_comment_handlers(comments, raw);
return {
ecmaVersion: 2020,
ecmaVersion: 2022,
sourceType: 'module',
allowAwaitOutsideFunction: true,
allowImportExportEverywhere: true,
Expand Down
48 changes: 48 additions & 0 deletions src/print/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ import { push_array } from '../utils/push_array.js';
/** @typedef {import('estree').Node} Node */
/** @typedef {import('estree').ObjectExpression} ObjectExpression */
/** @typedef {import('estree').Pattern} Pattern */
/** @typedef {import('estree').Property} Property */
/** @typedef {import('estree').PropertyDefinition} PropertyDefinition */
/** @typedef {import('estree').SequenceExpression} SequenceExpression */
/** @typedef {import('estree').SimpleCallExpression} SimpleCallExpression */
/** @typedef {import('estree').SwitchStatement} SwitchStatement */
/** @typedef {import('estree').VariableDeclaration} VariableDeclaration */
/** @typedef {import('estree').StaticBlock} StaticBlock */
/** @typedef {import('estree').PrivateIdentifier} PrivateIdenifier*/

/**
* @typedef {{
Expand Down Expand Up @@ -1372,6 +1376,50 @@ const handlers = {
}

return [c(node.raw || String(node.value), node)];
},

PropertyDefinition(/** @type {PropertyDefinition} */ node, state) {
const chunks = [];

if (node.static) {
chunks.push(c('static '));
}

if (node.computed) {
chunks.push(
c('['),
...handle(node.key, state),
c(']')
);
} else {
chunks.push(...handle(node.key, state));
}

if (node.value) {
chunks.push(c(' = '))

chunks.push(...handle(node.value, state));
}

chunks.push(c(';'));

return chunks;
},

StaticBlock(/** @type {StaticBlock} */ node, state) {
const chunks = [c('static ')];

push_array(chunks, handlers.BlockStatement(node, state));

return chunks;
},

PrivateIdentifier(/** @type {PrivateIdenifier} */node, state) {
const chunks = [c('#')];

push_array(chunks, [c(node.name, node)]);

return chunks;
}
};

Expand Down
33 changes: 33 additions & 0 deletions test/samples/class-private/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class A {
#p;
static #p1;
static #p2;

static is(obj) {
return #p2 in obj;
}

get #p3() {

}

set #p3(v) {

}

#m1() {
return this.#p1;
}

static #m2() {

}

*#m3() {

}

async #m4() {

}
}
1 change: 1 addition & 0 deletions test/samples/class-private/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions test/samples/class-private/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default ({ parse }) => parse(`class A {
#p
static #p1
static #p2

static is(obj) {
return #p2 in obj
}

get #p3() {}
set #p3(v) {}
#m1() {
return this.#p1
}
static #m2() {}
*#m3() {}
async #m4() {}
}`);
5 changes: 5 additions & 0 deletions test/samples/class-property/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Foo {
hi;
static foo = 1;
[KEY] = 2;
}
1 change: 1 addition & 0 deletions test/samples/class-property/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/samples/class-property/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default ({ b }) => b`
class Foo {
hi
static foo = 1;
[KEY] = 2
}
`;
5 changes: 5 additions & 0 deletions test/samples/class-static-block/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Foo {
static {
this.abc = 1;
}
}
1 change: 1 addition & 0 deletions test/samples/class-static-block/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/samples/class-static-block/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default ({ b }) => b`
class Foo {
static {
this.abc = 1;
}
}
`;
9 changes: 8 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ const b = (s, ...v) => remove_ranges(codered.b(s, ...v));
const x = (s, ...v) => remove_ranges(codered.x(s, ...v));
const p = (s, ...v) => remove_ranges(codered.p(s, ...v));
const print = codered.print;
const parse = (s) => codered.parse(s, {
ecmaVersion: 2022,
sourceType: 'module',
allowAwaitOutsideFunction: true,
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
})

/**
* @param {string} name
Expand Down Expand Up @@ -465,7 +472,7 @@ suite('print', test => {

const url = new URL(`./samples/${dir}/input.js`, import.meta.url);
const mod = await import(fileURLToPath(url.href));
const input = mod.default({ b, x, p });
const input = mod.default({ b, x, p, parse });

const expected = {
code: read(`test/samples/${dir}/expected.js`),
Expand Down