Skip to content

Commit 0656ffd

Browse files
stevesCopilotCopilot
authored
Use named imports for js-yaml (#62014)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 377448a commit 0656ffd

39 files changed

Lines changed: 104 additions & 108 deletions

src/ai-tools/lib/file-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs'
22
import path from 'path'
3-
import yaml from 'js-yaml'
3+
import { load } from 'js-yaml'
44
import readFrontmatter from '@/frame/lib/read-frontmatter'
55
import { schema } from '@/frame/lib/frontmatter'
66

@@ -96,7 +96,7 @@ export function mergeFrontmatterProperties(filePath: string, newPropertiesYaml:
9696
cleanedYaml = cleanedYaml.replace(/\n```\s*$/i, '')
9797
cleanedYaml = cleanedYaml.trim()
9898

99-
const newProperties = yaml.load(cleanedYaml) as FrontmatterProperties
99+
const newProperties = load(cleanedYaml) as FrontmatterProperties
100100

101101
// Security: Validate against prototype pollution using the official frontmatter schema
102102
const allowedKeys = Object.keys(schema.properties)

src/ai-tools/lib/prompt-utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fileURLToPath } from 'url'
22
import fs from 'fs'
3-
import yaml from 'js-yaml'
3+
import { load } from 'js-yaml'
44
import path from 'path'
55
import readFrontmatter from '@/frame/lib/read-frontmatter'
66
import { callModelsApi } from '@/ai-tools/lib/call-models-api'
@@ -142,7 +142,7 @@ export async function callEditor(
142142
}
143143
const promptTemplatePath = path.join(promptDir, 'prompt-template.yml')
144144

145-
const prompt = yaml.load(fs.readFileSync(promptTemplatePath, 'utf8')) as PromptData
145+
const prompt = load(fs.readFileSync(promptTemplatePath, 'utf8')) as PromptData
146146

147147
// Validate the prompt template has required properties
148148
if (!prompt.messages || !Array.isArray(prompt.messages)) {

src/content-linter/lib/helpers/get-lintable-yml.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import yaml from 'js-yaml'
1+
import { load } from 'js-yaml'
22
import fs from 'fs/promises'
33

44
import dataSchemas from '@/data-directory/lib/data-schemas/index'
@@ -71,7 +71,7 @@ export async function getLintableYml(dataFilePath: string): Promise<Record<strin
7171
const schema = (await import(schemaFilePath)).default
7272
if (!schema) return null
7373

74-
const data = yaml.load(await fs.readFile(dataFilePath, 'utf8'))
74+
const data = load(await fs.readFile(dataFilePath, 'utf8'))
7575

7676
mdDict.clear()
7777
// This validate function will call all keyword validator functions and populate mdDict

src/content-linter/lib/linting-rules/early-access-references.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addError } from 'markdownlint-rule-helpers'
2-
import yaml from 'js-yaml'
2+
import { dump } from 'js-yaml'
33

44
import { getRange, getFrontmatter } from '../helpers/utils'
55
import type { RuleParams, RuleErrorCallback, Rule } from '@/content-linter/types'
@@ -75,7 +75,7 @@ export const frontmatterEarlyAccessReferences: Rule = {
7575

7676
// Convert updated frontmatter back to a string
7777
// to search for 'early-access'.'
78-
const fmStrings = yaml.dump(fm).split('\n')
78+
const fmStrings = dump(fm).split('\n')
7979

8080
for (const line of fmStrings) {
8181
const matches = line.match(EARLY_ACCESS_REGEX)

src/content-linter/lib/linting-rules/third-party-action-pinning.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addError, filterTokens } from 'markdownlint-rule-helpers'
2-
import yaml from 'js-yaml'
2+
import { load, YAMLException } from 'js-yaml'
33

44
import { liquid } from '@/content-render/index'
55
import { allVersions } from '@/versions/lib/all-versions'
@@ -50,7 +50,7 @@ export const thirdPartyActionPinning: Rule = {
5050
// If we don't parse the Liquid first, yaml loading chokes on {% raw %} tags
5151
const renderedYaml = await liquid.parseAndRender(token.content, context)
5252
try {
53-
const yamlObj = yaml.load(renderedYaml) as WorkflowYaml
53+
const yamlObj = load(renderedYaml) as WorkflowYaml
5454
const steps = getWorkflowSteps(yamlObj)
5555
if (!steps.some((step) => step.uses)) return
5656

@@ -73,7 +73,7 @@ export const thirdPartyActionPinning: Rule = {
7373
}
7474
}
7575
} catch (e) {
76-
if (e instanceof yaml.YAMLException) {
76+
if (e instanceof YAMLException) {
7777
console.log('YAML Exception file:', params.name)
7878
console.error('YAML Exception:', e.message)
7979
} else {

src/content-linter/lib/linting-rules/yaml-scheduled-jobs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import yaml from 'js-yaml'
1+
import { load } from 'js-yaml'
22
import { addError, filterTokens } from 'markdownlint-rule-helpers'
33

44
import { liquid } from '@/content-render/index'
@@ -39,7 +39,7 @@ export const yamlScheduledJobs: Rule = {
3939
}
4040
// If we don't parse the Liquid first, yaml loading chokes on {% raw %} tags
4141
const renderedYaml = await liquid.parseAndRender(token.content, context)
42-
const yamlObj = yaml.load(renderedYaml) as YamlWorkflow
42+
const yamlObj = load(renderedYaml) as YamlWorkflow
4343
if (!yamlObj.on) return
4444
if (!yamlObj.on.schedule) return
4545

src/content-linter/scripts/find-unsed-variables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
*/
1919
import fs from 'fs'
20-
import yaml from 'js-yaml'
20+
import { load } from 'js-yaml'
2121

2222
import { program } from 'commander'
2323

@@ -108,7 +108,7 @@ function getVariables(): Map<string, string> {
108108
const variables = new Map<string, string>()
109109
for (const filePath of walkFiles('data/variables', '.yml')) {
110110
const dottedPathBase = `variables.${filePath.replace('data/variables/', '').replace('.yml', '').replace(/\//g, '.')}`
111-
const data = yaml.load(fs.readFileSync(filePath, 'utf-8')) as Record<string, unknown>
111+
const data = load(fs.readFileSync(filePath, 'utf-8')) as Record<string, unknown>
112112
for (const key of Object.keys(data)) {
113113
const dottedPath = `${dottedPathBase}.${key}`
114114
variables.set(dottedPath, filePath)

src/content-linter/style/base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import fs from 'fs'
2-
import yaml from 'js-yaml'
2+
import { load } from 'js-yaml'
33

44
const allowedCodeFenceLanguages = Object.keys(
5-
yaml.load(fs.readFileSync('data/code-languages.yml', 'utf8')) as Record<string, unknown>,
5+
load(fs.readFileSync('data/code-languages.yml', 'utf8')) as Record<string, unknown>,
66
)
77

88
type RuleConfig = {

src/content-linter/tests/lint-files.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fileURLToPath } from 'url'
22
import path from 'path'
3-
import yaml from 'js-yaml'
3+
import { load } from 'js-yaml'
44
import fs from 'fs/promises'
55

66
import slash from 'slash'
@@ -253,14 +253,14 @@ if (ymlToLint.length === 0) {
253253
let isEarlyAccess: boolean
254254
let fileContents: string
255255
// This variable is used to determine if the file was parsed successfully.
256-
// When `yaml.load()` fails to parse the file, it is overwritten with the error message.
256+
// When `load()` fails to parse the file, it is overwritten with the error message.
257257
// `false` is intentionally chosen since `null` and `undefined` are valid return values.
258258
let dictionaryError: unknown = false
259259

260260
beforeAll(async () => {
261261
fileContents = await fs.readFile(yamlAbsPath!, 'utf8')
262262
try {
263-
dictionary = yaml.load(fileContents, { filename: yamlRelPath })
263+
dictionary = load(fileContents, { filename: yamlRelPath })
264264
} catch (error) {
265265
dictionaryError = error
266266
}

src/content-pipelines/scripts/update.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import { execSync, execFileSync } from 'child_process'
2020
import fs from 'fs'
2121
import path from 'path'
22-
import yaml from 'js-yaml'
22+
import { load } from 'js-yaml'
2323
import { program } from 'commander'
2424

2525
// ---------------------------------------------------------------------------
@@ -39,10 +39,7 @@ const CONFIG_FILE = path.join(process.cwd(), 'src/content-pipelines/config.yml')
3939

4040
function loadConfig(id: string): ContentPipelineConfig | null {
4141
if (!fs.existsSync(CONFIG_FILE)) return null
42-
const raw = yaml.load(fs.readFileSync(CONFIG_FILE, 'utf-8')) as Record<
43-
string,
44-
ContentPipelineConfig
45-
>
42+
const raw = load(fs.readFileSync(CONFIG_FILE, 'utf-8')) as Record<string, ContentPipelineConfig>
4643
return raw[id] ?? null
4744
}
4845

0 commit comments

Comments
 (0)