Skip to content

added patches for markdown and unsafe-eval #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/aws-lsp-yaml-json-binary/src/tests/testUtilsCF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ Globals:
export const HOVER_YAML = {
contents: {
kind: 'markdown',
value: '#### Resources\n\nSource: [schema.json](https://raw.githubusercontent.com/aws/serverless-application-model/main/samtranslator/schema/schema.json)',
value: '',
},
range: {
start: { line: 0, character: 0 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe('Test YamlJsonServer with CloudFormation schema', () => {
expect(result).to.deep.equal(COMPLETIONS_EMPTY_OBJECT_YAML)
})

it('should return hover items, YAML', async () => {
it('should return hover item without header and footer, YAML', async () => {
const docUri = 'hover.yml'
client.didOpen({
textDocument: {
Expand Down
12 changes: 12 additions & 0 deletions core/aws-lsp-yaml-common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Temporary Patch for yaml-language-server Dependency

In the current state of this package, we need to apply a temporary patch to the yaml-language-server dependency.

The script applies 2 patches:
- `patches/markdown`: adds hover setting to enable/disable Title and Source from hover tooltip. [PR](https://github.com/redhat-developer/yaml-language-server/pull/892)
- `patches/unsafe-eval`: adds skipSchemaValidation settings to enable/disable json schema validation. [PR draft](https://github.com/redhat-developer/yaml-language-server/pull/965).


The patch file is applied during the installation process using the `postinstall` script in the `package.json` file. This script runs the `patchYamlJsonPackage.js` script, which searches path to the `yaml-language-server` package and applies the patches once.

### Note: This is a temporary solution, and we plan to remove the custom patching once the yaml-language-server package is updated. The features from PRs above should be merged to yaml-language-server package. After features are merged we need to update the version and enable skipSchemaValidation setting.
3 changes: 2 additions & 1 deletion core/aws-lsp-yaml-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "YAML Language Server common code library",
"main": "out/index.js",
"scripts": {
"compile": "tsc --build"
"compile": "tsc --build",
"postinstall": "node patchYamlJsonPackage.js"
},
"dependencies": {
"@aws/lsp-core": "^0.0.1",
Expand Down
52 changes: 52 additions & 0 deletions core/aws-lsp-yaml-common/patchYamlJsonPackage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const execSync = require('child_process').execSync
const fs = require('node:fs')

const pathToYamlPackage = require.resolve('yaml-language-server')
const rootPackage = pathToYamlPackage.substring(
0,
pathToYamlPackage.indexOf('node_modules/yaml-language-server/out/server/src/index.js')
)
const pathToFileHover = rootPackage + 'node_modules/yaml-language-server/lib/esm/languageservice/services/yamlHover.js'

const filePathToPatchPathMarkdown = {
'node_modules/yaml-language-server/lib/esm/languageservice/services/yamlHover.js':
'patches/markdown/yamlHover.esm.patch',
'node_modules/yaml-language-server/lib/umd/languageservice/services/yamlHover.js':
'patches/markdown/yamlHover.umd.patch',
'node_modules/yaml-language-server/out/server/src/languageservice/services/yamlHover.js':
'patches/markdown/yamlHover.src.patch',
'node_modules/yaml-language-server/lib/esm/languageservice/yamlLanguageService.d.ts':
'patches/markdown/yamlLanguageService.esm.patch',
'node_modules/yaml-language-server/lib/umd/languageservice/yamlLanguageService.d.ts':
'patches/markdown/yamlLanguageService.umd.patch',
'node_modules/yaml-language-server/out/server/src/languageservice/yamlLanguageService.d.ts':
'patches/markdown/yamlLanguageService.src.patch',
}

const filePathToPatchPathUnsafeEval = {
'node_modules/yaml-language-server/lib/esm/languageservice/services/yamlSchemaService.js':
'patches/unsafe-eval/yamlSchemaService.esm.patch',
'node_modules/yaml-language-server/lib/umd/languageservice/services/yamlSchemaService.js':
'patches/unsafe-eval/yamlSchemaService.umd.patch',
'node_modules/yaml-language-server/out/server/src/languageservice/services/yamlSchemaService.js':
'patches/unsafe-eval/yamlSchemaService.src.patch',
}

function applyPatch(filePathToPatchPath) {
for (var filePath in filePathToPatchPath) {
const pathToPatch = `${__dirname}/${filePathToPatchPath[filePath]}`
const script = `cd ${rootPackage} && patch ${filePath} ${pathToPatch}`
console.log(script)
const output = execSync(script, { encoding: 'utf-8', timeout: 2000 })
}
}

fs.readFile(pathToFileHover, function (err, data) {
if (err) throw err
if (data.indexOf('this.hoverSettings = {showSource: true, showTitle: true};') >= 0) {
console.log('Patch is already applied')
} else {
applyPatch(filePathToPatchPathMarkdown)
applyPatch(filePathToPatchPathUnsafeEval)
}
})
47 changes: 47 additions & 0 deletions core/aws-lsp-yaml-common/patches/markdown/yamlHover.esm.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--- a/node_modules/yaml-language-server/lib/esm/languageservice/services/yamlHover.js 2024-04-28 20:38:52
+++ b/node_modules/yaml-language-server/lib/esm/languageservice/services/yamlHover.js 2024-04-28 20:55:28
@@ -16,11 +16,13 @@
constructor(schemaService, telemetry) {
this.telemetry = telemetry;
this.shouldHover = true;
+ this.hoverSettings = {showSource: true, showTitle: true};
this.schemaService = schemaService;
}
configure(languageSettings) {
if (languageSettings) {
this.shouldHover = languageSettings.hover;
+ this.hoverSettings = languageSettings.hoverSettings;
this.indentation = languageSettings.indentation;
}
}
\ No newline at end of file
@@ -82,6 +84,8 @@
const removePipe = (value) => {
return value.replace(/\|\|\s*$/, '');
};
+ const showSource = this.hoverSettings?.showSource ?? true;
+ const showTitle = this.hoverSettings?.showTitle ?? true;
return this.schemaService.getSchemaForResource(document.uri, doc).then((schema) => {
if (schema && node && !schema.errors.length) {
const matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);
\ No newline at end of file
@@ -133,7 +137,7 @@
return true;
});
let result = '';
- if (title) {
+ if (showTitle && title) {
result = '#### ' + toMarkdown(title);
}
if (markdownDescription) {
\ No newline at end of file
@@ -157,7 +161,7 @@
result += `\n\n\`\`\`${example}\`\`\``;
});
}
- if (result.length > 0 && schema.schema.url) {
+ if (showSource && result.length > 0 && schema.schema.url) {
result += `\n\nSource: [${getSchemaName(schema.schema)}](${schema.schema.url})`;
}
return createHover(result);
\ No newline at end of file
47 changes: 47 additions & 0 deletions core/aws-lsp-yaml-common/patches/markdown/yamlHover.src.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--- a/node_modules/yaml-language-server/out/server/src/languageservice/services/yamlHover.js 2024-05-02 15:13:13
+++ b/node_modules/yaml-language-server/out/server/src/languageservice/services/yamlHover.js 2024-05-02 15:28:53
@@ -18,11 +18,13 @@
constructor(schemaService, telemetry) {
this.telemetry = telemetry;
this.shouldHover = true;
+ this.hoverSettings = {showSource: true, showTitle: true};
this.schemaService = schemaService;
}
configure(languageSettings) {
if (languageSettings) {
this.shouldHover = languageSettings.hover;
+ this.hoverSettings = languageSettings.hoverSettings;
this.indentation = languageSettings.indentation;
}
}
\ No newline at end of file
@@ -84,6 +86,8 @@
const removePipe = (value) => {
return value.replace(/\|\|\s*$/, '');
};
+ const showSource = this.hoverSettings?.showSource ?? true;
+ const showTitle = this.hoverSettings?.showTitle ?? true;
return this.schemaService.getSchemaForResource(document.uri, doc).then((schema) => {
if (schema && node && !schema.errors.length) {
const matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);
\ No newline at end of file
@@ -135,7 +139,7 @@
return true;
});
let result = '';
- if (title) {
+ if (showTitle && title) {
result = '#### ' + toMarkdown(title);
}
if (markdownDescription) {
\ No newline at end of file
@@ -159,7 +163,7 @@
result += `\n\n\`\`\`${example}\`\`\``;
});
}
- if (result.length > 0 && schema.schema.url) {
+ if (showSource && result.length > 0 && schema.schema.url) {
result += `\n\nSource: [${getSchemaName(schema.schema)}](${schema.schema.url})`;
}
return createHover(result);
\ No newline at end of file
47 changes: 47 additions & 0 deletions core/aws-lsp-yaml-common/patches/markdown/yamlHover.umd.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--- a/node_modules/yaml-language-server/lib/umd/languageservice/services/yamlHover.js 2024-05-02 15:13:13
+++ b/node_modules/yaml-language-server/lib/umd/languageservice/services/yamlHover.js 2024-05-02 15:20:47
@@ -27,11 +27,13 @@
constructor(schemaService, telemetry) {
this.telemetry = telemetry;
this.shouldHover = true;
+ this.hoverSettings = {showSource: true, showTitle: true};
this.schemaService = schemaService;
}
configure(languageSettings) {
if (languageSettings) {
this.shouldHover = languageSettings.hover;
+ this.hoverSettings = languageSettings.hoverSettings;
this.indentation = languageSettings.indentation;
}
}
\ No newline at end of file
@@ -93,6 +95,8 @@
const removePipe = (value) => {
return value.replace(/\|\|\s*$/, '');
};
+ const showSource = this.hoverSettings?.showSource ?? true;
+ const showTitle = this.hoverSettings?.showTitle ?? true;
return this.schemaService.getSchemaForResource(document.uri, doc).then((schema) => {
if (schema && node && !schema.errors.length) {
const matchingSchemas = doc.getMatchingSchemas(schema.schema, node.offset);
\ No newline at end of file
@@ -144,7 +148,7 @@
return true;
});
let result = '';
- if (title) {
+ if (showTitle && title) {
result = '#### ' + toMarkdown(title);
}
if (markdownDescription) {
\ No newline at end of file
@@ -168,7 +172,7 @@
result += `\n\n\`\`\`${example}\`\`\``;
});
}
- if (result.length > 0 && schema.schema.url) {
+ if (showSource && result.length > 0 && schema.schema.url) {
result += `\n\nSource: [${getSchemaName(schema.schema)}](${schema.schema.url})`;
}
return createHover(result);
\ No newline at end of file
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- a/node_modules/yaml-language-server/lib/esm/languageservice/yamlLanguageService.d.ts 2024-05-02 15:36:28
+++ b/node_modules/yaml-language-server/lib/esm/languageservice/yamlLanguageService.d.ts 2024-05-02 15:36:32
@@ -25,6 +25,7 @@
export interface LanguageSettings {
validate?: boolean;
hover?: boolean;
+ hoverSettings?: {showSource?: boolean; showTitle?: boolean;};
completion?: boolean;
format?: boolean;
isKubernetes?: boolean;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- a/node_modules/yaml-language-server/out/server/src/languageservice/yamlLanguageService.d.ts 2024-05-02 15:33:31
+++ b/node_modules/yaml-language-server/out/server/src/languageservice/yamlLanguageService.d.ts 2024-05-02 15:34:03
@@ -25,6 +25,7 @@
export interface LanguageSettings {
validate?: boolean;
hover?: boolean;
+ hoverSettings?: {showSource?: boolean; showTitle?: boolean;};
completion?: boolean;
format?: boolean;
isKubernetes?: boolean;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- a/node_modules/yaml-language-server/lib/umd/languageservice/yamlLanguageService.d.ts 2024-05-02 15:13:12
+++ b/node_modules/yaml-language-server/lib/umd/languageservice/yamlLanguageService.d.ts 2024-05-02 15:38:22
@@ -25,6 +25,7 @@
export interface LanguageSettings {
validate?: boolean;
hover?: boolean;
+ hoverSettings?: {showSource?: boolean; showTitle?: boolean;};
completion?: boolean;
format?: boolean;
isKubernetes?: boolean;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--- a/node_modules/yaml-language-server/lib/esm/languageservice/services/yamlSchemaService.js 2024-05-02 15:43:07
+++ b/node_modules/yaml-language-server/lib/esm/languageservice/services/yamlSchemaService.js 2024-05-02 15:42:46
@@ -12,14 +12,8 @@
import { parse } from 'yaml';
import * as path from 'path';
import { getSchemaFromModeline } from './modelineUtil';
-import Ajv from 'ajv';
import { getSchemaTitle } from '../utils/schemaUtils';
const localize = nls.loadMessageBundle();
-const ajv = new Ajv();
-// load JSON Schema 07 def to validate loaded schemas
-// eslint-disable-next-line @typescript-eslint/no-var-requires
-const jsonSchema07 = require('ajv/dist/refs/json-schema-draft-07.json');
-const schema07Validator = ajv.compile(jsonSchema07);
export var MODIFICATION_ACTIONS;
(function (MODIFICATION_ACTIONS) {
MODIFICATION_ACTIONS[MODIFICATION_ACTIONS["delete"] = 0] = "delete";
\ No newline at end of file
@@ -87,13 +81,7 @@
const resolveErrors = schemaToResolve.errors.slice(0);
let schema = schemaToResolve.schema;
const contextService = this.contextService;
- if (!schema07Validator(schema)) {
- const errs = [];
- for (const err of schema07Validator.errors) {
- errs.push(`${err.instancePath} : ${err.message}`);
- }
- resolveErrors.push(`Schema '${getSchemaTitle(schemaToResolve.schema, schemaURL)}' is not valid:\n${errs.join('\n')}`);
- }
+
const findSection = (schema, path) => {
if (!path) {
return schema;
\ No newline at end of file
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--- ./node_modules/yaml-language-server/out/server/src/languageservice/services/yamlSchemaService.js 2024-05-02 15:47:54
+++ ./node_modules/yaml-language-server/out/server/src/languageservice/services/yamlSchemaService.js 2024-05-02 15:47:40
@@ -14,14 +14,8 @@
const yaml_1 = require("yaml");
const path = require("path");
const modelineUtil_1 = require("./modelineUtil");
-const ajv_1 = require("ajv");
const schemaUtils_1 = require("../utils/schemaUtils");
const localize = nls.loadMessageBundle();
-const ajv = new ajv_1.default();
-// load JSON Schema 07 def to validate loaded schemas
-// eslint-disable-next-line @typescript-eslint/no-var-requires
-const jsonSchema07 = require('ajv/dist/refs/json-schema-draft-07.json');
-const schema07Validator = ajv.compile(jsonSchema07);
var MODIFICATION_ACTIONS;
(function (MODIFICATION_ACTIONS) {
MODIFICATION_ACTIONS[MODIFICATION_ACTIONS["delete"] = 0] = "delete";
\ No newline at end of file
@@ -90,13 +84,6 @@
const resolveErrors = schemaToResolve.errors.slice(0);
let schema = schemaToResolve.schema;
const contextService = this.contextService;
- if (!schema07Validator(schema)) {
- const errs = [];
- for (const err of schema07Validator.errors) {
- errs.push(`${err.instancePath} : ${err.message}`);
- }
- resolveErrors.push(`Schema '${(0, schemaUtils_1.getSchemaTitle)(schemaToResolve.schema, schemaURL)}' is not valid:\n${errs.join('\n')}`);
- }
const findSection = (schema, path) => {
if (!path) {
return schema;
\ No newline at end of file
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--- a/node_modules/yaml-language-server/lib/umd/languageservice/services/yamlSchemaService.js 2024-05-02 15:45:09
+++ b/node_modules/yaml-language-server/lib/umd/languageservice/services/yamlSchemaService.js 2024-05-02 15:44:54
@@ -23,14 +23,8 @@
const yaml_1 = require("yaml");
const path = require("path");
const modelineUtil_1 = require("./modelineUtil");
- const ajv_1 = require("ajv");
const schemaUtils_1 = require("../utils/schemaUtils");
const localize = nls.loadMessageBundle();
- const ajv = new ajv_1.default();
- // load JSON Schema 07 def to validate loaded schemas
- // eslint-disable-next-line @typescript-eslint/no-var-requires
- const jsonSchema07 = require('ajv/dist/refs/json-schema-draft-07.json');
- const schema07Validator = ajv.compile(jsonSchema07);
var MODIFICATION_ACTIONS;
(function (MODIFICATION_ACTIONS) {
MODIFICATION_ACTIONS[MODIFICATION_ACTIONS["delete"] = 0] = "delete";
\ No newline at end of file
@@ -99,13 +93,6 @@
const resolveErrors = schemaToResolve.errors.slice(0);
let schema = schemaToResolve.schema;
const contextService = this.contextService;
- if (!schema07Validator(schema)) {
- const errs = [];
- for (const err of schema07Validator.errors) {
- errs.push(`${err.instancePath} : ${err.message}`);
- }
- resolveErrors.push(`Schema '${(0, schemaUtils_1.getSchemaTitle)(schemaToResolve.schema, schemaURL)}' is not valid:\n${errs.join('\n')}`);
- }
const findSection = (schema, path) => {
if (!path) {
return schema;
\ No newline at end of file
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class YamlLanguageService implements AwsLanguageService {
private updateSchemaMapping(documentUri: string): void {
this.yamlService.configure({
hover: true,
hoverSettings: { showSource: false, showTitle: false },
completion: true,
format: true,
validate: true,
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.