Lint and format src/compiler/preprocess/index.js

pull/8569/head
Simon Holthausen 3 years ago
parent 822a849157
commit 5a17da61be

@ -1,5 +1,10 @@
import { getLocator } from 'locate-character'; import { getLocator } from 'locate-character';
import { MappedCode, parse_attached_sourcemap, sourcemap_add_offset, combine_sourcemaps } from '../utils/mapped_code'; import {
MappedCode,
parse_attached_sourcemap,
sourcemap_add_offset,
combine_sourcemaps
} from '../utils/mapped_code';
import { decode_map } from './decode_sourcemap.js'; import { decode_map } from './decode_sourcemap.js';
import { replace_in_code, slice_source } from './replace_in_code'; import { replace_in_code, slice_source } from './replace_in_code';
import { regex_whitespaces } from '../utils/patterns'; import { regex_whitespaces } from '../utils/patterns';
@ -80,7 +85,7 @@ class PreprocessResult {
// markup { code: markupCode, map: markupMap }, // markup { code: markupCode, map: markupMap },
code: this.source, code: this.source,
dependencies: [...new Set(this.dependencies)], dependencies: [...new Set(this.dependencies)],
map: map, map,
toString: () => this.source toString: () => this.source
}; };
} }
@ -128,13 +133,18 @@ function processed_tag_to_code(processed, tag_name, attributes, source) {
* @param {string} code * @param {string} code
* @param {number} offset * @param {number} offset
*/ */
const build_mapped_code = (code, offset) => MappedCode.from_source(slice_source(code, offset, source)); const build_mapped_code = (code, offset) =>
MappedCode.from_source(slice_source(code, offset, source));
const tag_open = `<${tag_name}${attributes || ''}>`; const tag_open = `<${tag_name}${attributes || ''}>`;
const tag_close = `</${tag_name}>`; const tag_close = `</${tag_name}>`;
const tag_open_code = build_mapped_code(tag_open, 0); const tag_open_code = build_mapped_code(tag_open, 0);
const tag_close_code = build_mapped_code(tag_close, tag_open.length + source.source.length); const tag_close_code = build_mapped_code(tag_close, tag_open.length + source.source.length);
parse_attached_sourcemap(processed, tag_name); parse_attached_sourcemap(processed, tag_name);
const content_code = processed_content_to_code(processed, get_location(tag_open.length), file_basename); const content_code = processed_content_to_code(
processed,
get_location(tag_open.length),
file_basename
);
return tag_open_code.concat(content_code).concat(tag_close_code); return tag_open_code.concat(content_code).concat(tag_close_code);
} }
const regex_quoted_value = /^['"](.*)['"]$/; const regex_quoted_value = /^['"](.*)['"]$/;
@ -178,22 +188,24 @@ async function process_tag(tag_name, preprocessor, source) {
* @returns {Promise<MappedCode>} * @returns {Promise<MappedCode>}
*/ */
async function process_single_tag(tag_with_content, attributes = '', content = '', tag_offset) { async function process_single_tag(tag_with_content, attributes = '', content = '', tag_offset) {
const no_change = () => MappedCode.from_source(slice_source(tag_with_content, tag_offset, source)); const no_change = () =>
if (!attributes && !content) MappedCode.from_source(slice_source(tag_with_content, tag_offset, source));
return no_change(); if (!attributes && !content) return no_change();
const processed = await preprocessor({ const processed = await preprocessor({
content: content || '', content: content || '',
attributes: parse_tag_attributes(attributes || ''), attributes: parse_tag_attributes(attributes || ''),
markup, markup,
filename filename
}); });
if (!processed) if (!processed) return no_change();
return no_change(); if (processed.dependencies) dependencies.push(...processed.dependencies);
if (processed.dependencies) if (!processed.map && processed.code === content) return no_change();
dependencies.push(...processed.dependencies); return processed_tag_to_code(
if (!processed.map && processed.code === content) processed,
return no_change(); tag_name,
return processed_tag_to_code(processed, tag_name, attributes, slice_source(content, tag_offset, source)); attributes,
slice_source(content, tag_offset, source)
);
} }
const { string, map } = await replace_in_code(tag_regex, process_single_tag, source); const { string, map } = await replace_in_code(tag_regex, process_single_tag, source);
return { string, map, dependencies }; return { string, map, dependencies };
@ -219,8 +231,7 @@ async function process_markup(process, source) {
: undefined, : undefined,
dependencies: processed.dependencies dependencies: processed.dependencies
}; };
} } else {
else {
return {}; return {};
} }
} }
@ -232,7 +243,6 @@ async function process_markup(process, source) {
* @returns {Promise<Processed>} * @returns {Promise<Processed>}
*/ */
export default async function preprocess(source, preprocessor, options) { export default async function preprocess(source, preprocessor, options) {
/** /**
* @type {string | undefined} * @type {string | undefined}
*/ */
@ -260,9 +270,6 @@ export default async function preprocess(source, preprocessor, options) {
return result.to_processed(); return result.to_processed();
} }
/** @typedef {Object} SourceUpdate /** @typedef {Object} SourceUpdate
* @property {string} [string] * @property {string} [string]
* @property {DecodedSourceMap} [map] * @property {DecodedSourceMap} [map]

Loading…
Cancel
Save