context.js — svelte Source File
Architecture documentation for context.js, a javascript file in the svelte codebase. 7 imports, 1 dependents.
Entity Profile
Dependency Diagram
graph LR caefc1b2_dc4c_2cff_4013_e8ded13e7974["context.js"] 0d221a90_b1cc_5826_235e_98f5216d6400["bracket.js"] caefc1b2_dc4c_2cff_4013_e8ded13e7974 --> 0d221a90_b1cc_5826_235e_98f5216d6400 dacf5984_89ce_530d_f035_1eac4b0b8e1f["match_bracket"] caefc1b2_dc4c_2cff_4013_e8ded13e7974 --> dacf5984_89ce_530d_f035_1eac4b0b8e1f 2627a38d_1d02_8fc1_b764_b2aaf06f372b["acorn.js"] caefc1b2_dc4c_2cff_4013_e8ded13e7974 --> 2627a38d_1d02_8fc1_b764_b2aaf06f372b bb0eb4c2_ab75_feac_3814_01d1e4346bb8["parse_expression_at"] caefc1b2_dc4c_2cff_4013_e8ded13e7974 --> bb0eb4c2_ab75_feac_3814_01d1e4346bb8 ce051dbd_4cf1_f117_d66e_12cfa122de37["patterns.js"] caefc1b2_dc4c_2cff_4013_e8ded13e7974 --> ce051dbd_4cf1_f117_d66e_12cfa122de37 495501a4_a342_6a4d_ac11_e3e2fee8b218["errors.js"] caefc1b2_dc4c_2cff_4013_e8ded13e7974 --> 495501a4_a342_6a4d_ac11_e3e2fee8b218 a146f6ac_0088_8736_b6ce_318f9f115170["e"] caefc1b2_dc4c_2cff_4013_e8ded13e7974 --> a146f6ac_0088_8736_b6ce_318f9f115170 367a364a_2912_a1aa_b2e1_d97a82783c38["tag.js"] 367a364a_2912_a1aa_b2e1_d97a82783c38 --> caefc1b2_dc4c_2cff_4013_e8ded13e7974 style caefc1b2_dc4c_2cff_4013_e8ded13e7974 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
/** @import { Pattern } from 'estree' */
/** @import { Parser } from '../index.js' */
import { match_bracket } from '../utils/bracket.js';
import { parse_expression_at } from '../acorn.js';
import { regex_not_newline_characters } from '../../patterns.js';
import * as e from '../../../errors.js';
/**
* @param {Parser} parser
* @returns {Pattern}
*/
export default function read_pattern(parser) {
const start = parser.index;
let i = parser.index;
const id = parser.read_identifier();
if (id.name !== '') {
const annotation = read_type_annotation(parser);
return {
...id,
typeAnnotation: annotation
};
}
const char = parser.template[i];
if (char !== '{' && char !== '[') {
e.expected_pattern(i);
}
i = match_bracket(parser, start);
parser.index = i;
const pattern_string = parser.template.slice(start, i);
try {
// the length of the `space_with_newline` has to be start - 1
// because we added a `(` in front of the pattern_string,
// which shifted the entire string to right by 1
// so we offset it by removing 1 character in the `space_with_newline`
// to achieve that, we remove the 1st space encountered,
// so it will not affect the `column` of the node
let space_with_newline = parser.template
.slice(0, start)
.replace(regex_not_newline_characters, ' ');
const first_space = space_with_newline.indexOf(' ');
space_with_newline =
space_with_newline.slice(0, first_space) + space_with_newline.slice(first_space + 1);
const expression = /** @type {any} */ (
parse_expression_at(
`${space_with_newline}(${pattern_string} = 1)`,
parser.root.comments,
parser.ts,
start - 1
)
).left;
expression.typeAnnotation = read_type_annotation(parser);
if (expression.typeAnnotation) {
expression.end = expression.typeAnnotation.end;
}
return expression;
} catch (error) {
parser.acorn_error(error);
}
}
/**
* @param {Parser} parser
* @returns {any}
*/
function read_type_annotation(parser) {
const start = parser.index;
parser.allow_whitespace();
if (!parser.eat(':')) {
parser.index = start;
return undefined;
}
// we need to trick Acorn into parsing the type annotation
const insert = '_ as ';
let a = parser.index - insert.length;
const template =
parser.template.slice(0, a).replace(/[^\n]/g, ' ') +
insert +
// If this is a type annotation for a function parameter, Acorn-TS will treat subsequent
// parameters as part of a sequence expression instead, and will then error on optional
// parameters (`?:`). Therefore replace that sequence with something that will not error.
parser.template.slice(parser.index).replace(/\?\s*:/g, ':');
let expression = parse_expression_at(template, parser.root.comments, parser.ts, a);
// `foo: bar = baz` gets mangled — fix it
if (expression.type === 'AssignmentExpression') {
let b = expression.right.start;
while (template[b] !== '=') b -= 1;
expression = parse_expression_at(template.slice(0, b), parser.root.comments, parser.ts, a);
}
// `array as item: string, index` becomes `string, index`, which is mistaken as a sequence expression - fix that
if (expression.type === 'SequenceExpression') {
expression = expression.expressions[0];
}
parser.index = /** @type {number} */ (expression.end);
return {
type: 'TSTypeAnnotation',
start,
end: parser.index,
typeAnnotation: /** @type {any} */ (expression).typeAnnotation
};
}
Domain
Subdomains
Functions
Source
Frequently Asked Questions
What does context.js do?
context.js is a source file in the svelte codebase, written in javascript. It belongs to the Compiler domain, Transformer subdomain.
What functions are defined in context.js?
context.js defines 2 function(s): read_pattern, read_type_annotation.
What does context.js depend on?
context.js imports 7 module(s): acorn.js, bracket.js, e, errors.js, match_bracket, parse_expression_at, patterns.js.
What files import context.js?
context.js is imported by 1 file(s): tag.js.
Where is context.js in the architecture?
context.js is located at packages/svelte/src/compiler/phases/1-parse/read/context.js (domain: Compiler, subdomain: Transformer, directory: packages/svelte/src/compiler/phases/1-parse/read).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free