提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
2.1 KiB

  1. # @volar/source-map
  2. Provides functionality related to source maps.
  3. ## API
  4. ### This package exports a `SourceMap` class with the following methods:
  5. Params:
  6. - `fallbackToAnyMatch`(default: false): allow the start and end offsets to come from different mappings.
  7. - `filter?: (data: Data) => boolean)`(default: undefined): according to mapping: Mapping<MyDataType>.data, filter out offsets that do not meet the custom conditions.
  8. Methods:
  9. - `toSourceRange(generatedStart: number, generatedEnd: number, fallbackToAnyMatch: boolean, filter?: (data: Data) => boolean)`: Returns all source start and end offsets for the given generated start and end offsets.
  10. - `toGeneratedRange(sourceStart: number, sourceEnd: number, fallbackToAnyMatch: boolean, filter?: (data: Data) => boolean) `: Returns all generated start and end offsets for the given source start and end offsets.
  11. - `toSourceLocation(generatedOffset: number, filter?: (data: Data) => boolean)`: Returns all source offsets for a given generated offset.
  12. - `toGeneratedLocation(sourceOffset: number, filter?: (data: Data) => boolean)`: Returns all generated offsets for a given source offset.
  13. ## Data Structures
  14. ### `Mapping`
  15. The `Mapping` is a tuple that represents a mapping in the source map. It consists of the following elements:
  16. - `source`: A string representing the source file. This can be `undefined`.
  17. - `sourceOffsets`: Offsets in the source code.
  18. - `generatedOffsets`: Offsets in the generated code.
  19. - `data`: The data associated with this mapping. The type of this data is generic and can be specified when creating a `SourceMap` instance.
  20. Here is an example of a `Mapping`:
  21. ```ts
  22. let mapping: Mapping<MyDataType> = {
  23. source: '.../sourceFile.ts',
  24. sourceOffsets: [10],
  25. generatedOffsets: [30],
  26. lengths: [10],
  27. data: myData,
  28. };
  29. ```
  30. In this example, `myData` is of type `MyDataType`, which is the type specified for the SourceMap instance.
  31. Remember to replace `MyDataType` and `myData` with actual types and data that are relevant to your project.
  32. ## License
  33. This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.