Skip to content

Commit

Permalink
Merge pull request #1262 from dart-lang/merge-source_maps-package
Browse files Browse the repository at this point in the history
Merge `package:source_maps`
  • Loading branch information
mosuem authored Dec 11, 2024
2 parents db4638d + e9fad4a commit db47a84
Show file tree
Hide file tree
Showing 26 changed files with 2,899 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/ISSUE_TEMPLATE/source_maps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
name: "package:source_maps"
about: "Create a bug or file a feature request against package:source_maps."
labels: "package:source_maps"
---
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
- changed-files:
- any-glob-to-any-file: 'pkgs/source_map_stack_trace/**'

'package:source_maps':
- changed-files:
- any-glob-to-any-file: 'pkgs/source_maps/**'

'package:unified_analytics':
- changed-files:
- any-glob-to-any-file: 'pkgs/unified_analytics/**'
72 changes: 72 additions & 0 deletions .github/workflows/source_maps.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: package:source_maps

on:
# Run on PRs and pushes to the default branch.
push:
branches: [ main ]
paths:
- '.github/workflows/source_maps.yaml'
- 'pkgs/source_maps/**'
pull_request:
branches: [ main ]
paths:
- '.github/workflows/source_maps.yaml'
- 'pkgs/source_maps/**'
schedule:
- cron: "0 0 * * 0"

env:
PUB_ENVIRONMENT: bot.github


defaults:
run:
working-directory: pkgs/source_maps/

jobs:
# Check code formatting and static analysis on a single OS (linux)
# against Dart dev.
analyze:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
sdk: [dev]
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
with:
sdk: ${{ matrix.sdk }}
- id: install
name: Install dependencies
run: dart pub get
- name: Check formatting
run: dart format --output=none --set-exit-if-changed .
if: always() && steps.install.outcome == 'success'
- name: Analyze code
run: dart analyze --fatal-infos
if: always() && steps.install.outcome == 'success'

# Run tests on a matrix consisting of two dimensions:
# 1. OS: ubuntu-latest, (macos-latest, windows-latest)
# 2. release channel: dev
test:
needs: analyze
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
# Add macos-latest and/or windows-latest if relevant for this package.
os: [ubuntu-latest]
sdk: [3.3.0, dev]
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
with:
sdk: ${{ matrix.sdk }}
- id: install
name: Install dependencies
run: dart pub get
- name: Run VM tests
run: dart test --platform vm
if: always() && steps.install.outcome == 'success'
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ don't naturally belong to other topic monorepos (like
| [pool](pkgs/pool/) | Manage a finite pool of resources. Useful for controlling concurrent file system or network requests. | [![package issues](https://img.shields.io/badge/package:pool-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Apool) | [![pub package](https://img.shields.io/pub/v/pool.svg)](https://pub.dev/packages/pool) |
| [pub_semver](pkgs/pub_semver/) | Versions and version constraints implementing pub's versioning policy. This is very similar to vanilla semver, with a few corner cases. | [![package issues](https://img.shields.io/badge/package:pub_semver-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Apub_semver) | [![pub package](https://img.shields.io/pub/v/pub_semver.svg)](https://pub.dev/packages/pub_semver) |
| [source_map_stack_trace](pkgs/source_map_stack_trace/) | A package for applying source maps to stack traces. | [![package issues](https://img.shields.io/badge/package:source_map_stack_trace-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Asource_map_stack_trace) | [![pub package](https://img.shields.io/pub/v/source_map_stack_trace.svg)](https://pub.dev/packages/source_map_stack_trace) |
| [source_maps](pkgs/source_maps/) | A library to programmatically manipulate source map files. | [![package issues](https://img.shields.io/badge/package:source_maps-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Asource_maps) | [![pub package](https://img.shields.io/pub/v/source_maps.svg)](https://pub.dev/packages/source_maps) |
| [unified_analytics](pkgs/unified_analytics/) | A package for logging analytics for all Dart and Flutter related tooling to Google Analytics. | [![package issues](https://img.shields.io/badge/package:unified_analytics-4774bc)](https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aunified_analytics) | [![pub package](https://img.shields.io/pub/v/unified_analytics.svg)](https://pub.dev/packages/unified_analytics) |

## Publishing automation
Expand Down
4 changes: 4 additions & 0 deletions pkgs/source_maps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dart_tool/
.packages
.pub/
pubspec.lock
131 changes: 131 additions & 0 deletions pkgs/source_maps/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
## 0.10.13

* Require Dart 3.3
* Move to `dart-lang/tools` monorepo.

## 0.10.12

* Add additional types at API boundaries.

## 0.10.11

* Populate the pubspec `repository` field.
* Update the source map documentation link in the readme.

## 0.10.10

* Stable release for null safety.

## 0.10.9

* Fix a number of document comment issues.
* Allow parsing source map files with a missing `names` field.

## 0.10.8

* Preserve source-map extensions in `SingleMapping`. Extensions are keys in the
json map that start with `"x_"`.

## 0.10.7

* Set max SDK version to `<3.0.0`, and adjust other dependencies.

## 0.10.6

* Require version 2.0.0 of the Dart SDK.

## 0.10.5

* Add a `SingleMapping.files` field which provides access to `SourceFile`s
representing the `"sourcesContent"` fields in the source map.

* Add an `includeSourceContents` flag to `SingleMapping.toJson()` which
indicates whether to include source file contents in the source map.

## 0.10.4
* Implement `highlight` in `SourceMapFileSpan`.
* Require version `^1.3.0` of `source_span`.

## 0.10.3
* Add `addMapping` and `containsMapping` members to `MappingBundle`.

## 0.10.2
* Support for extended source map format.
* Polish `MappingBundle.spanFor` handling of URIs that have a suffix that
exactly match a source map in the MappingBundle.

## 0.10.1+5
* Fix strong mode warning in test.

## 0.10.1+4

* Extend `MappingBundle.spanFor` to accept requests for output files that
don't have source maps.

## 0.10.1+3

* Add `MappingBundle` class that handles extended source map format that
supports source maps for multiple output files in a single mapper.
Extend `Mapping.spanFor` API to accept a uri parameter that is optional
for normal source maps but required for MappingBundle source maps.

## 0.10.1+2

* Fix more strong mode warnings.

## 0.10.1+1

* Fix all strong mode warnings.

## 0.10.1

* Add a `mapUrl` named argument to `parse` and `parseJson`. This argument is
used to resolve source URLs for source spans.

## 0.10.0+2

* Fix analyzer error (FileSpan has a new field since `source_span` 1.1.1)

## 0.10.0+1

* Remove an unnecessary warning printed when the "file" field is missing from a
Json formatted source map. This field is optional and its absence is not
unusual.

## 0.10.0

* Remove the `Span`, `Location` and `SourceFile` classes. Use the
corresponding `source_span` classes instead.

## 0.9.4

* Update `SpanFormatException` with `source` and `offset`.

* All methods that take `Span`s, `Location`s, and `SourceFile`s as inputs now
also accept the corresponding `source_span` classes as well. Using the old
classes is now deprecated and will be unsupported in version 0.10.0.

## 0.9.3

* Support writing SingleMapping objects to source map version 3 format.
* Support the `sourceRoot` field in the SingleMapping class.
* Support updating the `targetUrl` field in the SingleMapping class.

## 0.9.2+2

* Fix a bug in `FixedSpan.getLocationMessage`.

## 0.9.2+1

* Minor readability improvements to `FixedSpan.getLocationMessage` and
`SpanException.toString`.

## 0.9.2

* Add `SpanException` and `SpanFormatException` classes.

## 0.9.1

* Support unmapped areas in source maps.

* Increase the readability of location messages.
27 changes: 27 additions & 0 deletions pkgs/source_maps/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright 2014, the Dart project authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 changes: 25 additions & 0 deletions pkgs/source_maps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[![Build Status](https://github.com/dart-lang/tools/actions/workflows/source_maps.yaml/badge.svg)](https://github.com/dart-lang/tools/actions/workflows/source_maps.yaml)
[![pub package](https://img.shields.io/pub/v/source_maps.svg)](https://pub.dev/packages/source_maps)
[![package publisher](https://img.shields.io/pub/publisher/source_maps.svg)](https://pub.dev/packages/source_maps/publisher)

This project implements a Dart pub package to work with source maps.

## Docs and usage

The implementation is based on the [source map version 3 spec][spec] which was
originated from the [Closure Compiler][closure] and has been implemented in
Chrome and Firefox.

In this package we provide:

* Data types defining file locations and spans: these are not part of the
original source map specification. These data types are great for tracking
source locations on source maps, but they can also be used by tools to
reporting useful error messages that include on source locations.
* A builder that creates a source map programmatically and produces the encoded
source map format.
* A parser that reads the source map format and provides APIs to read the
mapping information.

[closure]: https://github.com/google/closure-compiler/wiki/Source-Maps
[spec]: https://docs.google.com/a/google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
1 change: 1 addition & 0 deletions pkgs/source_maps/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:dart_flutter_team_lints/analysis_options.yaml
84 changes: 84 additions & 0 deletions pkgs/source_maps/lib/builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Contains a builder object useful for creating source maps programatically.
library source_maps.builder;

// TODO(sigmund): add a builder for multi-section mappings.

import 'dart:convert';

import 'package:source_span/source_span.dart';

import 'parser.dart';
import 'src/source_map_span.dart';

/// Builds a source map given a set of mappings.
class SourceMapBuilder {
final List<Entry> _entries = <Entry>[];

/// Adds an entry mapping the [targetOffset] to [source].
void addFromOffset(SourceLocation source, SourceFile targetFile,
int targetOffset, String identifier) {
ArgumentError.checkNotNull(targetFile, 'targetFile');
_entries.add(Entry(source, targetFile.location(targetOffset), identifier));
}

/// Adds an entry mapping [target] to [source].
///
/// If [isIdentifier] is true or if [target] is a [SourceMapSpan] with
/// `isIdentifier` set to true, this entry is considered to represent an
/// identifier whose value will be stored in the source map. [isIdentifier]
/// takes precedence over [target]'s `isIdentifier` value.
void addSpan(SourceSpan source, SourceSpan target, {bool? isIdentifier}) {
isIdentifier ??= source is SourceMapSpan ? source.isIdentifier : false;

var name = isIdentifier ? source.text : null;
_entries.add(Entry(source.start, target.start, name));
}

/// Adds an entry mapping [target] to [source].
void addLocation(
SourceLocation source, SourceLocation target, String? identifier) {
_entries.add(Entry(source, target, identifier));
}

/// Encodes all mappings added to this builder as a json map.
Map<String, dynamic> build(String fileUrl) {
return SingleMapping.fromEntries(_entries, fileUrl).toJson();
}

/// Encodes all mappings added to this builder as a json string.
String toJson(String fileUrl) => jsonEncode(build(fileUrl));
}

/// An entry in the source map builder.
class Entry implements Comparable<Entry> {
/// Span denoting the original location in the input source file
final SourceLocation source;

/// Span indicating the corresponding location in the target file.
final SourceLocation target;

/// An identifier name, when this location is the start of an identifier.
final String? identifierName;

/// Creates a new [Entry] mapping [target] to [source].
Entry(this.source, this.target, this.identifierName);

/// Implements [Comparable] to ensure that entries are ordered by their
/// location in the target file. We sort primarily by the target offset
/// because source map files are encoded by printing each mapping in order as
/// they appear in the target file.
@override
int compareTo(Entry other) {
var res = target.compareTo(other.target);
if (res != 0) return res;
res = source.sourceUrl
.toString()
.compareTo(other.source.sourceUrl.toString());
if (res != 0) return res;
return source.compareTo(other.source);
}
}
Loading

0 comments on commit db47a84

Please sign in to comment.