Skip to content

Commit

Permalink
update acorn and parse expression as module script (sveltejs#5423)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored and taylorzane committed Dec 17, 2020
1 parent 9d9f869 commit 05b1fb8
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# Svelte changelog

## Unreleased

* Support `_` as numeric separator ([#5407](https://github.com/sveltejs/svelte/issues/5407))
* Support `import.meta` in template expressions ([#5422](https://github.com/sveltejs/svelte/issues/5422))

## 3.25.1

* Fix specificity of certain styles involving a child selector ([#4795](https://github.com/sveltejs/svelte/issues/4795))
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -68,7 +68,7 @@
"@types/node": "^8.10.53",
"@typescript-eslint/eslint-plugin": "^3.0.2",
"@typescript-eslint/parser": "^3.0.2",
"acorn": "^7.3.1",
"acorn": "^7.4.0",
"agadoo": "^1.1.0",
"c8": "^5.0.1",
"code-red": "^0.1.3",
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/compile/nodes/shared/Expression.ts
Expand Up @@ -64,6 +64,8 @@ export default class Expression {
enter(node: any, parent: any, key: string) {
// don't manipulate shorthand props twice
if (key === 'value' && parent.shorthand) return;
// don't manipulate `import.meta`, `new.target`
if (node.type === 'MetaProperty') return this.skip();

if (map.has(node)) {
scope = map.get(node);
Expand Down
5 changes: 3 additions & 2 deletions src/compiler/parse/acorn.ts
Expand Up @@ -3,11 +3,12 @@ import * as code_red from 'code-red';

export const parse = (source: string): Node => code_red.parse(source, {
sourceType: 'module',
ecmaVersion: 11,
ecmaVersion: 12,
locations: true
});

export const parse_expression_at = (source: string, index: number): Node => code_red.parseExpressionAt(source, index, {
ecmaVersion: 11,
sourceType: 'module',
ecmaVersion: 12,
locations: true
});
53 changes: 53 additions & 0 deletions test/js/samples/import-meta/expected.js
@@ -0,0 +1,53 @@
/* generated by Svelte vX.Y.Z */
import {
SvelteComponent,
detach,
init,
insert,
noop,
safe_not_equal,
space,
text
} from "svelte/internal";

function create_fragment(ctx) {
let t0;
let t1;
let t2_value = import.meta.url + "";
let t2;

return {
c() {
t0 = text(/*url*/ ctx[0]);
t1 = space();
t2 = text(t2_value);
},
m(target, anchor) {
insert(target, t0, anchor);
insert(target, t1, anchor);
insert(target, t2, anchor);
},
p: noop,
i: noop,
o: noop,
d(detaching) {
if (detaching) detach(t0);
if (detaching) detach(t1);
if (detaching) detach(t2);
}
};
}

function instance($$self) {
const url = import.meta.url;
return [url];
}

class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, {});
}
}

export default Component;
6 changes: 6 additions & 0 deletions test/js/samples/import-meta/input.svelte
@@ -0,0 +1,6 @@
<script>
const url = import.meta.url;
</script>

{url}
{import.meta.url}
3 changes: 3 additions & 0 deletions test/runtime/samples/numeric-seperator/_config.js
@@ -0,0 +1,3 @@
export default {
html: `2048 2048`
};
5 changes: 5 additions & 0 deletions test/runtime/samples/numeric-seperator/main.svelte
@@ -0,0 +1,5 @@
<script>
const num = 2_048;
</script>

{num} {2_048}
1 change: 1 addition & 0 deletions test/validator/samples/import-meta/errors.json
@@ -0,0 +1 @@
[]
6 changes: 6 additions & 0 deletions test/validator/samples/import-meta/input.svelte
@@ -0,0 +1,6 @@
<script>
const url = import.meta.url;
</script>

{url}
{import.meta.url}

0 comments on commit 05b1fb8

Please sign in to comment.