Skip to content

Commit

Permalink
preserve js comments where possible (sveltejs#4293)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry authored and taylorzane committed Dec 17, 2020
1 parent 41bffc2 commit 0fa7399
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Allow access to `let:` variables in sibling attributes on slot root ([#4173](https://github.com/sveltejs/svelte/issues/4173))
* Fix `~=` and class selector matching against values separated by any whitespace characters ([#4242](https://github.com/sveltejs/svelte/issues/4242))
* Fix code generation for `await`ed expressions that need parentheses ([#4267](https://github.com/sveltejs/svelte/issues/4267))
* Preserve JavaScript comments from the original component source where possible ([#4268](https://github.com/sveltejs/svelte/issues/4268))
* Add some more known globals ([#4276](https://github.com/sveltejs/svelte/pull/4276))
* Correctly apply event modifiers to `<svelte:body>` events ([#4278](https://github.com/sveltejs/svelte/issues/4278))

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
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"acorn": "^7.1.0",
"agadoo": "^1.1.0",
"c8": "^5.0.1",
"code-red": "0.0.30",
"code-red": "0.0.31",
"codecov": "^3.5.0",
"css-tree": "1.0.0-alpha22",
"eslint": "^6.3.0",
Expand Down
9 changes: 4 additions & 5 deletions src/compiler/parse/acorn.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as acorn from 'acorn';
import { Node } from 'acorn';
import * as code_red from 'code-red';

const Parser = acorn.Parser;

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

export const parse_expression_at = (source: string, index: number) => Parser.parseExpressionAt(source, index, {
export const parse_expression_at = (source: string, index: number): Node => code_red.parseExpressionAt(source, index, {
ecmaVersion: 11,
locations: true
});
2 changes: 1 addition & 1 deletion test/js/samples/action-custom-event-handler/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function handleFoo(bar) {

function foo(node, callback) {

}
} // code goes here

function instance($$self, $$props, $$invalidate) {
let { bar } = $$props;
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/collapses-text-around-comments/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ class Component extends SvelteComponent {
}
}

export default Component;
export default Component;
2 changes: 1 addition & 1 deletion test/js/samples/css-media-query/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ class Component extends SvelteComponent {
}
}

export default Component;
export default Component;
4 changes: 2 additions & 2 deletions test/js/samples/event-modifiers/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ function create_fragment(ctx) {

function handleTouchstart() {

}
} // ...

function handleClick() {

}
} // ...

class Component extends SvelteComponent {
constructor(options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/ssr-no-oncreate-etc/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function foo() {

function swipe(node, callback) {

}
} // TODO implement

const Component = create_ssr_component(($$result, $$props, $$bindings, $$slots) => {
onMount(() => {
Expand Down
10 changes: 9 additions & 1 deletion test/parser/samples/script-comment-only/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@
}
},
"body": [],
"sourceType": "module"
"sourceType": "module",
"trailingComments": [
{
"type": "Line",
"value": " TODO write some code",
"start": 10,
"end": 33
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@
"kind": "let"
}
],
"sourceType": "module"
"sourceType": "module",
"trailingComments": [
{
"type": "Block",
"value": "\n\ttrailing multiline comment\n",
"start": 32,
"end": 67
}
]
}
}
}
10 changes: 9 additions & 1 deletion test/parser/samples/script-comment-trailing/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@
"kind": "let"
}
],
"sourceType": "module"
"sourceType": "module",
"trailingComments": [
{
"type": "Line",
"value": " trailing line comment",
"start": 32,
"end": 56
}
]
}
}
}

0 comments on commit 0fa7399

Please sign in to comment.