From f3064f13f254300b97a20cec5db109bf5334a287 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Sun, 22 Sep 2019 13:15:53 -0400 Subject: [PATCH] most sourcemap tests working --- package-lock.json | 56 +++++++++++++++++++-------- package.json | 4 +- src/compiler/compile/Component.ts | 32 ++++++++++++++- src/compiler/parse/acorn.ts | 6 ++- test/sourcemaps/index.js | 8 ++-- test/sourcemaps/samples/basic/test.js | 2 +- 6 files changed, 81 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index e1b1d45992..d5abb0d992 100644 --- a/package-lock.json +++ b/package-lock.json @@ -331,7 +331,7 @@ "dev": true }, "astring": { - "version": "github:Rich-Harris/astring#00fa527b66cc1b57efb2a4b9052dd4e190ae3590", + "version": "github:Rich-Harris/astring#9f675d470d1c758de5ec42724aa5dd2e1dabf919", "from": "github:Rich-Harris/astring#generic-handler", "dev": true }, @@ -507,24 +507,16 @@ "dev": true }, "code-red": { - "version": "0.0.12", - "resolved": "https://registry.npmjs.org/code-red/-/code-red-0.0.12.tgz", - "integrity": "sha512-5R8zbm2z5CmHp2iSmV/kSDhXcdISJwBr898jIPeASEdsGrjFpT4Jhky0J659jLsZL9gzDBGn32rvR0YWDXzAvw==", + "version": "0.0.13", + "resolved": "https://registry.npmjs.org/code-red/-/code-red-0.0.13.tgz", + "integrity": "sha512-cBYTCCcEuxdkIvK7LnDlZslan3NoYCV4YAH0xVkePuCD7/TOKg/vWXug7TNjKePLJEnc9isgeK76RzsBy1pShA==", "dev": true, "requires": { "acorn": "^7.0.0", - "astring": "github:Rich-Harris/astring#00fa527b66cc1b57efb2a4b9052dd4e190ae3590", + "astring": "github:Rich-Harris/astring#9f675d470d1c758de5ec42724aa5dd2e1dabf919", "is-reference": "^1.1.3", "periscopic": "^1.0.0", "source-map": "^0.7.3" - }, - "dependencies": { - "source-map": { - "version": "0.7.3", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz", - "integrity": "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==", - "dev": true - } } }, "codecov": { @@ -830,6 +822,13 @@ "resolved": "https://registry.npmjs.org/esprima/-/esprima-3.1.3.tgz", "integrity": "sha1-/cpRzuYTOJXjyI1TXOSdv/YqRjM=", "dev": true + }, + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true, + "optional": true } } }, @@ -1522,6 +1521,14 @@ "optimist": "^0.6.1", "source-map": "^0.6.1", "uglify-js": "^3.1.4" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + } } }, "har-schema": { @@ -3271,9 +3278,9 @@ } }, "source-map": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.3.tgz", + "integrity": "sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==", "dev": true }, "source-map-support": { @@ -3284,6 +3291,14 @@ "requires": { "buffer-from": "^1.0.0", "source-map": "^0.6.0" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + } } }, "sourcemap-codec": { @@ -3591,6 +3606,15 @@ "requires": { "commander": "~2.20.0", "source-map": "~0.6.1" + }, + "dependencies": { + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true, + "optional": true + } } }, "uri-js": { diff --git a/package.json b/package.json index a45d1863e3..a357954da6 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "acorn": "^7.0.0", "agadoo": "^1.0.1", "c8": "^5.0.1", - "code-red": "0.0.12", + "code-red": "0.0.13", "codecov": "^3.5.0", "css-tree": "1.0.0-alpha22", "eslint": "^6.3.0", @@ -85,7 +85,7 @@ "rollup-plugin-sucrase": "^2.1.0", "rollup-plugin-typescript": "^1.0.1", "rollup-plugin-virtual": "^1.0.1", - "source-map": "^0.6.1", + "source-map": "^0.7.3", "source-map-support": "^0.5.13", "tiny-glob": "^0.2.6", "tslib": "^1.10.0", diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 0facb30c1d..77e01f8e79 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -1,4 +1,3 @@ -// @ts-ignore import { walk, childKeys } from 'estree-walker'; import { getLocator } from 'locate-character'; import Stats from '../Stats'; @@ -368,7 +367,17 @@ export default class Component { ? { code: null, map: null } : this.stylesheet.render(compile_options.cssOutputFilename, true); - js = print(program); + js = print(program, { + sourceMapSource: compile_options.filename + }); + + js.map.sources = [ + compile_options.filename ? get_relative_path(compile_options.outputFilename || '', compile_options.filename) : null + ]; + + js.map.sourcesContent = [ + this.source + ]; } return { @@ -1404,3 +1413,22 @@ function process_component_options(component: Component, nodes) { return component_options; } + +function get_relative_path(from: string, to: string) { + const from_parts = from.split(/[/\\]/); + const to_parts = to.split(/[/\\]/); + + from_parts.pop(); // get dirname + + while (from_parts[0] === to_parts[0]) { + from_parts.shift(); + to_parts.shift(); + } + + if (from_parts.length) { + let i = from_parts.length; + while (i--) from_parts[i] = '..'; + } + + return from_parts.concat(to_parts).join('/'); +} \ No newline at end of file diff --git a/src/compiler/parse/acorn.ts b/src/compiler/parse/acorn.ts index 308a99a422..a7d2703956 100644 --- a/src/compiler/parse/acorn.ts +++ b/src/compiler/parse/acorn.ts @@ -5,10 +5,12 @@ const Parser = acorn.Parser; export const parse = (source: string) => Parser.parse(source, { sourceType: 'module', // @ts-ignore TODO pending release of fixed types - ecmaVersion: 11 + ecmaVersion: 11, + locations: true }); export const parse_expression_at = (source: string, index: number) => Parser.parseExpressionAt(source, index, { // @ts-ignore TODO pending release of fixed types - ecmaVersion: 11 + ecmaVersion: 11, + locations: true }); \ No newline at end of file diff --git a/test/sourcemaps/index.js b/test/sourcemaps/index.js index ee169ebe1b..fb5814e8d8 100644 --- a/test/sourcemaps/index.js +++ b/test/sourcemaps/index.js @@ -5,7 +5,7 @@ import { svelte } from "../helpers.js"; import { SourceMapConsumer } from "source-map"; import { getLocator } from "locate-character"; -describe("sourcemaps", () => { +describe.only("sourcemaps", () => { fs.readdirSync("test/sourcemaps/samples").forEach(dir => { if (dir[0] === ".") return; @@ -17,7 +17,7 @@ describe("sourcemaps", () => { throw new Error("Forgot to remove `solo: true` from test"); } - (solo ? it.only : skip ? it.skip : it)(dir, () => { + (solo ? it.only : skip ? it.skip : it)(dir, async () => { const filename = path.resolve( `test/sourcemaps/samples/${dir}/input.svelte` ); @@ -61,10 +61,10 @@ describe("sourcemaps", () => { const locateInSource = getLocator(input); - const smc = new SourceMapConsumer(js.map); + const smc = await new SourceMapConsumer(js.map); const locateInGenerated = getLocator(_code); - const smcCss = css.map && new SourceMapConsumer(css.map); + const smcCss = css.map && await new SourceMapConsumer(css.map); const locateInGeneratedCss = getLocator(css.code || ''); test({ assert, code: _code, map: js.map, smc, smcCss, locateInSource, locateInGenerated, locateInGeneratedCss }); diff --git a/test/sourcemaps/samples/basic/test.js b/test/sourcemaps/samples/basic/test.js index f13ff5d0e1..f2ce4198fd 100644 --- a/test/sourcemaps/samples/basic/test.js +++ b/test/sourcemaps/samples/basic/test.js @@ -4,7 +4,7 @@ export function test({ assert, smc, locateInSource, locateInGenerated }) { let start; let actual; - start = locateInGenerated( 'foo.bar.baz' ); + start = locateInGenerated('foo.bar.baz'); actual = smc.originalPositionFor({ line: start.line + 1,