diff options
author | 2021-03-19 17:07:45 -0400 | |
---|---|---|
committer | 2021-03-19 17:07:45 -0400 | |
commit | 17c3c98f07628b43b941b84831e8e1f9bcd7ca46 (patch) | |
tree | 2e2b3c7d6bd67ebaabe6636ae6867ad368ac6c3a /src/compiler/parse/read/script.ts | |
parent | 8ebc077cb0d9f50aae22d2651bd5ef13fe4641d3 (diff) | |
download | astro-17c3c98f07628b43b941b84831e8e1f9bcd7ca46.tar.gz astro-17c3c98f07628b43b941b84831e8e1f9bcd7ca46.tar.zst astro-17c3c98f07628b43b941b84831e8e1f9bcd7ca46.zip |
Initial tests set up (#10)
* Begin debugging
* Initial tests set up
This adds tests using uvu (we can switch if people want) and restructures things a bit so that it's easier to test.
Like in snowpack you set up a little project. In our tests you can say:
```js
const result = await runtime.load('/blog/hello-world')
```
And analyze the result. I included a `test-helpers.js` which has a function that will turn HTML into a cheerio instance, for inspecting the result HTML.
* Add CI
* Remove extra console logs
* Formatting
Diffstat (limited to 'src/compiler/parse/read/script.ts')
-rw-r--r-- | src/compiler/parse/read/script.ts | 87 |
1 files changed, 46 insertions, 41 deletions
diff --git a/src/compiler/parse/read/script.ts b/src/compiler/parse/read/script.ts index c5fbee699..eb7a8c5b3 100644 --- a/src/compiler/parse/read/script.ts +++ b/src/compiler/parse/read/script.ts @@ -8,48 +8,53 @@ import { Node, Program } from 'estree'; const script_closing_tag = '</script>'; function get_context(parser: Parser, attributes: any[], start: number): string { - const context = attributes.find(attribute => attribute.name === 'context'); - if (!context) return 'default'; - - if (context.value.length !== 1 || context.value[0].type !== 'Text') { - parser.error({ - code: 'invalid-script', - message: 'context attribute must be static' - }, start); - } - - const value = context.value[0].data; - - if (value !== 'module') { - parser.error({ - code: 'invalid-script', - message: 'If the context attribute is supplied, its value must be "module"' - }, context.start); - } - - return value; + const context = attributes.find((attribute) => attribute.name === 'context'); + if (!context) return 'default'; + + if (context.value.length !== 1 || context.value[0].type !== 'Text') { + parser.error( + { + code: 'invalid-script', + message: 'context attribute must be static', + }, + start + ); + } + + const value = context.value[0].data; + + if (value !== 'module') { + parser.error( + { + code: 'invalid-script', + message: 'If the context attribute is supplied, its value must be "module"', + }, + context.start + ); + } + + return value; } export default function read_script(parser: Parser, start: number, attributes: Node[]): Script { - const script_start = parser.index; - const script_end = parser.template.indexOf(script_closing_tag, script_start); - - if (script_end === -1) { - parser.error({ - code: 'unclosed-script', - message: '<script> must have a closing tag' - }); - } - - const source = parser.template.slice(0, script_start).replace(/[^\n]/g, ' ') + - parser.template.slice(script_start, script_end); - parser.index = script_end + script_closing_tag.length; - - return { - type: 'Script', - start, - end: parser.index, - context: get_context(parser, attributes, start), - content: source - }; + const script_start = parser.index; + const script_end = parser.template.indexOf(script_closing_tag, script_start); + + if (script_end === -1) { + parser.error({ + code: 'unclosed-script', + message: '<script> must have a closing tag', + }); + } + + const source = parser.template.slice(0, script_start).replace(/[^\n]/g, ' ') + parser.template.slice(script_start, script_end); + parser.index = script_end + script_closing_tag.length; + + return { + type: 'Script', + start, + end: parser.index, + context: get_context(parser, attributes, start), + content: source, + }; } |