From 7284997aec36584b695d5d4d9269e30722020428 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 11 May 2023 17:11:49 +0200 Subject: [PATCH] cleanup preprocess and types --- src/compiler/preprocess/index.js | 60 ++++++++++--------- src/compiler/preprocess/private.d.ts | 15 +++++ .../preprocess/{types.ts => public.d.ts} | 12 ---- 3 files changed, 48 insertions(+), 39 deletions(-) create mode 100644 src/compiler/preprocess/private.d.ts rename src/compiler/preprocess/{types.ts => public.d.ts} (82%) diff --git a/src/compiler/preprocess/index.js b/src/compiler/preprocess/index.js index 5dd6d48443..ce1e3dcb35 100644 --- a/src/compiler/preprocess/index.js +++ b/src/compiler/preprocess/index.js @@ -4,11 +4,11 @@ import { parse_attached_sourcemap, sourcemap_add_offset, combine_sourcemaps -} from '../utils/mapped_code'; +} from '../utils/mapped_code.js'; import { decode_map } from './decode_sourcemap.js'; -import { replace_in_code, slice_source } from './replace_in_code'; -import { regex_whitespaces } from '../utils/patterns'; -export * from './types'; +import { replace_in_code, slice_source } from './replace_in_code.js'; +import { regex_whitespaces } from '../utils/patterns.js'; + const regex_filepath_separator = /[/\\]/; /** @@ -17,19 +17,23 @@ const regex_filepath_separator = /[/\\]/; function get_file_basename(filename) { return filename.split(regex_filepath_separator).pop(); } + /** * Represents intermediate states of the preprocessing. */ class PreprocessResult { + /** @type {string} */ source; + /** @type {string | undefined} */ filename; + // sourcemap_list is sorted in reverse order from last map (index 0) to first map (index -1) // so we use sourcemap_list.unshift() to add new maps // https://github.com/ampproject/remapping#multiple-transformations-of-a-file /** * @default [] - * @type {Array} + * @type {Array} */ sourcemap_list = []; @@ -45,9 +49,15 @@ class PreprocessResult { file_basename = undefined; /** - * @type {Class>} + * @type {ReturnType} */ get_location = undefined; + + /** + * + * @param {string} source + * @param {string} [filename] + */ constructor(source, filename) { this.source = source; this.filename = filename; @@ -57,7 +67,7 @@ class PreprocessResult { } /** - * @param {SourceUpdate} + * @param {import('./private.js').SourceUpdate} opts */ update_source({ string: source, map, dependencies }) { if (source != null) { @@ -73,7 +83,7 @@ class PreprocessResult { } /** - * @returns {Processed} + * @returns {import('./public.js').Processed} */ to_processed() { // Combine all the source maps for each preprocessor function into one @@ -92,8 +102,8 @@ class PreprocessResult { } /** * Convert preprocessor output for the tag content into MappedCode - * @param {Processed} processed - * @param {SourceLocation} location + * @param {import('./public.js').Processed} processed + * @param {import('../utils/mapped_code.js').SourceLocation} location * @param {string} file_basename * @returns {MappedCode} */ @@ -101,7 +111,7 @@ function processed_content_to_code(processed, location, file_basename) { // Convert the preprocessed code and its sourcemap to a MappedCode /** - * @type {DecodedSourceMap} + * @type {import('@ampproject/remapping').DecodedSourceMap} */ let decoded_map; if (processed.map) { @@ -120,10 +130,10 @@ function processed_content_to_code(processed, location, file_basename) { /** * Given the whole tag including content, return a `MappedCode` * representing the tag content replaced with `processed`. - * @param {Processed} processed + * @param {import('./public.js').Processed} processed * @param {'style' | 'script'} tag_name * @param {string} attributes - * @param {Source} source + * @param {import('./private.js').Source} source * @returns {MappedCode} */ function processed_tag_to_code(processed, tag_name, attributes, source) { @@ -164,14 +174,16 @@ function parse_tag_attributes(str) { return { ...attrs, [key]: unquoted ?? value ?? true }; }, {}); } + const regex_style_tags = /|([^]*?)<\/style>|\/>)/gi; const regex_script_tags = /|([^]*?)<\/script>|\/>)/gi; + /** * Calculate the updates required to process all instances of the specified tag. * @param {'style' | 'script'} tag_name - * @param {Preprocessor} preprocessor - * @param {Source} source - * @returns {Promise} + * @param {import('./public.js').Preprocessor} preprocessor + * @param {import('./private.js').Source} source + * @returns {Promise} */ async function process_tag(tag_name, preprocessor, source) { const { filename, source: markup } = source; @@ -212,8 +224,8 @@ async function process_tag(tag_name, preprocessor, source) { } /** - * @param {MarkupPreprocessor} process - * @param {Source} source + * @param {import('./public.js').MarkupPreprocessor} process + * @param {import('./private.js').Source} source */ async function process_markup(process, source) { const processed = await process({ @@ -238,15 +250,15 @@ async function process_markup(process, source) { /** * @param {string} source - * @param {PreprocessorGroup | PreprocessorGroup[]} preprocessor + * @param {import('./public.js').PreprocessorGroup | import('./public.js').PreprocessorGroup[]} preprocessor * @param {{ filename?: string }} options - * @returns {Promise} + * @returns {Promise} */ export default async function preprocess(source, preprocessor, options) { /** * @type {string | undefined} */ - const filename = (options && options.filename) || preprocessor.filename; // legacy + const filename = (options && options.filename) || /** @type {any} */ (preprocessor).filename; // legacy const preprocessors = preprocessor ? Array.isArray(preprocessor) ? preprocessor @@ -269,9 +281,3 @@ export default async function preprocess(source, preprocessor, options) { } return result.to_processed(); } - -/** @typedef {Object} SourceUpdate - * @property {string} [string] - * @property {DecodedSourceMap} [map] - * @property {string[]} [dependencies] - */ diff --git a/src/compiler/preprocess/private.d.ts b/src/compiler/preprocess/private.d.ts new file mode 100644 index 0000000000..dde1784df8 --- /dev/null +++ b/src/compiler/preprocess/private.d.ts @@ -0,0 +1,15 @@ +import { DecodedSourceMap } from '@ampproject/remapping'; +import { Location } from 'locate-character'; + +export interface Source { + source: string; + get_location: (search: number) => Location; + file_basename: string; + filename?: string; +} + +export interface SourceUpdate { + string?: string; + map?: DecodedSourceMap; + dependencies?: string[]; +} diff --git a/src/compiler/preprocess/types.ts b/src/compiler/preprocess/public.d.ts similarity index 82% rename from src/compiler/preprocess/types.ts rename to src/compiler/preprocess/public.d.ts index faef6c74ed..84a9473672 100644 --- a/src/compiler/preprocess/types.ts +++ b/src/compiler/preprocess/public.d.ts @@ -1,15 +1,3 @@ -import { Location } from 'locate-character'; - -/** - * @internal - */ -export interface Source { - source: string; - get_location: (search: number) => Location; - file_basename: string; - filename?: string; -} - export interface Processed { code: string; map?: string | object; // we are opaque with the type here to avoid dependency on the remapping module for our public types.